3 namespace Application\Migrations
;
5 use Doctrine\DBAL\Migrations\SkipMigrationException
;
6 use Doctrine\DBAL\Schema\Schema
;
7 use Wallabag\CoreBundle\Doctrine\WallabagMigration
;
10 * Added pocket_consumer_key field on wallabag_config.
12 class Version20161001072726
extends WallabagMigration
14 public function up(Schema
$schema)
16 $this->skipIf('sqlite' === $this->connection
->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
18 // remove all FK from entry_tag
19 switch ($this->connection
->getDatabasePlatform()->getName()) {
21 $query = $this->connection
->query("
22 SELECT CONSTRAINT_NAME
23 FROM information_schema.key_column_usage
24 WHERE TABLE_NAME = '" . $this->getTable('entry_tag', WallabagMigration
::UN_ESCAPED_TABLE
) . "' AND CONSTRAINT_NAME LIKE 'FK_%'
25 AND TABLE_SCHEMA = '" . $this->connection
->getDatabase() . "'"
29 foreach ($query->fetchAll() as $fk) {
30 $this->addSql('ALTER TABLE ' . $this->getTable('entry_tag') . ' DROP FOREIGN KEY ' . $fk['CONSTRAINT_NAME']);
34 // http://dba.stackexchange.com/questions/36979/retrieving-all-pk-and-fk
35 $query = $this->connection
->query("
36 SELECT conrelid::regclass AS table_from
38 ,pg_get_constraintdef(c.oid)
40 JOIN pg_namespace n ON n.oid = c.connamespace
42 AND conrelid::regclass::text = '" . $this->getTable('entry_tag', WallabagMigration
::UN_ESCAPED_TABLE
) . "'
43 AND n.nspname = 'public';"
47 foreach ($query->fetchAll() as $fk) {
48 $this->addSql('ALTER TABLE ' . $this->getTable('entry_tag') . ' DROP CONSTRAINT ' . $fk['conname']);
53 $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');
54 $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');
56 // remove entry FK from annotation
58 switch ($this->connection
->getDatabasePlatform()->getName()) {
60 $query = $this->connection
->query("
61 SELECT CONSTRAINT_NAME
62 FROM information_schema.key_column_usage
63 WHERE TABLE_NAME = '" . $this->getTable('annotation', WallabagMigration
::UN_ESCAPED_TABLE
) . "'
64 AND CONSTRAINT_NAME LIKE 'FK_%'
65 AND COLUMN_NAME = 'entry_id'
66 AND TABLE_SCHEMA = '" . $this->connection
->getDatabase() . "'"
70 foreach ($query->fetchAll() as $fk) {
71 $this->addSql('ALTER TABLE ' . $this->getTable('annotation') . ' DROP FOREIGN KEY ' . $fk['CONSTRAINT_NAME']);
75 // http://dba.stackexchange.com/questions/36979/retrieving-all-pk-and-fk
76 $query = $this->connection
->query("
77 SELECT conrelid::regclass AS table_from
79 ,pg_get_constraintdef(c.oid)
81 JOIN pg_namespace n ON n.oid = c.connamespace
83 AND conrelid::regclass::text = '" . $this->getTable('annotation', WallabagMigration
::UN_ESCAPED_TABLE
) . "'
84 AND n.nspname = 'public'
85 AND pg_get_constraintdef(c.oid) LIKE '%entry_id%';"
89 foreach ($query->fetchAll() as $fk) {
90 $this->addSql('ALTER TABLE ' . $this->getTable('annotation') . ' DROP CONSTRAINT ' . $fk['conname']);
95 $this->addSql('ALTER TABLE ' . $this->getTable('annotation') . ' ADD CONSTRAINT FK_annotation_entry FOREIGN KEY (entry_id) REFERENCES ' . $this->getTable('entry') . ' (id) ON DELETE CASCADE');
98 public function down(Schema
$schema)
100 throw new SkipMigrationException('Too complex ...');