diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/DoctrineMigrations/Version20170328185535.php | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/app/DoctrineMigrations/Version20170328185535.php b/app/DoctrineMigrations/Version20170328185535.php new file mode 100644 index 00000000..f0afb7b9 --- /dev/null +++ b/app/DoctrineMigrations/Version20170328185535.php | |||
@@ -0,0 +1,99 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Application\Migrations; | ||
4 | |||
5 | use Doctrine\DBAL\Migrations\AbstractMigration; | ||
6 | use Doctrine\DBAL\Schema\Schema; | ||
7 | use Doctrine\DBAL\Schema\SchemaException; | ||
8 | use Symfony\Component\DependencyInjection\ContainerAwareInterface; | ||
9 | use Symfony\Component\DependencyInjection\ContainerInterface; | ||
10 | use Doctrine\DBAL\Migrations\SkipMigrationException; | ||
11 | |||
12 | /** | ||
13 | * Creates the Change table. | ||
14 | */ | ||
15 | class Version20170328185535 extends AbstractMigration implements ContainerAwareInterface | ||
16 | { | ||
17 | /** | ||
18 | * @var ContainerInterface | ||
19 | */ | ||
20 | private $container; | ||
21 | |||
22 | public function setContainer(ContainerInterface $container = null) | ||
23 | { | ||
24 | $this->container = $container; | ||
25 | } | ||
26 | |||
27 | private function getTable($tableName) | ||
28 | { | ||
29 | return $this->container->getParameter('database_table_prefix').$tableName; | ||
30 | } | ||
31 | |||
32 | /** | ||
33 | * @param Schema $schema | ||
34 | */ | ||
35 | public function up(Schema $schema) | ||
36 | { | ||
37 | try { | ||
38 | $schema->getTable($this->getTable('change')); | ||
39 | } catch (SchemaException $e) { | ||
40 | // The Change table doesn't exist, we need to create it | ||
41 | if (10 == $e->getCode()) { | ||
42 | if ($this->connection->getDatabasePlatform()->getName() == 'sqlite') { | ||
43 | $this->addSql('CREATE TABLE '.$this->getTable('change').' (id INTEGER NOT NULL, entry_id INTEGER DEFAULT NULL, type INTEGER NOT NULL, created_at DATETIME NOT NULL, PRIMARY KEY(id), CONSTRAINT FK_133B9D0FBA364942 FOREIGN KEY (entry_id) REFERENCES '.$this->getTable('entry').' (id) NOT DEFERRABLE INITIALLY IMMEDIATE)'); | ||
44 | |||
45 | return true; | ||
46 | } | ||
47 | |||
48 | $changeTable = $schema->createTable($this->getTable('change')); | ||
49 | $changeTable->addColumn( | ||
50 | 'id', | ||
51 | 'integer', | ||
52 | ['autoincrement' => true] | ||
53 | ); | ||
54 | $changeTable->addColumn( | ||
55 | 'type', | ||
56 | 'integer', | ||
57 | ['notnull' => false] | ||
58 | ); | ||
59 | $changeTable->addColumn( | ||
60 | 'created_at', | ||
61 | 'datetime', | ||
62 | ['notnull' => false] | ||
63 | ); | ||
64 | $changeTable->addColumn( | ||
65 | 'entry_id', | ||
66 | 'integer', | ||
67 | ['notnull' => false] | ||
68 | ); | ||
69 | |||
70 | $changeTable->setPrimaryKey(['id']); | ||
71 | |||
72 | $changeTable->addForeignKeyConstraint( | ||
73 | $this->getTable('entry'), | ||
74 | ['entry_id'], | ||
75 | ['id'], | ||
76 | ['onDelete' => 'CASCADE'], | ||
77 | 'IDX_change_entry' | ||
78 | ); | ||
79 | |||
80 | return true; | ||
81 | } | ||
82 | } | ||
83 | |||
84 | throw new SkipMigrationException('It seems that you already played this migration.'); | ||
85 | } | ||
86 | |||
87 | /** | ||
88 | * @param Schema $schema | ||
89 | */ | ||
90 | public function down(Schema $schema) | ||
91 | { | ||
92 | try { | ||
93 | $changeTable = $schema->getTable($this->getTable('change')); | ||
94 | $schema->dropTable($changeTable->getName()); | ||
95 | } catch (SchemaException $e) { | ||
96 | throw new SkipMigrationException('It seems that you already played this migration.'); | ||
97 | } | ||
98 | } | ||
99 | } | ||