2014-01-29 18:48:12 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author martin-rueegg <martin.rueegg@metaworx.ch>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author tbelau666 <thomas.belau@gmx.de>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-06-25 12:43:55 +03:00
|
|
|
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
|
2020-12-16 16:54:15 +03:00
|
|
|
* @author Vincent Petry <vincent@nextcloud.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
2014-01-29 18:48:12 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-01-29 18:48:12 +04:00
|
|
|
namespace OC\DB;
|
|
|
|
|
2021-01-03 17:28:31 +03:00
|
|
|
use Doctrine\DBAL\Exception;
|
2021-01-08 18:25:40 +03:00
|
|
|
use Doctrine\DBAL\Platforms\MySQLPlatform;
|
2021-01-03 17:28:31 +03:00
|
|
|
use Doctrine\DBAL\Schema\AbstractAsset;
|
2019-11-22 22:52:10 +03:00
|
|
|
use Doctrine\DBAL\Schema\Comparator;
|
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
2016-06-29 15:54:41 +03:00
|
|
|
use Doctrine\DBAL\Types\StringType;
|
|
|
|
use Doctrine\DBAL\Types\Type;
|
2014-12-22 12:43:56 +03:00
|
|
|
use OCP\IConfig;
|
2019-05-28 20:46:36 +03:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2016-03-31 00:38:26 +03:00
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent;
|
2021-01-03 17:28:31 +03:00
|
|
|
use function preg_match;
|
2014-01-29 18:48:12 +04:00
|
|
|
|
|
|
|
class Migrator {
|
2013-10-17 22:16:27 +04:00
|
|
|
|
2017-06-01 17:56:34 +03:00
|
|
|
/** @var \Doctrine\DBAL\Connection */
|
2014-01-29 18:48:12 +04:00
|
|
|
protected $connection;
|
|
|
|
|
2014-12-22 12:43:56 +03:00
|
|
|
/** @var IConfig */
|
|
|
|
protected $config;
|
|
|
|
|
2019-05-28 20:46:36 +03:00
|
|
|
/** @var EventDispatcherInterface */
|
2016-03-31 00:38:26 +03:00
|
|
|
private $dispatcher;
|
|
|
|
|
2016-04-04 17:20:53 +03:00
|
|
|
/** @var bool */
|
|
|
|
private $noEmit = false;
|
|
|
|
|
2014-01-29 18:48:12 +04:00
|
|
|
/**
|
2020-03-06 18:28:42 +03:00
|
|
|
* @param \Doctrine\DBAL\Connection $connection
|
2014-12-22 12:43:56 +03:00
|
|
|
* @param IConfig $config
|
2019-05-28 20:46:36 +03:00
|
|
|
* @param EventDispatcherInterface $dispatcher
|
2014-01-29 18:48:12 +04:00
|
|
|
*/
|
2016-03-31 00:38:26 +03:00
|
|
|
public function __construct(\Doctrine\DBAL\Connection $connection,
|
|
|
|
IConfig $config,
|
2019-05-28 20:46:36 +03:00
|
|
|
EventDispatcherInterface $dispatcher = null) {
|
2014-01-29 18:48:12 +04:00
|
|
|
$this->connection = $connection;
|
2014-12-22 12:43:56 +03:00
|
|
|
$this->config = $config;
|
2016-03-31 00:38:26 +03:00
|
|
|
$this->dispatcher = $dispatcher;
|
2014-01-29 18:48:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Doctrine\DBAL\Schema\Schema $targetSchema
|
2021-01-12 14:24:36 +03:00
|
|
|
*
|
|
|
|
* @throws Exception
|
2014-01-29 18:48:12 +04:00
|
|
|
*/
|
|
|
|
public function migrate(Schema $targetSchema) {
|
2016-04-04 17:20:53 +03:00
|
|
|
$this->noEmit = true;
|
2014-01-29 18:48:12 +04:00
|
|
|
$this->applySchema($targetSchema);
|
|
|
|
}
|
|
|
|
|
2014-04-08 18:01:08 +04: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 22:16:27 +04:00
|
|
|
$script .= $this->convertStatementToScript($sql);
|
2014-04-08 18:01:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $script;
|
|
|
|
}
|
|
|
|
|
2021-01-12 14:24:36 +03:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2017-06-01 17:56:34 +03:00
|
|
|
public function createSchema() {
|
2021-01-03 17:28:31 +03:00
|
|
|
$this->connection->getConfiguration()->setSchemaAssetsFilter(function ($asset) {
|
|
|
|
/** @var string|AbstractAsset $asset */
|
|
|
|
$filterExpression = $this->getFilterExpression();
|
|
|
|
if ($asset instanceof AbstractAsset) {
|
2021-03-02 18:04:44 +03:00
|
|
|
return preg_match($filterExpression, $asset->getName()) === 1;
|
2021-01-03 17:28:31 +03:00
|
|
|
}
|
2021-03-02 18:04:44 +03:00
|
|
|
return preg_match($filterExpression, $asset) === 1;
|
2021-01-03 17:28:31 +03:00
|
|
|
});
|
2017-06-01 17:56:34 +03:00
|
|
|
return $this->connection->getSchemaManager()->createSchema();
|
|
|
|
}
|
|
|
|
|
2016-06-29 15:54:41 +03:00
|
|
|
/**
|
|
|
|
* @param Schema $targetSchema
|
|
|
|
* @param \Doctrine\DBAL\Connection $connection
|
|
|
|
* @return \Doctrine\DBAL\Schema\SchemaDiff
|
|
|
|
*/
|
2014-04-08 18:01:08 +04:00
|
|
|
protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) {
|
2016-06-29 15:54:41 +03: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 17:28:31 +03:00
|
|
|
$this->connection->getConfiguration()->setSchemaAssetsFilter(function ($asset) {
|
|
|
|
/** @var string|AbstractAsset $asset */
|
|
|
|
$filterExpression = $this->getFilterExpression();
|
|
|
|
if ($asset instanceof AbstractAsset) {
|
2021-03-02 18:04:44 +03:00
|
|
|
return preg_match($filterExpression, $asset->getName()) === 1;
|
2021-01-03 17:28:31 +03:00
|
|
|
}
|
2021-03-02 18:04:44 +03:00
|
|
|
return preg_match($filterExpression, $asset) === 1;
|
2021-01-03 17:28:31 +03:00
|
|
|
});
|
2014-04-08 18:01:08 +04:00
|
|
|
$sourceSchema = $connection->getSchemaManager()->createSchema();
|
2014-01-29 18:48:12 +04:00
|
|
|
|
|
|
|
// remove tables we don't know about
|
|
|
|
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-04-11 17:10:09 +04:00
|
|
|
return $comparator->compare($sourceSchema, $targetSchema);
|
2014-04-08 18:01:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Doctrine\DBAL\Schema\Schema $targetSchema
|
|
|
|
* @param \Doctrine\DBAL\Connection $connection
|
2021-01-12 14:24:36 +03:00
|
|
|
*
|
|
|
|
* @throws Exception
|
2014-04-08 18:01:08 +04:00
|
|
|
*/
|
|
|
|
protected function applySchema(Schema $targetSchema, \Doctrine\DBAL\Connection $connection = null) {
|
|
|
|
if (is_null($connection)) {
|
|
|
|
$connection = $this->connection;
|
2014-01-29 18:48:12 +04:00
|
|
|
}
|
|
|
|
|
2014-04-08 18:01:08 +04:00
|
|
|
$schemaDiff = $this->getDiff($targetSchema, $connection);
|
|
|
|
|
2021-01-08 18:25:40 +03:00
|
|
|
if (!$connection->getDatabasePlatform() instanceof MySQLPlatform) {
|
|
|
|
$connection->beginTransaction();
|
|
|
|
}
|
2016-03-31 00:38:26 +03:00
|
|
|
$sqls = $schemaDiff->toSql($connection->getDatabasePlatform());
|
|
|
|
$step = 0;
|
|
|
|
foreach ($sqls as $sql) {
|
|
|
|
$this->emit($sql, $step++, count($sqls));
|
2014-01-29 18:48:12 +04:00
|
|
|
$connection->query($sql);
|
|
|
|
}
|
2021-01-08 18:25:40 +03:00
|
|
|
if (!$connection->getDatabasePlatform() instanceof MySQLPlatform) {
|
|
|
|
$connection->commit();
|
|
|
|
}
|
2014-01-29 18:48:12 +04:00
|
|
|
}
|
|
|
|
|
2013-10-17 22:16:27 +04:00
|
|
|
/**
|
|
|
|
* @param $statement
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function convertStatementToScript($statement) {
|
|
|
|
$script = $statement . ';';
|
|
|
|
$script .= PHP_EOL;
|
|
|
|
$script .= PHP_EOL;
|
|
|
|
return $script;
|
|
|
|
}
|
2015-01-12 15:57:46 +03:00
|
|
|
|
|
|
|
protected function getFilterExpression() {
|
|
|
|
return '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/';
|
|
|
|
}
|
2016-03-31 00:38:26 +03:00
|
|
|
|
|
|
|
protected function emit($sql, $step, $max) {
|
2016-04-04 17:20:53 +03:00
|
|
|
if ($this->noEmit) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
if (is_null($this->dispatcher)) {
|
2016-03-31 00:38:26 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-10-05 16:12:57 +03:00
|
|
|
$this->dispatcher->dispatch('\OC\DB\Migrator::executeSql', new GenericEvent($sql, [$step + 1, $max]));
|
2016-03-31 00:38:26 +03:00
|
|
|
}
|
2014-01-29 18:48:12 +04:00
|
|
|
}
|