1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
8
|
|
|
|
9
|
|
|
use Doctrine\DBAL\Schema\Schema; |
10
|
|
|
use Doctrine\DBAL\Connection; |
11
|
|
|
use Psr\Log\LoggerInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Priority migrations are database changes that *need* to happen before anything else |
15
|
|
|
* because the related Entities in the code used for the migration already use |
16
|
|
|
* the new structure (entities cannot be changed on the fly). |
17
|
|
|
* An instance of this class is called at the beginning of any migration process. |
18
|
|
|
*/ |
19
|
|
|
class PriorityMigrationHelper |
20
|
|
|
{ |
21
|
|
|
private Connection $connection; |
22
|
|
|
private LoggerInterface $logger; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Constructor |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Connection $connection, LoggerInterface $logger) |
28
|
|
|
{ |
29
|
|
|
$this->connection = $connection; |
30
|
|
|
$this->logger = $logger; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function executeUp(Schema $schema): void |
34
|
|
|
{ |
35
|
|
|
$this->logger->info('Executing PriorityMigrationHelper up method.'); |
36
|
|
|
$this->renameSettingsTableUp(); |
37
|
|
|
$this->addDurationFields($schema); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function executeDown(Schema $schema): void |
41
|
|
|
{ |
42
|
|
|
$this->logger->info('Executing PriorityMigrationHelper down method.'); |
43
|
|
|
$this->renameSettingsTableDown(); |
44
|
|
|
$this->removeDurationFields($schema); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function addDurationFields(Schema $schema): void |
48
|
|
|
{ |
49
|
|
|
$tables = [ |
50
|
|
|
'course', |
51
|
|
|
'c_survey', |
52
|
|
|
'c_quiz', |
53
|
|
|
'c_quiz_question', |
54
|
|
|
'c_lp', |
55
|
|
|
'c_lp_item', |
56
|
|
|
'c_student_publication', |
57
|
|
|
'c_attendance_calendar' |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
foreach ($tables as $tableName) { |
61
|
|
|
$table = $schema->getTable($tableName); |
62
|
|
|
if (!$table->hasColumn('duration')) { |
63
|
|
|
$this->connection->executeQuery("ALTER TABLE $tableName ADD duration INT DEFAULT NULL"); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function removeDurationFields(Schema $schema): void |
69
|
|
|
{ |
70
|
|
|
$tables = [ |
71
|
|
|
'course', |
72
|
|
|
'c_survey', |
73
|
|
|
'c_quiz', |
74
|
|
|
'c_quiz_question', |
75
|
|
|
'c_lp', |
76
|
|
|
'c_lp_item', |
77
|
|
|
'c_student_publication', |
78
|
|
|
'c_attendance_calendar' |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
foreach ($tables as $tableName) { |
82
|
|
|
$table = $schema->getTable($tableName); |
83
|
|
|
if ($table->hasColumn('duration')) { |
84
|
|
|
$this->connection->executeQuery("ALTER TABLE $tableName DROP COLUMN duration"); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function renameSettingsTableUp(): void |
90
|
|
|
{ |
91
|
|
|
$schemaManager = $this->connection->createSchemaManager(); |
92
|
|
|
|
93
|
|
|
if ($schemaManager->tablesExist(['settings_current']) && !$schemaManager->tablesExist(['settings'])) { |
94
|
|
|
$this->connection->executeQuery('RENAME TABLE settings_current TO settings'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function renameSettingsTableDown(): void |
99
|
|
|
{ |
100
|
|
|
$schemaManager = $this->connection->createSchemaManager(); |
101
|
|
|
|
102
|
|
|
if ($schemaManager->tablesExist(['settings']) && !$schemaManager->tablesExist(['settings_current'])) { |
103
|
|
|
$this->connection->executeQuery('RENAME TABLE settings TO settings_current'); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|