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 Test\DB;
|
|
|
|
|
2021-01-03 17:28:31 +03:00
|
|
|
use Doctrine\DBAL\Exception;
|
2021-04-14 15:11:06 +03:00
|
|
|
use Doctrine\DBAL\ParameterType;
|
2021-01-11 18:10:16 +03:00
|
|
|
use Doctrine\DBAL\Platforms\MySQLPlatform;
|
2014-10-23 16:32:37 +04:00
|
|
|
use Doctrine\DBAL\Platforms\OraclePlatform;
|
2021-03-24 22:27:39 +03:00
|
|
|
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
|
2021-01-11 18:10:16 +03:00
|
|
|
use Doctrine\DBAL\Platforms\SqlitePlatform;
|
2019-11-22 22:52:10 +03:00
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
|
|
|
use Doctrine\DBAL\Schema\SchemaConfig;
|
2021-03-24 22:27:39 +03:00
|
|
|
use OC\DB\Migrator;
|
|
|
|
use OC\DB\MySQLMigrator;
|
|
|
|
use OC\DB\OracleMigrator;
|
|
|
|
use OC\DB\PostgreSqlMigrator;
|
|
|
|
use OC\DB\SQLiteMigrator;
|
2021-04-14 15:11:06 +03:00
|
|
|
use OCP\DB\Types;
|
2015-07-29 19:19:31 +03:00
|
|
|
use OCP\IConfig;
|
2014-01-29 18:48:12 +04:00
|
|
|
|
2015-11-03 03:52:41 +03:00
|
|
|
/**
|
2016-05-20 16:38:20 +03:00
|
|
|
* Class MigratorTest
|
2015-11-03 03:52:41 +03:00
|
|
|
*
|
|
|
|
* @group DB
|
|
|
|
*
|
|
|
|
* @package Test\DB
|
|
|
|
*/
|
2016-05-20 16:38:20 +03:00
|
|
|
class MigratorTest extends \Test\TestCase {
|
2014-01-29 18:48:12 +04:00
|
|
|
/**
|
|
|
|
* @var \Doctrine\DBAL\Connection $connection
|
|
|
|
*/
|
|
|
|
private $connection;
|
|
|
|
|
2015-05-18 17:57:59 +03:00
|
|
|
/**
|
|
|
|
* @var IConfig
|
|
|
|
**/
|
|
|
|
private $config;
|
|
|
|
|
2015-07-29 19:19:31 +03:00
|
|
|
/** @var string */
|
2014-01-29 18:48:12 +04:00
|
|
|
private $tableName;
|
|
|
|
|
2017-10-02 13:32:21 +03:00
|
|
|
/** @var string */
|
|
|
|
private $tableNameTmp;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2014-11-11 01:30:38 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2015-05-18 17:57:59 +03:00
|
|
|
$this->config = \OC::$server->getConfig();
|
2021-01-03 17:28:31 +03:00
|
|
|
$this->connection = \OC::$server->get(\OC\DB\Connection::class);
|
2021-03-24 22:27:39 +03:00
|
|
|
|
2017-10-02 13:32:21 +03:00
|
|
|
$this->tableName = $this->getUniqueTableName();
|
|
|
|
$this->tableNameTmp = $this->getUniqueTableName();
|
|
|
|
}
|
|
|
|
|
2021-03-24 22:27:39 +03:00
|
|
|
private function getMigrator(): Migrator {
|
|
|
|
$platform = $this->connection->getDatabasePlatform();
|
|
|
|
$random = \OC::$server->getSecureRandom();
|
|
|
|
$dispatcher = \OC::$server->getEventDispatcher();
|
|
|
|
if ($platform instanceof SqlitePlatform) {
|
|
|
|
return new SQLiteMigrator($this->connection, $this->config, $dispatcher);
|
|
|
|
} elseif ($platform instanceof OraclePlatform) {
|
|
|
|
return new OracleMigrator($this->connection, $this->config, $dispatcher);
|
|
|
|
} elseif ($platform instanceof MySQLPlatform) {
|
|
|
|
return new MySQLMigrator($this->connection, $this->config, $dispatcher);
|
|
|
|
} elseif ($platform instanceof PostgreSQL94Platform) {
|
|
|
|
return new PostgreSqlMigrator($this->connection, $this->config, $dispatcher);
|
|
|
|
}
|
|
|
|
return new Migrator($this->connection, $this->config, $dispatcher);
|
|
|
|
}
|
|
|
|
|
2017-10-02 13:32:21 +03:00
|
|
|
private function getUniqueTableName() {
|
|
|
|
return strtolower($this->getUniqueID($this->config->getSystemValue('dbtableprefix', 'oc_') . 'test_'));
|
2014-01-29 18:48:12 +04:00
|
|
|
}
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function tearDown(): void {
|
2017-10-02 13:32:21 +03:00
|
|
|
// Try to delete if exists (IF EXISTS NOT SUPPORTED IN ORACLE)
|
|
|
|
try {
|
|
|
|
$this->connection->exec('DROP TABLE ' . $this->connection->quoteIdentifier($this->tableNameTmp));
|
2021-01-03 17:28:31 +03:00
|
|
|
} catch (Exception $e) {
|
2020-04-10 15:19:56 +03:00
|
|
|
}
|
2017-10-02 13:32:21 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
$this->connection->exec('DROP TABLE ' . $this->connection->quoteIdentifier($this->tableName));
|
2021-01-03 17:28:31 +03:00
|
|
|
} catch (Exception $e) {
|
2020-04-10 15:19:56 +03:00
|
|
|
}
|
2014-11-11 01:30:38 +03:00
|
|
|
parent::tearDown();
|
2014-01-29 18:48:12 +04:00
|
|
|
}
|
|
|
|
|
2014-03-27 17:44:19 +04:00
|
|
|
/**
|
|
|
|
* @return \Doctrine\DBAL\Schema\Schema[]
|
|
|
|
*/
|
|
|
|
private function getDuplicateKeySchemas() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$startSchema = new Schema([], [], $this->getSchemaConfig());
|
2014-03-27 17:44:19 +04:00
|
|
|
$table = $startSchema->createTable($this->tableName);
|
2014-01-29 18:48:12 +04:00
|
|
|
$table->addColumn('id', 'integer');
|
|
|
|
$table->addColumn('name', 'string');
|
2020-03-26 11:30:18 +03:00
|
|
|
$table->addIndex(['id'], $this->tableName . '_id');
|
2014-01-29 18:48:12 +04:00
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$endSchema = new Schema([], [], $this->getSchemaConfig());
|
2014-03-27 17:44:19 +04:00
|
|
|
$table = $endSchema->createTable($this->tableName);
|
2014-01-29 18:48:12 +04:00
|
|
|
$table->addColumn('id', 'integer');
|
|
|
|
$table->addColumn('name', 'string');
|
2020-03-26 11:30:18 +03:00
|
|
|
$table->addUniqueIndex(['id'], $this->tableName . '_id');
|
2014-03-27 17:44:19 +04:00
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
return [$startSchema, $endSchema];
|
2014-01-29 18:48:12 +04:00
|
|
|
}
|
|
|
|
|
2020-12-04 10:23:12 +03:00
|
|
|
/**
|
|
|
|
* @return \Doctrine\DBAL\Schema\Schema[]
|
|
|
|
*/
|
|
|
|
private function getChangedTypeSchema($from, $to) {
|
|
|
|
$startSchema = new Schema([], [], $this->getSchemaConfig());
|
|
|
|
$table = $startSchema->createTable($this->tableName);
|
|
|
|
$table->addColumn('id', $from);
|
|
|
|
$table->addColumn('name', 'string');
|
|
|
|
$table->addIndex(['id'], $this->tableName . '_id');
|
|
|
|
|
|
|
|
$endSchema = new Schema([], [], $this->getSchemaConfig());
|
|
|
|
$table = $endSchema->createTable($this->tableName);
|
|
|
|
$table->addColumn('id', $to);
|
|
|
|
$table->addColumn('name', 'string');
|
|
|
|
$table->addIndex(['id'], $this->tableName . '_id');
|
|
|
|
|
|
|
|
return [$startSchema, $endSchema];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-29 18:48:12 +04:00
|
|
|
private function getSchemaConfig() {
|
|
|
|
$config = new SchemaConfig();
|
|
|
|
$config->setName($this->connection->getDatabase());
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
2014-03-27 17:44:19 +04:00
|
|
|
private function isSQLite() {
|
2021-01-11 18:10:16 +03:00
|
|
|
return $this->connection->getDatabasePlatform() instanceof SqlitePlatform;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function isMySQL() {
|
|
|
|
return $this->connection->getDatabasePlatform() instanceof MySQLPlatform;
|
2014-03-27 17:44:19 +04:00
|
|
|
}
|
|
|
|
|
2014-01-29 18:48:12 +04:00
|
|
|
public function testUpgrade() {
|
2021-01-12 12:15:48 +03:00
|
|
|
[$startSchema, $endSchema] = $this->getDuplicateKeySchemas();
|
2021-03-24 22:27:39 +03:00
|
|
|
$migrator = $this->getMigrator();
|
2014-03-27 17:44:19 +04:00
|
|
|
$migrator->migrate($startSchema);
|
2014-01-29 18:48:12 +04:00
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->connection->insert($this->tableName, ['id' => 1, 'name' => 'foo']);
|
|
|
|
$this->connection->insert($this->tableName, ['id' => 2, 'name' => 'bar']);
|
|
|
|
$this->connection->insert($this->tableName, ['id' => 3, 'name' => 'qwerty']);
|
2014-01-29 18:48:12 +04:00
|
|
|
|
2014-03-27 17:44:19 +04:00
|
|
|
$migrator->migrate($endSchema);
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2014-01-29 18:48:12 +04:00
|
|
|
}
|
2014-03-27 17:44:19 +04:00
|
|
|
|
2015-05-18 17:57:59 +03:00
|
|
|
public function testUpgradeDifferentPrefix() {
|
|
|
|
$oldTablePrefix = $this->config->getSystemValue('dbtableprefix', 'oc_');
|
|
|
|
|
|
|
|
$this->config->setSystemValue('dbtableprefix', 'ownc_');
|
|
|
|
$this->tableName = strtolower($this->getUniqueID($this->config->getSystemValue('dbtableprefix') . 'test_'));
|
|
|
|
|
2021-01-12 12:15:48 +03:00
|
|
|
[$startSchema, $endSchema] = $this->getDuplicateKeySchemas();
|
2021-03-24 22:27:39 +03:00
|
|
|
$migrator = $this->getMigrator();
|
2015-05-18 17:57:59 +03:00
|
|
|
$migrator->migrate($startSchema);
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->connection->insert($this->tableName, ['id' => 1, 'name' => 'foo']);
|
|
|
|
$this->connection->insert($this->tableName, ['id' => 2, 'name' => 'bar']);
|
|
|
|
$this->connection->insert($this->tableName, ['id' => 3, 'name' => 'qwerty']);
|
2015-05-18 17:57:59 +03:00
|
|
|
|
|
|
|
$migrator->migrate($endSchema);
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2015-05-18 17:57:59 +03:00
|
|
|
|
|
|
|
$this->config->setSystemValue('dbtableprefix', $oldTablePrefix);
|
|
|
|
}
|
|
|
|
|
2014-03-27 17:44:19 +04:00
|
|
|
public function testInsertAfterUpgrade() {
|
2021-01-12 12:15:48 +03:00
|
|
|
[$startSchema, $endSchema] = $this->getDuplicateKeySchemas();
|
2021-03-24 22:27:39 +03:00
|
|
|
$migrator = $this->getMigrator();
|
2014-03-27 17:44:19 +04:00
|
|
|
$migrator->migrate($startSchema);
|
|
|
|
|
|
|
|
$migrator->migrate($endSchema);
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->connection->insert($this->tableName, ['id' => 1, 'name' => 'foo']);
|
|
|
|
$this->connection->insert($this->tableName, ['id' => 2, 'name' => 'bar']);
|
2014-03-27 17:44:19 +04:00
|
|
|
try {
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->connection->insert($this->tableName, ['id' => 2, 'name' => 'qwerty']);
|
2014-03-27 17:44:19 +04:00
|
|
|
$this->fail('Expected duplicate key insert to fail');
|
2021-01-03 17:28:31 +03:00
|
|
|
} catch (Exception $e) {
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2014-03-27 17:44:19 +04:00
|
|
|
}
|
|
|
|
}
|
2014-06-17 17:07:36 +04:00
|
|
|
|
|
|
|
public function testAddingPrimaryKeyWithAutoIncrement() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$startSchema = new Schema([], [], $this->getSchemaConfig());
|
2014-06-17 17:07:36 +04:00
|
|
|
$table = $startSchema->createTable($this->tableName);
|
|
|
|
$table->addColumn('id', 'integer');
|
|
|
|
$table->addColumn('name', 'string');
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$endSchema = new Schema([], [], $this->getSchemaConfig());
|
2014-06-17 17:07:36 +04:00
|
|
|
$table = $endSchema->createTable($this->tableName);
|
2020-03-26 11:30:18 +03:00
|
|
|
$table->addColumn('id', 'integer', ['autoincrement' => true]);
|
2014-06-17 17:07:36 +04:00
|
|
|
$table->addColumn('name', 'string');
|
2020-03-26 11:30:18 +03:00
|
|
|
$table->setPrimaryKey(['id']);
|
2014-06-17 17:07:36 +04:00
|
|
|
|
2021-03-24 22:27:39 +03:00
|
|
|
$migrator = $this->getMigrator();
|
2014-06-17 17:07:36 +04:00
|
|
|
$migrator->migrate($startSchema);
|
|
|
|
|
|
|
|
$migrator->migrate($endSchema);
|
|
|
|
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2014-06-17 17:07:36 +04:00
|
|
|
}
|
2014-07-01 14:55:36 +04:00
|
|
|
|
|
|
|
public function testReservedKeywords() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$startSchema = new Schema([], [], $this->getSchemaConfig());
|
2014-07-01 14:55:36 +04:00
|
|
|
$table = $startSchema->createTable($this->tableName);
|
2020-03-26 11:30:18 +03:00
|
|
|
$table->addColumn('id', 'integer', ['autoincrement' => true]);
|
|
|
|
$table->addColumn('user', 'string', ['length' => 255]);
|
|
|
|
$table->setPrimaryKey(['id']);
|
2014-07-01 14:55:36 +04:00
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$endSchema = new Schema([], [], $this->getSchemaConfig());
|
2014-07-01 14:55:36 +04:00
|
|
|
$table = $endSchema->createTable($this->tableName);
|
2020-03-26 11:30:18 +03:00
|
|
|
$table->addColumn('id', 'integer', ['autoincrement' => true]);
|
|
|
|
$table->addColumn('user', 'string', ['length' => 64]);
|
|
|
|
$table->setPrimaryKey(['id']);
|
2014-07-01 14:55:36 +04:00
|
|
|
|
2021-03-24 22:27:39 +03:00
|
|
|
$migrator = $this->getMigrator();
|
2014-07-01 14:55:36 +04:00
|
|
|
$migrator->migrate($startSchema);
|
|
|
|
|
|
|
|
$migrator->migrate($endSchema);
|
|
|
|
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2014-07-01 14:55:36 +04:00
|
|
|
}
|
2017-10-02 13:32:21 +03:00
|
|
|
|
|
|
|
public function testAddingForeignKey() {
|
|
|
|
$startSchema = new Schema([], [], $this->getSchemaConfig());
|
|
|
|
$table = $startSchema->createTable($this->tableName);
|
|
|
|
$table->addColumn('id', 'integer', ['autoincrement' => true]);
|
|
|
|
$table->addColumn('name', 'string');
|
|
|
|
$table->setPrimaryKey(['id']);
|
|
|
|
|
|
|
|
$fkName = "fkc";
|
|
|
|
$tableFk = $startSchema->createTable($this->tableNameTmp);
|
|
|
|
$tableFk->addColumn('fk_id', 'integer');
|
|
|
|
$tableFk->addColumn('name', 'string');
|
2020-03-26 11:30:18 +03:00
|
|
|
$tableFk->addForeignKeyConstraint($this->tableName, ['fk_id'], ['id'], [], $fkName);
|
2017-10-02 13:32:21 +03:00
|
|
|
|
2021-03-24 22:27:39 +03:00
|
|
|
$migrator = $this->getMigrator();
|
2017-10-02 13:32:21 +03:00
|
|
|
$migrator->migrate($startSchema);
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($startSchema->getTable($this->tableNameTmp)->hasForeignKey($fkName));
|
|
|
|
}
|
2021-04-14 15:11:06 +03:00
|
|
|
|
2021-04-14 15:53:16 +03:00
|
|
|
public function dataNotNullEmptyValuesFailOracle(): array {
|
|
|
|
return [
|
|
|
|
[ParameterType::BOOLEAN, true, Types::BOOLEAN, false],
|
|
|
|
[ParameterType::BOOLEAN, false, Types::BOOLEAN, true],
|
2021-04-15 10:53:11 +03:00
|
|
|
|
2021-04-14 15:53:16 +03:00
|
|
|
[ParameterType::STRING, 'foo', Types::STRING, false],
|
|
|
|
[ParameterType::STRING, '', Types::STRING, true],
|
2021-04-15 10:53:11 +03:00
|
|
|
|
2021-04-14 15:53:16 +03:00
|
|
|
[ParameterType::INTEGER, 1234, Types::INTEGER, false],
|
2021-04-15 10:53:11 +03:00
|
|
|
[ParameterType::INTEGER, 0, Types::INTEGER, false], // Integer 0 is not stored as Null and therefor works
|
2021-04-14 15:53:16 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataNotNullEmptyValuesFailOracle
|
|
|
|
*
|
|
|
|
* @param int $parameterType
|
|
|
|
* @param bool|int|string $value
|
|
|
|
* @param string $columnType
|
|
|
|
* @param bool $oracleThrows
|
|
|
|
*/
|
|
|
|
public function testNotNullEmptyValuesFailOracle(int $parameterType, $value, string $columnType, bool $oracleThrows): void {
|
2021-04-14 15:11:06 +03:00
|
|
|
$startSchema = new Schema([], [], $this->getSchemaConfig());
|
|
|
|
$table = $startSchema->createTable($this->tableName);
|
|
|
|
$table->addColumn('id', Types::BIGINT);
|
2021-04-14 15:53:16 +03:00
|
|
|
$table->addColumn('will_it_blend', $columnType, [
|
2021-04-14 15:11:06 +03:00
|
|
|
'notnull' => true,
|
|
|
|
]);
|
|
|
|
$table->addIndex(['id'], $this->tableName . '_id');
|
|
|
|
|
|
|
|
$migrator = $this->getMigrator();
|
|
|
|
$migrator->migrate($startSchema);
|
|
|
|
|
2021-04-14 15:53:16 +03:00
|
|
|
if ($oracleThrows && $this->connection->getDatabasePlatform() instanceof OraclePlatform) {
|
|
|
|
// Oracle can not store false|empty string in notnull columns
|
|
|
|
$this->expectException(\Doctrine\DBAL\Exception\NotNullConstraintViolationException::class);
|
|
|
|
}
|
|
|
|
|
2021-04-14 15:11:06 +03:00
|
|
|
$this->connection->insert(
|
|
|
|
$this->tableName,
|
2021-04-14 15:53:16 +03:00
|
|
|
['id' => 1, 'will_it_blend' => $value],
|
|
|
|
['id' => ParameterType::INTEGER, 'will_it_blend' => $parameterType],
|
2021-04-14 15:11:06 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->addToAssertionCount(1);
|
|
|
|
}
|
2014-01-29 18:48:12 +04:00
|
|
|
}
|