2014-01-29 14:48:12 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author martin-rueegg <martin.rueegg@metaworx.ch>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 16:13:36 +00:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author tbelau666 <thomas.belau@gmx.de>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-06-25 09:43:55 +00:00
|
|
|
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @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 <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2014-01-29 14:48:12 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2014-01-29 14:48:12 +00:00
|
|
|
namespace OC\DB;
|
|
|
|
|
|
|
|
use \Doctrine\DBAL\DBALException;
|
2014-01-30 13:21:16 +00:00
|
|
|
use \Doctrine\DBAL\Schema\Index;
|
2014-01-29 14:48:12 +00:00
|
|
|
use \Doctrine\DBAL\Schema\Table;
|
|
|
|
use \Doctrine\DBAL\Schema\Schema;
|
|
|
|
use \Doctrine\DBAL\Schema\SchemaConfig;
|
2014-01-30 13:21:16 +00:00
|
|
|
use \Doctrine\DBAL\Schema\Comparator;
|
2016-06-29 12:54:41 +00:00
|
|
|
use Doctrine\DBAL\Types\StringType;
|
|
|
|
use Doctrine\DBAL\Types\Type;
|
2014-12-22 09:43:56 +00:00
|
|
|
use OCP\IConfig;
|
2013-10-17 18:16:27 +00:00
|
|
|
use OCP\Security\ISecureRandom;
|
2019-05-28 17:46:36 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2016-03-30 21:38:26 +00:00
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent;
|
2014-01-29 14:48:12 +00:00
|
|
|
|
|
|
|
class Migrator {
|
2013-10-17 18:16:27 +00:00
|
|
|
|
2017-06-01 14:56:34 +00:00
|
|
|
/** @var \Doctrine\DBAL\Connection */
|
2014-01-29 14:48:12 +00:00
|
|
|
protected $connection;
|
|
|
|
|
2017-06-01 14:56:34 +00:00
|
|
|
/** @var ISecureRandom */
|
2013-10-17 18:16:27 +00:00
|
|
|
private $random;
|
|
|
|
|
2014-12-22 09:43:56 +00:00
|
|
|
/** @var IConfig */
|
|
|
|
protected $config;
|
|
|
|
|
2019-05-28 17:46:36 +00:00
|
|
|
/** @var EventDispatcherInterface */
|
2016-03-30 21:38:26 +00:00
|
|
|
private $dispatcher;
|
|
|
|
|
2016-04-04 14:20:53 +00:00
|
|
|
/** @var bool */
|
|
|
|
private $noEmit = false;
|
|
|
|
|
2014-01-29 14:48:12 +00:00
|
|
|
/**
|
2016-03-30 21:38:26 +00:00
|
|
|
* @param \Doctrine\DBAL\Connection|Connection $connection
|
2013-10-17 18:16:27 +00:00
|
|
|
* @param ISecureRandom $random
|
2014-12-22 09:43:56 +00:00
|
|
|
* @param IConfig $config
|
2019-05-28 17:46:36 +00:00
|
|
|
* @param EventDispatcherInterface $dispatcher
|
2014-01-29 14:48:12 +00:00
|
|
|
*/
|
2016-03-30 21:38:26 +00:00
|
|
|
public function __construct(\Doctrine\DBAL\Connection $connection,
|
|
|
|
ISecureRandom $random,
|
|
|
|
IConfig $config,
|
2019-05-28 17:46:36 +00:00
|
|
|
EventDispatcherInterface $dispatcher = null) {
|
2014-01-29 14:48:12 +00:00
|
|
|
$this->connection = $connection;
|
2013-10-17 18:16:27 +00:00
|
|
|
$this->random = $random;
|
2014-12-22 09:43:56 +00:00
|
|
|
$this->config = $config;
|
2016-03-30 21:38:26 +00:00
|
|
|
$this->dispatcher = $dispatcher;
|
2014-01-29 14:48:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Doctrine\DBAL\Schema\Schema $targetSchema
|
|
|
|
*/
|
|
|
|
public function migrate(Schema $targetSchema) {
|
2016-04-04 14:20:53 +00:00
|
|
|
$this->noEmit = true;
|
2014-01-29 14:48:12 +00:00
|
|
|
$this->applySchema($targetSchema);
|
|
|
|
}
|
|
|
|
|
2014-04-08 14:01:08 +00:00
|
|
|
/**
|
|
|
|
* @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) {
|
2013-10-17 18:16:27 +00:00
|
|
|
$script .= $this->convertStatementToScript($sql);
|
2014-04-08 14:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $script;
|
|
|
|
}
|
|
|
|
|
2014-01-29 14:48:12 +00:00
|
|
|
/**
|
|
|
|
* @param Schema $targetSchema
|
2014-03-27 13:44:19 +00:00
|
|
|
* @throws \OC\DB\MigrationException
|
2014-01-29 14:48:12 +00:00
|
|
|
*/
|
|
|
|
public function checkMigrate(Schema $targetSchema) {
|
2016-04-04 14:20:53 +00:00
|
|
|
$this->noEmit = true;
|
|
|
|
/**@var \Doctrine\DBAL\Schema\Table[] $tables */
|
2014-01-29 14:48:12 +00:00
|
|
|
$tables = $targetSchema->getTables();
|
2015-01-12 12:57:46 +00:00
|
|
|
$filterExpression = $this->getFilterExpression();
|
2014-11-30 22:17:09 +00:00
|
|
|
$this->connection->getConfiguration()->
|
2015-01-12 12:57:46 +00:00
|
|
|
setFilterSchemaAssetsExpression($filterExpression);
|
2014-01-29 14:48:12 +00:00
|
|
|
$existingTables = $this->connection->getSchemaManager()->listTableNames();
|
|
|
|
|
2016-04-04 14:20:53 +00:00
|
|
|
$step = 0;
|
2014-01-29 14:48:12 +00:00
|
|
|
foreach ($tables as $table) {
|
2014-03-14 13:32:02 +00:00
|
|
|
if (strpos($table->getName(), '.')) {
|
|
|
|
list(, $tableName) = explode('.', $table->getName());
|
|
|
|
} else {
|
|
|
|
$tableName = $table->getName();
|
|
|
|
}
|
2016-04-04 14:20:53 +00:00
|
|
|
$this->emitCheckStep($tableName, $step++, count($tables));
|
2014-01-29 14:48:12 +00:00
|
|
|
// don't need to check for new tables
|
|
|
|
if (array_search($tableName, $existingTables) !== false) {
|
|
|
|
$this->checkTableMigrate($table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-03 09:24:31 +00:00
|
|
|
/**
|
|
|
|
* Create a unique name for the temporary table
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function generateTemporaryTableName($name) {
|
2015-03-03 00:49:15 +00:00
|
|
|
return $this->config->getSystemValue('dbtableprefix', 'oc_') . $name . '_' . $this->random->generate(13, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
|
2014-06-03 09:24:31 +00:00
|
|
|
}
|
|
|
|
|
2014-01-29 14:48:12 +00:00
|
|
|
/**
|
|
|
|
* 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 13:21:16 +00:00
|
|
|
* @throws \OC\DB\MigrationException
|
2014-01-29 14:48:12 +00:00
|
|
|
*/
|
|
|
|
protected function checkTableMigrate(Table $table) {
|
|
|
|
$name = $table->getName();
|
2014-06-03 09:24:31 +00:00
|
|
|
$tmpName = $this->generateTemporaryTableName($name);
|
2014-01-29 14:48:12 +00:00
|
|
|
|
|
|
|
$this->copyTable($name, $tmpName);
|
|
|
|
|
|
|
|
//create the migration schema for the temporary table
|
2014-01-30 13:21:16 +00:00
|
|
|
$tmpTable = $this->renameTableSchema($table, $tmpName);
|
2014-01-29 14:48:12 +00: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 15:28:56 +00:00
|
|
|
// pgsql needs to commit it's failed transaction before doing anything else
|
2014-07-23 18:13:19 +00:00
|
|
|
if ($this->connection->isTransactionActive()) {
|
|
|
|
$this->connection->commit();
|
|
|
|
}
|
2014-01-29 14:48:12 +00:00
|
|
|
$this->dropTable($tmpName);
|
|
|
|
throw new MigrationException($table->getName(), $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-30 13:21:16 +00:00
|
|
|
/**
|
|
|
|
* @param \Doctrine\DBAL\Schema\Table $table
|
|
|
|
* @param string $newName
|
|
|
|
* @return \Doctrine\DBAL\Schema\Table
|
|
|
|
*/
|
|
|
|
protected function renameTableSchema(Table $table, $newName) {
|
2014-03-12 13:23:00 +00:00
|
|
|
/**
|
|
|
|
* @var \Doctrine\DBAL\Schema\Index[] $indexes
|
|
|
|
*/
|
2014-01-30 13:21:16 +00:00
|
|
|
$indexes = $table->getIndexes();
|
|
|
|
$newIndexes = array();
|
|
|
|
foreach ($indexes as $index) {
|
2014-06-17 12:11:02 +00:00
|
|
|
if ($index->isPrimary()) {
|
|
|
|
// do not rename primary key
|
|
|
|
$indexName = $index->getName();
|
|
|
|
} else {
|
|
|
|
// avoid conflicts in index names
|
2015-06-08 15:59:24 +00:00
|
|
|
$indexName = $this->config->getSystemValue('dbtableprefix', 'oc_') . $this->random->generate(13, ISecureRandom::CHAR_LOWER);
|
2014-06-17 12:11:02 +00:00
|
|
|
}
|
2014-03-12 13:23:00 +00:00
|
|
|
$newIndexes[] = new Index($indexName, $index->getColumns(), $index->isUnique(), $index->isPrimary());
|
2014-01-30 13:21:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// foreign keys are not supported so we just set it to an empty array
|
2014-03-14 13:32:02 +00:00
|
|
|
return new Table($newName, $table->getColumns(), $newIndexes, array(), 0, $table->getOptions());
|
2014-01-30 13:21:16 +00:00
|
|
|
}
|
|
|
|
|
2017-06-01 14:56:34 +00:00
|
|
|
public function createSchema() {
|
|
|
|
$filterExpression = $this->getFilterExpression();
|
|
|
|
$this->connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
|
|
|
|
return $this->connection->getSchemaManager()->createSchema();
|
|
|
|
}
|
|
|
|
|
2016-06-29 12:54:41 +00:00
|
|
|
/**
|
|
|
|
* @param Schema $targetSchema
|
|
|
|
* @param \Doctrine\DBAL\Connection $connection
|
|
|
|
* @return \Doctrine\DBAL\Schema\SchemaDiff
|
|
|
|
* @throws DBALException
|
|
|
|
*/
|
2014-04-08 14:01:08 +00:00
|
|
|
protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) {
|
2016-06-29 12:54:41 +00:00
|
|
|
// adjust varchar columns with a length higher then getVarcharMaxLength to clob
|
|
|
|
foreach ($targetSchema->getTables() as $table) {
|
|
|
|
foreach ($table->getColumns() as $column) {
|
|
|
|
if ($column->getType() instanceof StringType) {
|
|
|
|
if ($column->getLength() > $connection->getDatabasePlatform()->getVarcharMaxLength()) {
|
|
|
|
$column->setType(Type::getType('text'));
|
|
|
|
$column->setLength(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 12:57:46 +00:00
|
|
|
$filterExpression = $this->getFilterExpression();
|
2017-06-01 14:56:34 +00:00
|
|
|
$this->connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
|
2014-04-08 14:01:08 +00:00
|
|
|
$sourceSchema = $connection->getSchemaManager()->createSchema();
|
2014-01-29 14:48:12 +00:00
|
|
|
|
|
|
|
// 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 13:21:16 +00:00
|
|
|
$comparator = new Comparator();
|
2014-04-11 13:10:09 +00:00
|
|
|
return $comparator->compare($sourceSchema, $targetSchema);
|
2014-04-08 14:01:08 +00: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;
|
2014-01-29 14:48:12 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 14:01:08 +00:00
|
|
|
$schemaDiff = $this->getDiff($targetSchema, $connection);
|
|
|
|
|
2014-01-29 14:48:12 +00:00
|
|
|
$connection->beginTransaction();
|
2016-03-30 21:38:26 +00:00
|
|
|
$sqls = $schemaDiff->toSql($connection->getDatabasePlatform());
|
|
|
|
$step = 0;
|
|
|
|
foreach ($sqls as $sql) {
|
|
|
|
$this->emit($sql, $step++, count($sqls));
|
2014-01-29 14:48:12 +00:00
|
|
|
$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 15:28:56 +00:00
|
|
|
$this->connection->exec('CREATE TABLE ' . $quotedTarget . ' (LIKE ' . $quotedSource . ')');
|
2014-01-29 14:48:12 +00: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));
|
|
|
|
}
|
2013-10-17 18:16:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $statement
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function convertStatementToScript($statement) {
|
|
|
|
$script = $statement . ';';
|
|
|
|
$script .= PHP_EOL;
|
|
|
|
$script .= PHP_EOL;
|
|
|
|
return $script;
|
|
|
|
}
|
2015-01-12 12:57:46 +00:00
|
|
|
|
|
|
|
protected function getFilterExpression() {
|
|
|
|
return '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/';
|
|
|
|
}
|
2016-03-30 21:38:26 +00:00
|
|
|
|
|
|
|
protected function emit($sql, $step, $max) {
|
2016-04-04 14:20:53 +00:00
|
|
|
if ($this->noEmit) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-30 21:38:26 +00:00
|
|
|
if(is_null($this->dispatcher)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->dispatcher->dispatch('\OC\DB\Migrator::executeSql', new GenericEvent($sql, [$step+1, $max]));
|
|
|
|
}
|
2016-04-04 14:20:53 +00:00
|
|
|
|
|
|
|
private function emitCheckStep($tableName, $step, $max) {
|
|
|
|
if(is_null($this->dispatcher)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->dispatcher->dispatch('\OC\DB\Migrator::checkTable', new GenericEvent($tableName, [$step+1, $max]));
|
|
|
|
}
|
2014-01-29 14:48:12 +00:00
|
|
|
}
|