3 namespace Application\Migrations
;
5 use Doctrine\DBAL\Migrations\AbstractMigration
;
6 use Doctrine\DBAL\Migrations\SkipMigrationException
;
7 use Doctrine\DBAL\Schema\Schema
;
8 use Symfony\Component\DependencyInjection\ContainerAwareInterface
;
9 use Symfony\Component\DependencyInjection\ContainerInterface
;
12 * Added pocket_consumer_key field on wallabag_config.
14 class Version20161001072726
extends AbstractMigration
implements ContainerAwareInterface
17 * @var ContainerInterface
21 public function setContainer(ContainerInterface
$container = null)
23 $this->container
= $container;
27 * @param Schema $schema
29 public function up(Schema
$schema)
31 $this->skipIf('sqlite' === $this->connection
->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
33 // remove all FK from entry_tag
34 switch ($this->connection
->getDatabasePlatform()->getName()) {
36 $query = $this->connection
->query("
37 SELECT CONSTRAINT_NAME
38 FROM information_schema.key_column_usage
39 WHERE TABLE_NAME = '" . $this->getTable('entry_tag') . "' AND CONSTRAINT_NAME LIKE 'FK_%'
40 AND TABLE_SCHEMA = '" . $this->connection
->getDatabase() . "'"
44 foreach ($query->fetchAll() as $fk) {
45 $this->addSql('ALTER TABLE ' . $this->getTable('entry_tag') . ' DROP FOREIGN KEY ' . $fk['CONSTRAINT_NAME']);
49 // http://dba.stackexchange.com/questions/36979/retrieving-all-pk-and-fk
50 $query = $this->connection
->query("
51 SELECT conrelid::regclass AS table_from
53 ,pg_get_constraintdef(c.oid)
55 JOIN pg_namespace n ON n.oid = c.connamespace
57 AND conrelid::regclass::text = '" . $this->getTable('entry_tag') . "'
58 AND n.nspname = 'public';"
62 foreach ($query->fetchAll() as $fk) {
63 $this->addSql('ALTER TABLE ' . $this->getTable('entry_tag') . ' DROP CONSTRAINT ' . $fk['conname']);
68 $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');
69 $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');
71 // remove entry FK from annotation
73 switch ($this->connection
->getDatabasePlatform()->getName()) {
75 $query = $this->connection
->query("
76 SELECT CONSTRAINT_NAME
77 FROM information_schema.key_column_usage
78 WHERE TABLE_NAME = '" . $this->getTable('annotation') . "'
79 AND CONSTRAINT_NAME LIKE 'FK_%'
80 AND COLUMN_NAME = 'entry_id'
81 AND TABLE_SCHEMA = '" . $this->connection
->getDatabase() . "'"
85 foreach ($query->fetchAll() as $fk) {
86 $this->addSql('ALTER TABLE ' . $this->getTable('annotation') . ' DROP FOREIGN KEY ' . $fk['CONSTRAINT_NAME']);
90 // http://dba.stackexchange.com/questions/36979/retrieving-all-pk-and-fk
91 $query = $this->connection
->query("
92 SELECT conrelid::regclass AS table_from
94 ,pg_get_constraintdef(c.oid)
96 JOIN pg_namespace n ON n.oid = c.connamespace
98 AND conrelid::regclass::text = '" . $this->getTable('annotation') . "'
99 AND n.nspname = 'public'
100 AND pg_get_constraintdef(c.oid) LIKE '%entry_id%';"
104 foreach ($query->fetchAll() as $fk) {
105 $this->addSql('ALTER TABLE ' . $this->getTable('annotation') . ' DROP CONSTRAINT ' . $fk['conname']);
110 $this->addSql('ALTER TABLE ' . $this->getTable('annotation') . ' ADD CONSTRAINT FK_annotation_entry FOREIGN KEY (entry_id) REFERENCES ' . $this->getTable('entry') . ' (id) ON DELETE CASCADE');
114 * @param Schema $schema
116 public function down(Schema
$schema)
118 throw new SkipMigrationException('Too complex ...');
121 private function getTable($tableName)
123 return $this->container
->getParameter('database_table_prefix') . $tableName;