OracleMigrator.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC\DB;
  9. use Doctrine\DBAL\Exception;
  10. use Doctrine\DBAL\Schema\Schema;
  11. class OracleMigrator extends Migrator {
  12. /**
  13. * @param Schema $targetSchema
  14. * @param \Doctrine\DBAL\Connection $connection
  15. * @return \Doctrine\DBAL\Schema\SchemaDiff
  16. * @throws Exception
  17. */
  18. protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection): \Doctrine\DBAL\Schema\SchemaDiff {
  19. // oracle forces us to quote the identifiers
  20. $quotedSchema = new Schema();
  21. foreach ($targetSchema->getTables() as $table) {
  22. $quotedTable = $quotedSchema->createTable(
  23. $this->connection->quoteIdentifier($table->getName()),
  24. );
  25. foreach ($table->getColumns() as $column) {
  26. $newColumn = $quotedTable->addColumn(
  27. $this->connection->quoteIdentifier($column->getName()),
  28. $column->getType()->getTypeRegistry()->lookupName($column->getType()),
  29. );
  30. $newColumn->setAutoincrement($column->getAutoincrement());
  31. $newColumn->setColumnDefinition($column->getColumnDefinition());
  32. $newColumn->setComment($column->getComment());
  33. $newColumn->setDefault($column->getDefault());
  34. $newColumn->setFixed($column->getFixed());
  35. $newColumn->setLength($column->getLength());
  36. $newColumn->setNotnull($column->getNotnull());
  37. $newColumn->setPrecision($column->getPrecision());
  38. $newColumn->setScale($column->getScale());
  39. $newColumn->setUnsigned($column->getUnsigned());
  40. $newColumn->setPlatformOptions($column->getPlatformOptions());
  41. }
  42. foreach ($table->getIndexes() as $index) {
  43. if ($index->isPrimary()) {
  44. $quotedTable->setPrimaryKey(
  45. array_map(function ($columnName) {
  46. return $this->connection->quoteIdentifier($columnName);
  47. }, $index->getColumns()),
  48. //TODO migrate existing uppercase indexes, then $this->connection->quoteIdentifier($index->getName()),
  49. $index->getName(),
  50. );
  51. } elseif ($index->isUnique()) {
  52. $quotedTable->addUniqueIndex(
  53. array_map(function ($columnName) {
  54. return $this->connection->quoteIdentifier($columnName);
  55. }, $index->getColumns()),
  56. //TODO migrate existing uppercase indexes, then $this->connection->quoteIdentifier($index->getName()),
  57. $index->getName(),
  58. $index->getOptions(),
  59. );
  60. } else {
  61. $quotedTable->addIndex(
  62. array_map(function ($columnName) {
  63. return $this->connection->quoteIdentifier($columnName);
  64. }, $index->getColumns()),
  65. //TODO migrate existing uppercase indexes, then $this->connection->quoteIdentifier($index->getName()),
  66. $index->getName(),
  67. $index->getFlags(),
  68. $index->getOptions(),
  69. );
  70. }
  71. }
  72. foreach ($table->getUniqueConstraints() as $constraint) {
  73. $quotedTable->addUniqueConstraint(
  74. array_map(function ($columnName) {
  75. return $this->connection->quoteIdentifier($columnName);
  76. }, $constraint->getColumns()),
  77. $this->connection->quoteIdentifier($constraint->getName()),
  78. $constraint->getFlags(),
  79. $constraint->getOptions(),
  80. );
  81. }
  82. foreach ($table->getForeignKeys() as $foreignKey) {
  83. $quotedTable->addForeignKeyConstraint(
  84. $this->connection->quoteIdentifier($foreignKey->getForeignTableName()),
  85. array_map(function ($columnName) {
  86. return $this->connection->quoteIdentifier($columnName);
  87. }, $foreignKey->getLocalColumns()),
  88. array_map(function ($columnName) {
  89. return $this->connection->quoteIdentifier($columnName);
  90. }, $foreignKey->getForeignColumns()),
  91. $foreignKey->getOptions(),
  92. $this->connection->quoteIdentifier($foreignKey->getName()),
  93. );
  94. }
  95. foreach ($table->getOptions() as $option => $value) {
  96. $quotedTable->addOption(
  97. $option,
  98. $value,
  99. );
  100. }
  101. }
  102. foreach ($targetSchema->getSequences() as $sequence) {
  103. $quotedSchema->createSequence(
  104. $sequence->getName(),
  105. $sequence->getAllocationSize(),
  106. $sequence->getInitialValue(),
  107. );
  108. }
  109. return parent::getDiff($quotedSchema, $connection);
  110. }
  111. /**
  112. * @param $statement
  113. * @return string
  114. */
  115. protected function convertStatementToScript($statement) {
  116. if (str_ends_with($statement, ';')) {
  117. return $statement . PHP_EOL . '/' . PHP_EOL;
  118. }
  119. $script = $statement . ';';
  120. $script .= PHP_EOL;
  121. $script .= PHP_EOL;
  122. return $script;
  123. }
  124. protected function getFilterExpression() {
  125. return '/^"' . preg_quote($this->config->getSystemValueString('dbtableprefix', 'oc_')) . '/';
  126. }
  127. }