]> git.immae.eu Git - github/wallabag/wallabag.git/blob - app/DoctrineMigrations/Version20170511211659.php
Add a real configuration for CS-Fixer
[github/wallabag/wallabag.git] / app / DoctrineMigrations / Version20170511211659.php
1 <?php
2
3 namespace Application\Migrations;
4
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;
10
11 /**
12 * Increase the length of the "quote" column of "annotation" table.
13 */
14 class Version20170511211659 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
26 public function up(Schema $schema)
27 {
28 $tableName = $this->getTable('annotation');
29
30 switch ($this->connection->getDatabasePlatform()->getName()) {
31 case 'sqlite':
32 $this->addSql(<<<EOD
33 CREATE TEMPORARY TABLE __temp__wallabag_annotation AS
34 SELECT id, user_id, entry_id, text, created_at, updated_at, quote, ranges
35 FROM ${tableName}
36 EOD
37 );
38 $this->addSql('DROP TABLE ' . $tableName);
39 $this->addSql(<<<EOD
40 CREATE TABLE ${tableName}
41 (
42 id INTEGER PRIMARY KEY NOT NULL,
43 user_id INTEGER DEFAULT NULL,
44 entry_id INTEGER DEFAULT NULL,
45 text CLOB NOT NULL,
46 created_at DATETIME NOT NULL,
47 updated_at DATETIME NOT NULL,
48 quote CLOB NOT NULL,
49 ranges CLOB NOT NULL,
50 CONSTRAINT FK_A7AED006A76ED395 FOREIGN KEY (user_id) REFERENCES wallabag_user (id),
51 CONSTRAINT FK_A7AED006BA364942 FOREIGN KEY (entry_id) REFERENCES wallabag_entry (id) ON DELETE CASCADE
52 );
53 CREATE INDEX IDX_A7AED006A76ED395 ON wallabag_annotation (user_id);
54 CREATE INDEX IDX_A7AED006BA364942 ON wallabag_annotation (entry_id);
55 EOD
56 );
57
58 $this->addSql(<<<EOD
59 INSERT INTO ${tableName} (id, user_id, entry_id, text, created_at, updated_at, quote, ranges)
60 SELECT id, user_id, entry_id, text, created_at, updated_at, quote, ranges
61 FROM __temp__wallabag_annotation;
62 EOD
63 );
64 $this->addSql('DROP TABLE __temp__wallabag_annotation');
65 break;
66 case 'mysql':
67 $this->addSql('ALTER TABLE ' . $tableName . ' MODIFY quote TEXT NOT NULL');
68 break;
69 case 'postgresql':
70 $this->addSql('ALTER TABLE ' . $tableName . ' ALTER COLUMN quote TYPE TEXT');
71 break;
72 }
73 }
74
75 public function down(Schema $schema)
76 {
77 $tableName = $this->getTable('annotation');
78
79 switch ($this->connection->getDatabasePlatform()->getName()) {
80 case 'sqlite':
81 throw new SkipMigrationException('Too complex ...');
82 break;
83 case 'mysql':
84 $this->addSql('ALTER TABLE ' . $tableName . ' MODIFY quote VARCHAR(255) NOT NULL');
85 break;
86 case 'postgresql':
87 $this->addSql('ALTER TABLE ' . $tableName . ' ALTER COLUMN quote TYPE VARCHAR(255)');
88 break;
89 }
90 }
91
92 private function getTable($tableName)
93 {
94 return $this->container->getParameter('database_table_prefix') . $tableName;
95 }
96 }