]> git.immae.eu Git - github/wallabag/wallabag.git/blob - app/DoctrineMigrations/Version20160906180558.php
Updated group migration
[github/wallabag/wallabag.git] / app / DoctrineMigrations / Version20160906180558.php
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 /**
11 * Added group table
12 */
13 class Version20160906180558 extends AbstractMigration
14 {
15 /**
16 * @var ContainerInterface
17 */
18 private $container;
19
20 public function setContainer(ContainerInterface $container = null)
21 {
22 $this->container = $container;
23 }
24
25 private function getTable($tableName)
26 {
27 return $this->container->getParameter('database_table_prefix') . $tableName;
28 }
29
30 /**
31 * @param Schema $schema
32 */
33 public function up(Schema $schema)
34 {
35
36 switch ($this->connection->getDatabasePlatform()->getName()) {
37 case 'sqlite':
38 $this->addSql('CREATE TABLE '.$this->getTable('group').' (id INTEGER NOT NULL, name VARCHAR(255) NOT NULL, roles CLOB NOT NULL, PRIMARY KEY(id))');
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)');
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
48 case 'postgresql':
49 $this->addSql('CREATE TABLE '.$this->getTable('group').' (id integer NOT NULL, name character varying(255) NOT NULL, roles text NOT NULL);');
50 $this->addSql('CREATE TABLE '.$this->getTable('user_group').' (user_id integer NOT NULL, group_id integer NOT NULL);');
51 $this->addSql('ALTER TABLE ONLY '.$this->getTable('user_group').' ADD CONSTRAINT fk_6e85169a76ed395 FOREIGN KEY (user_id) REFERENCES '.$this->getTable('user').'(id);');
52 $this->addSql('ALTER TABLE ONLY '.$this->getTable('user_group').' ADD CONSTRAINT fk_6e85169fe54d947 FOREIGN KEY (group_id) REFERENCES '.$this->getTable('group').'(id);');
53 }
54 }
55
56 /**
57 * @param Schema $schema
58 */
59 public function down(Schema $schema)
60 {
61 $this->abortIf($this->connection->getDatabasePlatform()->getName() == 'sqlite', 'Migration can only be executed safely on \'mysql\'.');
62
63 $this->addSql('ALTER TABLE '.$this->getTable('user_group').' DROP FOREIGN KEY FK_6E85169FE54D947');
64 $this->addSql('DROP TABLE '.$this->getTable('group'));
65 $this->addSql('DROP TABLE '.$this->getTable('user_group'));
66 }
67 }