From 336b6560cf75a1773fbb23df65390637487102a1 Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Mon, 16 Oct 2017 23:25:50 +0300 Subject: [PATCH 1/2] Register type mappings on createSchema invocation too --- lib/private/DB/MySQLMigrator.php | 60 +++++++++++++++++----------- lib/private/DB/SQLiteMigrator.php | 24 +++++++++-- tests/lib/DB/MySqlMigrationTest.php | 5 +++ tests/lib/DB/SqliteMigrationTest.php | 5 +++ 4 files changed, 67 insertions(+), 27 deletions(-) diff --git a/lib/private/DB/MySQLMigrator.php b/lib/private/DB/MySQLMigrator.php index cfe1115acdd8..f688ed7b65b9 100644 --- a/lib/private/DB/MySQLMigrator.php +++ b/lib/private/DB/MySQLMigrator.php @@ -28,15 +28,21 @@ use Doctrine\DBAL\Schema\Table; class MySQLMigrator extends Migrator { + /** + * @return Schema + */ + public function createSchema() { + $this->registerAdditionalMappings($this->connection); + return parent::createSchema(); + } + /** * @param Schema $targetSchema * @param \Doctrine\DBAL\Connection $connection * @return \Doctrine\DBAL\Schema\SchemaDiff */ protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { - $platform = $connection->getDatabasePlatform(); - $platform->registerDoctrineTypeMapping('enum', 'string'); - $platform->registerDoctrineTypeMapping('bit', 'string'); + $this->registerAdditionalMappings($connection); $schemaDiff = parent::getDiff($targetSchema, $connection); @@ -50,26 +56,34 @@ protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $conn return $schemaDiff; } - - /** - * Speed up migration test by disabling autocommit and unique indexes check - * - * @param \Doctrine\DBAL\Schema\Table $table - * @throws \OC\DB\MigrationException - */ - protected function checkTableMigrate(Table $table) { - $this->connection->exec('SET autocommit=0'); - $this->connection->exec('SET unique_checks=0'); - try { - parent::checkTableMigrate($table); - } catch (\Exception $e) { - $this->connection->exec('SET unique_checks=1'); - $this->connection->exec('SET autocommit=1'); - throw new MigrationException($table->getName(), $e->getMessage()); - } - $this->connection->exec('SET unique_checks=1'); - $this->connection->exec('SET autocommit=1'); - } + /** + * Speed up migration test by disabling autocommit and unique indexes check + * + * @param \Doctrine\DBAL\Schema\Table $table + * @throws \OC\DB\MigrationException + */ + protected function checkTableMigrate(Table $table) { + $this->connection->exec('SET autocommit=0'); + $this->connection->exec('SET unique_checks=0'); + try { + parent::checkTableMigrate($table); + } catch (\Exception $e) { + $this->connection->exec('SET unique_checks=1'); + $this->connection->exec('SET autocommit=1'); + throw new MigrationException($table->getName(), $e->getMessage()); + } + $this->connection->exec('SET unique_checks=1'); + $this->connection->exec('SET autocommit=1'); + } + + /** + * @param \Doctrine\DBAL\Connection $connection + */ + private function registerAdditionalMappings(\Doctrine\DBAL\Connection $connection){ + $platform = $connection->getDatabasePlatform(); + $platform->registerDoctrineTypeMapping('enum', 'string'); + $platform->registerDoctrineTypeMapping('bit', 'string'); + } } diff --git a/lib/private/DB/SQLiteMigrator.php b/lib/private/DB/SQLiteMigrator.php index 1e0fe38d7728..f3f516e70b85 100644 --- a/lib/private/DB/SQLiteMigrator.php +++ b/lib/private/DB/SQLiteMigrator.php @@ -30,16 +30,22 @@ use Doctrine\DBAL\Types\Type; class SQLiteMigrator extends Migrator { + + /** + * @return Schema + */ + public function createSchema() { + $this->registerAdditionalMappings($this->connection); + return parent::createSchema(); + } + /** * @param Schema $targetSchema * @param \Doctrine\DBAL\Connection $connection * @return \Doctrine\DBAL\Schema\SchemaDiff */ protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { - $platform = $connection->getDatabasePlatform(); - $platform->registerDoctrineTypeMapping('tinyint unsigned', 'integer'); - $platform->registerDoctrineTypeMapping('smallint unsigned', 'integer'); - $platform->registerDoctrineTypeMapping('varchar ', 'string'); + $this->registerAdditionalMappings($connection); // with sqlite autoincrement columns is of type integer foreach ($targetSchema->getTables() as $table) { @@ -52,4 +58,14 @@ protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $conn return parent::getDiff($targetSchema, $connection); } + + /** + * @param \Doctrine\DBAL\Connection $connection + */ + private function registerAdditionalMappings(\Doctrine\DBAL\Connection $connection) { + $platform = $connection->getDatabasePlatform(); + $platform->registerDoctrineTypeMapping('tinyint unsigned', 'integer'); + $platform->registerDoctrineTypeMapping('smallint unsigned', 'integer'); + $platform->registerDoctrineTypeMapping('varchar ', 'string'); + } } diff --git a/tests/lib/DB/MySqlMigrationTest.php b/tests/lib/DB/MySqlMigrationTest.php index cdc7ef47784f..b75a4a5031e6 100644 --- a/tests/lib/DB/MySqlMigrationTest.php +++ b/tests/lib/DB/MySqlMigrationTest.php @@ -46,4 +46,9 @@ public function testNonOCTables() { $this->assertTrue(true); } + public function testCreateSchemaWithNonOcTables() { + $this->connection->createSchema(); + $this->assertTrue(true); + } + } diff --git a/tests/lib/DB/SqliteMigrationTest.php b/tests/lib/DB/SqliteMigrationTest.php index 76002e1afce3..1e55426230b1 100644 --- a/tests/lib/DB/SqliteMigrationTest.php +++ b/tests/lib/DB/SqliteMigrationTest.php @@ -46,4 +46,9 @@ public function testNonOCTables() { $this->assertTrue(true); } + public function testCreateSchemaWithNonOcTables() { + $this->connection->createSchema(); + $this->assertTrue(true); + } + } From 14b1384e8c050d4cb080bb3fcab9751bc913e46c Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Mon, 16 Oct 2017 23:43:24 +0300 Subject: [PATCH 2/2] Die NoCheckMigrator --- lib/private/DB/MDB2SchemaManager.php | 2 +- lib/private/DB/NoCheckMigrator.php | 33 ---------------------------- lib/private/DB/OracleMigrator.php | 2 +- 3 files changed, 2 insertions(+), 35 deletions(-) delete mode 100644 lib/private/DB/NoCheckMigrator.php diff --git a/lib/private/DB/MDB2SchemaManager.php b/lib/private/DB/MDB2SchemaManager.php index 6dffca45dcb6..77b6f75dffe4 100644 --- a/lib/private/DB/MDB2SchemaManager.php +++ b/lib/private/DB/MDB2SchemaManager.php @@ -88,7 +88,7 @@ public function getMigrator() { } else if ($platform instanceof PostgreSqlPlatform) { return new PostgreSqlMigrator($this->conn, $random, $config, $dispatcher); } else { - return new NoCheckMigrator($this->conn, $random, $config, $dispatcher); + return new Migrator($this->conn, $random, $config, $dispatcher); } } diff --git a/lib/private/DB/NoCheckMigrator.php b/lib/private/DB/NoCheckMigrator.php deleted file mode 100644 index 34d70e688a8e..000000000000 --- a/lib/private/DB/NoCheckMigrator.php +++ /dev/null @@ -1,33 +0,0 @@ - - * @author Robin Appelman - * - * @copyright Copyright (c) 2017, ownCloud GmbH - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OC\DB; - -use Doctrine\DBAL\Schema\Schema; - -/** - * migrator for database platforms that don't support the upgrade check - * - * @package OC\DB - */ -class NoCheckMigrator extends Migrator { -} diff --git a/lib/private/DB/OracleMigrator.php b/lib/private/DB/OracleMigrator.php index ffd3188afc35..53bcd71a7dda 100644 --- a/lib/private/DB/OracleMigrator.php +++ b/lib/private/DB/OracleMigrator.php @@ -30,7 +30,7 @@ use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\ForeignKeyConstraint; -class OracleMigrator extends NoCheckMigrator { +class OracleMigrator extends Migrator { /** * Quote a column's name but changing the name requires recreating