2014-01-29 18:48:12 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\DB;
|
|
|
|
|
|
|
|
use \Doctrine\DBAL\DBALException;
|
2014-01-30 17:21:16 +04:00
|
|
|
use \Doctrine\DBAL\Schema\Index;
|
2014-01-29 18:48:12 +04:00
|
|
|
use \Doctrine\DBAL\Schema\Table;
|
|
|
|
use \Doctrine\DBAL\Schema\Schema;
|
|
|
|
use \Doctrine\DBAL\Schema\SchemaConfig;
|
2014-01-30 17:21:16 +04:00
|
|
|
use \Doctrine\DBAL\Schema\Comparator;
|
2014-01-29 18:48:12 +04:00
|
|
|
|
|
|
|
class Migrator {
|
|
|
|
/**
|
|
|
|
* @var \Doctrine\DBAL\Connection $connection
|
|
|
|
*/
|
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Doctrine\DBAL\Connection $connection
|
|
|
|
*/
|
|
|
|
public function __construct(\Doctrine\DBAL\Connection $connection) {
|
|
|
|
$this->connection = $connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Doctrine\DBAL\Schema\Schema $targetSchema
|
|
|
|
*/
|
|
|
|
public function migrate(Schema $targetSchema) {
|
|
|
|
$this->applySchema($targetSchema);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Schema $targetSchema
|
2014-03-27 17:44:19 +04:00
|
|
|
* @throws \OC\DB\MigrationException
|
2014-01-29 18:48:12 +04:00
|
|
|
*/
|
|
|
|
public function checkMigrate(Schema $targetSchema) {
|
|
|
|
/**
|
|
|
|
* @var \Doctrine\DBAL\Schema\Table[] $tables
|
|
|
|
*/
|
|
|
|
$tables = $targetSchema->getTables();
|
|
|
|
|
|
|
|
$existingTables = $this->connection->getSchemaManager()->listTableNames();
|
|
|
|
|
|
|
|
foreach ($tables as $table) {
|
2014-03-14 17:32:02 +04:00
|
|
|
if (strpos($table->getName(), '.')) {
|
|
|
|
list(, $tableName) = explode('.', $table->getName());
|
|
|
|
} else {
|
|
|
|
$tableName = $table->getName();
|
|
|
|
}
|
2014-01-29 18:48:12 +04:00
|
|
|
// don't need to check for new tables
|
|
|
|
if (array_search($tableName, $existingTables) !== false) {
|
|
|
|
$this->checkTableMigrate($table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the migration of a table on a copy so we can detect errors before messing with the real table
|
|
|
|
*
|
|
|
|
* @param \Doctrine\DBAL\Schema\Table $table
|
2014-01-30 17:21:16 +04:00
|
|
|
* @throws \OC\DB\MigrationException
|
2014-01-29 18:48:12 +04:00
|
|
|
*/
|
|
|
|
protected function checkTableMigrate(Table $table) {
|
|
|
|
$name = $table->getName();
|
2014-03-14 17:32:02 +04:00
|
|
|
$tmpName = 'oc_' . uniqid();
|
2014-01-29 18:48:12 +04:00
|
|
|
|
|
|
|
$this->copyTable($name, $tmpName);
|
|
|
|
|
|
|
|
//create the migration schema for the temporary table
|
2014-01-30 17:21:16 +04:00
|
|
|
$tmpTable = $this->renameTableSchema($table, $tmpName);
|
2014-01-29 18:48:12 +04:00
|
|
|
$schemaConfig = new SchemaConfig();
|
|
|
|
$schemaConfig->setName($this->connection->getDatabase());
|
|
|
|
$schema = new Schema(array($tmpTable), array(), $schemaConfig);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->applySchema($schema);
|
|
|
|
$this->dropTable($tmpName);
|
|
|
|
} catch (DBALException $e) {
|
2014-03-14 19:28:56 +04:00
|
|
|
// pgsql needs to commit it's failed transaction before doing anything else
|
|
|
|
$this->connection->commit();
|
2014-01-29 18:48:12 +04:00
|
|
|
$this->dropTable($tmpName);
|
|
|
|
throw new MigrationException($table->getName(), $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-30 17:21:16 +04:00
|
|
|
/**
|
|
|
|
* @param \Doctrine\DBAL\Schema\Table $table
|
|
|
|
* @param string $newName
|
|
|
|
* @return \Doctrine\DBAL\Schema\Table
|
|
|
|
*/
|
|
|
|
protected function renameTableSchema(Table $table, $newName) {
|
2014-03-12 17:23:00 +04:00
|
|
|
/**
|
|
|
|
* @var \Doctrine\DBAL\Schema\Index[] $indexes
|
|
|
|
*/
|
2014-01-30 17:21:16 +04:00
|
|
|
$indexes = $table->getIndexes();
|
|
|
|
$newIndexes = array();
|
|
|
|
foreach ($indexes as $index) {
|
2014-03-14 17:32:02 +04:00
|
|
|
$indexName = 'oc_' . uniqid(); // avoid conflicts in index names
|
2014-03-12 17:23:00 +04:00
|
|
|
$newIndexes[] = new Index($indexName, $index->getColumns(), $index->isUnique(), $index->isPrimary());
|
2014-01-30 17:21:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// foreign keys are not supported so we just set it to an empty array
|
2014-03-14 17:32:02 +04:00
|
|
|
return new Table($newName, $table->getColumns(), $newIndexes, array(), 0, $table->getOptions());
|
2014-01-30 17:21:16 +04:00
|
|
|
}
|
|
|
|
|
2014-01-29 18:48:12 +04:00
|
|
|
/**
|
|
|
|
* @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();
|
|
|
|
|
|
|
|
// remove tables we don't know about
|
|
|
|
/** @var $table \Doctrine\DBAL\Schema\Table */
|
|
|
|
foreach ($sourceSchema->getTables() as $table) {
|
|
|
|
if (!$targetSchema->hasTable($table->getName())) {
|
|
|
|
$sourceSchema->dropTable($table->getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// remove sequences we don't know about
|
|
|
|
foreach ($sourceSchema->getSequences() as $table) {
|
|
|
|
if (!$targetSchema->hasSequence($table->getName())) {
|
|
|
|
$sourceSchema->dropSequence($table->getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-30 17:21:16 +04:00
|
|
|
$comparator = new Comparator();
|
2014-01-29 18:48:12 +04:00
|
|
|
$schemaDiff = $comparator->compare($sourceSchema, $targetSchema);
|
|
|
|
|
|
|
|
foreach ($schemaDiff->changedTables as $tableDiff) {
|
|
|
|
$tableDiff->name = $this->connection->quoteIdentifier($tableDiff->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
$connection->beginTransaction();
|
|
|
|
foreach ($schemaDiff->toSql($connection->getDatabasePlatform()) as $sql) {
|
|
|
|
$connection->query($sql);
|
|
|
|
}
|
|
|
|
$connection->commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $sourceName
|
|
|
|
* @param string $targetName
|
|
|
|
*/
|
|
|
|
protected function copyTable($sourceName, $targetName) {
|
|
|
|
$quotedSource = $this->connection->quoteIdentifier($sourceName);
|
|
|
|
$quotedTarget = $this->connection->quoteIdentifier($targetName);
|
|
|
|
|
2014-03-14 19:28:56 +04:00
|
|
|
$this->connection->exec('CREATE TABLE ' . $quotedTarget . ' (LIKE ' . $quotedSource . ')');
|
2014-01-29 18:48:12 +04:00
|
|
|
$this->connection->exec('INSERT INTO ' . $quotedTarget . ' SELECT * FROM ' . $quotedSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
*/
|
|
|
|
protected function dropTable($name) {
|
|
|
|
$this->connection->exec('DROP TABLE ' . $this->connection->quoteIdentifier($name));
|
|
|
|
}
|
|
|
|
}
|