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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
<?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\Schema\Schema;
use \Doctrine\DBAL\Schema\SchemaConfig;
use OCP\IConfig;
/**
* Class Migrator
*
* @group DB
*
* @package Test\DB
*/
class Migrator extends \Test\TestCase {
/**
* @var \Doctrine\DBAL\Connection $connection
*/
private $connection;
/**
* @var \OC\DB\MDB2SchemaManager
*/
private $manager;
/**
* @var IConfig
**/
private $config;
/** @var string */
private $tableName;
protected function setUp() {
parent::setUp();
$this->config = \OC::$server->getConfig();
$this->connection = \OC::$server->getDatabaseConnection();
if ($this->connection->getDatabasePlatform() instanceof OraclePlatform) {
$this->markTestSkipped('DB migration tests are not supported on OCI');
}
$this->manager = new \OC\DB\MDB2SchemaManager($this->connection);
$this->tableName = strtolower($this->getUniqueID($this->config->getSystemValue('dbtableprefix', 'oc_') . 'test_'));
}
protected function tearDown() {
$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 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);
}
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);
}
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);
}
}
|