@@ -45,166 +45,166 @@ |
||
45 | 45 | |
46 | 46 | class Migrator { |
47 | 47 | |
48 | - /** @var \Doctrine\DBAL\Connection */ |
|
49 | - protected $connection; |
|
50 | - |
|
51 | - /** @var IConfig */ |
|
52 | - protected $config; |
|
53 | - |
|
54 | - /** @var EventDispatcherInterface */ |
|
55 | - private $dispatcher; |
|
56 | - |
|
57 | - /** @var bool */ |
|
58 | - private $noEmit = false; |
|
59 | - |
|
60 | - /** |
|
61 | - * @param \Doctrine\DBAL\Connection $connection |
|
62 | - * @param IConfig $config |
|
63 | - * @param EventDispatcherInterface $dispatcher |
|
64 | - */ |
|
65 | - public function __construct(\Doctrine\DBAL\Connection $connection, |
|
66 | - IConfig $config, |
|
67 | - EventDispatcherInterface $dispatcher = null) { |
|
68 | - $this->connection = $connection; |
|
69 | - $this->config = $config; |
|
70 | - $this->dispatcher = $dispatcher; |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
75 | - * |
|
76 | - * @throws Exception |
|
77 | - */ |
|
78 | - public function migrate(Schema $targetSchema) { |
|
79 | - $this->noEmit = true; |
|
80 | - $this->applySchema($targetSchema); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
85 | - * @return string |
|
86 | - */ |
|
87 | - public function generateChangeScript(Schema $targetSchema) { |
|
88 | - $schemaDiff = $this->getDiff($targetSchema, $this->connection); |
|
89 | - |
|
90 | - $script = ''; |
|
91 | - $sqls = $schemaDiff->toSql($this->connection->getDatabasePlatform()); |
|
92 | - foreach ($sqls as $sql) { |
|
93 | - $script .= $this->convertStatementToScript($sql); |
|
94 | - } |
|
95 | - |
|
96 | - return $script; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * @throws Exception |
|
101 | - */ |
|
102 | - public function createSchema() { |
|
103 | - $this->connection->getConfiguration()->setSchemaAssetsFilter(function ($asset) { |
|
104 | - /** @var string|AbstractAsset $asset */ |
|
105 | - $filterExpression = $this->getFilterExpression(); |
|
106 | - if ($asset instanceof AbstractAsset) { |
|
107 | - return preg_match($filterExpression, $asset->getName()) === 1; |
|
108 | - } |
|
109 | - return preg_match($filterExpression, $asset) === 1; |
|
110 | - }); |
|
111 | - return $this->connection->getSchemaManager()->createSchema(); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @param Schema $targetSchema |
|
116 | - * @param \Doctrine\DBAL\Connection $connection |
|
117 | - * @return \Doctrine\DBAL\Schema\SchemaDiff |
|
118 | - */ |
|
119 | - protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { |
|
120 | - // adjust varchar columns with a length higher then getVarcharMaxLength to clob |
|
121 | - foreach ($targetSchema->getTables() as $table) { |
|
122 | - foreach ($table->getColumns() as $column) { |
|
123 | - if ($column->getType() instanceof StringType) { |
|
124 | - if ($column->getLength() > $connection->getDatabasePlatform()->getVarcharMaxLength()) { |
|
125 | - $column->setType(Type::getType('text')); |
|
126 | - $column->setLength(null); |
|
127 | - } |
|
128 | - } |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - $this->connection->getConfiguration()->setSchemaAssetsFilter(function ($asset) { |
|
133 | - /** @var string|AbstractAsset $asset */ |
|
134 | - $filterExpression = $this->getFilterExpression(); |
|
135 | - if ($asset instanceof AbstractAsset) { |
|
136 | - return preg_match($filterExpression, $asset->getName()) === 1; |
|
137 | - } |
|
138 | - return preg_match($filterExpression, $asset) === 1; |
|
139 | - }); |
|
140 | - $sourceSchema = $connection->getSchemaManager()->createSchema(); |
|
141 | - |
|
142 | - // remove tables we don't know about |
|
143 | - foreach ($sourceSchema->getTables() as $table) { |
|
144 | - if (!$targetSchema->hasTable($table->getName())) { |
|
145 | - $sourceSchema->dropTable($table->getName()); |
|
146 | - } |
|
147 | - } |
|
148 | - // remove sequences we don't know about |
|
149 | - foreach ($sourceSchema->getSequences() as $table) { |
|
150 | - if (!$targetSchema->hasSequence($table->getName())) { |
|
151 | - $sourceSchema->dropSequence($table->getName()); |
|
152 | - } |
|
153 | - } |
|
154 | - |
|
155 | - $comparator = new Comparator(); |
|
156 | - return $comparator->compare($sourceSchema, $targetSchema); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
161 | - * @param \Doctrine\DBAL\Connection $connection |
|
162 | - * |
|
163 | - * @throws Exception |
|
164 | - */ |
|
165 | - protected function applySchema(Schema $targetSchema, \Doctrine\DBAL\Connection $connection = null) { |
|
166 | - if (is_null($connection)) { |
|
167 | - $connection = $this->connection; |
|
168 | - } |
|
169 | - |
|
170 | - $schemaDiff = $this->getDiff($targetSchema, $connection); |
|
171 | - |
|
172 | - if (!$connection->getDatabasePlatform() instanceof MySQLPlatform) { |
|
173 | - $connection->beginTransaction(); |
|
174 | - } |
|
175 | - $sqls = $schemaDiff->toSql($connection->getDatabasePlatform()); |
|
176 | - $step = 0; |
|
177 | - foreach ($sqls as $sql) { |
|
178 | - $this->emit($sql, $step++, count($sqls)); |
|
179 | - $connection->query($sql); |
|
180 | - } |
|
181 | - if (!$connection->getDatabasePlatform() instanceof MySQLPlatform) { |
|
182 | - $connection->commit(); |
|
183 | - } |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * @param $statement |
|
188 | - * @return string |
|
189 | - */ |
|
190 | - protected function convertStatementToScript($statement) { |
|
191 | - $script = $statement . ';'; |
|
192 | - $script .= PHP_EOL; |
|
193 | - $script .= PHP_EOL; |
|
194 | - return $script; |
|
195 | - } |
|
196 | - |
|
197 | - protected function getFilterExpression() { |
|
198 | - return '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
199 | - } |
|
200 | - |
|
201 | - protected function emit($sql, $step, $max) { |
|
202 | - if ($this->noEmit) { |
|
203 | - return; |
|
204 | - } |
|
205 | - if (is_null($this->dispatcher)) { |
|
206 | - return; |
|
207 | - } |
|
208 | - $this->dispatcher->dispatch('\OC\DB\Migrator::executeSql', new GenericEvent($sql, [$step + 1, $max])); |
|
209 | - } |
|
48 | + /** @var \Doctrine\DBAL\Connection */ |
|
49 | + protected $connection; |
|
50 | + |
|
51 | + /** @var IConfig */ |
|
52 | + protected $config; |
|
53 | + |
|
54 | + /** @var EventDispatcherInterface */ |
|
55 | + private $dispatcher; |
|
56 | + |
|
57 | + /** @var bool */ |
|
58 | + private $noEmit = false; |
|
59 | + |
|
60 | + /** |
|
61 | + * @param \Doctrine\DBAL\Connection $connection |
|
62 | + * @param IConfig $config |
|
63 | + * @param EventDispatcherInterface $dispatcher |
|
64 | + */ |
|
65 | + public function __construct(\Doctrine\DBAL\Connection $connection, |
|
66 | + IConfig $config, |
|
67 | + EventDispatcherInterface $dispatcher = null) { |
|
68 | + $this->connection = $connection; |
|
69 | + $this->config = $config; |
|
70 | + $this->dispatcher = $dispatcher; |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
75 | + * |
|
76 | + * @throws Exception |
|
77 | + */ |
|
78 | + public function migrate(Schema $targetSchema) { |
|
79 | + $this->noEmit = true; |
|
80 | + $this->applySchema($targetSchema); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
85 | + * @return string |
|
86 | + */ |
|
87 | + public function generateChangeScript(Schema $targetSchema) { |
|
88 | + $schemaDiff = $this->getDiff($targetSchema, $this->connection); |
|
89 | + |
|
90 | + $script = ''; |
|
91 | + $sqls = $schemaDiff->toSql($this->connection->getDatabasePlatform()); |
|
92 | + foreach ($sqls as $sql) { |
|
93 | + $script .= $this->convertStatementToScript($sql); |
|
94 | + } |
|
95 | + |
|
96 | + return $script; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * @throws Exception |
|
101 | + */ |
|
102 | + public function createSchema() { |
|
103 | + $this->connection->getConfiguration()->setSchemaAssetsFilter(function ($asset) { |
|
104 | + /** @var string|AbstractAsset $asset */ |
|
105 | + $filterExpression = $this->getFilterExpression(); |
|
106 | + if ($asset instanceof AbstractAsset) { |
|
107 | + return preg_match($filterExpression, $asset->getName()) === 1; |
|
108 | + } |
|
109 | + return preg_match($filterExpression, $asset) === 1; |
|
110 | + }); |
|
111 | + return $this->connection->getSchemaManager()->createSchema(); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @param Schema $targetSchema |
|
116 | + * @param \Doctrine\DBAL\Connection $connection |
|
117 | + * @return \Doctrine\DBAL\Schema\SchemaDiff |
|
118 | + */ |
|
119 | + protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { |
|
120 | + // adjust varchar columns with a length higher then getVarcharMaxLength to clob |
|
121 | + foreach ($targetSchema->getTables() as $table) { |
|
122 | + foreach ($table->getColumns() as $column) { |
|
123 | + if ($column->getType() instanceof StringType) { |
|
124 | + if ($column->getLength() > $connection->getDatabasePlatform()->getVarcharMaxLength()) { |
|
125 | + $column->setType(Type::getType('text')); |
|
126 | + $column->setLength(null); |
|
127 | + } |
|
128 | + } |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + $this->connection->getConfiguration()->setSchemaAssetsFilter(function ($asset) { |
|
133 | + /** @var string|AbstractAsset $asset */ |
|
134 | + $filterExpression = $this->getFilterExpression(); |
|
135 | + if ($asset instanceof AbstractAsset) { |
|
136 | + return preg_match($filterExpression, $asset->getName()) === 1; |
|
137 | + } |
|
138 | + return preg_match($filterExpression, $asset) === 1; |
|
139 | + }); |
|
140 | + $sourceSchema = $connection->getSchemaManager()->createSchema(); |
|
141 | + |
|
142 | + // remove tables we don't know about |
|
143 | + foreach ($sourceSchema->getTables() as $table) { |
|
144 | + if (!$targetSchema->hasTable($table->getName())) { |
|
145 | + $sourceSchema->dropTable($table->getName()); |
|
146 | + } |
|
147 | + } |
|
148 | + // remove sequences we don't know about |
|
149 | + foreach ($sourceSchema->getSequences() as $table) { |
|
150 | + if (!$targetSchema->hasSequence($table->getName())) { |
|
151 | + $sourceSchema->dropSequence($table->getName()); |
|
152 | + } |
|
153 | + } |
|
154 | + |
|
155 | + $comparator = new Comparator(); |
|
156 | + return $comparator->compare($sourceSchema, $targetSchema); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * @param \Doctrine\DBAL\Schema\Schema $targetSchema |
|
161 | + * @param \Doctrine\DBAL\Connection $connection |
|
162 | + * |
|
163 | + * @throws Exception |
|
164 | + */ |
|
165 | + protected function applySchema(Schema $targetSchema, \Doctrine\DBAL\Connection $connection = null) { |
|
166 | + if (is_null($connection)) { |
|
167 | + $connection = $this->connection; |
|
168 | + } |
|
169 | + |
|
170 | + $schemaDiff = $this->getDiff($targetSchema, $connection); |
|
171 | + |
|
172 | + if (!$connection->getDatabasePlatform() instanceof MySQLPlatform) { |
|
173 | + $connection->beginTransaction(); |
|
174 | + } |
|
175 | + $sqls = $schemaDiff->toSql($connection->getDatabasePlatform()); |
|
176 | + $step = 0; |
|
177 | + foreach ($sqls as $sql) { |
|
178 | + $this->emit($sql, $step++, count($sqls)); |
|
179 | + $connection->query($sql); |
|
180 | + } |
|
181 | + if (!$connection->getDatabasePlatform() instanceof MySQLPlatform) { |
|
182 | + $connection->commit(); |
|
183 | + } |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * @param $statement |
|
188 | + * @return string |
|
189 | + */ |
|
190 | + protected function convertStatementToScript($statement) { |
|
191 | + $script = $statement . ';'; |
|
192 | + $script .= PHP_EOL; |
|
193 | + $script .= PHP_EOL; |
|
194 | + return $script; |
|
195 | + } |
|
196 | + |
|
197 | + protected function getFilterExpression() { |
|
198 | + return '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
199 | + } |
|
200 | + |
|
201 | + protected function emit($sql, $step, $max) { |
|
202 | + if ($this->noEmit) { |
|
203 | + return; |
|
204 | + } |
|
205 | + if (is_null($this->dispatcher)) { |
|
206 | + return; |
|
207 | + } |
|
208 | + $this->dispatcher->dispatch('\OC\DB\Migrator::executeSql', new GenericEvent($sql, [$step + 1, $max])); |
|
209 | + } |
|
210 | 210 | } |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | * @throws Exception |
101 | 101 | */ |
102 | 102 | public function createSchema() { |
103 | - $this->connection->getConfiguration()->setSchemaAssetsFilter(function ($asset) { |
|
103 | + $this->connection->getConfiguration()->setSchemaAssetsFilter(function($asset) { |
|
104 | 104 | /** @var string|AbstractAsset $asset */ |
105 | 105 | $filterExpression = $this->getFilterExpression(); |
106 | 106 | if ($asset instanceof AbstractAsset) { |
@@ -129,7 +129,7 @@ discard block |
||
129 | 129 | } |
130 | 130 | } |
131 | 131 | |
132 | - $this->connection->getConfiguration()->setSchemaAssetsFilter(function ($asset) { |
|
132 | + $this->connection->getConfiguration()->setSchemaAssetsFilter(function($asset) { |
|
133 | 133 | /** @var string|AbstractAsset $asset */ |
134 | 134 | $filterExpression = $this->getFilterExpression(); |
135 | 135 | if ($asset instanceof AbstractAsset) { |
@@ -188,14 +188,14 @@ discard block |
||
188 | 188 | * @return string |
189 | 189 | */ |
190 | 190 | protected function convertStatementToScript($statement) { |
191 | - $script = $statement . ';'; |
|
191 | + $script = $statement.';'; |
|
192 | 192 | $script .= PHP_EOL; |
193 | 193 | $script .= PHP_EOL; |
194 | 194 | return $script; |
195 | 195 | } |
196 | 196 | |
197 | 197 | protected function getFilterExpression() { |
198 | - return '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
198 | + return '/^'.preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')).'/'; |
|
199 | 199 | } |
200 | 200 | |
201 | 201 | protected function emit($sql, $step, $max) { |
@@ -40,185 +40,185 @@ |
||
40 | 40 | |
41 | 41 | class OracleMigrator extends Migrator { |
42 | 42 | |
43 | - /** |
|
44 | - * Quote a column's name but changing the name requires recreating |
|
45 | - * the column instance and copying over all properties. |
|
46 | - * |
|
47 | - * @param Column $column old column |
|
48 | - * @return Column new column instance with new name |
|
49 | - */ |
|
50 | - protected function quoteColumn(Column $column) { |
|
51 | - $newColumn = new Column( |
|
52 | - $this->connection->quoteIdentifier($column->getName()), |
|
53 | - $column->getType() |
|
54 | - ); |
|
55 | - $newColumn->setAutoincrement($column->getAutoincrement()); |
|
56 | - $newColumn->setColumnDefinition($column->getColumnDefinition()); |
|
57 | - $newColumn->setComment($column->getComment()); |
|
58 | - $newColumn->setDefault($column->getDefault()); |
|
59 | - $newColumn->setFixed($column->getFixed()); |
|
60 | - $newColumn->setLength($column->getLength()); |
|
61 | - $newColumn->setNotnull($column->getNotnull()); |
|
62 | - $newColumn->setPrecision($column->getPrecision()); |
|
63 | - $newColumn->setScale($column->getScale()); |
|
64 | - $newColumn->setUnsigned($column->getUnsigned()); |
|
65 | - $newColumn->setPlatformOptions($column->getPlatformOptions()); |
|
66 | - $newColumn->setCustomSchemaOptions($column->getPlatformOptions()); |
|
67 | - return $newColumn; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Quote an index's name but changing the name requires recreating |
|
72 | - * the index instance and copying over all properties. |
|
73 | - * |
|
74 | - * @param Index $index old index |
|
75 | - * @return Index new index instance with new name |
|
76 | - */ |
|
77 | - protected function quoteIndex($index) { |
|
78 | - return new Index( |
|
79 | - //TODO migrate existing uppercase indexes, then $this->connection->quoteIdentifier($index->getName()), |
|
80 | - $index->getName(), |
|
81 | - array_map(function ($columnName) { |
|
82 | - return $this->connection->quoteIdentifier($columnName); |
|
83 | - }, $index->getColumns()), |
|
84 | - $index->isUnique(), |
|
85 | - $index->isPrimary(), |
|
86 | - $index->getFlags(), |
|
87 | - $index->getOptions() |
|
88 | - ); |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Quote an ForeignKeyConstraint's name but changing the name requires recreating |
|
93 | - * the ForeignKeyConstraint instance and copying over all properties. |
|
94 | - * |
|
95 | - * @param ForeignKeyConstraint $fkc old fkc |
|
96 | - * @return ForeignKeyConstraint new fkc instance with new name |
|
97 | - */ |
|
98 | - protected function quoteForeignKeyConstraint($fkc) { |
|
99 | - return new ForeignKeyConstraint( |
|
100 | - array_map(function ($columnName) { |
|
101 | - return $this->connection->quoteIdentifier($columnName); |
|
102 | - }, $fkc->getLocalColumns()), |
|
103 | - $this->connection->quoteIdentifier($fkc->getForeignTableName()), |
|
104 | - array_map(function ($columnName) { |
|
105 | - return $this->connection->quoteIdentifier($columnName); |
|
106 | - }, $fkc->getForeignColumns()), |
|
107 | - $fkc->getName(), |
|
108 | - $fkc->getOptions() |
|
109 | - ); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @param Schema $targetSchema |
|
114 | - * @param \Doctrine\DBAL\Connection $connection |
|
115 | - * @return \Doctrine\DBAL\Schema\SchemaDiff |
|
116 | - * @throws Exception |
|
117 | - */ |
|
118 | - protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { |
|
119 | - $schemaDiff = parent::getDiff($targetSchema, $connection); |
|
120 | - |
|
121 | - // oracle forces us to quote the identifiers |
|
122 | - $schemaDiff->newTables = array_map(function (Table $table) { |
|
123 | - return new Table( |
|
124 | - $this->connection->quoteIdentifier($table->getName()), |
|
125 | - array_map(function (Column $column) { |
|
126 | - return $this->quoteColumn($column); |
|
127 | - }, $table->getColumns()), |
|
128 | - array_map(function (Index $index) { |
|
129 | - return $this->quoteIndex($index); |
|
130 | - }, $table->getIndexes()), |
|
131 | - [], |
|
132 | - array_map(function (ForeignKeyConstraint $fck) { |
|
133 | - return $this->quoteForeignKeyConstraint($fck); |
|
134 | - }, $table->getForeignKeys()), |
|
135 | - $table->getOptions() |
|
136 | - ); |
|
137 | - }, $schemaDiff->newTables); |
|
138 | - |
|
139 | - $schemaDiff->removedTables = array_map(function (Table $table) { |
|
140 | - return new Table( |
|
141 | - $this->connection->quoteIdentifier($table->getName()), |
|
142 | - $table->getColumns(), |
|
143 | - $table->getIndexes(), |
|
144 | - [], |
|
145 | - $table->getForeignKeys(), |
|
146 | - $table->getOptions() |
|
147 | - ); |
|
148 | - }, $schemaDiff->removedTables); |
|
149 | - |
|
150 | - foreach ($schemaDiff->changedTables as $tableDiff) { |
|
151 | - $tableDiff->name = $this->connection->quoteIdentifier($tableDiff->name); |
|
152 | - |
|
153 | - $tableDiff->addedColumns = array_map(function (Column $column) { |
|
154 | - return $this->quoteColumn($column); |
|
155 | - }, $tableDiff->addedColumns); |
|
156 | - |
|
157 | - foreach ($tableDiff->changedColumns as $column) { |
|
158 | - $column->oldColumnName = $this->connection->quoteIdentifier($column->oldColumnName); |
|
159 | - // auto increment is not relevant for oracle and can anyhow not be applied on change |
|
160 | - $column->changedProperties = array_diff($column->changedProperties, ['autoincrement', 'unsigned']); |
|
161 | - } |
|
162 | - // remove columns that no longer have changed (because autoincrement and unsigned are not supported) |
|
163 | - $tableDiff->changedColumns = array_filter($tableDiff->changedColumns, function (ColumnDiff $column) { |
|
164 | - return count($column->changedProperties) > 0; |
|
165 | - }); |
|
166 | - |
|
167 | - $tableDiff->removedColumns = array_map(function (Column $column) { |
|
168 | - return $this->quoteColumn($column); |
|
169 | - }, $tableDiff->removedColumns); |
|
170 | - |
|
171 | - $tableDiff->renamedColumns = array_map(function (Column $column) { |
|
172 | - return $this->quoteColumn($column); |
|
173 | - }, $tableDiff->renamedColumns); |
|
174 | - |
|
175 | - $tableDiff->addedIndexes = array_map(function (Index $index) { |
|
176 | - return $this->quoteIndex($index); |
|
177 | - }, $tableDiff->addedIndexes); |
|
178 | - |
|
179 | - $tableDiff->changedIndexes = array_map(function (Index $index) { |
|
180 | - return $this->quoteIndex($index); |
|
181 | - }, $tableDiff->changedIndexes); |
|
182 | - |
|
183 | - $tableDiff->removedIndexes = array_map(function (Index $index) { |
|
184 | - return $this->quoteIndex($index); |
|
185 | - }, $tableDiff->removedIndexes); |
|
186 | - |
|
187 | - $tableDiff->renamedIndexes = array_map(function (Index $index) { |
|
188 | - return $this->quoteIndex($index); |
|
189 | - }, $tableDiff->renamedIndexes); |
|
190 | - |
|
191 | - $tableDiff->addedForeignKeys = array_map(function (ForeignKeyConstraint $fkc) { |
|
192 | - return $this->quoteForeignKeyConstraint($fkc); |
|
193 | - }, $tableDiff->addedForeignKeys); |
|
194 | - |
|
195 | - $tableDiff->changedForeignKeys = array_map(function (ForeignKeyConstraint $fkc) { |
|
196 | - return $this->quoteForeignKeyConstraint($fkc); |
|
197 | - }, $tableDiff->changedForeignKeys); |
|
198 | - |
|
199 | - $tableDiff->removedForeignKeys = array_map(function (ForeignKeyConstraint $fkc) { |
|
200 | - return $this->quoteForeignKeyConstraint($fkc); |
|
201 | - }, $tableDiff->removedForeignKeys); |
|
202 | - } |
|
203 | - |
|
204 | - return $schemaDiff; |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * @param $statement |
|
209 | - * @return string |
|
210 | - */ |
|
211 | - protected function convertStatementToScript($statement) { |
|
212 | - if (substr($statement, -1) === ';') { |
|
213 | - return $statement . PHP_EOL . '/' . PHP_EOL; |
|
214 | - } |
|
215 | - $script = $statement . ';'; |
|
216 | - $script .= PHP_EOL; |
|
217 | - $script .= PHP_EOL; |
|
218 | - return $script; |
|
219 | - } |
|
220 | - |
|
221 | - protected function getFilterExpression() { |
|
222 | - return '/^"' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
223 | - } |
|
43 | + /** |
|
44 | + * Quote a column's name but changing the name requires recreating |
|
45 | + * the column instance and copying over all properties. |
|
46 | + * |
|
47 | + * @param Column $column old column |
|
48 | + * @return Column new column instance with new name |
|
49 | + */ |
|
50 | + protected function quoteColumn(Column $column) { |
|
51 | + $newColumn = new Column( |
|
52 | + $this->connection->quoteIdentifier($column->getName()), |
|
53 | + $column->getType() |
|
54 | + ); |
|
55 | + $newColumn->setAutoincrement($column->getAutoincrement()); |
|
56 | + $newColumn->setColumnDefinition($column->getColumnDefinition()); |
|
57 | + $newColumn->setComment($column->getComment()); |
|
58 | + $newColumn->setDefault($column->getDefault()); |
|
59 | + $newColumn->setFixed($column->getFixed()); |
|
60 | + $newColumn->setLength($column->getLength()); |
|
61 | + $newColumn->setNotnull($column->getNotnull()); |
|
62 | + $newColumn->setPrecision($column->getPrecision()); |
|
63 | + $newColumn->setScale($column->getScale()); |
|
64 | + $newColumn->setUnsigned($column->getUnsigned()); |
|
65 | + $newColumn->setPlatformOptions($column->getPlatformOptions()); |
|
66 | + $newColumn->setCustomSchemaOptions($column->getPlatformOptions()); |
|
67 | + return $newColumn; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Quote an index's name but changing the name requires recreating |
|
72 | + * the index instance and copying over all properties. |
|
73 | + * |
|
74 | + * @param Index $index old index |
|
75 | + * @return Index new index instance with new name |
|
76 | + */ |
|
77 | + protected function quoteIndex($index) { |
|
78 | + return new Index( |
|
79 | + //TODO migrate existing uppercase indexes, then $this->connection->quoteIdentifier($index->getName()), |
|
80 | + $index->getName(), |
|
81 | + array_map(function ($columnName) { |
|
82 | + return $this->connection->quoteIdentifier($columnName); |
|
83 | + }, $index->getColumns()), |
|
84 | + $index->isUnique(), |
|
85 | + $index->isPrimary(), |
|
86 | + $index->getFlags(), |
|
87 | + $index->getOptions() |
|
88 | + ); |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Quote an ForeignKeyConstraint's name but changing the name requires recreating |
|
93 | + * the ForeignKeyConstraint instance and copying over all properties. |
|
94 | + * |
|
95 | + * @param ForeignKeyConstraint $fkc old fkc |
|
96 | + * @return ForeignKeyConstraint new fkc instance with new name |
|
97 | + */ |
|
98 | + protected function quoteForeignKeyConstraint($fkc) { |
|
99 | + return new ForeignKeyConstraint( |
|
100 | + array_map(function ($columnName) { |
|
101 | + return $this->connection->quoteIdentifier($columnName); |
|
102 | + }, $fkc->getLocalColumns()), |
|
103 | + $this->connection->quoteIdentifier($fkc->getForeignTableName()), |
|
104 | + array_map(function ($columnName) { |
|
105 | + return $this->connection->quoteIdentifier($columnName); |
|
106 | + }, $fkc->getForeignColumns()), |
|
107 | + $fkc->getName(), |
|
108 | + $fkc->getOptions() |
|
109 | + ); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @param Schema $targetSchema |
|
114 | + * @param \Doctrine\DBAL\Connection $connection |
|
115 | + * @return \Doctrine\DBAL\Schema\SchemaDiff |
|
116 | + * @throws Exception |
|
117 | + */ |
|
118 | + protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { |
|
119 | + $schemaDiff = parent::getDiff($targetSchema, $connection); |
|
120 | + |
|
121 | + // oracle forces us to quote the identifiers |
|
122 | + $schemaDiff->newTables = array_map(function (Table $table) { |
|
123 | + return new Table( |
|
124 | + $this->connection->quoteIdentifier($table->getName()), |
|
125 | + array_map(function (Column $column) { |
|
126 | + return $this->quoteColumn($column); |
|
127 | + }, $table->getColumns()), |
|
128 | + array_map(function (Index $index) { |
|
129 | + return $this->quoteIndex($index); |
|
130 | + }, $table->getIndexes()), |
|
131 | + [], |
|
132 | + array_map(function (ForeignKeyConstraint $fck) { |
|
133 | + return $this->quoteForeignKeyConstraint($fck); |
|
134 | + }, $table->getForeignKeys()), |
|
135 | + $table->getOptions() |
|
136 | + ); |
|
137 | + }, $schemaDiff->newTables); |
|
138 | + |
|
139 | + $schemaDiff->removedTables = array_map(function (Table $table) { |
|
140 | + return new Table( |
|
141 | + $this->connection->quoteIdentifier($table->getName()), |
|
142 | + $table->getColumns(), |
|
143 | + $table->getIndexes(), |
|
144 | + [], |
|
145 | + $table->getForeignKeys(), |
|
146 | + $table->getOptions() |
|
147 | + ); |
|
148 | + }, $schemaDiff->removedTables); |
|
149 | + |
|
150 | + foreach ($schemaDiff->changedTables as $tableDiff) { |
|
151 | + $tableDiff->name = $this->connection->quoteIdentifier($tableDiff->name); |
|
152 | + |
|
153 | + $tableDiff->addedColumns = array_map(function (Column $column) { |
|
154 | + return $this->quoteColumn($column); |
|
155 | + }, $tableDiff->addedColumns); |
|
156 | + |
|
157 | + foreach ($tableDiff->changedColumns as $column) { |
|
158 | + $column->oldColumnName = $this->connection->quoteIdentifier($column->oldColumnName); |
|
159 | + // auto increment is not relevant for oracle and can anyhow not be applied on change |
|
160 | + $column->changedProperties = array_diff($column->changedProperties, ['autoincrement', 'unsigned']); |
|
161 | + } |
|
162 | + // remove columns that no longer have changed (because autoincrement and unsigned are not supported) |
|
163 | + $tableDiff->changedColumns = array_filter($tableDiff->changedColumns, function (ColumnDiff $column) { |
|
164 | + return count($column->changedProperties) > 0; |
|
165 | + }); |
|
166 | + |
|
167 | + $tableDiff->removedColumns = array_map(function (Column $column) { |
|
168 | + return $this->quoteColumn($column); |
|
169 | + }, $tableDiff->removedColumns); |
|
170 | + |
|
171 | + $tableDiff->renamedColumns = array_map(function (Column $column) { |
|
172 | + return $this->quoteColumn($column); |
|
173 | + }, $tableDiff->renamedColumns); |
|
174 | + |
|
175 | + $tableDiff->addedIndexes = array_map(function (Index $index) { |
|
176 | + return $this->quoteIndex($index); |
|
177 | + }, $tableDiff->addedIndexes); |
|
178 | + |
|
179 | + $tableDiff->changedIndexes = array_map(function (Index $index) { |
|
180 | + return $this->quoteIndex($index); |
|
181 | + }, $tableDiff->changedIndexes); |
|
182 | + |
|
183 | + $tableDiff->removedIndexes = array_map(function (Index $index) { |
|
184 | + return $this->quoteIndex($index); |
|
185 | + }, $tableDiff->removedIndexes); |
|
186 | + |
|
187 | + $tableDiff->renamedIndexes = array_map(function (Index $index) { |
|
188 | + return $this->quoteIndex($index); |
|
189 | + }, $tableDiff->renamedIndexes); |
|
190 | + |
|
191 | + $tableDiff->addedForeignKeys = array_map(function (ForeignKeyConstraint $fkc) { |
|
192 | + return $this->quoteForeignKeyConstraint($fkc); |
|
193 | + }, $tableDiff->addedForeignKeys); |
|
194 | + |
|
195 | + $tableDiff->changedForeignKeys = array_map(function (ForeignKeyConstraint $fkc) { |
|
196 | + return $this->quoteForeignKeyConstraint($fkc); |
|
197 | + }, $tableDiff->changedForeignKeys); |
|
198 | + |
|
199 | + $tableDiff->removedForeignKeys = array_map(function (ForeignKeyConstraint $fkc) { |
|
200 | + return $this->quoteForeignKeyConstraint($fkc); |
|
201 | + }, $tableDiff->removedForeignKeys); |
|
202 | + } |
|
203 | + |
|
204 | + return $schemaDiff; |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * @param $statement |
|
209 | + * @return string |
|
210 | + */ |
|
211 | + protected function convertStatementToScript($statement) { |
|
212 | + if (substr($statement, -1) === ';') { |
|
213 | + return $statement . PHP_EOL . '/' . PHP_EOL; |
|
214 | + } |
|
215 | + $script = $statement . ';'; |
|
216 | + $script .= PHP_EOL; |
|
217 | + $script .= PHP_EOL; |
|
218 | + return $script; |
|
219 | + } |
|
220 | + |
|
221 | + protected function getFilterExpression() { |
|
222 | + return '/^"' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
223 | + } |
|
224 | 224 | } |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | return new Index( |
79 | 79 | //TODO migrate existing uppercase indexes, then $this->connection->quoteIdentifier($index->getName()), |
80 | 80 | $index->getName(), |
81 | - array_map(function ($columnName) { |
|
81 | + array_map(function($columnName) { |
|
82 | 82 | return $this->connection->quoteIdentifier($columnName); |
83 | 83 | }, $index->getColumns()), |
84 | 84 | $index->isUnique(), |
@@ -97,11 +97,11 @@ discard block |
||
97 | 97 | */ |
98 | 98 | protected function quoteForeignKeyConstraint($fkc) { |
99 | 99 | return new ForeignKeyConstraint( |
100 | - array_map(function ($columnName) { |
|
100 | + array_map(function($columnName) { |
|
101 | 101 | return $this->connection->quoteIdentifier($columnName); |
102 | 102 | }, $fkc->getLocalColumns()), |
103 | 103 | $this->connection->quoteIdentifier($fkc->getForeignTableName()), |
104 | - array_map(function ($columnName) { |
|
104 | + array_map(function($columnName) { |
|
105 | 105 | return $this->connection->quoteIdentifier($columnName); |
106 | 106 | }, $fkc->getForeignColumns()), |
107 | 107 | $fkc->getName(), |
@@ -119,24 +119,24 @@ discard block |
||
119 | 119 | $schemaDiff = parent::getDiff($targetSchema, $connection); |
120 | 120 | |
121 | 121 | // oracle forces us to quote the identifiers |
122 | - $schemaDiff->newTables = array_map(function (Table $table) { |
|
122 | + $schemaDiff->newTables = array_map(function(Table $table) { |
|
123 | 123 | return new Table( |
124 | 124 | $this->connection->quoteIdentifier($table->getName()), |
125 | - array_map(function (Column $column) { |
|
125 | + array_map(function(Column $column) { |
|
126 | 126 | return $this->quoteColumn($column); |
127 | 127 | }, $table->getColumns()), |
128 | - array_map(function (Index $index) { |
|
128 | + array_map(function(Index $index) { |
|
129 | 129 | return $this->quoteIndex($index); |
130 | 130 | }, $table->getIndexes()), |
131 | 131 | [], |
132 | - array_map(function (ForeignKeyConstraint $fck) { |
|
132 | + array_map(function(ForeignKeyConstraint $fck) { |
|
133 | 133 | return $this->quoteForeignKeyConstraint($fck); |
134 | 134 | }, $table->getForeignKeys()), |
135 | 135 | $table->getOptions() |
136 | 136 | ); |
137 | 137 | }, $schemaDiff->newTables); |
138 | 138 | |
139 | - $schemaDiff->removedTables = array_map(function (Table $table) { |
|
139 | + $schemaDiff->removedTables = array_map(function(Table $table) { |
|
140 | 140 | return new Table( |
141 | 141 | $this->connection->quoteIdentifier($table->getName()), |
142 | 142 | $table->getColumns(), |
@@ -150,7 +150,7 @@ discard block |
||
150 | 150 | foreach ($schemaDiff->changedTables as $tableDiff) { |
151 | 151 | $tableDiff->name = $this->connection->quoteIdentifier($tableDiff->name); |
152 | 152 | |
153 | - $tableDiff->addedColumns = array_map(function (Column $column) { |
|
153 | + $tableDiff->addedColumns = array_map(function(Column $column) { |
|
154 | 154 | return $this->quoteColumn($column); |
155 | 155 | }, $tableDiff->addedColumns); |
156 | 156 | |
@@ -160,43 +160,43 @@ discard block |
||
160 | 160 | $column->changedProperties = array_diff($column->changedProperties, ['autoincrement', 'unsigned']); |
161 | 161 | } |
162 | 162 | // remove columns that no longer have changed (because autoincrement and unsigned are not supported) |
163 | - $tableDiff->changedColumns = array_filter($tableDiff->changedColumns, function (ColumnDiff $column) { |
|
163 | + $tableDiff->changedColumns = array_filter($tableDiff->changedColumns, function(ColumnDiff $column) { |
|
164 | 164 | return count($column->changedProperties) > 0; |
165 | 165 | }); |
166 | 166 | |
167 | - $tableDiff->removedColumns = array_map(function (Column $column) { |
|
167 | + $tableDiff->removedColumns = array_map(function(Column $column) { |
|
168 | 168 | return $this->quoteColumn($column); |
169 | 169 | }, $tableDiff->removedColumns); |
170 | 170 | |
171 | - $tableDiff->renamedColumns = array_map(function (Column $column) { |
|
171 | + $tableDiff->renamedColumns = array_map(function(Column $column) { |
|
172 | 172 | return $this->quoteColumn($column); |
173 | 173 | }, $tableDiff->renamedColumns); |
174 | 174 | |
175 | - $tableDiff->addedIndexes = array_map(function (Index $index) { |
|
175 | + $tableDiff->addedIndexes = array_map(function(Index $index) { |
|
176 | 176 | return $this->quoteIndex($index); |
177 | 177 | }, $tableDiff->addedIndexes); |
178 | 178 | |
179 | - $tableDiff->changedIndexes = array_map(function (Index $index) { |
|
179 | + $tableDiff->changedIndexes = array_map(function(Index $index) { |
|
180 | 180 | return $this->quoteIndex($index); |
181 | 181 | }, $tableDiff->changedIndexes); |
182 | 182 | |
183 | - $tableDiff->removedIndexes = array_map(function (Index $index) { |
|
183 | + $tableDiff->removedIndexes = array_map(function(Index $index) { |
|
184 | 184 | return $this->quoteIndex($index); |
185 | 185 | }, $tableDiff->removedIndexes); |
186 | 186 | |
187 | - $tableDiff->renamedIndexes = array_map(function (Index $index) { |
|
187 | + $tableDiff->renamedIndexes = array_map(function(Index $index) { |
|
188 | 188 | return $this->quoteIndex($index); |
189 | 189 | }, $tableDiff->renamedIndexes); |
190 | 190 | |
191 | - $tableDiff->addedForeignKeys = array_map(function (ForeignKeyConstraint $fkc) { |
|
191 | + $tableDiff->addedForeignKeys = array_map(function(ForeignKeyConstraint $fkc) { |
|
192 | 192 | return $this->quoteForeignKeyConstraint($fkc); |
193 | 193 | }, $tableDiff->addedForeignKeys); |
194 | 194 | |
195 | - $tableDiff->changedForeignKeys = array_map(function (ForeignKeyConstraint $fkc) { |
|
195 | + $tableDiff->changedForeignKeys = array_map(function(ForeignKeyConstraint $fkc) { |
|
196 | 196 | return $this->quoteForeignKeyConstraint($fkc); |
197 | 197 | }, $tableDiff->changedForeignKeys); |
198 | 198 | |
199 | - $tableDiff->removedForeignKeys = array_map(function (ForeignKeyConstraint $fkc) { |
|
199 | + $tableDiff->removedForeignKeys = array_map(function(ForeignKeyConstraint $fkc) { |
|
200 | 200 | return $this->quoteForeignKeyConstraint($fkc); |
201 | 201 | }, $tableDiff->removedForeignKeys); |
202 | 202 | } |
@@ -210,15 +210,15 @@ discard block |
||
210 | 210 | */ |
211 | 211 | protected function convertStatementToScript($statement) { |
212 | 212 | if (substr($statement, -1) === ';') { |
213 | - return $statement . PHP_EOL . '/' . PHP_EOL; |
|
213 | + return $statement.PHP_EOL.'/'.PHP_EOL; |
|
214 | 214 | } |
215 | - $script = $statement . ';'; |
|
215 | + $script = $statement.';'; |
|
216 | 216 | $script .= PHP_EOL; |
217 | 217 | $script .= PHP_EOL; |
218 | 218 | return $script; |
219 | 219 | } |
220 | 220 | |
221 | 221 | protected function getFilterExpression() { |
222 | - return '/^"' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
222 | + return '/^"'.preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')).'/'; |
|
223 | 223 | } |
224 | 224 | } |
@@ -27,26 +27,26 @@ |
||
27 | 27 | use Doctrine\DBAL\Schema\Schema; |
28 | 28 | |
29 | 29 | class MySQLMigrator extends Migrator { |
30 | - /** |
|
31 | - * @param Schema $targetSchema |
|
32 | - * @param \Doctrine\DBAL\Connection $connection |
|
33 | - * @return \Doctrine\DBAL\Schema\SchemaDiff |
|
34 | - */ |
|
35 | - protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { |
|
36 | - $platform = $connection->getDatabasePlatform(); |
|
37 | - $platform->registerDoctrineTypeMapping('enum', 'string'); |
|
38 | - $platform->registerDoctrineTypeMapping('bit', 'string'); |
|
30 | + /** |
|
31 | + * @param Schema $targetSchema |
|
32 | + * @param \Doctrine\DBAL\Connection $connection |
|
33 | + * @return \Doctrine\DBAL\Schema\SchemaDiff |
|
34 | + */ |
|
35 | + protected function getDiff(Schema $targetSchema, \Doctrine\DBAL\Connection $connection) { |
|
36 | + $platform = $connection->getDatabasePlatform(); |
|
37 | + $platform->registerDoctrineTypeMapping('enum', 'string'); |
|
38 | + $platform->registerDoctrineTypeMapping('bit', 'string'); |
|
39 | 39 | |
40 | - $schemaDiff = parent::getDiff($targetSchema, $connection); |
|
40 | + $schemaDiff = parent::getDiff($targetSchema, $connection); |
|
41 | 41 | |
42 | - // identifiers need to be quoted for mysql |
|
43 | - foreach ($schemaDiff->changedTables as $tableDiff) { |
|
44 | - $tableDiff->name = $this->connection->quoteIdentifier($tableDiff->name); |
|
45 | - foreach ($tableDiff->changedColumns as $column) { |
|
46 | - $column->oldColumnName = $this->connection->quoteIdentifier($column->oldColumnName); |
|
47 | - } |
|
48 | - } |
|
42 | + // identifiers need to be quoted for mysql |
|
43 | + foreach ($schemaDiff->changedTables as $tableDiff) { |
|
44 | + $tableDiff->name = $this->connection->quoteIdentifier($tableDiff->name); |
|
45 | + foreach ($tableDiff->changedColumns as $column) { |
|
46 | + $column->oldColumnName = $this->connection->quoteIdentifier($column->oldColumnName); |
|
47 | + } |
|
48 | + } |
|
49 | 49 | |
50 | - return $schemaDiff; |
|
51 | - } |
|
50 | + return $schemaDiff; |
|
51 | + } |
|
52 | 52 | } |
@@ -38,124 +38,124 @@ |
||
38 | 38 | use Doctrine\DBAL\Schema\Schema; |
39 | 39 | |
40 | 40 | class MDB2SchemaManager { |
41 | - /** @var Connection $conn */ |
|
42 | - protected $conn; |
|
41 | + /** @var Connection $conn */ |
|
42 | + protected $conn; |
|
43 | 43 | |
44 | - /** |
|
45 | - * @param Connection $conn |
|
46 | - */ |
|
47 | - public function __construct($conn) { |
|
48 | - $this->conn = $conn; |
|
49 | - } |
|
44 | + /** |
|
45 | + * @param Connection $conn |
|
46 | + */ |
|
47 | + public function __construct($conn) { |
|
48 | + $this->conn = $conn; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Creates tables from XML file |
|
53 | - * @param string $file file to read structure from |
|
54 | - * @return bool |
|
55 | - * |
|
56 | - * TODO: write more documentation |
|
57 | - */ |
|
58 | - public function createDbFromStructure($file) { |
|
59 | - $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $this->conn->getDatabasePlatform()); |
|
60 | - $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); |
|
61 | - $toSchema = $schemaReader->loadSchemaFromFile($file, $toSchema); |
|
62 | - return $this->executeSchemaChange($toSchema); |
|
63 | - } |
|
51 | + /** |
|
52 | + * Creates tables from XML file |
|
53 | + * @param string $file file to read structure from |
|
54 | + * @return bool |
|
55 | + * |
|
56 | + * TODO: write more documentation |
|
57 | + */ |
|
58 | + public function createDbFromStructure($file) { |
|
59 | + $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $this->conn->getDatabasePlatform()); |
|
60 | + $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); |
|
61 | + $toSchema = $schemaReader->loadSchemaFromFile($file, $toSchema); |
|
62 | + return $this->executeSchemaChange($toSchema); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * @return \OC\DB\Migrator |
|
67 | - */ |
|
68 | - public function getMigrator() { |
|
69 | - $random = \OC::$server->getSecureRandom(); |
|
70 | - $platform = $this->conn->getDatabasePlatform(); |
|
71 | - $config = \OC::$server->getConfig(); |
|
72 | - $dispatcher = \OC::$server->getEventDispatcher(); |
|
73 | - if ($platform instanceof SqlitePlatform) { |
|
74 | - return new SQLiteMigrator($this->conn, $config, $dispatcher); |
|
75 | - } elseif ($platform instanceof OraclePlatform) { |
|
76 | - return new OracleMigrator($this->conn, $config, $dispatcher); |
|
77 | - } elseif ($platform instanceof MySQLPlatform) { |
|
78 | - return new MySQLMigrator($this->conn, $config, $dispatcher); |
|
79 | - } elseif ($platform instanceof PostgreSQL94Platform) { |
|
80 | - return new PostgreSqlMigrator($this->conn, $config, $dispatcher); |
|
81 | - } else { |
|
82 | - return new Migrator($this->conn, $config, $dispatcher); |
|
83 | - } |
|
84 | - } |
|
65 | + /** |
|
66 | + * @return \OC\DB\Migrator |
|
67 | + */ |
|
68 | + public function getMigrator() { |
|
69 | + $random = \OC::$server->getSecureRandom(); |
|
70 | + $platform = $this->conn->getDatabasePlatform(); |
|
71 | + $config = \OC::$server->getConfig(); |
|
72 | + $dispatcher = \OC::$server->getEventDispatcher(); |
|
73 | + if ($platform instanceof SqlitePlatform) { |
|
74 | + return new SQLiteMigrator($this->conn, $config, $dispatcher); |
|
75 | + } elseif ($platform instanceof OraclePlatform) { |
|
76 | + return new OracleMigrator($this->conn, $config, $dispatcher); |
|
77 | + } elseif ($platform instanceof MySQLPlatform) { |
|
78 | + return new MySQLMigrator($this->conn, $config, $dispatcher); |
|
79 | + } elseif ($platform instanceof PostgreSQL94Platform) { |
|
80 | + return new PostgreSqlMigrator($this->conn, $config, $dispatcher); |
|
81 | + } else { |
|
82 | + return new Migrator($this->conn, $config, $dispatcher); |
|
83 | + } |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Reads database schema from file |
|
88 | - * |
|
89 | - * @param string $file file to read from |
|
90 | - * @return \Doctrine\DBAL\Schema\Schema |
|
91 | - */ |
|
92 | - private function readSchemaFromFile($file) { |
|
93 | - $platform = $this->conn->getDatabasePlatform(); |
|
94 | - $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $platform); |
|
95 | - $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); |
|
96 | - return $schemaReader->loadSchemaFromFile($file, $toSchema); |
|
97 | - } |
|
86 | + /** |
|
87 | + * Reads database schema from file |
|
88 | + * |
|
89 | + * @param string $file file to read from |
|
90 | + * @return \Doctrine\DBAL\Schema\Schema |
|
91 | + */ |
|
92 | + private function readSchemaFromFile($file) { |
|
93 | + $platform = $this->conn->getDatabasePlatform(); |
|
94 | + $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $platform); |
|
95 | + $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); |
|
96 | + return $schemaReader->loadSchemaFromFile($file, $toSchema); |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * update the database scheme |
|
101 | - * @param string $file file to read structure from |
|
102 | - * @param bool $generateSql only return the sql needed for the upgrade |
|
103 | - * @return string|boolean |
|
104 | - */ |
|
105 | - public function updateDbFromStructure($file, $generateSql = false) { |
|
106 | - $toSchema = $this->readSchemaFromFile($file); |
|
107 | - $migrator = $this->getMigrator(); |
|
99 | + /** |
|
100 | + * update the database scheme |
|
101 | + * @param string $file file to read structure from |
|
102 | + * @param bool $generateSql only return the sql needed for the upgrade |
|
103 | + * @return string|boolean |
|
104 | + */ |
|
105 | + public function updateDbFromStructure($file, $generateSql = false) { |
|
106 | + $toSchema = $this->readSchemaFromFile($file); |
|
107 | + $migrator = $this->getMigrator(); |
|
108 | 108 | |
109 | - if ($generateSql) { |
|
110 | - return $migrator->generateChangeScript($toSchema); |
|
111 | - } else { |
|
112 | - $migrator->migrate($toSchema); |
|
113 | - return true; |
|
114 | - } |
|
115 | - } |
|
109 | + if ($generateSql) { |
|
110 | + return $migrator->generateChangeScript($toSchema); |
|
111 | + } else { |
|
112 | + $migrator->migrate($toSchema); |
|
113 | + return true; |
|
114 | + } |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * @param \Doctrine\DBAL\Schema\Schema $schema |
|
119 | - * @return string |
|
120 | - */ |
|
121 | - public function generateChangeScript($schema) { |
|
122 | - $migrator = $this->getMigrator(); |
|
123 | - return $migrator->generateChangeScript($schema); |
|
124 | - } |
|
117 | + /** |
|
118 | + * @param \Doctrine\DBAL\Schema\Schema $schema |
|
119 | + * @return string |
|
120 | + */ |
|
121 | + public function generateChangeScript($schema) { |
|
122 | + $migrator = $this->getMigrator(); |
|
123 | + return $migrator->generateChangeScript($schema); |
|
124 | + } |
|
125 | 125 | |
126 | - /** |
|
127 | - * remove all tables defined in a database structure xml file |
|
128 | - * |
|
129 | - * @param string $file the xml file describing the tables |
|
130 | - */ |
|
131 | - public function removeDBStructure($file) { |
|
132 | - $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $this->conn->getDatabasePlatform()); |
|
133 | - $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); |
|
134 | - $fromSchema = $schemaReader->loadSchemaFromFile($file, $toSchema); |
|
135 | - $toSchema = clone $fromSchema; |
|
136 | - foreach ($toSchema->getTables() as $table) { |
|
137 | - $toSchema->dropTable($table->getName()); |
|
138 | - } |
|
139 | - $comparator = new \Doctrine\DBAL\Schema\Comparator(); |
|
140 | - $schemaDiff = $comparator->compare($fromSchema, $toSchema); |
|
141 | - $this->executeSchemaChange($schemaDiff); |
|
142 | - } |
|
126 | + /** |
|
127 | + * remove all tables defined in a database structure xml file |
|
128 | + * |
|
129 | + * @param string $file the xml file describing the tables |
|
130 | + */ |
|
131 | + public function removeDBStructure($file) { |
|
132 | + $schemaReader = new MDB2SchemaReader(\OC::$server->getConfig(), $this->conn->getDatabasePlatform()); |
|
133 | + $toSchema = new Schema([], [], $this->conn->getSchemaManager()->createSchemaConfig()); |
|
134 | + $fromSchema = $schemaReader->loadSchemaFromFile($file, $toSchema); |
|
135 | + $toSchema = clone $fromSchema; |
|
136 | + foreach ($toSchema->getTables() as $table) { |
|
137 | + $toSchema->dropTable($table->getName()); |
|
138 | + } |
|
139 | + $comparator = new \Doctrine\DBAL\Schema\Comparator(); |
|
140 | + $schemaDiff = $comparator->compare($fromSchema, $toSchema); |
|
141 | + $this->executeSchemaChange($schemaDiff); |
|
142 | + } |
|
143 | 143 | |
144 | - /** |
|
145 | - * @param \Doctrine\DBAL\Schema\Schema|\Doctrine\DBAL\Schema\SchemaDiff $schema |
|
146 | - * @return bool |
|
147 | - */ |
|
148 | - private function executeSchemaChange($schema) { |
|
149 | - $this->conn->beginTransaction(); |
|
150 | - foreach ($schema->toSql($this->conn->getDatabasePlatform()) as $sql) { |
|
151 | - $this->conn->query($sql); |
|
152 | - } |
|
153 | - $this->conn->commit(); |
|
144 | + /** |
|
145 | + * @param \Doctrine\DBAL\Schema\Schema|\Doctrine\DBAL\Schema\SchemaDiff $schema |
|
146 | + * @return bool |
|
147 | + */ |
|
148 | + private function executeSchemaChange($schema) { |
|
149 | + $this->conn->beginTransaction(); |
|
150 | + foreach ($schema->toSql($this->conn->getDatabasePlatform()) as $sql) { |
|
151 | + $this->conn->query($sql); |
|
152 | + } |
|
153 | + $this->conn->commit(); |
|
154 | 154 | |
155 | - if ($this->conn->getDatabasePlatform() instanceof SqlitePlatform) { |
|
156 | - $this->conn->close(); |
|
157 | - $this->conn->connect(); |
|
158 | - } |
|
159 | - return true; |
|
160 | - } |
|
155 | + if ($this->conn->getDatabasePlatform() instanceof SqlitePlatform) { |
|
156 | + $this->conn->close(); |
|
157 | + $this->conn->connect(); |
|
158 | + } |
|
159 | + return true; |
|
160 | + } |
|
161 | 161 | } |