OracleMigrator.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023, Joas Schilling <coding@schilljs.com>
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. *
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Piotr Mrowczynski <mrow4a@yahoo.com>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OC\DB;
  32. use Doctrine\DBAL\Exception;
  33. use Doctrine\DBAL\Schema\Schema;
  34. class OracleMigrator extends Migrator {
  35. /**
  36. * @param Schema $targetSchema
  37. * @param \Doctrine\DBAL\Connection $connection
  38. * @return \Doctrine\DBAL\Schema\SchemaDiff
  39. * @throws Exception
  40. */
  41. protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection): \Doctrine\DBAL\Schema\SchemaDiff {
  42. // oracle forces us to quote the identifiers
  43. $quotedSchema = new Schema();
  44. foreach ($targetSchema->getTables() as $table) {
  45. $quotedTable = $quotedSchema->createTable(
  46. $this->connection->quoteIdentifier($table->getName()),
  47. );
  48. foreach ($table->getColumns() as $column) {
  49. $newColumn = $quotedTable->addColumn(
  50. $this->connection->quoteIdentifier($column->getName()),
  51. $column->getType()->getTypeRegistry()->lookupName($column->getType()),
  52. );
  53. $newColumn->setAutoincrement($column->getAutoincrement());
  54. $newColumn->setColumnDefinition($column->getColumnDefinition());
  55. $newColumn->setComment($column->getComment());
  56. $newColumn->setDefault($column->getDefault());
  57. $newColumn->setFixed($column->getFixed());
  58. $newColumn->setLength($column->getLength());
  59. $newColumn->setNotnull($column->getNotnull());
  60. $newColumn->setPrecision($column->getPrecision());
  61. $newColumn->setScale($column->getScale());
  62. $newColumn->setUnsigned($column->getUnsigned());
  63. $newColumn->setPlatformOptions($column->getPlatformOptions());
  64. }
  65. foreach ($table->getIndexes() as $index) {
  66. if ($index->isPrimary()) {
  67. $quotedTable->setPrimaryKey(
  68. array_map(function ($columnName) {
  69. return $this->connection->quoteIdentifier($columnName);
  70. }, $index->getColumns()),
  71. //TODO migrate existing uppercase indexes, then $this->connection->quoteIdentifier($index->getName()),
  72. $index->getName(),
  73. );
  74. } elseif ($index->isUnique()) {
  75. $quotedTable->addUniqueIndex(
  76. array_map(function ($columnName) {
  77. return $this->connection->quoteIdentifier($columnName);
  78. }, $index->getColumns()),
  79. //TODO migrate existing uppercase indexes, then $this->connection->quoteIdentifier($index->getName()),
  80. $index->getName(),
  81. $index->getOptions(),
  82. );
  83. } else {
  84. $quotedTable->addIndex(
  85. array_map(function ($columnName) {
  86. return $this->connection->quoteIdentifier($columnName);
  87. }, $index->getColumns()),
  88. //TODO migrate existing uppercase indexes, then $this->connection->quoteIdentifier($index->getName()),
  89. $index->getName(),
  90. $index->getFlags(),
  91. $index->getOptions(),
  92. );
  93. }
  94. }
  95. foreach ($table->getUniqueConstraints() as $constraint) {
  96. $quotedTable->addUniqueConstraint(
  97. array_map(function ($columnName) {
  98. return $this->connection->quoteIdentifier($columnName);
  99. }, $constraint->getColumns()),
  100. $this->connection->quoteIdentifier($constraint->getName()),
  101. $constraint->getFlags(),
  102. $constraint->getOptions(),
  103. );
  104. }
  105. foreach ($table->getForeignKeys() as $foreignKey) {
  106. $quotedTable->addForeignKeyConstraint(
  107. $this->connection->quoteIdentifier($foreignKey->getForeignTableName()),
  108. array_map(function ($columnName) {
  109. return $this->connection->quoteIdentifier($columnName);
  110. }, $foreignKey->getLocalColumns()),
  111. array_map(function ($columnName) {
  112. return $this->connection->quoteIdentifier($columnName);
  113. }, $foreignKey->getForeignColumns()),
  114. $foreignKey->getOptions(),
  115. $this->connection->quoteIdentifier($foreignKey->getName()),
  116. );
  117. }
  118. foreach ($table->getOptions() as $option => $value) {
  119. $quotedTable->addOption(
  120. $option,
  121. $value,
  122. );
  123. }
  124. }
  125. foreach ($targetSchema->getSequences() as $sequence) {
  126. $quotedSchema->createSequence(
  127. $sequence->getName(),
  128. $sequence->getAllocationSize(),
  129. $sequence->getInitialValue(),
  130. );
  131. }
  132. return parent::getDiff($quotedSchema, $connection);
  133. }
  134. /**
  135. * @param $statement
  136. * @return string
  137. */
  138. protected function convertStatementToScript($statement) {
  139. if (str_ends_with($statement, ';')) {
  140. return $statement . PHP_EOL . '/' . PHP_EOL;
  141. }
  142. $script = $statement . ';';
  143. $script .= PHP_EOL;
  144. $script .= PHP_EOL;
  145. return $script;
  146. }
  147. protected function getFilterExpression() {
  148. return '/^"' . preg_quote($this->config->getSystemValueString('dbtableprefix', 'oc_')) . '/';
  149. }
  150. }