Skip to content

Commit 1c2a9b4

Browse files
committed
Skip db migration simulation for core schema changes
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent f318423 commit 1c2a9b4

File tree

6 files changed

+6
-217
lines changed

6 files changed

+6
-217
lines changed

lib/private/DB/MDB2SchemaManager.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ public function getMigrator() {
8383
$config = \OC::$server->getConfig();
8484
$dispatcher = \OC::$server->getEventDispatcher();
8585
if ($platform instanceof SqlitePlatform) {
86-
return new SQLiteMigrator($this->conn, $random, $config, $dispatcher);
86+
return new SQLiteMigrator($this->conn, $config, $dispatcher);
8787
} elseif ($platform instanceof OraclePlatform) {
88-
return new OracleMigrator($this->conn, $random, $config, $dispatcher);
88+
return new OracleMigrator($this->conn, $config, $dispatcher);
8989
} elseif ($platform instanceof MySqlPlatform) {
90-
return new MySQLMigrator($this->conn, $random, $config, $dispatcher);
90+
return new MySQLMigrator($this->conn, $config, $dispatcher);
9191
} elseif ($platform instanceof PostgreSqlPlatform) {
92-
return new PostgreSqlMigrator($this->conn, $random, $config, $dispatcher);
92+
return new PostgreSqlMigrator($this->conn, $config, $dispatcher);
9393
} else {
94-
return new Migrator($this->conn, $random, $config, $dispatcher);
94+
return new Migrator($this->conn, $config, $dispatcher);
9595
}
9696
}
9797

lib/private/DB/Migrator.php

-131
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,10 @@
3333

3434
use Doctrine\DBAL\DBALException;
3535
use Doctrine\DBAL\Schema\Comparator;
36-
use Doctrine\DBAL\Schema\Index;
3736
use Doctrine\DBAL\Schema\Schema;
38-
use Doctrine\DBAL\Schema\SchemaConfig;
39-
use Doctrine\DBAL\Schema\Table;
4037
use Doctrine\DBAL\Types\StringType;
4138
use Doctrine\DBAL\Types\Type;
4239
use OCP\IConfig;
43-
use OCP\Security\ISecureRandom;
4440
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
4541
use Symfony\Component\EventDispatcher\GenericEvent;
4642

