summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2014-01-29 15:48:12 +0100
committerRobin Appelman <icewind@owncloud.com>2014-06-03 11:17:21 +0200
commit9c6a93a87c835dee5fb0b580865d4f70836685cf (patch)
treeccfde22f21bb3be23f2f667f944eb0db1f28d7bc /tests
parentb62cb007715e649d9656c32bfe4b42599fd96983 (diff)
downloadnextcloud-server-9c6a93a87c835dee5fb0b580865d4f70836685cf.tar.gz
nextcloud-server-9c6a93a87c835dee5fb0b580865d4f70836685cf.zip
Add a mechanism to try the database migration on a copy of the tables before running it on the "real" data
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/db/migrator.php84
1 files changed, 84 insertions, 0 deletions
diff --git a/tests/lib/db/migrator.php b/tests/lib/db/migrator.php
new file mode 100644
index 00000000000..ed7c36dbdf4
--- /dev/null
+++ b/tests/lib/db/migrator.php
@@ -0,0 +1,84 @@
+<?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\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();
+ $this->tableName = $this->connection->getDatabase() . '.' . $this->tableName;
+ }
+
+ public function tearDown() {
+ $this->connection->exec('DROP TABLE ' . $this->tableName);
+ }
+
+ private function getInitialSchema() {
+ $schema = new Schema(array(), array(), $this->getSchemaConfig());
+ $table = $schema->createTable($this->tableName);
+ $table->addColumn('id', 'integer');
+ $table->addColumn('name', 'string');
+ return $schema;
+ }
+
+ private function getNewSchema() {
+ $schema = new Schema(array(), array(), $this->getSchemaConfig());
+ $table = $schema->createTable($this->tableName);
+ $table->addColumn('id', 'integer');
+ $table->addColumn('name', 'string');
+ $table->addUniqueIndex(array('id'));
+ return $schema;
+ }
+
+ private function getSchemaConfig() {
+ $config = new SchemaConfig();
+ $config->setName($this->connection->getDatabase());
+ return $config;
+ }
+
+ /**
+ * @expectedException \OC\DB\MigrationException
+ */
+ public function testDuplicateKeyUpgrade() {
+ $migrator = new \OC\DB\Migrator($this->connection);
+ $migrator->migrate($this->getInitialSchema());
+
+ $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($this->getNewSchema());
+ $this->fail('checkMigrate should have failed');
+ }
+
+ public function testUpgrade() {
+ $migrator = new \OC\DB\Migrator($this->connection);
+ $migrator->migrate($this->getInitialSchema());
+
+ $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'));
+
+ $newSchema = $this->getNewSchema();
+ $migrator->checkMigrate($newSchema);
+ $migrator->migrate($newSchema);
+ $this->assertTrue(true);
+ }
+}