]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Used createTable
authorNicolas LÅ“uillet <nicolas@loeuillet.org>
Sat, 26 Nov 2016 12:21:23 +0000 (13:21 +0100)
committerThomas Citharel <tcit@tcit.fr>
Fri, 23 Jun 2017 07:24:20 +0000 (09:24 +0200)
app/DoctrineMigrations/Version20160906180558.php

index df44796735d983d19c353e7609c700e7f2c5a22a..431b9968f847058d128afc37745f63392bd254fd 100644 (file)
@@ -8,9 +8,9 @@ use Symfony\Component\DependencyInjection\ContainerAwareInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
- * Added group table
+ * Added group table.
  */
-class Version20160906180558 extends AbstractMigration
+class Version20160906180558 extends AbstractMigration implements ContainerAwareInterface
 {
     /**
      * @var ContainerInterface
@@ -24,7 +24,7 @@ class Version20160906180558 extends AbstractMigration
 
     private function getTable($tableName)
     {
-        return $this->container->getParameter('database_table_prefix') . $tableName;
+        return $this->container->getParameter('database_table_prefix').$tableName;
     }
 
     /**
@@ -32,25 +32,30 @@ class Version20160906180558 extends AbstractMigration
      */
     public function up(Schema $schema)
     {
+        $groupTable = $schema->createTable($this->getTable('group'));
+        $groupTable->addColumn('id', 'integer');
+        $groupTable->addColumn('name', 'string');
+        $groupTable->addColumn('roles', 'blob');
+        $groupTable->setPrimaryKey(['id']);
 
-        switch ($this->connection->getDatabasePlatform()->getName()) {
-            case 'sqlite':
-                $this->addSql('CREATE TABLE '.$this->getTable('group').' (id INTEGER NOT NULL, name VARCHAR(255) NOT NULL, roles CLOB NOT NULL, PRIMARY KEY(id))');
-                $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)');
-            break;
-            case 'mysql':
-                $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');
-                $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');
-                $this->addSql('ALTER TABLE '.$this->getTable('user_group').' ADD CONSTRAINT FK_6E85169A76ED395 FOREIGN KEY (user_id) REFERENCES '.$this->getTable('user').' (id)');
-                $this->addSql('ALTER TABLE '.$this->getTable('user_group').' ADD CONSTRAINT FK_6E85169FE54D947 FOREIGN KEY (group_id) REFERENCES '.$this->getTable('group').' (id)');
-            break;
+        $userGroupTable = $schema->createTable($this->getTable('user_group'));
+        $userGroupTable->addColumn('user_id', 'integer');
+        $userGroupTable->addColumn('group_id', 'integer');
+        $userGroupTable->setPrimaryKey(['user_id', 'group_id']);
 
-            case 'postgresql':
-                $this->addSql('CREATE TABLE '.$this->getTable('group').' (id integer NOT NULL, name character varying(255) NOT NULL, roles text NOT NULL);');
-                $this->addSql('CREATE TABLE '.$this->getTable('user_group').' (user_id integer NOT NULL, group_id integer NOT NULL);');
-                $this->addSql('ALTER TABLE ONLY '.$this->getTable('user_group').' ADD CONSTRAINT fk_6e85169a76ed395 FOREIGN KEY (user_id) REFERENCES '.$this->getTable('user').'(id);');
-                $this->addSql('ALTER TABLE ONLY '.$this->getTable('user_group').' ADD CONSTRAINT fk_6e85169fe54d947 FOREIGN KEY (group_id) REFERENCES '.$this->getTable('group').'(id);');
-        }
+        $userGroupTable->addForeignKeyConstraint(
+            $groupTable,
+            array('group_id'),
+            array('id'),
+            array('onDelete' => 'CASCADE')
+        );
+
+        $userGroupTable->addForeignKeyConstraint(
+            $this->getTable('user'),
+            array('user_id'),
+            array('id'),
+            array('onDelete' => 'CASCADE')
+        );
     }
 
     /**
@@ -60,8 +65,7 @@ class Version20160906180558 extends AbstractMigration
     {
         $this->abortIf($this->connection->getDatabasePlatform()->getName() == 'sqlite', 'Migration can only be executed safely on \'mysql\'.');
 
-        $this->addSql('ALTER TABLE '.$this->getTable('user_group').' DROP FOREIGN KEY FK_6E85169FE54D947');
-        $this->addSql('DROP TABLE '.$this->getTable('group'));
-        $this->addSql('DROP TABLE '.$this->getTable('user_group'));
+        $schema->dropTable($this->getTable('group'));
+        $schema->dropTable($this->getTable('user_group'));
     }
 }