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
;
10 class Version20161001072726
extends AbstractMigration
implements ContainerAwareInterface
13 * @var ContainerInterface
17 public function setContainer(ContainerInterface
$container = null)
19 $this->container
= $container;
22 private function getTable($tableName)
24 return $this->container
->getParameter('database_table_prefix').$tableName;
28 * @param Schema $schema
30 public function up(Schema
$schema)
32 $this->skipIf($this->connection
->getDatabasePlatform()->getName() == 'sqlite', 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
34 // remove all FK from entry_tag
35 switch ($this->connection
->getDatabasePlatform()->getName()) {
37 $query = $this->connection
->query("
38 SELECT CONSTRAINT_NAME
39 FROM information_schema.key_column_usage
40 WHERE TABLE_NAME = '".$this->getTable('entry_tag')."' AND CONSTRAINT_NAME LIKE 'FK_%'
41 AND TABLE_SCHEMA = '".$this->connection
->getDatabase()."'"
45 foreach ($query->fetchAll() as $fk) {
46 $this->addSql('ALTER TABLE '.$this->getTable('entry_tag').' DROP FOREIGN KEY '.$fk['CONSTRAINT_NAME']);
51 // http://dba.stackexchange.com/questions/36979/retrieving-all-pk-and-fk
52 $query = $this->connection
->query("
53 SELECT conrelid::regclass AS table_from
55 ,pg_get_constraintdef(c.oid)
57 JOIN pg_namespace n ON n.oid = c.connamespace
59 AND conrelid::regclass::text = '".$this->getTable('entry_tag')."'
60 AND n.nspname = 'public';"
64 foreach ($query->fetchAll() as $fk) {
65 $this->addSql('ALTER TABLE '.$this->getTable('entry_tag').' DROP CONSTRAINT '.$fk['conname']);
70 $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');
71 $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');
73 // remove entry FK from annotation
75 switch ($this->connection
->getDatabasePlatform()->getName()) {
77 $query = $this->connection
->query("
78 SELECT CONSTRAINT_NAME
79 FROM information_schema.key_column_usage
80 WHERE TABLE_NAME = '".$this->getTable('annotation')."'
81 AND CONSTRAINT_NAME LIKE 'FK_%'
82 AND COLUMN_NAME = 'entry_id'
83 AND TABLE_SCHEMA = '".$this->connection
->getDatabase()."'"
87 foreach ($query->fetchAll() as $fk) {
88 $this->addSql('ALTER TABLE '.$this->getTable('annotation').' DROP FOREIGN KEY '.$fk['CONSTRAINT_NAME']);
93 // http://dba.stackexchange.com/questions/36979/retrieving-all-pk-and-fk
94 $query = $this->connection
->query("
95 SELECT conrelid::regclass AS table_from
97 ,pg_get_constraintdef(c.oid)
99 JOIN pg_namespace n ON n.oid = c.connamespace
101 AND conrelid::regclass::text = '".$this->getTable('annotation')."'
102 AND n.nspname = 'public'
103 AND pg_get_constraintdef(c.oid) LIKE '%entry_id%';"
107 foreach ($query->fetchAll() as $fk) {
108 $this->addSql('ALTER TABLE '.$this->getTable('annotation').' DROP CONSTRAINT '.$fk['conname']);
113 $this->addSql('ALTER TABLE '.$this->getTable('annotation').' ADD CONSTRAINT FK_annotation_entry FOREIGN KEY (entry_id) REFERENCES '.$this->getTable('entry').' (id) ON DELETE CASCADE');
117 * @param Schema $schema
119 public function down(Schema
$schema)
121 throw new SkipMigrationException('Too complex ...');