1
0

OracleMigrator.php 7.7 KB

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