From 3b4555ca918f549d48d55b6f01a641bc0a21850e Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 8 Apr 2014 16:01:08 +0200 Subject: Try and check migration before applying it --- lib/private/db/migrator.php | 46 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) (limited to 'lib/private/db/migrator.php') diff --git a/lib/private/db/migrator.php b/lib/private/db/migrator.php index 5ba16e34311..917f92f64b6 100644 --- a/lib/private/db/migrator.php +++ b/lib/private/db/migrator.php @@ -35,6 +35,23 @@ class Migrator { $this->applySchema($targetSchema); } + /** + * @param \Doctrine\DBAL\Schema\Schema $targetSchema + * @return string + */ + public function generateChangeScript(Schema $targetSchema) { + $schemaDiff = $this->getDiff($targetSchema, $this->connection); + + $script = ''; + $sqls = $schemaDiff->toSql($this->connection->getDatabasePlatform()); + foreach ($sqls as $sql) { + $script .= $sql . ';'; + $script .= PHP_EOL; + } + + return $script; + } + /** * @param Schema $targetSchema * @throws \OC\DB\MigrationException @@ -109,16 +126,8 @@ class Migrator { return new Table($newName, $table->getColumns(), $newIndexes, array(), 0, $table->getOptions()); } - /** - * @param \Doctrine\DBAL\Schema\Schema $targetSchema - * @param \Doctrine\DBAL\Connection $connection - */ - protected function applySchema(Schema $targetSchema, \Doctrine\DBAL\Connection $connection = null) { - if (is_null($connection)) { - $connection = $this->connection; - } - - $sourceSchema = $this->connection->getSchemaManager()->createSchema(); + protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { + $sourceSchema = $connection->getSchemaManager()->createSchema(); // remove tables we don't know about /** @var $table \Doctrine\DBAL\Schema\Table */ @@ -139,8 +148,25 @@ class Migrator { foreach ($schemaDiff->changedTables as $tableDiff) { $tableDiff->name = $this->connection->quoteIdentifier($tableDiff->name); + foreach ($tableDiff->changedColumns as $column) { + $column->oldColumnName = $this->connection->quoteIdentifier($column->oldColumnName); + } + } + + return $schemaDiff; + } + + /** + * @param \Doctrine\DBAL\Schema\Schema $targetSchema + * @param \Doctrine\DBAL\Connection $connection + */ + protected function applySchema(Schema $targetSchema, \Doctrine\DBAL\Connection $connection = null) { + if (is_null($connection)) { + $connection = $this->connection; } + $schemaDiff = $this->getDiff($targetSchema, $connection); + $connection->beginTransaction(); foreach ($schemaDiff->toSql($connection->getDatabasePlatform()) as $sql) { $connection->query($sql); -- cgit v1.2.3