nextcloud/tests/lib/db/migrator.php

172 lines
5.5 KiB
PHP
Raw Normal View History

<?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;
use \Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use \Doctrine\DBAL\Schema\Schema;
use \Doctrine\DBAL\Schema\SchemaConfig;
class Migrator extends \Test\TestCase {
/**
* @var \Doctrine\DBAL\Connection $connection
*/
private $connection;
/**
* @var \OC\DB\MDB2SchemaManager
*/
private $manager;
private $tableName;
protected function setUp() {
parent::setUp();
2014-04-10 15:54:50 +04:00
$this->connection = \OC_DB::getConnection();
if ($this->connection->getDatabasePlatform() instanceof OraclePlatform) {
$this->markTestSkipped('DB migration tests are not supported on OCI');
}
if ($this->connection->getDatabasePlatform() instanceof SQLServerPlatform) {
$this->markTestSkipped('DB migration tests are not supported on MSSQL');
2014-04-09 15:00:32 +04:00
}
$this->manager = new \OC\DB\MDB2SchemaManager($this->connection);
$this->tableName = strtolower($this->getUniqueID('test_'));
}
protected function tearDown() {
2014-03-14 19:28:56 +04:00
$this->connection->exec('DROP TABLE ' . $this->tableName);
parent::tearDown();
}
/**
* @return \Doctrine\DBAL\Schema\Schema[]
*/
private function getDuplicateKeySchemas() {
$startSchema = new Schema(array(), array(), $this->getSchemaConfig());
$table = $startSchema->createTable($this->tableName);
$table->addColumn('id', 'integer');
$table->addColumn('name', 'string');
$table->addIndex(array('id'), $this->tableName . '_id');
$endSchema = new Schema(array(), array(), $this->getSchemaConfig());
$table = $endSchema->createTable($this->tableName);
$table->addColumn('id', 'integer');
$table->addColumn('name', 'string');
$table->addUniqueIndex(array('id'), $this->tableName . '_id');
return array($startSchema, $endSchema);
}
private function getSchemaConfig() {
$config = new SchemaConfig();
$config->setName($this->connection->getDatabase());
return $config;
}
private function isSQLite() {
return $this->connection->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver;
}
/**
* @expectedException \OC\DB\MigrationException
*/
public function testDuplicateKeyUpgrade() {
if ($this->isSQLite()) {
$this->markTestSkipped('sqlite does not throw errors when creating a new key on existing data');
}
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' => 2, 'name' => 'qwerty'));
$migrator->checkMigrate($endSchema);
$this->fail('checkMigrate should have failed');
}
public function testUpgrade() {
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);
}
public function testInsertAfterUpgrade() {
list($startSchema, $endSchema) = $this->getDuplicateKeySchemas();
$migrator = $this->manager->getMigrator();
$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);
}
}
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'));
$migrator = $this->manager->getMigrator();
$migrator->migrate($startSchema);
$migrator->checkMigrate($endSchema);
$migrator->migrate($endSchema);
$this->assertTrue(true);
}
2014-07-01 14:55:36 +04: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);
}
}