Migration: Implement priority migration and helper class to avoid issues with db structure temporarily not matching entities definitions

Author: @christianbeeznest
pull/5641/head
christianbeeznest 1 year ago committed by GitHub
parent 1162ad8c8e
commit f2c90bda64
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 18
      src/CoreBundle/Migrations/AbstractMigrationChamilo.php
  2. 106
      src/CoreBundle/Migrations/Schema/V200/PriorityMigrationHelper.php
  3. 17
      src/CoreBundle/Migrations/Schema/V200/Version20.php
  4. 23
      src/CoreBundle/Migrations/Schema/V200/Version20170523100000.php
  5. 42
      src/CoreBundle/Migrations/Schema/V200/Version20240704120400.php

@ -22,8 +22,10 @@ use Chamilo\CoreBundle\Repository\SessionRepository;
use Chamilo\CourseBundle\Repository\CGroupRepository;
use DateTime;
use DateTimeZone;
use Doctrine\DBAL\Connection;
use Doctrine\Migrations\AbstractMigration;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\UploadedFile;
@ -35,6 +37,22 @@ abstract class AbstractMigrationChamilo extends AbstractMigration
protected ?EntityManagerInterface $entityManager = null;
protected ?ContainerInterface $container = null;
private LoggerInterface $logger;
/**
* Constructor
*/
public function __construct(Connection $connection, LoggerInterface $logger)
{
parent::__construct($connection, $logger);
$this->logger = $logger;
}
protected function getLogger(): LoggerInterface
{
return $this->logger;
}
public function setEntityManager(EntityManagerInterface $entityManager): void
{
$this->entityManager = $entityManager;

@ -0,0 +1,106 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Connection;
use Psr\Log\LoggerInterface;
/**
* Priority migrations are database changes that *need* to happen before anything else
* because the related Entities in the code used for the migration already use
* the new structure (entities cannot be changed on the fly).
* An instance of this class is called at the beginning of any migration process.
*/
class PriorityMigrationHelper
{
private Connection $connection;
private LoggerInterface $logger;
/**
* Constructor
*/
public function __construct(Connection $connection, LoggerInterface $logger)
{
$this->connection = $connection;
$this->logger = $logger;
}
public function executeUp(Schema $schema): void
{
$this->logger->info('Executing PriorityMigrationHelper up method.');
$this->renameSettingsTableUp();
$this->addDurationFields($schema);
}
public function executeDown(Schema $schema): void
{
$this->logger->info('Executing PriorityMigrationHelper down method.');
$this->renameSettingsTableDown();
$this->removeDurationFields($schema);
}
private function addDurationFields(Schema $schema): void
{
$tables = [
'course',
'c_survey',
'c_quiz',
'c_quiz_question',
'c_lp',
'c_lp_item',
'c_student_publication',
'c_attendance_calendar'
];
foreach ($tables as $tableName) {
$table = $schema->getTable($tableName);
if (!$table->hasColumn('duration')) {
$this->connection->executeQuery("ALTER TABLE $tableName ADD duration INT DEFAULT NULL");
}
}
}
private function removeDurationFields(Schema $schema): void
{
$tables = [
'course',
'c_survey',
'c_quiz',
'c_quiz_question',
'c_lp',
'c_lp_item',
'c_student_publication',
'c_attendance_calendar'
];
foreach ($tables as $tableName) {
$table = $schema->getTable($tableName);
if ($table->hasColumn('duration')) {
$this->connection->executeQuery("ALTER TABLE $tableName DROP COLUMN duration");
}
}
}
private function renameSettingsTableUp(): void
{
$schemaManager = $this->connection->createSchemaManager();
if ($schemaManager->tablesExist(['settings_current']) && !$schemaManager->tablesExist(['settings'])) {
$this->connection->executeQuery('RENAME TABLE settings_current TO settings');
}
}
private function renameSettingsTableDown(): void
{
$schemaManager = $this->connection->createSchemaManager();
if ($schemaManager->tablesExist(['settings']) && !$schemaManager->tablesExist(['settings_current'])) {
$this->connection->executeQuery('RENAME TABLE settings TO settings_current');
}
}
}

@ -9,12 +9,22 @@ namespace Chamilo\CoreBundle\Migrations\Schema\V200;
use Chamilo\CoreBundle\DataFixtures\LanguageFixtures;
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Connection;
use Psr\Log\LoggerInterface;
/**
* Migrate file to updated to Chamilo 2.0.
*/
class Version20 extends AbstractMigrationChamilo
{
private PriorityMigrationHelper $priorityMigrationHelper;
public function __construct(Connection $connection, LoggerInterface $logger)
{
parent::__construct($connection, $logger);
$this->priorityMigrationHelper = new PriorityMigrationHelper($connection, $logger);
}
public function getDescription(): string
{
return 'Basic changes';
@ -22,6 +32,8 @@ class Version20 extends AbstractMigrationChamilo
public function up(Schema $schema): void
{
$this->priorityMigrationHelper->executeUp($schema);
$this->addSql('set sql_mode=""');
// Optimize bulk operations - see https://dev.mysql.com/doc/refman/5.6/en//optimizing-innodb-bulk-data-loading.html
// $this->addSql('set autocommit=0');
@ -579,5 +591,8 @@ class Version20 extends AbstractMigrationChamilo
}
}
public function down(Schema $schema): void {}
public function down(Schema $schema): void
{
$this->priorityMigrationHelper->executeDown($schema);
}
}

@ -1,23 +0,0 @@
<?php
/* For licensing terms, see /license.txt */
declare(strict_types=1);
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Doctrine\DBAL\Schema\Schema;
class Version20170523100000 extends AbstractMigrationChamilo
{
public function up(Schema $schema): void
{
$this->addSql('RENAME TABLE settings_current TO settings');
}
public function down(Schema $schema): void
{
$this->addSql('RENAME TABLE settings TO settings_current');
}
}

@ -1,42 +0,0 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Doctrine\DBAL\Schema\Schema;
final class Version20240704120400 extends AbstractMigrationChamilo
{
public function getDescription(): string
{
return 'Add duration field to multiple tables';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE course ADD duration INT DEFAULT NULL');
$this->addSql('ALTER TABLE c_survey ADD duration INT DEFAULT NULL');
$this->addSql('ALTER TABLE c_quiz ADD duration INT DEFAULT NULL');
$this->addSql('ALTER TABLE c_quiz_question ADD duration INT DEFAULT NULL');
$this->addSql('ALTER TABLE c_lp ADD duration INT DEFAULT NULL');
$this->addSql('ALTER TABLE c_lp_item ADD duration INT DEFAULT NULL');
$this->addSql('ALTER TABLE c_student_publication ADD duration INT DEFAULT NULL');
$this->addSql('ALTER TABLE c_attendance_calendar ADD duration INT DEFAULT NULL');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE course DROP duration');
$this->addSql('ALTER TABLE c_survey DROP duration');
$this->addSql('ALTER TABLE c_quiz DROP duration');
$this->addSql('ALTER TABLE c_quiz_question DROP duration');
$this->addSql('ALTER TABLE c_lp DROP duration');
$this->addSql('ALTER TABLE c_lp_item DROP duration');
$this->addSql('ALTER TABLE c_student_publication DROP duration');
$this->addSql('ALTER TABLE c_attendance_calendar DROP duration');
}
}
Loading…
Cancel
Save