]>
Commit | Line | Data |
---|---|---|
206bade5 JB |
1 | <?php |
2 | ||
3 | namespace Application\Migrations; | |
4 | ||
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 | ||
10 | class Version20161001072726 extends AbstractMigration implements ContainerAwareInterface | |
11 | { | |
12 | /** | |
13 | * @var ContainerInterface | |
14 | */ | |
15 | private $container; | |
16 | ||
17 | public function setContainer(ContainerInterface $container = null) | |
18 | { | |
19 | $this->container = $container; | |
20 | } | |
21 | ||
22 | private function getTable($tableName) | |
23 | { | |
24 | return $this->container->getParameter('database_table_prefix') . $tableName; | |
25 | } | |
26 | ||
27 | /** | |
28 | * @param Schema $schema | |
29 | */ | |
30 | public function up(Schema $schema) | |
31 | { | |
5ce15289 JB |
32 | $this->skipIf($this->connection->getDatabasePlatform()->getName() == 'sqlite', 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.'); |
33 | ||
34 | // remove all FK from entry_tag | |
35 | $query = $this->connection->query("SELECT CONSTRAINT_NAME FROM information_schema.key_column_usage WHERE TABLE_NAME = '".$this->getTable('entry_tag')."' AND CONSTRAINT_NAME LIKE 'FK_%' AND TABLE_SCHEMA = '".$this->connection->getDatabase()."'"); | |
36 | $query->execute(); | |
37 | ||
38 | foreach ($query->fetchAll() as $fk) { | |
39 | $this->addSql('ALTER TABLE '.$this->getTable('entry_tag').' DROP FOREIGN KEY '.$fk['CONSTRAINT_NAME']); | |
40 | } | |
41 | ||
42 | $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'); | |
43 | $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'); | |
44 | ||
45 | // remove entry FK from annotation | |
46 | $query = $this->connection->query("SELECT CONSTRAINT_NAME FROM information_schema.key_column_usage WHERE TABLE_NAME = '".$this->getTable('annotation')."' AND CONSTRAINT_NAME LIKE 'FK_%' and COLUMN_NAME = 'entry_id' AND TABLE_SCHEMA = '".$this->connection->getDatabase()."'"); | |
47 | $query->execute(); | |
48 | ||
49 | foreach ($query->fetchAll() as $fk) { | |
50 | $this->addSql('ALTER TABLE '.$this->getTable('annotation').' DROP FOREIGN KEY '.$fk['CONSTRAINT_NAME']); | |
51 | } | |
52 | ||
53 | $this->addSql('ALTER TABLE '.$this->getTable('annotation').' ADD CONSTRAINT FK_annotation_entry FOREIGN KEY (entry_id) REFERENCES '.$this->getTable('entry').' (id) ON DELETE CASCADE'); | |
206bade5 JB |
54 | } |
55 | ||
56 | /** | |
57 | * @param Schema $schema | |
58 | */ | |
59 | public function down(Schema $schema) | |
60 | { | |
5ce15289 | 61 | throw new SkipMigrationException('Too complex ...'); |
206bade5 JB |
62 | } |
63 | } |