]> git.immae.eu Git - github/wallabag/wallabag.git/blame - app/DoctrineMigrations/Version20161001072726.php
Jump to 2.3.6-dev and update release process
[github/wallabag/wallabag.git] / app / DoctrineMigrations / Version20161001072726.php
CommitLineData
206bade5
JB
1<?php
2
3namespace Application\Migrations;
4
f808b016 5use Doctrine\DBAL\Migrations\SkipMigrationException;
206bade5 6use Doctrine\DBAL\Schema\Schema;
bfe7a692 7use Wallabag\CoreBundle\Doctrine\WallabagMigration;
206bade5 8
b87f1712 9/**
01736b5a 10 * Added pocket_consumer_key field on wallabag_config.
b87f1712 11 */
bfe7a692 12class Version20161001072726 extends WallabagMigration
206bade5 13{
206bade5
JB
14 /**
15 * @param Schema $schema
16 */
17 public function up(Schema $schema)
18 {
3ef055ce 19 $this->skipIf('sqlite' === $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
5ce15289
JB
20
21 // remove all FK from entry_tag
a72f3dc3
JB
22 switch ($this->connection->getDatabasePlatform()->getName()) {
23 case 'mysql':
24 $query = $this->connection->query("
25 SELECT CONSTRAINT_NAME
26 FROM information_schema.key_column_usage
49b4c875 27 WHERE TABLE_NAME = '" . $this->getTable('entry_tag', WallabagMigration::UN_ESCAPED_TABLE) . "' AND CONSTRAINT_NAME LIKE 'FK_%'
f808b016 28 AND TABLE_SCHEMA = '" . $this->connection->getDatabase() . "'"
a72f3dc3
JB
29 );
30 $query->execute();
5ce15289 31
a72f3dc3 32 foreach ($query->fetchAll() as $fk) {
f808b016 33 $this->addSql('ALTER TABLE ' . $this->getTable('entry_tag') . ' DROP FOREIGN KEY ' . $fk['CONSTRAINT_NAME']);
a72f3dc3
JB
34 }
35 break;
a72f3dc3
JB
36 case 'postgresql':
37 // http://dba.stackexchange.com/questions/36979/retrieving-all-pk-and-fk
38 $query = $this->connection->query("
39 SELECT conrelid::regclass AS table_from
40 ,conname
41 ,pg_get_constraintdef(c.oid)
42 FROM pg_constraint c
43 JOIN pg_namespace n ON n.oid = c.connamespace
44 WHERE contype = 'f'
49b4c875 45 AND conrelid::regclass::text = '" . $this->getTable('entry_tag', WallabagMigration::UN_ESCAPED_TABLE) . "'
a72f3dc3
JB
46 AND n.nspname = 'public';"
47 );
48 $query->execute();
49
50 foreach ($query->fetchAll() as $fk) {
f808b016 51 $this->addSql('ALTER TABLE ' . $this->getTable('entry_tag') . ' DROP CONSTRAINT ' . $fk['conname']);
a72f3dc3
JB
52 }
53 break;
5ce15289
JB
54 }
55
f808b016
JB
56 $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');
57 $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');
5ce15289
JB
58
59 // remove entry FK from annotation
5ce15289 60
a72f3dc3
JB
61 switch ($this->connection->getDatabasePlatform()->getName()) {
62 case 'mysql':
63 $query = $this->connection->query("
64 SELECT CONSTRAINT_NAME
65 FROM information_schema.key_column_usage
49b4c875 66 WHERE TABLE_NAME = '" . $this->getTable('annotation', WallabagMigration::UN_ESCAPED_TABLE) . "'
a72f3dc3
JB
67 AND CONSTRAINT_NAME LIKE 'FK_%'
68 AND COLUMN_NAME = 'entry_id'
f808b016 69 AND TABLE_SCHEMA = '" . $this->connection->getDatabase() . "'"
a72f3dc3
JB
70 );
71 $query->execute();
72
73 foreach ($query->fetchAll() as $fk) {
f808b016 74 $this->addSql('ALTER TABLE ' . $this->getTable('annotation') . ' DROP FOREIGN KEY ' . $fk['CONSTRAINT_NAME']);
a72f3dc3
JB
75 }
76 break;
a72f3dc3
JB
77 case 'postgresql':
78 // http://dba.stackexchange.com/questions/36979/retrieving-all-pk-and-fk
79 $query = $this->connection->query("
80 SELECT conrelid::regclass AS table_from
81 ,conname
82 ,pg_get_constraintdef(c.oid)
83 FROM pg_constraint c
84 JOIN pg_namespace n ON n.oid = c.connamespace
85 WHERE contype = 'f'
49b4c875 86 AND conrelid::regclass::text = '" . $this->getTable('annotation', WallabagMigration::UN_ESCAPED_TABLE) . "'
a72f3dc3
JB
87 AND n.nspname = 'public'
88 AND pg_get_constraintdef(c.oid) LIKE '%entry_id%';"
89 );
90 $query->execute();
91
92 foreach ($query->fetchAll() as $fk) {
f808b016 93 $this->addSql('ALTER TABLE ' . $this->getTable('annotation') . ' DROP CONSTRAINT ' . $fk['conname']);
a72f3dc3
JB
94 }
95 break;
5ce15289
JB
96 }
97
f808b016 98 $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
99 }
100
101 /**
102 * @param Schema $schema
103 */
104 public function down(Schema $schema)
105 {
5ce15289 106 throw new SkipMigrationException('Too complex ...');
206bade5
JB
107 }
108}