Migration: Implement priority migration and helper class to avoid issues with db structure temporarily not matching entities definitions
	
		
	
				
					
				
			Author: @christianbeeznestpull/5641/head
							parent
							
								
									1162ad8c8e
								
							
						
					
					
						commit
						f2c90bda64
					
				@ -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'); | 
				
			||||
        } | 
				
			||||
    } | 
				
			||||
} | 
				
			||||
@ -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…
					
					
				
		Reference in new issue