2014-01-29 14:48:12 +00: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;
|
|
|
|
|
2014-03-27 13:44:19 +00:00
|
|
|
use \Doctrine\DBAL\DBALException;
|
2014-10-23 12:32:37 +00:00
|
|
|
use Doctrine\DBAL\Platforms\OraclePlatform;
|
2014-01-29 14:48:12 +00:00
|
|
|
use \Doctrine\DBAL\Schema\Schema;
|
|
|
|
use \Doctrine\DBAL\Schema\SchemaConfig;
|
2015-07-29 16:19:31 +00:00
|
|
|
use OCP\IConfig;
|
2014-01-29 14:48:12 +00:00
|
|
|
|
2014-11-10 22:30:38 +00:00
|
|
|
class Migrator extends \Test\TestCase {
|
2014-01-29 14:48:12 +00:00
|
|
|
/**
|
|
|
|
* @var \Doctrine\DBAL\Connection $connection
|
|
|
|
*/
|
|
|
|
private $connection;
|
|
|
|
|
2014-07-01 10:54:35 +00:00
|
|
|
/**
|
|
|
|
* @var \OC\DB\MDB2SchemaManager
|
|
|
|
*/
|
|
|
|
private $manager;
|
|
|
|
|
2015-05-18 14:57:59 +00:00
|
|
|
/**
|
|
|
|
* @var IConfig
|
|
|
|
**/
|
|
|
|
private $config;
|
|
|
|
|
2015-07-29 16:19:31 +00:00
|
|
|
/** @var string */
|
2014-01-29 14:48:12 +00:00
|
|
|
private $tableName;
|
|
|
|
|
2014-11-10 22:30:38 +00:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2015-05-18 14:57:59 +00:00
|
|
|
$this->config = \OC::$server->getConfig();
|
2014-04-10 11:54:50 +00:00
|
|
|
$this->connection = \OC_DB::getConnection();
|
2014-10-23 12:32:37 +00:00
|
|
|
if ($this->connection->getDatabasePlatform() instanceof OraclePlatform) {
|
|
|
|
$this->markTestSkipped('DB migration tests are not supported on OCI');
|
|
|
|
}
|
2014-07-01 10:54:35 +00:00
|
|
|
$this->manager = new \OC\DB\MDB2SchemaManager($this->connection);
|
2015-05-18 14:57:59 +00:00
|
|
|
$this->tableName = strtolower($this->getUniqueID($this->config->getSystemValue('dbtableprefix', 'oc_') . 'test_'));
|
2014-01-29 14:48:12 +00:00
|
|
|
}
|
|
|
|
|
2014-11-10 22:30:38 +00:00
|
|
|
protected function tearDown() {
|
2014-03-14 15:28:56 +00:00
|
|
|
$this->connection->exec('DROP TABLE ' . $this->tableName);
|
2014-11-10 22:30:38 +00:00
|
|
|
parent::tearDown();
|
2014-01-29 14:48:12 +00:00
|
|
|
}
|
|
|
|
|
2014-03-27 13:44:19 +00:00
|
|
|
/**
|
|
|
|
* @return \Doctrine\DBAL\Schema\Schema[]
|
|
|
|
*/
|
|
|
|
private function getDuplicateKeySchemas() {
|
|
|
|
$startSchema = new Schema(array(), array(), $this->getSchemaConfig());
|
|
|
|
$table = $startSchema->createTable($this->tableName);
|
2014-01-29 14:48:12 +00:00
|
|
|
$table->addColumn('id', 'integer');
|
|
|
|
$table->addColumn('name', 'string');
|
2014-01-30 13:21:16 +00:00
|
|
|
$table->addIndex(array('id'), $this->tableName . '_id');
|
2014-01-29 14:48:12 +00:00
|
|
|
|
2014-03-27 13:44:19 +00:00
|
|
|
$endSchema = new Schema(array(), array(), $this->getSchemaConfig());
|
|
|
|
$table = $endSchema->createTable($this->tableName);
|
2014-01-29 14:48:12 +00:00
|
|
|
$table->addColumn('id', 'integer');
|
|
|
|
$table->addColumn('name', 'string');
|
2014-01-30 13:21:16 +00:00
|
|
|
$table->addUniqueIndex(array('id'), $this->tableName . '_id');
|
2014-03-27 13:44:19 +00:00
|
|
|
|
|
|
|
return array($startSchema, $endSchema);
|
2014-01-29 14:48:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getSchemaConfig() {
|
|
|
|
$config = new SchemaConfig();
|
|
|
|
$config->setName($this->connection->getDatabase());
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
2014-03-27 13:44:19 +00:00
|
|
|
private function isSQLite() {
|
|
|
|
return $this->connection->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver;
|
|
|
|
}
|
|
|
|
|
2014-01-29 14:48:12 +00:00
|
|
|
/**
|
|
|
|
* @expectedException \OC\DB\MigrationException
|
|
|
|
*/
|
|
|
|
public function testDuplicateKeyUpgrade() {
|
2014-03-27 13:44:19 +00:00
|
|
|
if ($this->isSQLite()) {
|
2014-10-23 12:32:37 +00:00
|
|
|
$this->markTestSkipped('sqlite does not throw errors when creating a new key on existing data');
|
2014-03-27 13:44:19 +00:00
|
|
|
}
|
|
|
|
list($startSchema, $endSchema) = $this->getDuplicateKeySchemas();
|
2014-07-01 10:54:35 +00:00
|
|
|
$migrator = $this->manager->getMigrator();
|
2014-03-27 13:44:19 +00:00
|
|
|
$migrator->migrate($startSchema);
|
2014-01-29 14:48:12 +00:00
|
|
|
|
|
|
|
$this->connection->insert($this->tableName, array('id' => 1, 'name' => 'foo'));
|
|
|
|
$this->connection->insert($this->tableName, array('id' => 2, 'name' => 'bar'));
|
|
|
|
$this->connection->insert($this->tableName, array('id' => 2, 'name' => 'qwerty'));
|
|
|
|
|
2014-03-27 13:44:19 +00:00
|
|
|
$migrator->checkMigrate($endSchema);
|
2014-01-29 14:48:12 +00:00
|
|
|
$this->fail('checkMigrate should have failed');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpgrade() {
|
2014-03-27 13:44:19 +00:00
|
|
|
list($startSchema, $endSchema) = $this->getDuplicateKeySchemas();
|
2014-07-01 10:54:35 +00:00
|
|
|
$migrator = $this->manager->getMigrator();
|
2014-03-27 13:44:19 +00:00
|
|
|
$migrator->migrate($startSchema);
|
2014-01-29 14:48:12 +00:00
|
|
|
|
|
|
|
$this->connection->insert($this->tableName, array('id' => 1, 'name' => 'foo'));
|
|
|
|
$this->connection->insert($this->tableName, array('id' => 2, 'name' => 'bar'));
|
|
|
|
$this->connection->insert($this->tableName, array('id' => 3, 'name' => 'qwerty'));
|
|
|
|
|
2014-03-27 13:44:19 +00:00
|
|
|
$migrator->checkMigrate($endSchema);
|
|
|
|
$migrator->migrate($endSchema);
|
2014-01-29 14:48:12 +00:00
|
|
|
$this->assertTrue(true);
|
|
|
|
}
|
2014-03-27 13:44:19 +00:00
|
|
|
|
2015-05-18 14:57:59 +00: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_'));
|
|
|
|
|
|
|
|
list($startSchema, $endSchema) = $this->getDuplicateKeySchemas();
|
|
|
|
$migrator = $this->manager->getMigrator();
|
|
|
|
$migrator->migrate($startSchema);
|
|
|
|
|
|
|
|
$this->connection->insert($this->tableName, array('id' => 1, 'name' => 'foo'));
|
|
|
|
$this->connection->insert($this->tableName, array('id' => 2, 'name' => 'bar'));
|
|
|
|
$this->connection->insert($this->tableName, array('id' => 3, 'name' => 'qwerty'));
|
|
|
|
|
|
|
|
$migrator->checkMigrate($endSchema);
|
|
|
|
$migrator->migrate($endSchema);
|
|
|
|
$this->assertTrue(true);
|
|
|
|
|
|
|
|
$this->config->setSystemValue('dbtableprefix', $oldTablePrefix);
|
|
|
|
}
|
|
|
|
|
2014-03-27 13:44:19 +00:00
|
|
|
public function testInsertAfterUpgrade() {
|
|
|
|
list($startSchema, $endSchema) = $this->getDuplicateKeySchemas();
|
2014-07-01 10:54:35 +00:00
|
|
|
$migrator = $this->manager->getMigrator();
|
2014-03-27 13:44:19 +00:00
|
|
|
$migrator->migrate($startSchema);
|
|
|
|
|
|
|
|
$migrator->migrate($endSchema);
|
|
|
|
|
|
|
|
$this->connection->insert($this->tableName, array('id' => 1, 'name' => 'foo'));
|
|
|
|
$this->connection->insert($this->tableName, array('id' => 2, 'name' => 'bar'));
|
|
|
|
try {
|
|
|
|
$this->connection->insert($this->tableName, array('id' => 2, 'name' => 'qwerty'));
|
|
|
|
$this->fail('Expected duplicate key insert to fail');
|
|
|
|
} catch (DBALException $e) {
|
|
|
|
$this->assertTrue(true);
|
|
|
|
}
|
|
|
|
}
|
2014-06-17 13:07:36 +00:00
|
|
|
|
|
|
|
public function testAddingPrimaryKeyWithAutoIncrement() {
|
|
|
|
$startSchema = new Schema(array(), array(), $this->getSchemaConfig());
|
|
|
|
$table = $startSchema->createTable($this->tableName);
|
|
|
|
$table->addColumn('id', 'integer');
|
|
|
|
$table->addColumn('name', 'string');
|
|
|
|
|
|
|
|
$endSchema = new Schema(array(), array(), $this->getSchemaConfig());
|
|
|
|
$table = $endSchema->createTable($this->tableName);
|
|
|
|
$table->addColumn('id', 'integer', array('autoincrement' => true));
|
|
|
|
$table->addColumn('name', 'string');
|
|
|
|
$table->setPrimaryKey(array('id'));
|
|
|
|
|
2014-07-01 10:54:35 +00:00
|
|
|
$migrator = $this->manager->getMigrator();
|
2014-06-17 13:07:36 +00:00
|
|
|
$migrator->migrate($startSchema);
|
|
|
|
|
|
|
|
$migrator->checkMigrate($endSchema);
|
|
|
|
$migrator->migrate($endSchema);
|
|
|
|
|
|
|
|
$this->assertTrue(true);
|
|
|
|
}
|
2014-07-01 10:55:36 +00:00
|
|
|
|
|
|
|
public function testReservedKeywords() {
|
|
|
|
$startSchema = new Schema(array(), array(), $this->getSchemaConfig());
|
|
|
|
$table = $startSchema->createTable($this->tableName);
|
|
|
|
$table->addColumn('id', 'integer', array('autoincrement' => true));
|
|
|
|
$table->addColumn('user', 'string', array('length' => 255));
|
|
|
|
$table->setPrimaryKey(array('id'));
|
|
|
|
|
|
|
|
$endSchema = new Schema(array(), array(), $this->getSchemaConfig());
|
|
|
|
$table = $endSchema->createTable($this->tableName);
|
|
|
|
$table->addColumn('id', 'integer', array('autoincrement' => true));
|
|
|
|
$table->addColumn('user', 'string', array('length' => 64));
|
|
|
|
$table->setPrimaryKey(array('id'));
|
|
|
|
|
|
|
|
$migrator = $this->manager->getMigrator();
|
|
|
|
$migrator->migrate($startSchema);
|
|
|
|
|
|
|
|
$migrator->checkMigrate($endSchema);
|
|
|
|
$migrator->migrate($endSchema);
|
|
|
|
|
|
|
|
$this->assertTrue(true);
|
|
|
|
}
|
2014-01-29 14:48:12 +00:00
|
|
|
}
|