SchemaDiffTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\SchemaDiff;
  23. use OC\DB\MDB2SchemaManager;
  24. use OC\DB\MDB2SchemaReader;
  25. use OCP\IConfig;
  26. use Test\TestCase;
  27. /**
  28. * Class MigratorTest
  29. *
  30. * @group DB
  31. *
  32. * @package Test\DB
  33. */
  34. class SchemaDiffTest extends TestCase {
  35. /** @var \Doctrine\DBAL\Connection $connection */
  36. private $connection;
  37. /** @var MDB2SchemaManager */
  38. private $manager;
  39. /** @var IConfig */
  40. private $config;
  41. /** @var string */
  42. private $testPrefix;
  43. protected function setUp() {
  44. parent::setUp();
  45. $this->config = \OC::$server->getConfig();
  46. $this->connection = \OC::$server->getDatabaseConnection();
  47. $this->manager = new MDB2SchemaManager($this->connection);
  48. $this->testPrefix= strtolower($this->getUniqueID($this->config->getSystemValue('dbtableprefix', 'oc_'), 3));
  49. }
  50. protected function tearDown() {
  51. $this->manager->removeDBStructure('static://test_db_scheme');
  52. parent::tearDown();
  53. }
  54. /**
  55. * @dataProvider providesSchemaFiles
  56. * @param string $xml
  57. */
  58. public function testZeroChangeOnSchemaMigrations($xml) {
  59. $xml = str_replace( '*dbprefix*', $this->testPrefix, $xml );
  60. $schemaFile = 'static://test_db_scheme';
  61. file_put_contents($schemaFile, $xml);
  62. // apply schema
  63. $this->manager->createDbFromStructure($schemaFile);
  64. $schemaReader = new MDB2SchemaReader($this->config, $this->connection->getDatabasePlatform());
  65. $endSchema = $schemaReader->loadSchemaFromFile($schemaFile);
  66. // get the diff
  67. /** @var SchemaDiff $diff */
  68. $migrator = $this->manager->getMigrator();
  69. $diff = $this->invokePrivate($migrator, 'getDiff', [$endSchema, $this->connection]);
  70. // no sql statement is expected
  71. $sqls = $diff->toSql($this->connection->getDatabasePlatform());
  72. $this->assertEquals([], $sqls);
  73. }
  74. public function providesSchemaFiles() {
  75. return [
  76. 'explicit test on autoincrement' => [file_get_contents(__DIR__ . '/schemDiffData/autoincrement.xml')],
  77. 'explicit test on clob' => [file_get_contents(__DIR__ . '/schemDiffData/clob.xml')],
  78. 'explicit test on unsigned' => [file_get_contents(__DIR__ . '/schemDiffData/unsigned.xml')],
  79. 'explicit test on default -1' => [file_get_contents(__DIR__ . '/schemDiffData/default-1.xml')],
  80. 'testing core schema' => [file_get_contents(__DIR__ . '/schemDiffData/core.xml')],
  81. ];
  82. }
  83. }