3 namespace Application\Migrations
;
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 use Doctrine\DBAL\Migrations\SkipMigrationException
;
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;
26 private function getTable($tableName)
28 return $this->container
->getParameter('database_table_prefix').$tableName;
32 * @param Schema $schema
34 public function up(Schema
$schema)
36 $this->skipIf($this->connection
->getDatabasePlatform()->getName() == 'sqlite', 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
38 // remove all FK from entry_tag
39 switch ($this->connection
->getDatabasePlatform()->getName()) {
41 $query = $this->connection
->query("
42 SELECT CONSTRAINT_NAME
43 FROM information_schema.key_column_usage
44 WHERE TABLE_NAME = '".$this->getTable('entry_tag')."' AND CONSTRAINT_NAME LIKE 'FK_%'
45 AND TABLE_SCHEMA = '".$this->connection
->getDatabase()."'"
49 foreach ($query->fetchAll() as $fk) {
50 $this->addSql('ALTER TABLE '.$this->getTable('entry_tag').' DROP FOREIGN KEY '.$fk['CONSTRAINT_NAME']);
55 // http://dba.stackexchange.com/questions/36979/retrieving-all-pk-and-fk
56 $query = $this->connection
->query("
57 SELECT conrelid::regclass AS table_from
59 ,pg_get_constraintdef(c.oid)
61 JOIN pg_namespace n ON n.oid = c.connamespace
63 AND conrelid::regclass::text = '".$this->getTable('entry_tag')."'
64 AND n.nspname = 'public';"
68 foreach ($query->fetchAll() as $fk) {
69 $this->addSql('ALTER TABLE '.$this->getTable('entry_tag').' DROP CONSTRAINT '.$fk['conname']);
74 $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');
75 $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');
77 // remove entry FK from annotation
79 switch ($this->connection
->getDatabasePlatform()->getName()) {
81 $query = $this->connection
->query("
82 SELECT CONSTRAINT_NAME
83 FROM information_schema.key_column_usage
84 WHERE TABLE_NAME = '".$this->getTable('annotation')."'
85 AND CONSTRAINT_NAME LIKE 'FK_%'
86 AND COLUMN_NAME = 'entry_id'
87 AND TABLE_SCHEMA = '".$this->connection
->getDatabase()."'"
91 foreach ($query->fetchAll() as $fk) {
92 $this->addSql('ALTER TABLE '.$this->getTable('annotation').' DROP FOREIGN KEY '.$fk['CONSTRAINT_NAME']);
97 // http://dba.stackexchange.com/questions/36979/retrieving-all-pk-and-fk
98 $query = $this->connection
->query("
99 SELECT conrelid::regclass AS table_from
101 ,pg_get_constraintdef(c.oid)
103 JOIN pg_namespace n ON n.oid = c.connamespace
105 AND conrelid::regclass::text = '".$this->getTable('annotation')."'
106 AND n.nspname = 'public'
107 AND pg_get_constraintdef(c.oid) LIKE '%entry_id%';"
111 foreach ($query->fetchAll() as $fk) {
112 $this->addSql('ALTER TABLE '.$this->getTable('annotation').' DROP CONSTRAINT '.$fk['conname']);
117 $this->addSql('ALTER TABLE '.$this->getTable('annotation').' ADD CONSTRAINT FK_annotation_entry FOREIGN KEY (entry_id) REFERENCES '.$this->getTable('entry').' (id) ON DELETE CASCADE');
121 * @param Schema $schema
123 public function down(Schema
$schema)
125 throw new SkipMigrationException('Too complex ...');