]>
Commit | Line | Data |
---|---|---|
206bade5 JB |
1 | <?php |
2 | ||
3 | namespace Application\Migrations; | |
4 | ||
5 | use Doctrine\DBAL\Migrations\AbstractMigration; | |
6 | use Doctrine\DBAL\Schema\Schema; | |
7 | use Symfony\Component\DependencyInjection\ContainerAwareInterface; | |
8 | use Symfony\Component\DependencyInjection\ContainerInterface; | |
9 | ||
b87f1712 NL |
10 | /** |
11 | * Added pocket_consumer_key field on wallabag_config | |
12 | */ | |
206bade5 JB |
13 | class Version20161001072726 extends AbstractMigration implements ContainerAwareInterface |
14 | { | |
15 | /** | |
16 | * @var ContainerInterface | |
17 | */ | |
18 | private $container; | |
19 | ||
20 | public function setContainer(ContainerInterface $container = null) | |
21 | { | |
22 | $this->container = $container; | |
23 | } | |
24 | ||
25 | private function getTable($tableName) | |
26 | { | |
a4d55a91 | 27 | return $this->container->getParameter('database_table_prefix').$tableName; |
206bade5 JB |
28 | } |
29 | ||
30 | /** | |
31 | * @param Schema $schema | |
32 | */ | |
33 | public function up(Schema $schema) | |
34 | { | |
5ce15289 JB |
35 | $this->skipIf($this->connection->getDatabasePlatform()->getName() == 'sqlite', 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.'); |
36 | ||
37 | // remove all FK from entry_tag | |
a72f3dc3 JB |
38 | switch ($this->connection->getDatabasePlatform()->getName()) { |
39 | case 'mysql': | |
40 | $query = $this->connection->query(" | |
41 | SELECT CONSTRAINT_NAME | |
42 | FROM information_schema.key_column_usage | |
43 | WHERE TABLE_NAME = '".$this->getTable('entry_tag')."' AND CONSTRAINT_NAME LIKE 'FK_%' | |
44 | AND TABLE_SCHEMA = '".$this->connection->getDatabase()."'" | |
45 | ); | |
46 | $query->execute(); | |
5ce15289 | 47 | |
a72f3dc3 JB |
48 | foreach ($query->fetchAll() as $fk) { |
49 | $this->addSql('ALTER TABLE '.$this->getTable('entry_tag').' DROP FOREIGN KEY '.$fk['CONSTRAINT_NAME']); | |
50 | } | |
51 | break; | |
52 | ||
53 | case 'postgresql': | |
54 | // http://dba.stackexchange.com/questions/36979/retrieving-all-pk-and-fk | |
55 | $query = $this->connection->query(" | |
56 | SELECT conrelid::regclass AS table_from | |
57 | ,conname | |
58 | ,pg_get_constraintdef(c.oid) | |
59 | FROM pg_constraint c | |
60 | JOIN pg_namespace n ON n.oid = c.connamespace | |
61 | WHERE contype = 'f' | |
62 | AND conrelid::regclass::text = '".$this->getTable('entry_tag')."' | |
63 | AND n.nspname = 'public';" | |
64 | ); | |
65 | $query->execute(); | |
66 | ||
67 | foreach ($query->fetchAll() as $fk) { | |
68 | $this->addSql('ALTER TABLE '.$this->getTable('entry_tag').' DROP CONSTRAINT '.$fk['conname']); | |
69 | } | |
70 | break; | |
5ce15289 JB |
71 | } |
72 | ||
73 | $this->addSql('ALTER TABLE '.$this->getTable('entry_tag').' ADD CONSTRAINT FK_entry_tag_entry FOREIGN KEY (entry_id) REFERENCES '.$this->getTable('entry').' (id) ON DELETE CASCADE'); | |
74 | $this->addSql('ALTER TABLE '.$this->getTable('entry_tag').' ADD CONSTRAINT FK_entry_tag_tag FOREIGN KEY (tag_id) REFERENCES '.$this->getTable('tag').' (id) ON DELETE CASCADE'); | |
75 | ||
76 | // remove entry FK from annotation | |
5ce15289 | 77 | |
a72f3dc3 JB |
78 | switch ($this->connection->getDatabasePlatform()->getName()) { |
79 | case 'mysql': | |
80 | $query = $this->connection->query(" | |
81 | SELECT CONSTRAINT_NAME | |
82 | FROM information_schema.key_column_usage | |
83 | WHERE TABLE_NAME = '".$this->getTable('annotation')."' | |
84 | AND CONSTRAINT_NAME LIKE 'FK_%' | |
85 | AND COLUMN_NAME = 'entry_id' | |
86 | AND TABLE_SCHEMA = '".$this->connection->getDatabase()."'" | |
87 | ); | |
88 | $query->execute(); | |
89 | ||
90 | foreach ($query->fetchAll() as $fk) { | |
91 | $this->addSql('ALTER TABLE '.$this->getTable('annotation').' DROP FOREIGN KEY '.$fk['CONSTRAINT_NAME']); | |
92 | } | |
93 | break; | |
94 | ||
95 | case 'postgresql': | |
96 | // http://dba.stackexchange.com/questions/36979/retrieving-all-pk-and-fk | |
97 | $query = $this->connection->query(" | |
98 | SELECT conrelid::regclass AS table_from | |
99 | ,conname | |
100 | ,pg_get_constraintdef(c.oid) | |
101 | FROM pg_constraint c | |
102 | JOIN pg_namespace n ON n.oid = c.connamespace | |
103 | WHERE contype = 'f' | |
104 | AND conrelid::regclass::text = '".$this->getTable('annotation')."' | |
105 | AND n.nspname = 'public' | |
106 | AND pg_get_constraintdef(c.oid) LIKE '%entry_id%';" | |
107 | ); | |
108 | $query->execute(); | |
109 | ||
110 | foreach ($query->fetchAll() as $fk) { | |
111 | $this->addSql('ALTER TABLE '.$this->getTable('annotation').' DROP CONSTRAINT '.$fk['conname']); | |
112 | } | |
113 | break; | |
5ce15289 JB |
114 | } |
115 | ||
116 | $this->addSql('ALTER TABLE '.$this->getTable('annotation').' ADD CONSTRAINT FK_annotation_entry FOREIGN KEY (entry_id) REFERENCES '.$this->getTable('entry').' (id) ON DELETE CASCADE'); | |
206bade5 JB |
117 | } |
118 | ||
119 | /** | |
120 | * @param Schema $schema | |
121 | */ | |
122 | public function down(Schema $schema) | |
123 | { | |
5ce15289 | 124 | throw new SkipMigrationException('Too complex ...'); |
206bade5 JB |
125 | } |
126 | } |