@@ -49,9 +45,6 @@ class Migrator {
4945
/** @var \Doctrine\DBAL\Connection */
5046
protected $connection;
5147

52-
/** @var ISecureRandom */
53-
private $random;
54-
5548
/** @var IConfig */
5649
protected $config;
5750

@@ -63,16 +56,13 @@ class Migrator {
6356

6457
/**
6558
* @param \Doctrine\DBAL\Connection $connection
66-
* @param ISecureRandom $random
6759
* @param IConfig $config
6860
* @param EventDispatcherInterface $dispatcher
6961
*/
7062
public function __construct(\Doctrine\DBAL\Connection $connection,
71-
ISecureRandom $random,
7263
IConfig $config,
7364
EventDispatcherInterface $dispatcher = null) {
7465
$this->connection = $connection;
75-
$this->random = $random;
7666
$this->config = $config;
7767
$this->dispatcher = $dispatcher;
7868
}
@@ -101,101 +91,6 @@ public function generateChangeScript(Schema $targetSchema) {
10191
return $script;
10292
}
10393

104-
/**
105-
* @param Schema $targetSchema
106-
* @throws \OC\DB\MigrationException
107-
*/
108-
public function checkMigrate(Schema $targetSchema) {
109-
$this->noEmit = true;
110-
/**@var \Doctrine\DBAL\Schema\Table[] $tables */
111-
$tables = $targetSchema->getTables();
112-
$filterExpression = $this->getFilterExpression();
113-
$this->connection->getConfiguration()->
114-
setFilterSchemaAssetsExpression($filterExpression);
115-
$existingTables = $this->connection->getSchemaManager()->listTableNames();
116-
117-
$step = 0;
118-
foreach ($tables as $table) {
119-
if (strpos($table->getName(), '.')) {
120-
list(, $tableName) = explode('.', $table->getName());
121-
} else {
122-
$tableName = $table->getName();
123-
}
124-
$this->emitCheckStep($tableName, $step++, count($tables));
125-
// don't need to check for new tables
126-
if (array_search($tableName, $existingTables) !== false) {
127-
$this->checkTableMigrate($table);
128-
}
129-
}
130-
}
131-
132-
/**
133-
* Create a unique name for the temporary table
134-
*
135-
* @param string $name
136-
* @return string
137-
*/
138-
protected function generateTemporaryTableName($name) {
139-
return $this->config->getSystemValue('dbtableprefix', 'oc_') . $name . '_' . $this->random->generate(13, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
140-
}
141-
142-
/**
143-
* Check the migration of a table on a copy so we can detect errors before messing with the real table
144-
*
145-
* @param \Doctrine\DBAL\Schema\Table $table
146-
* @throws \OC\DB\MigrationException
147-
*/
148-
protected function checkTableMigrate(Table $table) {
149-
$name = $table->getName();
150-
$tmpName = $this->generateTemporaryTableName($name);
151-
152-
$this->copyTable($name, $tmpName);
153-
154-
//create the migration schema for the temporary table
155-
$tmpTable = $this->renameTableSchema($table, $tmpName);
156-
$schemaConfig = new SchemaConfig();
157-
$schemaConfig->setName($this->connection->getDatabase());
158-
$schema = new Schema([$tmpTable], [], $schemaConfig);
159-
160-
try {
161-
$this->applySchema($schema);
162-
$this->dropTable($tmpName);
163-
} catch (DBALException $e) {
164-
// pgsql needs to commit it's failed transaction before doing anything else
165-
if ($this->connection->isTransactionActive()) {
166-
$this->connection->commit();
167-
}
168-
$this->dropTable($tmpName);
169-
throw new MigrationException($table->getName(), $e->getMessage());
170-
}
171-
}
172-
173-
/**
174-
* @param \Doctrine\DBAL\Schema\Table $table
175-
* @param string $newName
176-
* @return \Doctrine\DBAL\Schema\Table
177-
*/
178-
protected function renameTableSchema(Table $table, $newName) {
179-
/**
180-
* @var \Doctrine\DBAL\Schema\Index[] $indexes
181-
*/
182-
$indexes = $table->getIndexes();
183-
$newIndexes = [];
184-
foreach ($indexes as $index) {
185-
if ($index->isPrimary()) {
186-
// do not rename primary key
187-
$indexName = $index->getName();
188-
} else {
189-
// avoid conflicts in index names
190-
$indexName = $this->config->getSystemValue('dbtableprefix', 'oc_') . $this->random->generate(13, ISecureRandom::CHAR_LOWER);
191-
}
192-
$newIndexes[] = new Index($indexName, $index->getColumns(), $index->isUnique(), $index->isPrimary());
193-
}
194-
195-
// foreign keys are not supported so we just set it to an empty array
196-
return new Table($newName, $table->getColumns(), $newIndexes, [], 0, $table->getOptions());
197-
}
198-
19994
public function createSchema() {
20095
$filterExpression = $this->getFilterExpression();
20196
$this->connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
@@ -263,25 +158,6 @@ protected function applySchema(Schema $targetSchema, \Doctrine\DBAL\Connection $
263158
$connection->commit();
264159
}
265160

266-
/**
267-
* @param string $sourceName
268-
* @param string $targetName
269-
*/
270-
protected function copyTable($sourceName, $targetName) {
271-
$quotedSource = $this->connection->quoteIdentifier($sourceName);
272-
$quotedTarget = $this->connection->quoteIdentifier($targetName);
273-
274-
$this->connection->exec('CREATE TABLE ' . $quotedTarget . ' (LIKE ' . $quotedSource . ')');
275-
$this->connection->exec('INSERT INTO ' . $quotedTarget . ' SELECT * FROM ' . $quotedSource);
276-
}
277-
278-
/**
279-
* @param string $name
280-
*/
281-
protected function dropTable($name) {
282-
$this->connection->exec('DROP TABLE ' . $this->connection->quoteIdentifier($name));
283-
}
284-
285161
/**
286162
* @param $statement
287163
* @return string
@@ -306,11 +182,4 @@ protected function emit($sql, $step, $max) {
306182
}
307183
$this->dispatcher->dispatch('\OC\DB\Migrator::executeSql', new GenericEvent($sql, [$step + 1, $max]));
308184
}
309-
310-
private function emitCheckStep($tableName, $step, $max) {
311-
if (is_null($this->dispatcher)) {
312-
return;
313-
}
314-
$this->dispatcher->dispatch('\OC\DB\Migrator::checkTable', new GenericEvent($tableName, [$step + 1, $max]));
315-
}
316185
}

lib/private/DB/MySQLMigrator.php

-22
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
namespace OC\DB;
2828

2929
use Doctrine\DBAL\Schema\Schema;
30-
use Doctrine\DBAL\Schema\Table;
3130

3231
class MySQLMigrator extends Migrator {
3332
/**
@@ -52,25 +51,4 @@ protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $conn
5251

5352
return $schemaDiff;
5453
}
55-
56-
/**
57-
* Speed up migration test by disabling autocommit and unique indexes check
58-
*
59-
* @param \Doctrine\DBAL\Schema\Table $table
60-
* @throws \OC\DB\MigrationException
61-
*/
62-
protected function checkTableMigrate(Table $table) {
63-
$this->connection->exec('SET autocommit=0');
64-
$this->connection->exec('SET unique_checks=0');
65-
66-
try {
67-
parent::checkTableMigrate($table);
68-
} catch (\Exception $e) {
69-
$this->connection->exec('SET unique_checks=1');
70-
$this->connection->exec('SET autocommit=1');
71-
throw new MigrationException($table->getName(), $e->getMessage());
72-
}
73-
$this->connection->exec('SET unique_checks=1');
74-
$this->connection->exec('SET autocommit=1');
75-
}
7654
}

lib/private/DB/OracleMigrator.php

-8
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,6 @@ protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $conn
204204
return $schemaDiff;
205205
}
206206

207-
/**
208-
* @param string $name
209-
* @return string
210-
*/
211-
protected function generateTemporaryTableName($name) {
212-
return 'oc_' . uniqid();
213-
}
214-
215207
/**
216208
* @param $statement
217209
* @return string

lib/private/DB/SQLiteMigrator.php

-28
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,12 @@
2626

2727
namespace OC\DB;
2828

29-
use Doctrine\DBAL\DBALException;
3029
use Doctrine\DBAL\Schema\Schema;
3130
use Doctrine\DBAL\Types\BigIntType;
3231
use Doctrine\DBAL\Types\Type;
3332

3433
class SQLiteMigrator extends Migrator {
3534

36-
/**
37-
* @param \Doctrine\DBAL\Schema\Schema $targetSchema
38-
* @throws \OC\DB\MigrationException
39-
*
40-
* For sqlite we simple make a copy of the entire database, and test the migration on that
41-
*/
42-
public function checkMigrate(\Doctrine\DBAL\Schema\Schema $targetSchema) {
43-
$dbFile = $this->connection->getDatabase();
44-
$tmpFile = $this->buildTempDatabase();
45-
copy($dbFile, $tmpFile);
46-
47-
$connectionParams = [
48-
'path' => $tmpFile,
49-
'driver' => 'pdo_sqlite',
50-
];
51-
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);
52-
try {
53-
$this->applySchema($targetSchema, $conn);
54-
$conn->close();
55-
unlink($tmpFile);
56-
} catch (DBALException $e) {
57-
$conn->close();
58-
unlink($tmpFile);
59-
throw new MigrationException('', $e->getMessage());
60-
}
61-
}
62-
6335
/**
6436
* @return string
6537
*/

tests/lib/DB/MigratorTest.php

+1-23
Original file line numberDiff line numberDiff line change
@@ -104,25 +104,6 @@ private function isSQLite() {
104104
return $this->connection->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver;
105105
}
106106

107-
108-
public function testDuplicateKeyUpgrade() {
109-
$this->expectException(\OC\DB\MigrationException::class);
110-
111-
if ($this->isSQLite()) {
112-
$this->markTestSkipped('sqlite does not throw errors when creating a new key on existing data');
113-
}
114-
list($startSchema, $endSchema) = $this->getDuplicateKeySchemas();
115-
$migrator = $this->manager->getMigrator();
116-
$migrator->migrate($startSchema);
117-
118-
$this->connection->insert($this->tableName, ['id' => 1, 'name' => 'foo']);
119-
$this->connection->insert($this->tableName, ['id' => 2, 'name' => 'bar']);
120-
$this->connection->insert($this->tableName, ['id' => 2, 'name' => 'qwerty']);
121-
122-
$migrator->checkMigrate($endSchema);
123-
$this->fail('checkMigrate should have failed');
124-
}
125-
126107
public function testUpgrade() {
127108
list($startSchema, $endSchema) = $this->getDuplicateKeySchemas();
128109
$migrator = $this->manager->getMigrator();
@@ -132,7 +113,6 @@ public function testUpgrade() {
132113
$this->connection->insert($this->tableName, ['id' => 2, 'name' => 'bar']);
133114
$this->connection->insert($this->tableName, ['id' => 3, 'name' => 'qwerty']);
134115

135-
$migrator->checkMigrate($endSchema);
136116
$migrator->migrate($endSchema);
137117
$this->addToAssertionCount(1);
138118
}
@@ -151,7 +131,6 @@ public function testUpgradeDifferentPrefix() {
151131
$this->connection->insert($this->tableName, ['id' => 2, 'name' => 'bar']);
152132
$this->connection->insert($this->tableName, ['id' => 3, 'name' => 'qwerty']);
153133

154-
$migrator->checkMigrate($endSchema);
155134
$migrator->migrate($endSchema);
156135
$this->addToAssertionCount(1);
157136

@@ -190,7 +169,6 @@ public function testAddingPrimaryKeyWithAutoIncrement() {
190169
$migrator = $this->manager->getMigrator();
191170
$migrator->migrate($startSchema);
192171

193-
$migrator->checkMigrate($endSchema);
194172
$migrator->migrate($endSchema);
195173

196174
$this->addToAssertionCount(1);
@@ -212,7 +190,7 @@ public function testReservedKeywords() {
212190
$migrator = $this->manager->getMigrator();
213191
$migrator->migrate($startSchema);
214192

215-
$migrator->checkMigrate($endSchema);
193+
216194
$migrator->migrate($endSchema);
217195

218196
$this->addToAssertionCount(1);

0 commit comments

Comments
 (0)