summaryrefslogtreecommitdiffstats
path: root/tests/lib/db/migrator.php
blob: 258d6451847cf529d31dd2f33c8b9a4a6fda6027 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?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;

	private $fullTableName;

	public function setUp() {
		$this->tableName = 'test_' . uniqid();
		$this->connection = \OC_DB::getConnection();
		if ($this->connection->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver) {
			$this->markTestSkipped('Migration tests dont function on sqlite since sqlite doesnt give an error on existing duplicate data');
		} else {
			$this->fullTableName = $this->connection->getDatabase() . '.' . $this->tableName;
		}
	}

	public function tearDown() {
		$this->connection->exec('DROP TABLE ' . $this->fullTableName);
	}

	private function getInitialSchema() {
		$schema = new Schema(array(), array(), $this->getSchemaConfig());
		$table = $schema->createTable($this->fullTableName);
		$table->addColumn('id', 'integer');
		$table->addColumn('name', 'string');
		$table->addIndex(array('id'), $this->tableName . '_id');
		return $schema;
	}

	private function getNewSchema() {
		$schema = new Schema(array(), array(), $this->getSchemaConfig());
		$table = $schema->createTable($this->fullTableName);
		$table->addColumn('id', 'integer');
		$table->addColumn('name', 'string');
		$table->addUniqueIndex(array('id'), $this->tableName . '_id');
		return $schema;
	}

	private function getSchemaConfig() {
		$config = new SchemaConfig();
		$config->setName($this->connection->getDatabase());
		return $config;
	}

	private function getMigrator() {
		return new \OC\DB\Migrator($this->connection);
	}

	/**
	 * @expectedException \OC\DB\MigrationException
	 */
	public function testDuplicateKeyUpgrade() {
		$migrator = $this->getMigrator();
		$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 = $this->getMigrator();
		$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);
	}
}