diff options
Diffstat (limited to 'app/DoctrineMigrations/Version20160906180558.php')
-rw-r--r-- | app/DoctrineMigrations/Version20160906180558.php | 50 |
1 files changed, 27 insertions, 23 deletions
diff --git a/app/DoctrineMigrations/Version20160906180558.php b/app/DoctrineMigrations/Version20160906180558.php index df447967..431b9968 100644 --- a/app/DoctrineMigrations/Version20160906180558.php +++ b/app/DoctrineMigrations/Version20160906180558.php | |||
@@ -8,9 +8,9 @@ use Symfony\Component\DependencyInjection\ContainerAwareInterface; | |||
8 | use Symfony\Component\DependencyInjection\ContainerInterface; | 8 | use Symfony\Component\DependencyInjection\ContainerInterface; |
9 | 9 | ||
10 | /** | 10 | /** |
11 | * Added group table | 11 | * Added group table. |
12 | */ | 12 | */ |
13 | class Version20160906180558 extends AbstractMigration | 13 | class Version20160906180558 extends AbstractMigration implements ContainerAwareInterface |
14 | { | 14 | { |
15 | /** | 15 | /** |
16 | * @var ContainerInterface | 16 | * @var ContainerInterface |
@@ -24,7 +24,7 @@ class Version20160906180558 extends AbstractMigration | |||
24 | 24 | ||
25 | private function getTable($tableName) | 25 | private function getTable($tableName) |
26 | { | 26 | { |
27 | return $this->container->getParameter('database_table_prefix') . $tableName; | 27 | return $this->container->getParameter('database_table_prefix').$tableName; |
28 | } | 28 | } |
29 | 29 | ||
30 | /** | 30 | /** |
@@ -32,25 +32,30 @@ class Version20160906180558 extends AbstractMigration | |||
32 | */ | 32 | */ |
33 | public function up(Schema $schema) | 33 | public function up(Schema $schema) |
34 | { | 34 | { |
35 | $groupTable = $schema->createTable($this->getTable('group')); | ||
36 | $groupTable->addColumn('id', 'integer'); | ||
37 | $groupTable->addColumn('name', 'string'); | ||
38 | $groupTable->addColumn('roles', 'blob'); | ||
39 | $groupTable->setPrimaryKey(['id']); | ||
35 | 40 | ||
36 | switch ($this->connection->getDatabasePlatform()->getName()) { | 41 | $userGroupTable = $schema->createTable($this->getTable('user_group')); |
37 | case 'sqlite': | 42 | $userGroupTable->addColumn('user_id', 'integer'); |
38 | $this->addSql('CREATE TABLE '.$this->getTable('group').' (id INTEGER NOT NULL, name VARCHAR(255) NOT NULL, roles CLOB NOT NULL, PRIMARY KEY(id))'); | 43 | $userGroupTable->addColumn('group_id', 'integer'); |
39 | $this->addSql('CREATE TABLE '.$this->getTable('user_group').' (user_id INTEGER NOT NULL, group_id INTEGER NOT NULL, PRIMARY KEY(user_id, group_id), CONSTRAINT FK_6E85169A76ED395 FOREIGN KEY (user_id) REFERENCES '.$this->getTable('user').' (id) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_6E85169FE54D947 FOREIGN KEY (group_id) REFERENCES '.$this->getTable('group').' (id) NOT DEFERRABLE INITIALLY IMMEDIATE)'); | 44 | $userGroupTable->setPrimaryKey(['user_id', 'group_id']); |
40 | break; | ||
41 | case 'mysql': | ||
42 | $this->addSql('CREATE TABLE '.$this->getTable('group').' (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, roles LONGTEXT NOT NULL, UNIQUE INDEX UNIQ_B2305B375E237E06 (name), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'); | ||
43 | $this->addSql('CREATE TABLE '.$this->getTable('user_group').' (user_id INT NOT NULL, group_id INT NOT NULL, INDEX IDX_6E85169A76ED395 (user_id), INDEX IDX_6E85169FE54D947 (group_id), PRIMARY KEY(user_id, group_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'); | ||
44 | $this->addSql('ALTER TABLE '.$this->getTable('user_group').' ADD CONSTRAINT FK_6E85169A76ED395 FOREIGN KEY (user_id) REFERENCES '.$this->getTable('user').' (id)'); | ||
45 | $this->addSql('ALTER TABLE '.$this->getTable('user_group').' ADD CONSTRAINT FK_6E85169FE54D947 FOREIGN KEY (group_id) REFERENCES '.$this->getTable('group').' (id)'); | ||
46 | break; | ||
47 | 45 | ||
48 | case 'postgresql': | 46 | $userGroupTable->addForeignKeyConstraint( |
49 | $this->addSql('CREATE TABLE '.$this->getTable('group').' (id integer NOT NULL, name character varying(255) NOT NULL, roles text NOT NULL);'); | 47 | $groupTable, |
50 | $this->addSql('CREATE TABLE '.$this->getTable('user_group').' (user_id integer NOT NULL, group_id integer NOT NULL);'); | 48 | array('group_id'), |
51 | $this->addSql('ALTER TABLE ONLY '.$this->getTable('user_group').' ADD CONSTRAINT fk_6e85169a76ed395 FOREIGN KEY (user_id) REFERENCES '.$this->getTable('user').'(id);'); | 49 | array('id'), |
52 | $this->addSql('ALTER TABLE ONLY '.$this->getTable('user_group').' ADD CONSTRAINT fk_6e85169fe54d947 FOREIGN KEY (group_id) REFERENCES '.$this->getTable('group').'(id);'); | 50 | array('onDelete' => 'CASCADE') |
53 | } | 51 | ); |
52 | |||
53 | $userGroupTable->addForeignKeyConstraint( | ||
54 | $this->getTable('user'), | ||
55 | array('user_id'), | ||
56 | array('id'), | ||
57 | array('onDelete' => 'CASCADE') | ||
58 | ); | ||
54 | } | 59 | } |
55 | 60 | ||
56 | /** | 61 | /** |
@@ -60,8 +65,7 @@ class Version20160906180558 extends AbstractMigration | |||
60 | { | 65 | { |
61 | $this->abortIf($this->connection->getDatabasePlatform()->getName() == 'sqlite', 'Migration can only be executed safely on \'mysql\'.'); | 66 | $this->abortIf($this->connection->getDatabasePlatform()->getName() == 'sqlite', 'Migration can only be executed safely on \'mysql\'.'); |
62 | 67 | ||
63 | $this->addSql('ALTER TABLE '.$this->getTable('user_group').' DROP FOREIGN KEY FK_6E85169FE54D947'); | 68 | $schema->dropTable($this->getTable('group')); |
64 | $this->addSql('DROP TABLE '.$this->getTable('group')); | 69 | $schema->dropTable($this->getTable('user_group')); |
65 | $this->addSql('DROP TABLE '.$this->getTable('user_group')); | ||
66 | } | 70 | } |
67 | } | 71 | } |