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 Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
10
|
|
|
use Doctrine\DBAL\Schema\AbstractSchemaManager; |
11
|
|
|
use Doctrine\DBAL\Schema\Schema; |
12
|
|
|
use Doctrine\DBAL\Schema\Table; |
13
|
|
|
|
14
|
|
|
final class Version20250918170500 extends AbstractMigrationChamilo |
15
|
|
|
{ |
16
|
|
|
public function getDescription(): string |
17
|
|
|
{ |
18
|
|
|
return 'Dropbox: link c_dropbox_file to resource_node with FK + unique index, and set integer defaults.'; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function up(Schema $schema): void |
22
|
|
|
{ |
23
|
|
|
$sm = $this->connection->createSchemaManager(); |
24
|
|
|
|
25
|
|
|
if (!$sm->tablesExist(['c_dropbox_file'])) { |
26
|
|
|
return; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$table = $sm->introspectTable('c_dropbox_file'); |
30
|
|
|
|
31
|
|
|
if (!$table->hasColumn('resource_node_id')) { |
32
|
|
|
$this->addSql("ALTER TABLE c_dropbox_file ADD resource_node_id INT DEFAULT NULL"); |
33
|
|
|
$table = $sm->introspectTable('c_dropbox_file'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->addSql(" |
37
|
|
|
ALTER TABLE c_dropbox_file |
38
|
|
|
CHANGE c_id c_id INT DEFAULT 0 NOT NULL, |
39
|
|
|
CHANGE filesize filesize INT DEFAULT 0 NOT NULL, |
40
|
|
|
CHANGE cat_id cat_id INT DEFAULT 0 NOT NULL, |
41
|
|
|
CHANGE session_id session_id INT DEFAULT 0 NOT NULL |
42
|
|
|
"); |
43
|
|
|
|
44
|
|
|
$fkName = 'FK_4D71B46C1BAD783F'; |
45
|
|
|
if (!$this->foreignKeyExists($sm, 'c_dropbox_file', $fkName)) { |
46
|
|
|
$this->addSql(" |
47
|
|
|
ALTER TABLE c_dropbox_file |
48
|
|
|
ADD CONSTRAINT $fkName |
49
|
|
|
FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE |
50
|
|
|
"); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$uniqueName = 'UNIQ_4D71B46C1BAD783F'; |
54
|
|
|
if (!$this->indexExists($table, $uniqueName)) { |
55
|
|
|
$this->addSql("CREATE UNIQUE INDEX $uniqueName ON c_dropbox_file (resource_node_id)"); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function foreignKeyExists(AbstractSchemaManager $sm, string $tableName, string $fkName): bool |
60
|
|
|
{ |
61
|
|
|
foreach ($sm->listTableForeignKeys($tableName) as $fk) { |
62
|
|
|
if (strcasecmp($fk->getName(), $fkName) === 0) { |
63
|
|
|
return true; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function indexExists(Table $table, string $indexName): bool |
70
|
|
|
{ |
71
|
|
|
foreach ($table->getIndexes() as $idx) { |
72
|
|
|
if (strcasecmp($idx->getName(), $indexName) === 0) { |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function down(Schema $schema): void |
80
|
|
|
{ |
81
|
|
|
// Drop unique index + FK + column (best-effort rollback) |
82
|
|
|
$this->addSql("DROP INDEX UNIQ_4D71B46C1BAD783F ON c_dropbox_file"); |
83
|
|
|
$this->addSql("ALTER TABLE c_dropbox_file DROP FOREIGN KEY FK_4D71B46C1BAD783F"); |
84
|
|
|
$this->addSql("ALTER TABLE c_dropbox_file DROP COLUMN resource_node_id"); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|