]> git.immae.eu Git - github/wallabag/wallabag.git/blame - app/DoctrineMigrations/Version20161001072726.php
Add a real configuration for CS-Fixer
[github/wallabag/wallabag.git] / app / DoctrineMigrations / Version20161001072726.php
CommitLineData
206bade5
JB
1<?php
2
3namespace Application\Migrations;
4
5use Doctrine\DBAL\Migrations\AbstractMigration;
f808b016 6use Doctrine\DBAL\Migrations\SkipMigrationException;
206bade5
JB
7use Doctrine\DBAL\Schema\Schema;
8use Symfony\Component\DependencyInjection\ContainerAwareInterface;
9use Symfony\Component\DependencyInjection\ContainerInterface;
10
b87f1712 11/**
01736b5a 12 * Added pocket_consumer_key field on wallabag_config.
b87f1712 13 */
206bade5
JB
14class Version20161001072726 extends AbstractMigration implements ContainerAwareInterface
15{
16 /**
17 * @var ContainerInterface
18 */
19 private $container;
20
21 public function setContainer(ContainerInterface $container = null)
22 {
23 $this->container = $container;
24 }
25
206bade5
JB
26 /**
27 * @param Schema $schema
28 */
29 public function up(Schema $schema)
30 {
f808b016 31 $this->skipIf($this->connection->getDatabasePlatform()->getName() === 'sqlite', 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
5ce15289
JB
32
33 // remove all FK from entry_tag
a72f3dc3
JB
34 switch ($this->connection->getDatabasePlatform()->getName()) {
35 case 'mysql':
36 $query = $this->connection->query("
37 SELECT CONSTRAINT_NAME
38 FROM information_schema.key_column_usage
f808b016
JB
39 WHERE TABLE_NAME = '" . $this->getTable('entry_tag') . "' AND CONSTRAINT_NAME LIKE 'FK_%'
40 AND TABLE_SCHEMA = '" . $this->connection->getDatabase() . "'"
a72f3dc3
JB
41 );
42 $query->execute();
5ce15289 43
a72f3dc3 44 foreach ($query->fetchAll() as $fk) {
f808b016 45 $this->addSql('ALTER TABLE ' . $this->getTable('entry_tag') . ' DROP FOREIGN KEY ' . $fk['CONSTRAINT_NAME']);
a72f3dc3
JB
46 }
47 break;
a72f3dc3
JB
48 case 'postgresql':
49 // http://dba.stackexchange.com/questions/36979/retrieving-all-pk-and-fk
50 $query = $this->connection->query("
51 SELECT conrelid::regclass AS table_from
52 ,conname
53 ,pg_get_constraintdef(c.oid)
54 FROM pg_constraint c
55 JOIN pg_namespace n ON n.oid = c.connamespace
56 WHERE contype = 'f'
f808b016 57 AND conrelid::regclass::text = '" . $this->getTable('entry_tag') . "'
a72f3dc3
JB
58 AND n.nspname = 'public';"
59 );
60 $query->execute();
61
62 foreach ($query->fetchAll() as $fk) {
f808b016 63 $this->addSql('ALTER TABLE ' . $this->getTable('entry_tag') . ' DROP CONSTRAINT ' . $fk['conname']);
a72f3dc3
JB
64 }
65 break;
5ce15289
JB
66 }
67
f808b016
JB
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');
5ce15289
JB
70
71 // remove entry FK from annotation
5ce15289 72
a72f3dc3
JB
73 switch ($this->connection->getDatabasePlatform()->getName()) {
74 case 'mysql':
75 $query = $this->connection->query("
76 SELECT CONSTRAINT_NAME
77 FROM information_schema.key_column_usage
f808b016 78 WHERE TABLE_NAME = '" . $this->getTable('annotation') . "'
a72f3dc3
JB
79 AND CONSTRAINT_NAME LIKE 'FK_%'
80 AND COLUMN_NAME = 'entry_id'
f808b016 81 AND TABLE_SCHEMA = '" . $this->connection->getDatabase() . "'"
a72f3dc3
JB
82 );
83 $query->execute();
84
85 foreach ($query->fetchAll() as $fk) {
f808b016 86 $this->addSql('ALTER TABLE ' . $this->getTable('annotation') . ' DROP FOREIGN KEY ' . $fk['CONSTRAINT_NAME']);
a72f3dc3
JB
87 }
88 break;
a72f3dc3
JB
89 case 'postgresql':
90 // http://dba.stackexchange.com/questions/36979/retrieving-all-pk-and-fk
91 $query = $this->connection->query("
92 SELECT conrelid::regclass AS table_from
93 ,conname
94 ,pg_get_constraintdef(c.oid)
95 FROM pg_constraint c
96 JOIN pg_namespace n ON n.oid = c.connamespace
97 WHERE contype = 'f'
f808b016 98 AND conrelid::regclass::text = '" . $this->getTable('annotation') . "'
a72f3dc3
JB
99 AND n.nspname = 'public'
100 AND pg_get_constraintdef(c.oid) LIKE '%entry_id%';"
101 );
102 $query->execute();
103
104 foreach ($query->fetchAll() as $fk) {
f808b016 105 $this->addSql('ALTER TABLE ' . $this->getTable('annotation') . ' DROP CONSTRAINT ' . $fk['conname']);
a72f3dc3
JB
106 }
107 break;
5ce15289
JB
108 }
109
f808b016 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');
206bade5
JB
111 }
112
113 /**
114 * @param Schema $schema
115 */
116 public function down(Schema $schema)
117 {
5ce15289 118 throw new SkipMigrationException('Too complex ...');
206bade5 119 }
f808b016
JB
120
121 private function getTable($tableName)
122 {
123 return $this->container->getParameter('database_table_prefix') . $tableName;
124 }
206bade5 125}