From 43e1711eb66a55e954bcffa2f37fd30c1b3b1fae Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Tue, 12 Dec 2017 11:10:21 +0100 Subject: Change the way to check for initial migration --- app/DoctrineMigrations/Version20160401000000.php | 29 ++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/app/DoctrineMigrations/Version20160401000000.php b/app/DoctrineMigrations/Version20160401000000.php index a8603abf..54bfb0f0 100644 --- a/app/DoctrineMigrations/Version20160401000000.php +++ b/app/DoctrineMigrations/Version20160401000000.php @@ -4,20 +4,36 @@ namespace Application\Migrations; use Doctrine\DBAL\Migrations\AbstractMigration; use Doctrine\DBAL\Schema\Schema; +use Doctrine\DBAL\Schema\SchemaException; +use Symfony\Component\DependencyInjection\ContainerAwareInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Initial database structure. */ -class Version20160401000000 extends AbstractMigration +class Version20160401000000 extends AbstractMigration implements ContainerAwareInterface { + /** + * @var ContainerInterface + */ + private $container; + + public function setContainer(ContainerInterface $container = null) + { + $this->container = $container; + } + /** * @param Schema $schema */ public function up(Schema $schema) { - if ($this->version->getConfiguration()->getNumberOfExecutedMigrations() > 0) { - $this->version->markMigrated(); + try { + $schema->getTable($this->getTable('entry')); + $this->skipIf(true, 'Database already initialized'); + } catch (SchemaException $e) { + // it's ok, the table does not exist we can proceed to the initial migration } switch ($this->connection->getDatabasePlatform()->getName()) { @@ -160,7 +176,7 @@ ALTER TABLE wallabag_oauth2_refresh_tokens ADD CONSTRAINT FK_20C9FB24A76ED395 FO ALTER TABLE wallabag_oauth2_auth_codes ADD CONSTRAINT FK_EE52E3FA19EB6921 FOREIGN KEY (client_id) REFERENCES wallabag_oauth2_clients (id) NOT DEFERRABLE INITIALLY IMMEDIATE; ALTER TABLE wallabag_oauth2_auth_codes ADD CONSTRAINT FK_EE52E3FAA76ED395 FOREIGN KEY (user_id) REFERENCES "wallabag_user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE; ALTER TABLE wallabag_annotation ADD CONSTRAINT FK_A7AED006A76ED395 FOREIGN KEY (user_id) REFERENCES "wallabag_user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE; -ALTER TABLE wallabag_annotation ADD CONSTRAINT FK_A7AED006BA364942 FOREIGN KEY (entry_id) REFERENCES "wallabag_entry" (id) NOT DEFERRABLE INITIALLY IMMEDIATE; +ALTER TABLE wallabag_annotation ADD CONSTRAINT FK_A7AED006BA364942 FOREIGN KEY (entry_id) REFERENCES "wallabag_entry" (id) NOT DEFERRABLE INITIALLY IMMEDIATE; SQL ; foreach (explode("\n", $sql) as $query) { @@ -188,4 +204,9 @@ SQL $this->addSql('DROP TABLE "wallabag_user"'); $this->addSql('DROP TABLE wallabag_annotation'); } + + private function getTable($tableName) + { + return $this->container->getParameter('database_table_prefix') . $tableName; + } } -- cgit v1.2.3 From ad46f2fb46bf97d520a82ec5c15266e3c48c4e0b Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Tue, 12 Dec 2017 13:41:44 +0100 Subject: Use hasTable instead of try/catch --- app/DoctrineMigrations/Version20160401000000.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/app/DoctrineMigrations/Version20160401000000.php b/app/DoctrineMigrations/Version20160401000000.php index 54bfb0f0..9916321f 100644 --- a/app/DoctrineMigrations/Version20160401000000.php +++ b/app/DoctrineMigrations/Version20160401000000.php @@ -28,13 +28,7 @@ class Version20160401000000 extends AbstractMigration implements ContainerAwareI */ public function up(Schema $schema) { - try { - $schema->getTable($this->getTable('entry')); - - $this->skipIf(true, 'Database already initialized'); - } catch (SchemaException $e) { - // it's ok, the table does not exist we can proceed to the initial migration - } + $this->skipIf($schema->hasTable($this->getTable('entry')), 'Database already initialized'); switch ($this->connection->getDatabasePlatform()->getName()) { case 'sqlite': -- cgit v1.2.3 From 3fadf42a1c177fc41748c7732cbb4b13cbee4a14 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Tue, 12 Dec 2017 14:30:22 +0100 Subject: CS --- app/DoctrineMigrations/Version20160401000000.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/DoctrineMigrations/Version20160401000000.php b/app/DoctrineMigrations/Version20160401000000.php index 9916321f..a88d2dc0 100644 --- a/app/DoctrineMigrations/Version20160401000000.php +++ b/app/DoctrineMigrations/Version20160401000000.php @@ -4,7 +4,6 @@ namespace Application\Migrations; use Doctrine\DBAL\Migrations\AbstractMigration; use Doctrine\DBAL\Schema\Schema; -use Doctrine\DBAL\Schema\SchemaException; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerInterface; -- cgit v1.2.3