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
15 * @param Schema $schema
17 public function up(Schema
$schema)
19 $this->skipIf('sqlite' === $this->connection
->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
21 // remove all FK from entry_tag
22 switch ($this->connection
->getDatabasePlatform()->getName()) {
24 $query = $this->connection
->query("
25 SELECT CONSTRAINT_NAME
26 FROM information_schema.key_column_usage
27 WHERE TABLE_NAME = '" . $this->getTable('entry_tag', WallabagMigration
::UN_ESCAPED_TABLE
) . "' AND CONSTRAINT_NAME LIKE 'FK_%'
28 AND TABLE_SCHEMA = '" . $this->connection
->getDatabase() . "'"
32 foreach ($query->fetchAll() as $fk) {
33 $this->addSql('ALTER TABLE ' . $this->getTable('entry_tag') . ' DROP FOREIGN KEY ' . $fk['CONSTRAINT_NAME']);
37 // http://dba.stackexchange.com/questions/36979/retrieving-all-pk-and-fk
38 $query = $this->connection
->query("
39 SELECT conrelid::regclass AS table_from
41 ,pg_get_constraintdef(c.oid)
43 JOIN pg_namespace n ON n.oid = c.connamespace
45 AND conrelid::regclass::text = '" . $this->getTable('entry_tag', WallabagMigration
::UN_ESCAPED_TABLE
) . "'
46 AND n.nspname = 'public';"
50 foreach ($query->fetchAll() as $fk) {
51 $this->addSql('ALTER TABLE ' . $this->getTable('entry_tag') . ' DROP CONSTRAINT ' . $fk['conname']);
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');
59 // remove entry FK from annotation
61 switch ($this->connection
->getDatabasePlatform()->getName()) {
63 $query = $this->connection
->query("
64 SELECT CONSTRAINT_NAME
65 FROM information_schema.key_column_usage
66 WHERE TABLE_NAME = '" . $this->getTable('annotation', WallabagMigration
::UN_ESCAPED_TABLE
) . "'
67 AND CONSTRAINT_NAME LIKE 'FK_%'
68 AND COLUMN_NAME = 'entry_id'
69 AND TABLE_SCHEMA = '" . $this->connection
->getDatabase() . "'"
73 foreach ($query->fetchAll() as $fk) {
74 $this->addSql('ALTER TABLE ' . $this->getTable('annotation') . ' DROP FOREIGN KEY ' . $fk['CONSTRAINT_NAME']);
78 // http://dba.stackexchange.com/questions/36979/retrieving-all-pk-and-fk
79 $query = $this->connection
->query("
80 SELECT conrelid::regclass AS table_from
82 ,pg_get_constraintdef(c.oid)
84 JOIN pg_namespace n ON n.oid = c.connamespace
86 AND conrelid::regclass::text = '" . $this->getTable('annotation', WallabagMigration
::UN_ESCAPED_TABLE
) . "'
87 AND n.nspname = 'public'
88 AND pg_get_constraintdef(c.oid) LIKE '%entry_id%';"
92 foreach ($query->fetchAll() as $fk) {
93 $this->addSql('ALTER TABLE ' . $this->getTable('annotation') . ' DROP CONSTRAINT ' . $fk['conname']);
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');
102 * @param Schema $schema
104 public function down(Schema
$schema)
106 throw new SkipMigrationException('Too complex ...');