OracleMigrator.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Piotr Mrowczynski <mrow4a@yahoo.com>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\DB;
  30. use Doctrine\DBAL\Exception;
  31. use Doctrine\DBAL\Schema\Column;
  32. use Doctrine\DBAL\Schema\ColumnDiff;
  33. use Doctrine\DBAL\Schema\ForeignKeyConstraint;
  34. use Doctrine\DBAL\Schema\Index;
  35. use Doctrine\DBAL\Schema\Schema;
  36. use Doctrine\DBAL\Schema\Table;
  37. class OracleMigrator extends Migrator {
  38. /**
  39. * Quote a column's name but changing the name requires recreating
  40. * the column instance and copying over all properties.
  41. *
  42. * @param Column $column old column
  43. * @return Column new column instance with new name
  44. */
  45. protected function quoteColumn(Column $column) {
  46. $newColumn = new Column(
  47. $this->connection->quoteIdentifier($column->getName()),
  48. $column->getType()
  49. );
  50. $newColumn->setAutoincrement($column->getAutoincrement());
  51. $newColumn->setColumnDefinition($column->getColumnDefinition());
  52. $newColumn->setComment($column->getComment());
  53. $newColumn->setDefault($column->getDefault());
  54. $newColumn->setFixed($column->getFixed());
  55. $newColumn->setLength($column->getLength());
  56. $newColumn->setNotnull($column->getNotnull());
  57. $newColumn->setPrecision($column->getPrecision());
  58. $newColumn->setScale($column->getScale());
  59. $newColumn->setUnsigned($column->getUnsigned());
  60. $newColumn->setPlatformOptions($column->getPlatformOptions());
  61. $newColumn->setCustomSchemaOptions($column->getPlatformOptions());
  62. return $newColumn;
  63. }
  64. /**
  65. * Quote an index's name but changing the name requires recreating
  66. * the index instance and copying over all properties.
  67. *
  68. * @param Index $index old index
  69. * @return Index new index instance with new name
  70. */
  71. protected function quoteIndex($index) {
  72. return new Index(
  73. //TODO migrate existing uppercase indexes, then $this->connection->quoteIdentifier($index->getName()),
  74. $index->getName(),
  75. array_map(function ($columnName) {
  76. return $this->connection->quoteIdentifier($columnName);
  77. }, $index->getColumns()),
  78. $index->isUnique(),
  79. $index->isPrimary(),
  80. $index->getFlags(),
  81. $index->getOptions()
  82. );
  83. }
  84. /**
  85. * Quote an ForeignKeyConstraint's name but changing the name requires recreating
  86. * the ForeignKeyConstraint instance and copying over all properties.
  87. *
  88. * @param ForeignKeyConstraint $fkc old fkc
  89. * @return ForeignKeyConstraint new fkc instance with new name
  90. */
  91. protected function quoteForeignKeyConstraint($fkc) {
  92. return new ForeignKeyConstraint(
  93. array_map(function ($columnName) {
  94. return $this->connection->quoteIdentifier($columnName);
  95. }, $fkc->getLocalColumns()),
  96. $this->connection->quoteIdentifier($fkc->getForeignTableName()),
  97. array_map(function ($columnName) {
  98. return $this->connection->quoteIdentifier($columnName);
  99. }, $fkc->getForeignColumns()),
  100. $fkc->getName(),
  101. $fkc->getOptions()
  102. );
  103. }
  104. /**
  105. * @param Schema $targetSchema
  106. * @param \Doctrine\DBAL\Connection $connection
  107. * @return \Doctrine\DBAL\Schema\SchemaDiff
  108. * @throws Exception
  109. */
  110. protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) {
  111. $schemaDiff = parent::getDiff($targetSchema, $connection);
  112. // oracle forces us to quote the identifiers
  113. $schemaDiff->newTables = array_map(function (Table $table) {
  114. return new Table(
  115. $this->connection->quoteIdentifier($table->getName()),
  116. array_map(function (Column $column) {
  117. return $this->quoteColumn($column);
  118. }, $table->getColumns()),
  119. array_map(function (Index $index) {
  120. return $this->quoteIndex($index);
  121. }, $table->getIndexes()),
  122. [],
  123. array_map(function (ForeignKeyConstraint $fck) {
  124. return $this->quoteForeignKeyConstraint($fck);
  125. }, $table->getForeignKeys()),
  126. $table->getOptions()
  127. );
  128. }, $schemaDiff->newTables);
  129. $schemaDiff->removedTables = array_map(function (Table $table) {
  130. return new Table(
  131. $this->connection->quoteIdentifier($table->getName()),
  132. $table->getColumns(),
  133. $table->getIndexes(),
  134. [],
  135. $table->getForeignKeys(),
  136. $table->getOptions()
  137. );
  138. }, $schemaDiff->removedTables);
  139. foreach ($schemaDiff->changedTables as $tableDiff) {
  140. $tableDiff->name = $this->connection->quoteIdentifier($tableDiff->name);
  141. $tableDiff->addedColumns = array_map(function (Column $column) {
  142. return $this->quoteColumn($column);
  143. }, $tableDiff->addedColumns);
  144. foreach ($tableDiff->changedColumns as $column) {
  145. $column->oldColumnName = $this->connection->quoteIdentifier($column->oldColumnName);
  146. // auto increment is not relevant for oracle and can anyhow not be applied on change
  147. $column->changedProperties = array_diff($column->changedProperties, ['autoincrement', 'unsigned']);
  148. }
  149. // remove columns that no longer have changed (because autoincrement and unsigned are not supported)
  150. $tableDiff->changedColumns = array_filter($tableDiff->changedColumns, function (ColumnDiff $column) {
  151. return count($column->changedProperties) > 0;
  152. });
  153. $tableDiff->removedColumns = array_map(function (Column $column) {
  154. return $this->quoteColumn($column);
  155. }, $tableDiff->removedColumns);
  156. $tableDiff->renamedColumns = array_map(function (Column $column) {
  157. return $this->quoteColumn($column);
  158. }, $tableDiff->renamedColumns);
  159. $tableDiff->addedIndexes = array_map(function (Index $index) {
  160. return $this->quoteIndex($index);
  161. }, $tableDiff->addedIndexes);
  162. $tableDiff->changedIndexes = array_map(function (Index $index) {
  163. return $this->quoteIndex($index);
  164. }, $tableDiff->changedIndexes);
  165. $tableDiff->removedIndexes = array_map(function (Index $index) {
  166. return $this->quoteIndex($index);
  167. }, $tableDiff->removedIndexes);
  168. $tableDiff->renamedIndexes = array_map(function (Index $index) {
  169. return $this->quoteIndex($index);
  170. }, $tableDiff->renamedIndexes);
  171. $tableDiff->addedForeignKeys = array_map(function (ForeignKeyConstraint $fkc) {
  172. return $this->quoteForeignKeyConstraint($fkc);
  173. }, $tableDiff->addedForeignKeys);
  174. $tableDiff->changedForeignKeys = array_map(function (ForeignKeyConstraint $fkc) {
  175. return $this->quoteForeignKeyConstraint($fkc);
  176. }, $tableDiff->changedForeignKeys);
  177. $tableDiff->removedForeignKeys = array_map(function (ForeignKeyConstraint $fkc) {
  178. return $this->quoteForeignKeyConstraint($fkc);
  179. }, $tableDiff->removedForeignKeys);
  180. }
  181. return $schemaDiff;
  182. }
  183. /**
  184. * @param $statement
  185. * @return string
  186. */
  187. protected function convertStatementToScript($statement) {
  188. if (substr($statement, -1) === ';') {
  189. return $statement . PHP_EOL . '/' . PHP_EOL;
  190. }
  191. $script = $statement . ';';
  192. $script .= PHP_EOL;
  193. $script .= PHP_EOL;
  194. return $script;
  195. }
  196. protected function getFilterExpression() {
  197. return '/^"' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/';
  198. }
  199. }