MigratorTest.php 10 KB

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