1
0

MigratorTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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\Exception;
  10. use Doctrine\DBAL\ParameterType;
  11. use Doctrine\DBAL\Platforms\OraclePlatform;
  12. use Doctrine\DBAL\Platforms\SqlitePlatform;
  13. use Doctrine\DBAL\Schema\Schema;
  14. use Doctrine\DBAL\Schema\SchemaConfig;
  15. use OC\DB\Migrator;
  16. use OC\DB\OracleMigrator;
  17. use OC\DB\SQLiteMigrator;
  18. use OCP\DB\Types;
  19. use OCP\IConfig;
  20. /**
  21. * Class MigratorTest
  22. *
  23. * @group DB
  24. *
  25. * @package Test\DB
  26. */
  27. class MigratorTest extends \Test\TestCase {
  28. /**
  29. * @var \Doctrine\DBAL\Connection $connection
  30. */
  31. private $connection;
  32. /**
  33. * @var IConfig
  34. **/
  35. private $config;
  36. /** @var string */
  37. private $tableName;
  38. /** @var string */
  39. private $tableNameTmp;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $this->config = \OC::$server->getConfig();
  43. $this->connection = \OC::$server->get(\OC\DB\Connection::class);
  44. $this->tableName = $this->getUniqueTableName();
  45. $this->tableNameTmp = $this->getUniqueTableName();
  46. }
  47. private function getMigrator(): Migrator {
  48. $platform = $this->connection->getDatabasePlatform();
  49. $random = \OC::$server->getSecureRandom();
  50. $dispatcher = \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class);
  51. if ($platform instanceof SqlitePlatform) {
  52. return new SQLiteMigrator($this->connection, $this->config, $dispatcher);
  53. } elseif ($platform instanceof OraclePlatform) {
  54. return new OracleMigrator($this->connection, $this->config, $dispatcher);
  55. }
  56. return new Migrator($this->connection, $this->config, $dispatcher);
  57. }
  58. private function getUniqueTableName() {
  59. return strtolower($this->getUniqueID($this->config->getSystemValueString('dbtableprefix', 'oc_') . 'test_'));
  60. }
  61. protected function tearDown(): void {
  62. // Try to delete if exists (IF EXISTS NOT SUPPORTED IN ORACLE)
  63. try {
  64. $this->connection->exec('DROP TABLE ' . $this->connection->quoteIdentifier($this->tableNameTmp));
  65. } catch (Exception $e) {
  66. }
  67. try {
  68. $this->connection->exec('DROP TABLE ' . $this->connection->quoteIdentifier($this->tableName));
  69. } catch (Exception $e) {
  70. }
  71. parent::tearDown();
  72. }
  73. /**
  74. * @return \Doctrine\DBAL\Schema\Schema[]
  75. */
  76. private function getDuplicateKeySchemas() {
  77. $startSchema = new Schema([], [], $this->getSchemaConfig());
  78. $table = $startSchema->createTable($this->tableName);
  79. $table->addColumn('id', 'integer');
  80. $table->addColumn('name', 'string');
  81. $table->addIndex(['id'], $this->tableName . '_id');
  82. $endSchema = new Schema([], [], $this->getSchemaConfig());
  83. $table = $endSchema->createTable($this->tableName);
  84. $table->addColumn('id', 'integer');
  85. $table->addColumn('name', 'string');
  86. $table->addUniqueIndex(['id'], $this->tableName . '_id');
  87. return [$startSchema, $endSchema];
  88. }
  89. /**
  90. * @return \Doctrine\DBAL\Schema\Schema[]
  91. */
  92. private function getChangedTypeSchema($from, $to) {
  93. $startSchema = new Schema([], [], $this->getSchemaConfig());
  94. $table = $startSchema->createTable($this->tableName);
  95. $table->addColumn('id', $from);
  96. $table->addColumn('name', 'string');
  97. $table->addIndex(['id'], $this->tableName . '_id');
  98. $endSchema = new Schema([], [], $this->getSchemaConfig());
  99. $table = $endSchema->createTable($this->tableName);
  100. $table->addColumn('id', $to);
  101. $table->addColumn('name', 'string');
  102. $table->addIndex(['id'], $this->tableName . '_id');
  103. return [$startSchema, $endSchema];
  104. }
  105. private function getSchemaConfig() {
  106. $config = new SchemaConfig();
  107. $config->setName($this->connection->getDatabase());
  108. return $config;
  109. }
  110. public function testUpgrade() {
  111. [$startSchema, $endSchema] = $this->getDuplicateKeySchemas();
  112. $migrator = $this->getMigrator();
  113. $migrator->migrate($startSchema);
  114. $this->connection->insert($this->tableName, ['id' => 1, 'name' => 'foo']);
  115. $this->connection->insert($this->tableName, ['id' => 2, 'name' => 'bar']);
  116. $this->connection->insert($this->tableName, ['id' => 3, 'name' => 'qwerty']);
  117. $migrator->migrate($endSchema);
  118. $this->addToAssertionCount(1);
  119. }
  120. public function testUpgradeDifferentPrefix() {
  121. $oldTablePrefix = $this->config->getSystemValueString('dbtableprefix', 'oc_');
  122. $this->config->setSystemValue('dbtableprefix', 'ownc_');
  123. $this->tableName = strtolower($this->getUniqueID($this->config->getSystemValueString('dbtableprefix') . 'test_'));
  124. [$startSchema, $endSchema] = $this->getDuplicateKeySchemas();
  125. $migrator = $this->getMigrator();
  126. $migrator->migrate($startSchema);
  127. $this->connection->insert($this->tableName, ['id' => 1, 'name' => 'foo']);
  128. $this->connection->insert($this->tableName, ['id' => 2, 'name' => 'bar']);
  129. $this->connection->insert($this->tableName, ['id' => 3, 'name' => 'qwerty']);
  130. $migrator->migrate($endSchema);
  131. $this->addToAssertionCount(1);
  132. $this->config->setSystemValue('dbtableprefix', $oldTablePrefix);
  133. }
  134. public function testInsertAfterUpgrade() {
  135. [$startSchema, $endSchema] = $this->getDuplicateKeySchemas();
  136. $migrator = $this->getMigrator();
  137. $migrator->migrate($startSchema);
  138. $migrator->migrate($endSchema);
  139. $this->connection->insert($this->tableName, ['id' => 1, 'name' => 'foo']);
  140. $this->connection->insert($this->tableName, ['id' => 2, 'name' => 'bar']);
  141. try {
  142. $this->connection->insert($this->tableName, ['id' => 2, 'name' => 'qwerty']);
  143. $this->fail('Expected duplicate key insert to fail');
  144. } catch (Exception $e) {
  145. $this->addToAssertionCount(1);
  146. }
  147. }
  148. public function testAddingPrimaryKeyWithAutoIncrement() {
  149. $startSchema = new Schema([], [], $this->getSchemaConfig());
  150. $table = $startSchema->createTable($this->tableName);
  151. $table->addColumn('id', 'integer');
  152. $table->addColumn('name', 'string');
  153. $endSchema = new Schema([], [], $this->getSchemaConfig());
  154. $table = $endSchema->createTable($this->tableName);
  155. $table->addColumn('id', 'integer', ['autoincrement' => true]);
  156. $table->addColumn('name', 'string');
  157. $table->setPrimaryKey(['id']);
  158. $migrator = $this->getMigrator();
  159. $migrator->migrate($startSchema);
  160. $migrator->migrate($endSchema);
  161. $this->addToAssertionCount(1);
  162. }
  163. public function testReservedKeywords() {
  164. $startSchema = new Schema([], [], $this->getSchemaConfig());
  165. $table = $startSchema->createTable($this->tableName);
  166. $table->addColumn('id', 'integer', ['autoincrement' => true]);
  167. $table->addColumn('user', 'string', ['length' => 255]);
  168. $table->setPrimaryKey(['id']);
  169. $endSchema = new Schema([], [], $this->getSchemaConfig());
  170. $table = $endSchema->createTable($this->tableName);
  171. $table->addColumn('id', 'integer', ['autoincrement' => true]);
  172. $table->addColumn('user', 'string', ['length' => 64]);
  173. $table->setPrimaryKey(['id']);
  174. $migrator = $this->getMigrator();
  175. $migrator->migrate($startSchema);
  176. $migrator->migrate($endSchema);
  177. $this->addToAssertionCount(1);
  178. }
  179. /**
  180. * Test for nextcloud/server#36803
  181. */
  182. public function testColumnCommentsInUpdate() {
  183. $startSchema = new Schema([], [], $this->getSchemaConfig());
  184. $table = $startSchema->createTable($this->tableName);
  185. $table->addColumn('id', 'integer', ['autoincrement' => true, 'comment' => 'foo']);
  186. $table->setPrimaryKey(['id']);
  187. $endSchema = new Schema([], [], $this->getSchemaConfig());
  188. $table = $endSchema->createTable($this->tableName);
  189. $table->addColumn('id', 'integer', ['autoincrement' => true, 'comment' => 'foo']);
  190. // Assert adding comments on existing tables work (or at least does not throw)
  191. $table->addColumn('time', 'integer', ['comment' => 'unix-timestamp', 'notnull' => false]);
  192. $table->setPrimaryKey(['id']);
  193. $migrator = $this->getMigrator();
  194. $migrator->migrate($startSchema);
  195. $migrator->migrate($endSchema);
  196. $this->addToAssertionCount(1);
  197. }
  198. public function testAddingForeignKey() {
  199. $startSchema = new Schema([], [], $this->getSchemaConfig());
  200. $table = $startSchema->createTable($this->tableName);
  201. $table->addColumn('id', 'integer', ['autoincrement' => true]);
  202. $table->addColumn('name', 'string');
  203. $table->setPrimaryKey(['id']);
  204. $fkName = "fkc";
  205. $tableFk = $startSchema->createTable($this->tableNameTmp);
  206. $tableFk->addColumn('fk_id', 'integer');
  207. $tableFk->addColumn('name', 'string');
  208. $tableFk->addForeignKeyConstraint($this->tableName, ['fk_id'], ['id'], [], $fkName);
  209. $migrator = $this->getMigrator();
  210. $migrator->migrate($startSchema);
  211. $this->assertTrue($startSchema->getTable($this->tableNameTmp)->hasForeignKey($fkName));
  212. }
  213. public function dataNotNullEmptyValuesFailOracle(): array {
  214. return [
  215. [ParameterType::BOOLEAN, true, Types::BOOLEAN, false],
  216. [ParameterType::BOOLEAN, false, Types::BOOLEAN, true],
  217. [ParameterType::STRING, 'foo', Types::STRING, false],
  218. [ParameterType::STRING, '', Types::STRING, true],
  219. [ParameterType::INTEGER, 1234, Types::INTEGER, false],
  220. [ParameterType::INTEGER, 0, Types::INTEGER, false], // Integer 0 is not stored as Null and therefor works
  221. [ParameterType::STRING, '{"a": 2}', Types::JSON, false],
  222. ];
  223. }
  224. /**
  225. * @dataProvider dataNotNullEmptyValuesFailOracle
  226. *
  227. * @param int $parameterType
  228. * @param bool|int|string $value
  229. * @param string $columnType
  230. * @param bool $oracleThrows
  231. */
  232. public function testNotNullEmptyValuesFailOracle(int $parameterType, $value, string $columnType, bool $oracleThrows): void {
  233. $startSchema = new Schema([], [], $this->getSchemaConfig());
  234. $table = $startSchema->createTable($this->tableName);
  235. $table->addColumn('id', Types::BIGINT);
  236. $table->addColumn('will_it_blend', $columnType, [
  237. 'notnull' => true,
  238. ]);
  239. $table->addIndex(['id'], $this->tableName . '_id');
  240. $migrator = $this->getMigrator();
  241. $migrator->migrate($startSchema);
  242. if ($oracleThrows && $this->connection->getDatabasePlatform() instanceof OraclePlatform) {
  243. // Oracle can not store false|empty string in notnull columns
  244. $this->expectException(\Doctrine\DBAL\Exception\NotNullConstraintViolationException::class);
  245. }
  246. $this->connection->insert(
  247. $this->tableName,
  248. ['id' => 1, 'will_it_blend' => $value],
  249. ['id' => ParameterType::INTEGER, 'will_it_blend' => $parameterType],
  250. );
  251. $this->addToAssertionCount(1);
  252. }
  253. }