MigratorTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. /** @var string */
  37. private $tableNameTmp;
  38. protected function setUp() {
  39. parent::setUp();
  40. $this->config = \OC::$server->getConfig();
  41. $this->connection = \OC::$server->getDatabaseConnection();
  42. if ($this->connection->getDatabasePlatform() instanceof OraclePlatform) {
  43. $this->markTestSkipped('DB migration tests are not supported on OCI');
  44. }
  45. $this->manager = new \OC\DB\MDB2SchemaManager($this->connection);
  46. $this->tableName = $this->getUniqueTableName();
  47. $this->tableNameTmp = $this->getUniqueTableName();
  48. }
  49. private function getUniqueTableName() {
  50. return strtolower($this->getUniqueID($this->config->getSystemValue('dbtableprefix', 'oc_') . 'test_'));
  51. }
  52. protected function tearDown() {
  53. // Try to delete if exists (IF EXISTS NOT SUPPORTED IN ORACLE)
  54. try {
  55. $this->connection->exec('DROP TABLE ' . $this->connection->quoteIdentifier($this->tableNameTmp));
  56. } catch (\Doctrine\DBAL\DBALException $e) {}
  57. try {
  58. $this->connection->exec('DROP TABLE ' . $this->connection->quoteIdentifier($this->tableName));
  59. } catch (\Doctrine\DBAL\DBALException $e) {}
  60. parent::tearDown();
  61. }
  62. /**
  63. * @return \Doctrine\DBAL\Schema\Schema[]
  64. */
  65. private function getDuplicateKeySchemas() {
  66. $startSchema = new Schema(array(), array(), $this->getSchemaConfig());
  67. $table = $startSchema->createTable($this->tableName);
  68. $table->addColumn('id', 'integer');
  69. $table->addColumn('name', 'string');
  70. $table->addIndex(array('id'), $this->tableName . '_id');
  71. $endSchema = new Schema(array(), array(), $this->getSchemaConfig());
  72. $table = $endSchema->createTable($this->tableName);
  73. $table->addColumn('id', 'integer');
  74. $table->addColumn('name', 'string');
  75. $table->addUniqueIndex(array('id'), $this->tableName . '_id');
  76. return array($startSchema, $endSchema);
  77. }
  78. private function getSchemaConfig() {
  79. $config = new SchemaConfig();
  80. $config->setName($this->connection->getDatabase());
  81. return $config;
  82. }
  83. private function isSQLite() {
  84. return $this->connection->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver;
  85. }
  86. /**
  87. * @expectedException \OC\DB\MigrationException
  88. */
  89. public function testDuplicateKeyUpgrade() {
  90. if ($this->isSQLite()) {
  91. $this->markTestSkipped('sqlite does not throw errors when creating a new key on existing data');
  92. }
  93. list($startSchema, $endSchema) = $this->getDuplicateKeySchemas();
  94. $migrator = $this->manager->getMigrator();
  95. $migrator->migrate($startSchema);
  96. $this->connection->insert($this->tableName, array('id' => 1, 'name' => 'foo'));
  97. $this->connection->insert($this->tableName, array('id' => 2, 'name' => 'bar'));
  98. $this->connection->insert($this->tableName, array('id' => 2, 'name' => 'qwerty'));
  99. $migrator->checkMigrate($endSchema);
  100. $this->fail('checkMigrate should have failed');
  101. }
  102. public function testUpgrade() {
  103. list($startSchema, $endSchema) = $this->getDuplicateKeySchemas();
  104. $migrator = $this->manager->getMigrator();
  105. $migrator->migrate($startSchema);
  106. $this->connection->insert($this->tableName, array('id' => 1, 'name' => 'foo'));
  107. $this->connection->insert($this->tableName, array('id' => 2, 'name' => 'bar'));
  108. $this->connection->insert($this->tableName, array('id' => 3, 'name' => 'qwerty'));
  109. $migrator->checkMigrate($endSchema);
  110. $migrator->migrate($endSchema);
  111. $this->addToAssertionCount(1);
  112. }
  113. public function testUpgradeDifferentPrefix() {
  114. $oldTablePrefix = $this->config->getSystemValue('dbtableprefix', 'oc_');
  115. $this->config->setSystemValue('dbtableprefix', 'ownc_');
  116. $this->tableName = strtolower($this->getUniqueID($this->config->getSystemValue('dbtableprefix') . 'test_'));
  117. list($startSchema, $endSchema) = $this->getDuplicateKeySchemas();
  118. $migrator = $this->manager->getMigrator();
  119. $migrator->migrate($startSchema);
  120. $this->connection->insert($this->tableName, array('id' => 1, 'name' => 'foo'));
  121. $this->connection->insert($this->tableName, array('id' => 2, 'name' => 'bar'));
  122. $this->connection->insert($this->tableName, array('id' => 3, 'name' => 'qwerty'));
  123. $migrator->checkMigrate($endSchema);
  124. $migrator->migrate($endSchema);
  125. $this->addToAssertionCount(1);
  126. $this->config->setSystemValue('dbtableprefix', $oldTablePrefix);
  127. }
  128. public function testInsertAfterUpgrade() {
  129. list($startSchema, $endSchema) = $this->getDuplicateKeySchemas();
  130. $migrator = $this->manager->getMigrator();
  131. $migrator->migrate($startSchema);
  132. $migrator->migrate($endSchema);
  133. $this->connection->insert($this->tableName, array('id' => 1, 'name' => 'foo'));
  134. $this->connection->insert($this->tableName, array('id' => 2, 'name' => 'bar'));
  135. try {
  136. $this->connection->insert($this->tableName, array('id' => 2, 'name' => 'qwerty'));
  137. $this->fail('Expected duplicate key insert to fail');
  138. } catch (DBALException $e) {
  139. $this->addToAssertionCount(1);
  140. }
  141. }
  142. public function testAddingPrimaryKeyWithAutoIncrement() {
  143. $startSchema = new Schema(array(), array(), $this->getSchemaConfig());
  144. $table = $startSchema->createTable($this->tableName);
  145. $table->addColumn('id', 'integer');
  146. $table->addColumn('name', 'string');
  147. $endSchema = new Schema(array(), array(), $this->getSchemaConfig());
  148. $table = $endSchema->createTable($this->tableName);
  149. $table->addColumn('id', 'integer', array('autoincrement' => true));
  150. $table->addColumn('name', 'string');
  151. $table->setPrimaryKey(array('id'));
  152. $migrator = $this->manager->getMigrator();
  153. $migrator->migrate($startSchema);
  154. $migrator->checkMigrate($endSchema);
  155. $migrator->migrate($endSchema);
  156. $this->addToAssertionCount(1);
  157. }
  158. public function testReservedKeywords() {
  159. $startSchema = new Schema(array(), array(), $this->getSchemaConfig());
  160. $table = $startSchema->createTable($this->tableName);
  161. $table->addColumn('id', 'integer', array('autoincrement' => true));
  162. $table->addColumn('user', 'string', array('length' => 255));
  163. $table->setPrimaryKey(array('id'));
  164. $endSchema = new Schema(array(), array(), $this->getSchemaConfig());
  165. $table = $endSchema->createTable($this->tableName);
  166. $table->addColumn('id', 'integer', array('autoincrement' => true));
  167. $table->addColumn('user', 'string', array('length' => 64));
  168. $table->setPrimaryKey(array('id'));
  169. $migrator = $this->manager->getMigrator();
  170. $migrator->migrate($startSchema);
  171. $migrator->checkMigrate($endSchema);
  172. $migrator->migrate($endSchema);
  173. $this->addToAssertionCount(1);
  174. }
  175. public function testAddingForeignKey() {
  176. $startSchema = new Schema([], [], $this->getSchemaConfig());
  177. $table = $startSchema->createTable($this->tableName);
  178. $table->addColumn('id', 'integer', ['autoincrement' => true]);
  179. $table->addColumn('name', 'string');
  180. $table->setPrimaryKey(['id']);
  181. $fkName = "fkc";
  182. $tableFk = $startSchema->createTable($this->tableNameTmp);
  183. $tableFk->addColumn('fk_id', 'integer');
  184. $tableFk->addColumn('name', 'string');
  185. $tableFk->addForeignKeyConstraint($this->tableName, array('fk_id'), array('id'), array(), $fkName);
  186. $migrator = $this->manager->getMigrator();
  187. $migrator->migrate($startSchema);
  188. $this->assertTrue($startSchema->getTable($this->tableNameTmp)->hasForeignKey($fkName));
  189. }
  190. }