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;
|
|
|
|
|
2014-03-27 17:44:19 +04:00
|
|
|
use \Doctrine\DBAL\DBALException;
|
2014-01-29 18:48:12 +04:00
|
|
|
use \Doctrine\DBAL\Schema\Schema;
|
|
|
|
use \Doctrine\DBAL\Schema\SchemaConfig;
|
|
|
|
|
|
|
|
class Migrator extends \PHPUnit_Framework_TestCase {
|
|
|
|
/**
|
|
|
|
* @var \Doctrine\DBAL\Connection $connection
|
|
|
|
*/
|
|
|
|
private $connection;
|
|
|
|
|
|
|
|
private $tableName;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
$this->tableName = 'test_' . uniqid();
|
|
|
|
$this->connection = \OC_DB::getConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
2014-03-14 19:28:56 +04:00
|
|
|
$this->connection->exec('DROP TABLE ' . $this->tableName);
|
2014-01-29 18:48:12 +04:00
|
|
|
}
|
|
|
|
|
2014-03-27 17:44:19 +04:00
|
|
|
/**
|
|
|
|
* @return \Doctrine\DBAL\Schema\Schema[]
|
|
|
|
*/
|
|
|
|
private function getDuplicateKeySchemas() {
|
|
|
|
$startSchema = new Schema(array(), array(), $this->getSchemaConfig());
|
|
|
|
$table = $startSchema->createTable($this->tableName);
|
2014-01-29 18:48:12 +04:00
|
|
|
$table->addColumn('id', 'integer');
|
|
|
|
$table->addColumn('name', 'string');
|
2014-01-30 17:21:16 +04:00
|
|
|
$table->addIndex(array('id'), $this->tableName . '_id');
|
2014-01-29 18:48:12 +04:00
|
|
|
|
2014-03-27 17:44:19 +04:00
|
|
|
$endSchema = new Schema(array(), array(), $this->getSchemaConfig());
|
|
|
|
$table = $endSchema->createTable($this->tableName);
|
2014-01-29 18:48:12 +04:00
|
|
|
$table->addColumn('id', 'integer');
|
|
|
|
$table->addColumn('name', 'string');
|
2014-01-30 17:21:16 +04:00
|
|
|
$table->addUniqueIndex(array('id'), $this->tableName . '_id');
|
2014-03-27 17:44:19 +04:00
|
|
|
|
|
|
|
return array($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() {
|
|
|
|
return $this->connection->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver;
|
|
|
|
}
|
|
|
|
|
2014-03-14 17:38:34 +04:00
|
|
|
private function getMigrator() {
|
2014-03-27 17:44:19 +04:00
|
|
|
if ($this->isSQLite()) {
|
|
|
|
return new \OC\DB\SQLiteMigrator($this->connection);
|
|
|
|
} else {
|
|
|
|
return new \OC\DB\Migrator($this->connection);
|
|
|
|
}
|
2014-03-14 17:38:34 +04:00
|
|
|
}
|
|
|
|
|
2014-01-29 18:48:12 +04:00
|
|
|
/**
|
|
|
|
* @expectedException \OC\DB\MigrationException
|
|
|
|
*/
|
|
|
|
public function testDuplicateKeyUpgrade() {
|
2014-03-27 17:44:19 +04:00
|
|
|
if ($this->isSQLite()) {
|
|
|
|
$this->markTestSkipped('sqlite doesnt throw errors when creating a new key on existing data');
|
|
|
|
}
|
|
|
|
list($startSchema, $endSchema) = $this->getDuplicateKeySchemas();
|
2014-03-14 17:38:34 +04:00
|
|
|
$migrator = $this->getMigrator();
|
2014-03-27 17:44:19 +04:00
|
|
|
$migrator->migrate($startSchema);
|
2014-01-29 18:48:12 +04: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 17:44:19 +04:00
|
|
|
$migrator->checkMigrate($endSchema);
|
2014-01-29 18:48:12 +04:00
|
|
|
$this->fail('checkMigrate should have failed');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpgrade() {
|
2014-03-27 17:44:19 +04:00
|
|
|
list($startSchema, $endSchema) = $this->getDuplicateKeySchemas();
|
2014-03-14 17:38:34 +04:00
|
|
|
$migrator = $this->getMigrator();
|
2014-03-27 17:44:19 +04:00
|
|
|
$migrator->migrate($startSchema);
|
2014-01-29 18:48:12 +04: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 17:44:19 +04:00
|
|
|
$migrator->checkMigrate($endSchema);
|
|
|
|
$migrator->migrate($endSchema);
|
2014-01-29 18:48:12 +04:00
|
|
|
$this->assertTrue(true);
|
|
|
|
}
|
2014-03-27 17:44:19 +04:00
|
|
|
|
|
|
|
public function testInsertAfterUpgrade() {
|
|
|
|
list($startSchema, $endSchema) = $this->getDuplicateKeySchemas();
|
|
|
|
$migrator = $this->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);
|
|
|
|
}
|
|
|
|
}
|
2014-01-29 18:48:12 +04:00
|
|
|
}
|