You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OracleMigrator.php 7.8KB

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