MigratorTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\DB;
  9. use \Doctrine\DBAL\DBALException;
  10. use Doctrine\DBAL\Platforms\OraclePlatform;
  11. use \Doctrine\DBAL\Schema\Schema;
  12. use \Doctrine\DBAL\Schema\SchemaConfig;
  13. use OCP\IConfig;
  14. /**
  15. * Class MigratorTest
  16. *
  17. * @group DB
  18. *
  19. * @package Test\DB
  20. */
  21. class MigratorTest extends \Test\TestCase {
  22. /**
  23. * @var \Doctrine\DBAL\Connection $connection
  24. */
  25. private $connection;
  26. /**
  27. * @var \OC\DB\MDB2SchemaManager
  28. */
  29. private $manager;
  30. /**
  31. * @var IConfig
  32. **/
  33. private $config;
  34. /** @var string */
  35. private $tableName;
  36. protected function setUp() {
  37. parent::setUp();
  38. $this->config = \OC::$server->getConfig();
  39. $this->connection = \OC::$server->getDatabaseConnection();
  40. if ($this->connection->getDatabasePlatform() instanceof OraclePlatform) {
  41. $this->markTestSkipped('DB migration tests are not supported on OCI');
  42. }
  43. $this->manager = new \OC\DB\MDB2SchemaManager($this->connection);
  44. $this->tableName = strtolower($this->getUniqueID($this->config->getSystemValue('dbtableprefix', 'oc_') . 'test_'));
  45. }
  46. protected function tearDown() {
  47. $this->connection->exec('DROP TABLE ' . $this->tableName);
  48. parent::tearDown();
  49. }
  50. /**
  51. * @return \Doctrine\DBAL\Schema\Schema[]
  52. */
  53. private function getDuplicateKeySchemas() {
  54. $startSchema = new Schema(array(), array(), $this->getSchemaConfig());
  55. $table = $startSchema->createTable($this->tableName);
  56. $table->addColumn('id', 'integer');
  57. $table->addColumn('name', 'string');
  58. $table->addIndex(array('id'), $this->tableName . '_id');
  59. $endSchema = new Schema(array(), array(), $this->getSchemaConfig());
  60. $table = $endSchema->createTable($this->tableName);
  61. $table->addColumn('id', 'integer');
  62. $table->addColumn('name', 'string');
  63. $table->addUniqueIndex(array('id'), $this->tableName . '_id');
  64. return array($startSchema, $endSchema);
  65. }
  66. private function getSchemaConfig() {
  67. $config = new SchemaConfig();
  68. $config->setName($this->connection->getDatabase());
  69. return $config;
  70. }
  71. private function isSQLite() {
  72. return $this->connection->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver;
  73. }
  74. /**
  75. * @expectedException \OC\DB\MigrationException
  76. */
  77. public function testDuplicateKeyUpgrade() {
  78. if ($this->isSQLite()) {
  79. $this->markTestSkipped('sqlite does not throw errors when creating a new key on existing data');
  80. }
  81. list($startSchema, $endSchema) = $this->getDuplicateKeySchemas();
  82. $migrator = $this->manager->getMigrator();
  83. $migrator->migrate($startSchema);
  84. $this->connection->insert($this->tableName, array('id' => 1, 'name' => 'foo'));
  85. $this->connection->insert($this->tableName, array('id' => 2, 'name' => 'bar'));
  86. $this->connection->insert($this->tableName, array('id' => 2, 'name' => 'qwerty'));
  87. $migrator->checkMigrate($endSchema);
  88. $this->fail('checkMigrate should have failed');
  89. }
  90. public function testUpgrade() {
  91. list($startSchema, $endSchema) = $this->getDuplicateKeySchemas();
  92. $migrator = $this->manager->getMigrator();
  93. $migrator->migrate($startSchema);
  94. $this->connection->insert($this->tableName, array('id' => 1, 'name' => 'foo'));
  95. $this->connection->insert($this->tableName, array('id' => 2, 'name' => 'bar'));
  96. $this->connection->insert($this->tableName, array('id' => 3, 'name' => 'qwerty'));
  97. $migrator->checkMigrate($endSchema);
  98. $migrator->migrate($endSchema);
  99. $this->assertTrue(true);
  100. }
  101. public function testUpgradeDifferentPrefix() {
  102. $oldTablePrefix = $this->config->getSystemValue('dbtableprefix', 'oc_');
  103. $this->config->setSystemValue('dbtableprefix', 'ownc_');
  104. $this->tableName = strtolower($this->getUniqueID($this->config->getSystemValue('dbtableprefix') . 'test_'));
  105. list($startSchema, $endSchema) = $this->getDuplicateKeySchemas();
  106. $migrator = $this->manager->getMigrator();
  107. $migrator->migrate($startSchema);
  108. $this->connection->insert($this->tableName, array('id' => 1, 'name' => 'foo'));
  109. $this->connection->insert($this->tableName, array('id' => 2, 'name' => 'bar'));
  110. $this->connection->insert($this->tableName, array('id' => 3, 'name' => 'qwerty'));
  111. $migrator->checkMigrate($endSchema);
  112. $migrator->migrate($endSchema);
  113. $this->assertTrue(true);
  114. $this->config->setSystemValue('dbtableprefix', $oldTablePrefix);
  115. }
  116. public function testInsertAfterUpgrade() {
  117. list($startSchema, $endSchema) = $this->getDuplicateKeySchemas();
  118. $migrator = $this->manager->getMigrator();
  119. $migrator->migrate($startSchema);
  120. $migrator->migrate($endSchema);
  121. $this->connection->insert($this->tableName, array('id' => 1, 'name' => 'foo'));
  122. $this->connection->insert($this->tableName, array('id' => 2, 'name' => 'bar'));
  123. try {
  124. $this->connection->insert($this->tableName, array('id' => 2, 'name' => 'qwerty'));
  125. $this->fail('Expected duplicate key insert to fail');
  126. } catch (DBALException $e) {
  127. $this->assertTrue(true);
  128. }
  129. }
  130. public function testAddingPrimaryKeyWithAutoIncrement() {
  131. $startSchema = new Schema(array(), array(), $this->getSchemaConfig());
  132. $table = $startSchema->createTable($this->tableName);
  133. $table->addColumn('id', 'integer');
  134. $table->addColumn('name', 'string');
  135. $endSchema = new Schema(array(), array(), $this->getSchemaConfig());
  136. $table = $endSchema->createTable($this->tableName);
  137. $table->addColumn('id', 'integer', array('autoincrement' => true));
  138. $table->addColumn('name', 'string');
  139. $table->setPrimaryKey(array('id'));
  140. $migrator = $this->manager->getMigrator();
  141. $migrator->migrate($startSchema);
  142. $migrator->checkMigrate($endSchema);
  143. $migrator->migrate($endSchema);
  144. $this->assertTrue(true);
  145. }
  146. public function testReservedKeywords() {
  147. $startSchema = new Schema(array(), array(), $this->getSchemaConfig());
  148. $table = $startSchema->createTable($this->tableName);
  149. $table->addColumn('id', 'integer', array('autoincrement' => true));
  150. $table->addColumn('user', 'string', array('length' => 255));
  151. $table->setPrimaryKey(array('id'));
  152. $endSchema = new Schema(array(), array(), $this->getSchemaConfig());
  153. $table = $endSchema->createTable($this->tableName);
  154. $table->addColumn('id', 'integer', array('autoincrement' => true));
  155. $table->addColumn('user', 'string', array('length' => 64));
  156. $table->setPrimaryKey(array('id'));
  157. $migrator = $this->manager->getMigrator();
  158. $migrator->migrate($startSchema);
  159. $migrator->checkMigrate($endSchema);
  160. $migrator->migrate($endSchema);
  161. $this->assertTrue(true);
  162. }
  163. }