1
0

MigratorTest.php 9.9 KB

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