aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/DB/Migrator.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/DB/Migrator.php')
-rw-r--r--lib/private/DB/Migrator.php60
1 files changed, 20 insertions, 40 deletions
diff --git a/lib/private/DB/Migrator.php b/lib/private/DB/Migrator.php
index 97d91e1c100..40f8ad9676a 100644
--- a/lib/private/DB/Migrator.php
+++ b/lib/private/DB/Migrator.php
@@ -1,29 +1,9 @@
<?php
+
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Daniel Kesselberg <mail@danielkesselberg.de>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Thomas Müller <thomas.mueller@tmit.eu>
- *
- * @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/>
- *
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OC\DB;
@@ -31,14 +11,13 @@ use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Schema\AbstractAsset;
-use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaDiff;
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\Type;
+use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use function preg_match;
-use OCP\EventDispatcher\IEventDispatcher;
class Migrator {
/** @var Connection */
@@ -53,8 +32,8 @@ class Migrator {
private $noEmit = false;
public function __construct(Connection $connection,
- IConfig $config,
- ?IEventDispatcher $dispatcher = null) {
+ IConfig $config,
+ ?IEventDispatcher $dispatcher = null) {
$this->connection = $connection;
$this->config = $config;
$this->dispatcher = $dispatcher;
@@ -75,7 +54,7 @@ class Migrator {
$schemaDiff = $this->getDiff($targetSchema, $this->connection);
$script = '';
- $sqls = $schemaDiff->toSql($this->connection->getDatabasePlatform());
+ $sqls = $this->connection->getDatabasePlatform()->getAlterSchemaSQL($schemaDiff);
foreach ($sqls as $sql) {
$script .= $this->convertStatementToScript($sql);
}
@@ -95,18 +74,20 @@ class Migrator {
}
return preg_match($filterExpression, $asset) === 1;
});
- return $this->connection->getSchemaManager()->createSchema();
+ return $this->connection->createSchemaManager()->introspectSchema();
}
/**
* @return SchemaDiff
*/
protected function getDiff(Schema $targetSchema, Connection $connection) {
- // adjust varchar columns with a length higher then getVarcharMaxLength to clob
+ // Adjust STRING columns with a length higher than 4000 to TEXT (clob)
+ // for consistency between the supported databases and
+ // old vs. new installations.
foreach ($targetSchema->getTables() as $table) {
foreach ($table->getColumns() as $column) {
if ($column->getType() instanceof StringType) {
- if ($column->getLength() > $connection->getDatabasePlatform()->getVarcharMaxLength()) {
+ if ($column->getLength() > 4000) {
$column->setType(Type::getType('text'));
$column->setLength(null);
}
@@ -122,7 +103,7 @@ class Migrator {
}
return preg_match($filterExpression, $asset) === 1;
});
- $sourceSchema = $connection->getSchemaManager()->createSchema();
+ $sourceSchema = $connection->createSchemaManager()->introspectSchema();
// remove tables we don't know about
foreach ($sourceSchema->getTables() as $table) {
@@ -137,15 +118,14 @@ class Migrator {
}
}
- /** @psalm-suppress InternalMethod */
- $comparator = new Comparator();
- return $comparator->compare($sourceSchema, $targetSchema);
+ $comparator = $connection->createSchemaManager()->createComparator();
+ return $comparator->compareSchemas($sourceSchema, $targetSchema);
}
/**
* @throws Exception
*/
- protected function applySchema(Schema $targetSchema, Connection $connection = null) {
+ protected function applySchema(Schema $targetSchema, ?Connection $connection = null) {
if (is_null($connection)) {
$connection = $this->connection;
}
@@ -155,11 +135,11 @@ class Migrator {
if (!$connection->getDatabasePlatform() instanceof MySQLPlatform) {
$connection->beginTransaction();
}
- $sqls = $schemaDiff->toSql($connection->getDatabasePlatform());
+ $sqls = $connection->getDatabasePlatform()->getAlterSchemaSQL($schemaDiff);
$step = 0;
foreach ($sqls as $sql) {
$this->emit($sql, $step++, count($sqls));
- $connection->query($sql);
+ $connection->executeStatement($sql);
}
if (!$connection->getDatabasePlatform() instanceof MySQLPlatform) {
$connection->commit();
@@ -178,7 +158,7 @@ class Migrator {
}
protected function getFilterExpression() {
- return '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/';
+ return '/^' . preg_quote($this->config->getSystemValueString('dbtableprefix', 'oc_'), '/') . '/';
}
protected function emit(string $sql, int $step, int $max): void {