@@ -34,194 +34,194 @@ |
||
| 34 | 34 | |
| 35 | 35 | class OracleMigrator extends Migrator { |
| 36 | 36 | |
| 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 | - /** |
|
| 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 | - /** |
|
| 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 | - /** |
|
| 107 | - * @param Schema $targetSchema |
|
| 108 | - * @param \Doctrine\DBAL\Connection $connection |
|
| 109 | - * @return \Doctrine\DBAL\Schema\SchemaDiff |
|
| 110 | - * @throws DBALException |
|
| 111 | - */ |
|
| 112 | - protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { |
|
| 113 | - $schemaDiff = parent::getDiff($targetSchema, $connection); |
|
| 114 | - |
|
| 115 | - // oracle forces us to quote the identifiers |
|
| 116 | - $schemaDiff->newTables = array_map(function(Table $table) { |
|
| 117 | - return new Table( |
|
| 118 | - $this->connection->quoteIdentifier($table->getName()), |
|
| 119 | - array_map(function(Column $column) { |
|
| 120 | - return $this->quoteColumn($column); |
|
| 121 | - }, $table->getColumns()), |
|
| 122 | - array_map(function(Index $index) { |
|
| 123 | - return $this->quoteIndex($index); |
|
| 124 | - }, $table->getIndexes()), |
|
| 125 | - array_map(function(ForeignKeyConstraint $fck) { |
|
| 126 | - return $this->quoteForeignKeyConstraint($fck); |
|
| 127 | - }, $table->getForeignKeys()), |
|
| 128 | - 0, |
|
| 129 | - $table->getOptions() |
|
| 130 | - ); |
|
| 131 | - }, $schemaDiff->newTables); |
|
| 132 | - |
|
| 133 | - $schemaDiff->removedTables = array_map(function(Table $table) { |
|
| 134 | - return new Table( |
|
| 135 | - $this->connection->quoteIdentifier($table->getName()), |
|
| 136 | - $table->getColumns(), |
|
| 137 | - $table->getIndexes(), |
|
| 138 | - $table->getForeignKeys(), |
|
| 139 | - 0, |
|
| 140 | - $table->getOptions() |
|
| 141 | - ); |
|
| 142 | - }, $schemaDiff->removedTables); |
|
| 143 | - |
|
| 144 | - foreach ($schemaDiff->changedTables as $tableDiff) { |
|
| 145 | - $tableDiff->name = $this->connection->quoteIdentifier($tableDiff->name); |
|
| 146 | - |
|
| 147 | - $tableDiff->addedColumns = array_map(function(Column $column) { |
|
| 148 | - return $this->quoteColumn($column); |
|
| 149 | - }, $tableDiff->addedColumns); |
|
| 150 | - |
|
| 151 | - foreach ($tableDiff->changedColumns as $column) { |
|
| 152 | - $column->oldColumnName = $this->connection->quoteIdentifier($column->oldColumnName); |
|
| 153 | - // auto increment is not relevant for oracle and can anyhow not be applied on change |
|
| 154 | - $column->changedProperties = array_diff($column->changedProperties, ['autoincrement', 'unsigned']); |
|
| 155 | - } |
|
| 156 | - // remove columns that no longer have changed (because autoincrement and unsigned are not supported) |
|
| 157 | - $tableDiff->changedColumns = array_filter($tableDiff->changedColumns, function (ColumnDiff $column) { |
|
| 158 | - return count($column->changedProperties) > 0; |
|
| 159 | - }); |
|
| 160 | - |
|
| 161 | - $tableDiff->removedColumns = array_map(function(Column $column) { |
|
| 162 | - return $this->quoteColumn($column); |
|
| 163 | - }, $tableDiff->removedColumns); |
|
| 164 | - |
|
| 165 | - $tableDiff->renamedColumns = array_map(function(Column $column) { |
|
| 166 | - return $this->quoteColumn($column); |
|
| 167 | - }, $tableDiff->renamedColumns); |
|
| 168 | - |
|
| 169 | - $tableDiff->addedIndexes = array_map(function(Index $index) { |
|
| 170 | - return $this->quoteIndex($index); |
|
| 171 | - }, $tableDiff->addedIndexes); |
|
| 172 | - |
|
| 173 | - $tableDiff->changedIndexes = array_map(function(Index $index) { |
|
| 174 | - return $this->quoteIndex($index); |
|
| 175 | - }, $tableDiff->changedIndexes); |
|
| 176 | - |
|
| 177 | - $tableDiff->removedIndexes = array_map(function(Index $index) { |
|
| 178 | - return $this->quoteIndex($index); |
|
| 179 | - }, $tableDiff->removedIndexes); |
|
| 180 | - |
|
| 181 | - $tableDiff->renamedIndexes = array_map(function(Index $index) { |
|
| 182 | - return $this->quoteIndex($index); |
|
| 183 | - }, $tableDiff->renamedIndexes); |
|
| 184 | - |
|
| 185 | - $tableDiff->addedForeignKeys = array_map(function(ForeignKeyConstraint $fkc) { |
|
| 186 | - return $this->quoteForeignKeyConstraint($fkc); |
|
| 187 | - }, $tableDiff->addedForeignKeys); |
|
| 188 | - |
|
| 189 | - $tableDiff->changedForeignKeys = array_map(function(ForeignKeyConstraint $fkc) { |
|
| 190 | - return $this->quoteForeignKeyConstraint($fkc); |
|
| 191 | - }, $tableDiff->changedForeignKeys); |
|
| 192 | - |
|
| 193 | - $tableDiff->removedForeignKeys = array_map(function(ForeignKeyConstraint $fkc) { |
|
| 194 | - return $this->quoteForeignKeyConstraint($fkc); |
|
| 195 | - }, $tableDiff->removedForeignKeys); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - return $schemaDiff; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * @param string $name |
|
| 203 | - * @return string |
|
| 204 | - */ |
|
| 205 | - protected function generateTemporaryTableName($name) { |
|
| 206 | - return 'oc_' . uniqid(); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * @param $statement |
|
| 211 | - * @return string |
|
| 212 | - */ |
|
| 213 | - protected function convertStatementToScript($statement) { |
|
| 214 | - if (substr($statement, -1) === ';') { |
|
| 215 | - return $statement . PHP_EOL . '/' . PHP_EOL; |
|
| 216 | - } |
|
| 217 | - $script = $statement . ';'; |
|
| 218 | - $script .= PHP_EOL; |
|
| 219 | - $script .= PHP_EOL; |
|
| 220 | - return $script; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - protected function getFilterExpression() { |
|
| 224 | - return '/^"' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
| 225 | - } |
|
| 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 | + /** |
|
| 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 | + /** |
|
| 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 | + /** |
|
| 107 | + * @param Schema $targetSchema |
|
| 108 | + * @param \Doctrine\DBAL\Connection $connection |
|
| 109 | + * @return \Doctrine\DBAL\Schema\SchemaDiff |
|
| 110 | + * @throws DBALException |
|
| 111 | + */ |
|
| 112 | + protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { |
|
| 113 | + $schemaDiff = parent::getDiff($targetSchema, $connection); |
|
| 114 | + |
|
| 115 | + // oracle forces us to quote the identifiers |
|
| 116 | + $schemaDiff->newTables = array_map(function(Table $table) { |
|
| 117 | + return new Table( |
|
| 118 | + $this->connection->quoteIdentifier($table->getName()), |
|
| 119 | + array_map(function(Column $column) { |
|
| 120 | + return $this->quoteColumn($column); |
|
| 121 | + }, $table->getColumns()), |
|
| 122 | + array_map(function(Index $index) { |
|
| 123 | + return $this->quoteIndex($index); |
|
| 124 | + }, $table->getIndexes()), |
|
| 125 | + array_map(function(ForeignKeyConstraint $fck) { |
|
| 126 | + return $this->quoteForeignKeyConstraint($fck); |
|
| 127 | + }, $table->getForeignKeys()), |
|
| 128 | + 0, |
|
| 129 | + $table->getOptions() |
|
| 130 | + ); |
|
| 131 | + }, $schemaDiff->newTables); |
|
| 132 | + |
|
| 133 | + $schemaDiff->removedTables = array_map(function(Table $table) { |
|
| 134 | + return new Table( |
|
| 135 | + $this->connection->quoteIdentifier($table->getName()), |
|
| 136 | + $table->getColumns(), |
|
| 137 | + $table->getIndexes(), |
|
| 138 | + $table->getForeignKeys(), |
|
| 139 | + 0, |
|
| 140 | + $table->getOptions() |
|
| 141 | + ); |
|
| 142 | + }, $schemaDiff->removedTables); |
|
| 143 | + |
|
| 144 | + foreach ($schemaDiff->changedTables as $tableDiff) { |
|
| 145 | + $tableDiff->name = $this->connection->quoteIdentifier($tableDiff->name); |
|
| 146 | + |
|
| 147 | + $tableDiff->addedColumns = array_map(function(Column $column) { |
|
| 148 | + return $this->quoteColumn($column); |
|
| 149 | + }, $tableDiff->addedColumns); |
|
| 150 | + |
|
| 151 | + foreach ($tableDiff->changedColumns as $column) { |
|
| 152 | + $column->oldColumnName = $this->connection->quoteIdentifier($column->oldColumnName); |
|
| 153 | + // auto increment is not relevant for oracle and can anyhow not be applied on change |
|
| 154 | + $column->changedProperties = array_diff($column->changedProperties, ['autoincrement', 'unsigned']); |
|
| 155 | + } |
|
| 156 | + // remove columns that no longer have changed (because autoincrement and unsigned are not supported) |
|
| 157 | + $tableDiff->changedColumns = array_filter($tableDiff->changedColumns, function (ColumnDiff $column) { |
|
| 158 | + return count($column->changedProperties) > 0; |
|
| 159 | + }); |
|
| 160 | + |
|
| 161 | + $tableDiff->removedColumns = array_map(function(Column $column) { |
|
| 162 | + return $this->quoteColumn($column); |
|
| 163 | + }, $tableDiff->removedColumns); |
|
| 164 | + |
|
| 165 | + $tableDiff->renamedColumns = array_map(function(Column $column) { |
|
| 166 | + return $this->quoteColumn($column); |
|
| 167 | + }, $tableDiff->renamedColumns); |
|
| 168 | + |
|
| 169 | + $tableDiff->addedIndexes = array_map(function(Index $index) { |
|
| 170 | + return $this->quoteIndex($index); |
|
| 171 | + }, $tableDiff->addedIndexes); |
|
| 172 | + |
|
| 173 | + $tableDiff->changedIndexes = array_map(function(Index $index) { |
|
| 174 | + return $this->quoteIndex($index); |
|
| 175 | + }, $tableDiff->changedIndexes); |
|
| 176 | + |
|
| 177 | + $tableDiff->removedIndexes = array_map(function(Index $index) { |
|
| 178 | + return $this->quoteIndex($index); |
|
| 179 | + }, $tableDiff->removedIndexes); |
|
| 180 | + |
|
| 181 | + $tableDiff->renamedIndexes = array_map(function(Index $index) { |
|
| 182 | + return $this->quoteIndex($index); |
|
| 183 | + }, $tableDiff->renamedIndexes); |
|
| 184 | + |
|
| 185 | + $tableDiff->addedForeignKeys = array_map(function(ForeignKeyConstraint $fkc) { |
|
| 186 | + return $this->quoteForeignKeyConstraint($fkc); |
|
| 187 | + }, $tableDiff->addedForeignKeys); |
|
| 188 | + |
|
| 189 | + $tableDiff->changedForeignKeys = array_map(function(ForeignKeyConstraint $fkc) { |
|
| 190 | + return $this->quoteForeignKeyConstraint($fkc); |
|
| 191 | + }, $tableDiff->changedForeignKeys); |
|
| 192 | + |
|
| 193 | + $tableDiff->removedForeignKeys = array_map(function(ForeignKeyConstraint $fkc) { |
|
| 194 | + return $this->quoteForeignKeyConstraint($fkc); |
|
| 195 | + }, $tableDiff->removedForeignKeys); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + return $schemaDiff; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * @param string $name |
|
| 203 | + * @return string |
|
| 204 | + */ |
|
| 205 | + protected function generateTemporaryTableName($name) { |
|
| 206 | + return 'oc_' . uniqid(); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * @param $statement |
|
| 211 | + * @return string |
|
| 212 | + */ |
|
| 213 | + protected function convertStatementToScript($statement) { |
|
| 214 | + if (substr($statement, -1) === ';') { |
|
| 215 | + return $statement . PHP_EOL . '/' . PHP_EOL; |
|
| 216 | + } |
|
| 217 | + $script = $statement . ';'; |
|
| 218 | + $script .= PHP_EOL; |
|
| 219 | + $script .= PHP_EOL; |
|
| 220 | + return $script; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + protected function getFilterExpression() { |
|
| 224 | + return '/^"' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
| 225 | + } |
|
| 226 | 226 | |
| 227 | 227 | } |
@@ -154,7 +154,7 @@ discard block |
||
| 154 | 154 | $column->changedProperties = array_diff($column->changedProperties, ['autoincrement', 'unsigned']); |
| 155 | 155 | } |
| 156 | 156 | // remove columns that no longer have changed (because autoincrement and unsigned are not supported) |
| 157 | - $tableDiff->changedColumns = array_filter($tableDiff->changedColumns, function (ColumnDiff $column) { |
|
| 157 | + $tableDiff->changedColumns = array_filter($tableDiff->changedColumns, function(ColumnDiff $column) { |
|
| 158 | 158 | return count($column->changedProperties) > 0; |
| 159 | 159 | }); |
| 160 | 160 | |
@@ -203,7 +203,7 @@ discard block |
||
| 203 | 203 | * @return string |
| 204 | 204 | */ |
| 205 | 205 | protected function generateTemporaryTableName($name) { |
| 206 | - return 'oc_' . uniqid(); |
|
| 206 | + return 'oc_'.uniqid(); |
|
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | /** |
@@ -212,16 +212,16 @@ discard block |
||
| 212 | 212 | */ |
| 213 | 213 | protected function convertStatementToScript($statement) { |
| 214 | 214 | if (substr($statement, -1) === ';') { |
| 215 | - return $statement . PHP_EOL . '/' . PHP_EOL; |
|
| 215 | + return $statement.PHP_EOL.'/'.PHP_EOL; |
|
| 216 | 216 | } |
| 217 | - $script = $statement . ';'; |
|
| 217 | + $script = $statement.';'; |
|
| 218 | 218 | $script .= PHP_EOL; |
| 219 | 219 | $script .= PHP_EOL; |
| 220 | 220 | return $script; |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | protected function getFilterExpression() { |
| 224 | - return '/^"' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
| 224 | + return '/^"'.preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')).'/'; |
|
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | } |
@@ -37,136 +37,136 @@ |
||
| 37 | 37 | use OCP\IDBConnection; |
| 38 | 38 | |
| 39 | 39 | class MDB2SchemaManager { |
| 40 | - /** @var \OC\DB\Connection $conn */ |
|
| 41 | - protected $conn; |
|
| 40 | + /** @var \OC\DB\Connection $conn */ |
|
| 41 | + protected $conn; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @param IDBConnection $conn |
|
| 45 | - */ |
|
| 46 | - public function __construct($conn) { |
|
| 47 | - $this->conn = $conn; |
|
| 48 | - } |
|
| 43 | + /** |
|
| 44 | + * @param IDBConnection $conn |
|
| 45 | + */ |
|
| 46 | + public function __construct($conn) { |
|
| 47 | + $this->conn = $conn; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * saves database scheme to xml file |
|
| 52 | - * @param string $file name of file |
|
| 53 | - * @return bool |
|
| 54 | - * |
|
| 55 | - * TODO: write more documentation |
|
| 56 | - */ |
|
| 57 | - public function getDbStructure($file) { |
|
| 58 | - return \OC\DB\MDB2SchemaWriter::saveSchemaToFile($file, $this->conn); |
|
| 59 | - } |
|
| 50 | + /** |
|
| 51 | + * saves database scheme to xml file |
|
| 52 | + * @param string $file name of file |
|
| 53 | + * @return bool |
|
| 54 | + * |
|
| 55 | + * TODO: write more documentation |
|
| 56 | + */ |
|
| 57 | + public function getDbStructure($file) { |
|
| 58 | + return \OC\DB\MDB2SchemaWriter::saveSchemaToFile($file, $this->conn); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Creates tables from XML file |
|
| 63 | - * @param string $file file to read structure from |
|
| 64 | - * @return bool |
|
| 65 | - * |
|
| 66 | - * TODO: write more documentation |
|
| 67 | - */ |
|
| 68 | - public function createDbFromStructure($file) { |
|
| 69 | - $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $this->conn->getDatabasePlatform()); |
|
| 70 | - $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); |
|
| 71 | - $toSchema = $schemaReader->loadSchemaFromFile($file, $toSchema); |
|
| 72 | - return $this->executeSchemaChange($toSchema); |
|
| 73 | - } |
|
| 61 | + /** |
|
| 62 | + * Creates tables from XML file |
|
| 63 | + * @param string $file file to read structure from |
|
| 64 | + * @return bool |
|
| 65 | + * |
|
| 66 | + * TODO: write more documentation |
|
| 67 | + */ |
|
| 68 | + public function createDbFromStructure($file) { |
|
| 69 | + $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $this->conn->getDatabasePlatform()); |
|
| 70 | + $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); |
|
| 71 | + $toSchema = $schemaReader->loadSchemaFromFile($file, $toSchema); |
|
| 72 | + return $this->executeSchemaChange($toSchema); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * @return \OC\DB\Migrator |
|
| 77 | - */ |
|
| 78 | - public function getMigrator() { |
|
| 79 | - $random = \OC::$server->getSecureRandom(); |
|
| 80 | - $platform = $this->conn->getDatabasePlatform(); |
|
| 81 | - $config = \OC::$server->getConfig(); |
|
| 82 | - $dispatcher = \OC::$server->getEventDispatcher(); |
|
| 83 | - if ($platform instanceof SqlitePlatform) { |
|
| 84 | - return new SQLiteMigrator($this->conn, $random, $config, $dispatcher); |
|
| 85 | - } else if ($platform instanceof OraclePlatform) { |
|
| 86 | - return new OracleMigrator($this->conn, $random, $config, $dispatcher); |
|
| 87 | - } else if ($platform instanceof MySqlPlatform) { |
|
| 88 | - return new MySQLMigrator($this->conn, $random, $config, $dispatcher); |
|
| 89 | - } else if ($platform instanceof PostgreSqlPlatform) { |
|
| 90 | - return new PostgreSqlMigrator($this->conn, $random, $config, $dispatcher); |
|
| 91 | - } else { |
|
| 92 | - return new Migrator($this->conn, $random, $config, $dispatcher); |
|
| 93 | - } |
|
| 94 | - } |
|
| 75 | + /** |
|
| 76 | + * @return \OC\DB\Migrator |
|
| 77 | + */ |
|
| 78 | + public function getMigrator() { |
|
| 79 | + $random = \OC::$server->getSecureRandom(); |
|
| 80 | + $platform = $this->conn->getDatabasePlatform(); |
|
| 81 | + $config = \OC::$server->getConfig(); |
|
| 82 | + $dispatcher = \OC::$server->getEventDispatcher(); |
|
| 83 | + if ($platform instanceof SqlitePlatform) { |
|
| 84 | + return new SQLiteMigrator($this->conn, $random, $config, $dispatcher); |
|
| 85 | + } else if ($platform instanceof OraclePlatform) { |
|
| 86 | + return new OracleMigrator($this->conn, $random, $config, $dispatcher); |
|
| 87 | + } else if ($platform instanceof MySqlPlatform) { |
|
| 88 | + return new MySQLMigrator($this->conn, $random, $config, $dispatcher); |
|
| 89 | + } else if ($platform instanceof PostgreSqlPlatform) { |
|
| 90 | + return new PostgreSqlMigrator($this->conn, $random, $config, $dispatcher); |
|
| 91 | + } else { |
|
| 92 | + return new Migrator($this->conn, $random, $config, $dispatcher); |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * Reads database schema from file |
|
| 98 | - * |
|
| 99 | - * @param string $file file to read from |
|
| 100 | - * @return \Doctrine\DBAL\Schema\Schema |
|
| 101 | - */ |
|
| 102 | - private function readSchemaFromFile($file) { |
|
| 103 | - $platform = $this->conn->getDatabasePlatform(); |
|
| 104 | - $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $platform); |
|
| 105 | - $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); |
|
| 106 | - return $schemaReader->loadSchemaFromFile($file, $toSchema); |
|
| 107 | - } |
|
| 96 | + /** |
|
| 97 | + * Reads database schema from file |
|
| 98 | + * |
|
| 99 | + * @param string $file file to read from |
|
| 100 | + * @return \Doctrine\DBAL\Schema\Schema |
|
| 101 | + */ |
|
| 102 | + private function readSchemaFromFile($file) { |
|
| 103 | + $platform = $this->conn->getDatabasePlatform(); |
|
| 104 | + $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $platform); |
|
| 105 | + $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); |
|
| 106 | + return $schemaReader->loadSchemaFromFile($file, $toSchema); |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * update the database scheme |
|
| 111 | - * @param string $file file to read structure from |
|
| 112 | - * @param bool $generateSql only return the sql needed for the upgrade |
|
| 113 | - * @return string|boolean |
|
| 114 | - */ |
|
| 115 | - public function updateDbFromStructure($file, $generateSql = false) { |
|
| 116 | - $toSchema = $this->readSchemaFromFile($file); |
|
| 117 | - $migrator = $this->getMigrator(); |
|
| 109 | + /** |
|
| 110 | + * update the database scheme |
|
| 111 | + * @param string $file file to read structure from |
|
| 112 | + * @param bool $generateSql only return the sql needed for the upgrade |
|
| 113 | + * @return string|boolean |
|
| 114 | + */ |
|
| 115 | + public function updateDbFromStructure($file, $generateSql = false) { |
|
| 116 | + $toSchema = $this->readSchemaFromFile($file); |
|
| 117 | + $migrator = $this->getMigrator(); |
|
| 118 | 118 | |
| 119 | - if ($generateSql) { |
|
| 120 | - return $migrator->generateChangeScript($toSchema); |
|
| 121 | - } else { |
|
| 122 | - $migrator->migrate($toSchema); |
|
| 123 | - return true; |
|
| 124 | - } |
|
| 125 | - } |
|
| 119 | + if ($generateSql) { |
|
| 120 | + return $migrator->generateChangeScript($toSchema); |
|
| 121 | + } else { |
|
| 122 | + $migrator->migrate($toSchema); |
|
| 123 | + return true; |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * @param \Doctrine\DBAL\Schema\Schema $schema |
|
| 129 | - * @return string |
|
| 130 | - */ |
|
| 131 | - public function generateChangeScript($schema) { |
|
| 132 | - $migrator = $this->getMigrator(); |
|
| 133 | - return $migrator->generateChangeScript($schema); |
|
| 134 | - } |
|
| 127 | + /** |
|
| 128 | + * @param \Doctrine\DBAL\Schema\Schema $schema |
|
| 129 | + * @return string |
|
| 130 | + */ |
|
| 131 | + public function generateChangeScript($schema) { |
|
| 132 | + $migrator = $this->getMigrator(); |
|
| 133 | + return $migrator->generateChangeScript($schema); |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - /** |
|
| 137 | - * remove all tables defined in a database structure xml file |
|
| 138 | - * |
|
| 139 | - * @param string $file the xml file describing the tables |
|
| 140 | - */ |
|
| 141 | - public function removeDBStructure($file) { |
|
| 142 | - $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $this->conn->getDatabasePlatform()); |
|
| 143 | - $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); |
|
| 144 | - $fromSchema = $schemaReader->loadSchemaFromFile($file, $toSchema); |
|
| 145 | - $toSchema = clone $fromSchema; |
|
| 146 | - /** @var $table \Doctrine\DBAL\Schema\Table */ |
|
| 147 | - foreach ($toSchema->getTables() as $table) { |
|
| 148 | - $toSchema->dropTable($table->getName()); |
|
| 149 | - } |
|
| 150 | - $comparator = new \Doctrine\DBAL\Schema\Comparator(); |
|
| 151 | - $schemaDiff = $comparator->compare($fromSchema, $toSchema); |
|
| 152 | - $this->executeSchemaChange($schemaDiff); |
|
| 153 | - } |
|
| 136 | + /** |
|
| 137 | + * remove all tables defined in a database structure xml file |
|
| 138 | + * |
|
| 139 | + * @param string $file the xml file describing the tables |
|
| 140 | + */ |
|
| 141 | + public function removeDBStructure($file) { |
|
| 142 | + $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $this->conn->getDatabasePlatform()); |
|
| 143 | + $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); |
|
| 144 | + $fromSchema = $schemaReader->loadSchemaFromFile($file, $toSchema); |
|
| 145 | + $toSchema = clone $fromSchema; |
|
| 146 | + /** @var $table \Doctrine\DBAL\Schema\Table */ |
|
| 147 | + foreach ($toSchema->getTables() as $table) { |
|
| 148 | + $toSchema->dropTable($table->getName()); |
|
| 149 | + } |
|
| 150 | + $comparator = new \Doctrine\DBAL\Schema\Comparator(); |
|
| 151 | + $schemaDiff = $comparator->compare($fromSchema, $toSchema); |
|
| 152 | + $this->executeSchemaChange($schemaDiff); |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - /** |
|
| 156 | - * @param \Doctrine\DBAL\Schema\Schema|\Doctrine\DBAL\Schema\SchemaDiff $schema |
|
| 157 | - * @return bool |
|
| 158 | - */ |
|
| 159 | - private function executeSchemaChange($schema) { |
|
| 160 | - $this->conn->beginTransaction(); |
|
| 161 | - foreach ($schema->toSql($this->conn->getDatabasePlatform()) as $sql) { |
|
| 162 | - $this->conn->query($sql); |
|
| 163 | - } |
|
| 164 | - $this->conn->commit(); |
|
| 155 | + /** |
|
| 156 | + * @param \Doctrine\DBAL\Schema\Schema|\Doctrine\DBAL\Schema\SchemaDiff $schema |
|
| 157 | + * @return bool |
|
| 158 | + */ |
|
| 159 | + private function executeSchemaChange($schema) { |
|
| 160 | + $this->conn->beginTransaction(); |
|
| 161 | + foreach ($schema->toSql($this->conn->getDatabasePlatform()) as $sql) { |
|
| 162 | + $this->conn->query($sql); |
|
| 163 | + } |
|
| 164 | + $this->conn->commit(); |
|
| 165 | 165 | |
| 166 | - if ($this->conn->getDatabasePlatform() instanceof SqlitePlatform) { |
|
| 167 | - $this->conn->close(); |
|
| 168 | - $this->conn->connect(); |
|
| 169 | - } |
|
| 170 | - return true; |
|
| 171 | - } |
|
| 166 | + if ($this->conn->getDatabasePlatform() instanceof SqlitePlatform) { |
|
| 167 | + $this->conn->close(); |
|
| 168 | + $this->conn->connect(); |
|
| 169 | + } |
|
| 170 | + return true; |
|
| 171 | + } |
|
| 172 | 172 | } |