SchemaDiffTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @author Thomas Müller <thomas.mueller@tmit.eu>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\DB;
  22. use Doctrine\DBAL\Schema\Schema;
  23. use Doctrine\DBAL\Schema\SchemaDiff;
  24. use OC\DB\MDB2SchemaManager;
  25. use OC\DB\MDB2SchemaReader;
  26. use OCP\IConfig;
  27. use Test\TestCase;
  28. /**
  29. * Class MigratorTest
  30. *
  31. * @group DB
  32. *
  33. * @package Test\DB
  34. */
  35. class SchemaDiffTest extends TestCase {
  36. /** @var \Doctrine\DBAL\Connection $connection */
  37. private $connection;
  38. /** @var MDB2SchemaManager */
  39. private $manager;
  40. /** @var IConfig */
  41. private $config;
  42. /** @var string */
  43. private $testPrefix;
  44. private $schemaFile;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->schemaFile = \OC::$server->getTempManager()->getTemporaryFile();
  48. $this->config = \OC::$server->getConfig();
  49. $this->connection = \OC::$server->getDatabaseConnection();
  50. $this->manager = new MDB2SchemaManager($this->connection);
  51. $this->testPrefix = strtolower($this->getUniqueID($this->config->getSystemValue('dbtableprefix', 'oc_'), 3));
  52. }
  53. protected function tearDown(): void {
  54. $this->manager->removeDBStructure($this->schemaFile);
  55. parent::tearDown();
  56. }
  57. /**
  58. * @dataProvider providesSchemaFiles
  59. * @param string $xml
  60. */
  61. public function testZeroChangeOnSchemaMigrations($xml) {
  62. $xml = str_replace('*dbprefix*', $this->testPrefix, $xml);
  63. $schemaFile = $this->schemaFile;
  64. file_put_contents($schemaFile, $xml);
  65. // apply schema
  66. $this->manager->createDbFromStructure($schemaFile);
  67. $schemaReader = new MDB2SchemaReader($this->config, $this->connection->getDatabasePlatform());
  68. $toSchema = new Schema([], [], $this->connection->getSchemaManager()->createSchemaConfig());
  69. $endSchema = $schemaReader->loadSchemaFromFile($schemaFile, $toSchema);
  70. // get the diff
  71. /** @var SchemaDiff $diff */
  72. $migrator = $this->manager->getMigrator();
  73. $diff = $this->invokePrivate($migrator, 'getDiff', [$endSchema, $this->connection]);
  74. // no sql statement is expected
  75. $sqls = $diff->toSql($this->connection->getDatabasePlatform());
  76. $this->assertEquals([], $sqls);
  77. }
  78. public function providesSchemaFiles() {
  79. return [
  80. 'explicit test on autoincrement' => [file_get_contents(__DIR__ . '/schemDiffData/autoincrement.xml')],
  81. 'explicit test on clob' => [file_get_contents(__DIR__ . '/schemDiffData/clob.xml')],
  82. 'explicit test on unsigned' => [file_get_contents(__DIR__ . '/schemDiffData/unsigned.xml')],
  83. 'explicit test on default -1' => [file_get_contents(__DIR__ . '/schemDiffData/default-1.xml')],
  84. 'testing core schema' => [file_get_contents(__DIR__ . '/schemDiffData/core.xml')],
  85. ];
  86. }
  87. }