33
33
34
34
use Doctrine \DBAL \DBALException ;
35
35
use Doctrine \DBAL \Schema \Comparator ;
36
- use Doctrine \DBAL \Schema \Index ;
37
36
use Doctrine \DBAL \Schema \Schema ;
38
- use Doctrine \DBAL \Schema \SchemaConfig ;
39
- use Doctrine \DBAL \Schema \Table ;
40
37
use Doctrine \DBAL \Types \StringType ;
41
38
use Doctrine \DBAL \Types \Type ;
42
39
use OCP \IConfig ;
43
- use OCP \Security \ISecureRandom ;
44
40
use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
45
41
use Symfony \Component \EventDispatcher \GenericEvent ;
46
42
@@ -49,9 +45,6 @@ class Migrator {
49
45
/** @var \Doctrine\DBAL\Connection */
50
46
protected $ connection ;
51
47
52
- /** @var ISecureRandom */
53
- private $ random ;
54
-
55
48
/** @var IConfig */
56
49
protected $ config ;
57
50
@@ -63,16 +56,13 @@ class Migrator {
63
56
64
57
/**
65
58
* @param \Doctrine\DBAL\Connection $connection
66
- * @param ISecureRandom $random
67
59
* @param IConfig $config
68
60
* @param EventDispatcherInterface $dispatcher
69
61
*/
70
62
public function __construct (\Doctrine \DBAL \Connection $ connection ,
71
- ISecureRandom $ random ,
72
63
IConfig $ config ,
73
64
EventDispatcherInterface $ dispatcher = null ) {
74
65
$ this ->connection = $ connection ;
75
- $ this ->random = $ random ;
76
66
$ this ->config = $ config ;
77
67
$ this ->dispatcher = $ dispatcher ;
78
68
}
@@ -101,101 +91,6 @@ public function generateChangeScript(Schema $targetSchema) {
101
91
return $ script ;
102
92
}
103
93
104
- /**
105
- * @param Schema $targetSchema
106
- * @throws \OC\DB\MigrationException
107
- */
108
- public function checkMigrate (Schema $ targetSchema ) {
109
- $ this ->noEmit = true ;
110
- /**@var \Doctrine\DBAL\Schema\Table[] $tables */
111
- $ tables = $ targetSchema ->getTables ();
112
- $ filterExpression = $ this ->getFilterExpression ();
113
- $ this ->connection ->getConfiguration ()->
114
- setFilterSchemaAssetsExpression ($ filterExpression );
115
- $ existingTables = $ this ->connection ->getSchemaManager ()->listTableNames ();
116
-
117
- $ step = 0 ;
118
- foreach ($ tables as $ table ) {
119
- if (strpos ($ table ->getName (), '. ' )) {
120
- list (, $ tableName ) = explode ('. ' , $ table ->getName ());
121
- } else {
122
- $ tableName = $ table ->getName ();
123
- }
124
- $ this ->emitCheckStep ($ tableName , $ step ++, count ($ tables ));
125
- // don't need to check for new tables
126
- if (array_search ($ tableName , $ existingTables ) !== false ) {
127
- $ this ->checkTableMigrate ($ table );
128
- }
129
- }
130
- }
131
-
132
- /**
133
- * Create a unique name for the temporary table
134
- *
135
- * @param string $name
136
- * @return string
137
- */
138
- protected function generateTemporaryTableName ($ name ) {
139
- return $ this ->config ->getSystemValue ('dbtableprefix ' , 'oc_ ' ) . $ name . '_ ' . $ this ->random ->generate (13 , ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS );
140
- }
141
-
142
- /**
143
- * Check the migration of a table on a copy so we can detect errors before messing with the real table
144
- *
145
- * @param \Doctrine\DBAL\Schema\Table $table
146
- * @throws \OC\DB\MigrationException
147
- */
148
- protected function checkTableMigrate (Table $ table ) {
149
- $ name = $ table ->getName ();
150
- $ tmpName = $ this ->generateTemporaryTableName ($ name );
151
-
152
- $ this ->copyTable ($ name , $ tmpName );
153
-
154
- //create the migration schema for the temporary table
155
- $ tmpTable = $ this ->renameTableSchema ($ table , $ tmpName );
156
- $ schemaConfig = new SchemaConfig ();
157
- $ schemaConfig ->setName ($ this ->connection ->getDatabase ());
158
- $ schema = new Schema ([$ tmpTable ], [], $ schemaConfig );
159
-
160
- try {
161
- $ this ->applySchema ($ schema );
162
- $ this ->dropTable ($ tmpName );
163
- } catch (DBALException $ e ) {
164
- // pgsql needs to commit it's failed transaction before doing anything else
165
- if ($ this ->connection ->isTransactionActive ()) {
166
- $ this ->connection ->commit ();
167
- }
168
- $ this ->dropTable ($ tmpName );
169
- throw new MigrationException ($ table ->getName (), $ e ->getMessage ());
170
- }
171
- }
172
-
173
- /**
174
- * @param \Doctrine\DBAL\Schema\Table $table
175
- * @param string $newName
176
- * @return \Doctrine\DBAL\Schema\Table
177
- */
178
- protected function renameTableSchema (Table $ table , $ newName ) {
179
- /**
180
- * @var \Doctrine\DBAL\Schema\Index[] $indexes
181
- */
182
- $ indexes = $ table ->getIndexes ();
183
- $ newIndexes = [];
184
- foreach ($ indexes as $ index ) {
185
- if ($ index ->isPrimary ()) {
186
- // do not rename primary key
187
- $ indexName = $ index ->getName ();
188
- } else {
189
- // avoid conflicts in index names
190
- $ indexName = $ this ->config ->getSystemValue ('dbtableprefix ' , 'oc_ ' ) . $ this ->random ->generate (13 , ISecureRandom::CHAR_LOWER );
191
- }
192
- $ newIndexes [] = new Index ($ indexName , $ index ->getColumns (), $ index ->isUnique (), $ index ->isPrimary ());
193
- }
194
-
195
- // foreign keys are not supported so we just set it to an empty array
196
- return new Table ($ newName , $ table ->getColumns (), $ newIndexes , [], 0 , $ table ->getOptions ());
197
- }
198
-
199
94
public function createSchema () {
200
95
$ filterExpression = $ this ->getFilterExpression ();
201
96
$ this ->connection ->getConfiguration ()->setFilterSchemaAssetsExpression ($ filterExpression );
@@ -263,25 +158,6 @@ protected function applySchema(Schema $targetSchema, \Doctrine\DBAL\Connection $
263
158
$ connection ->commit ();
264
159
}
265
160
266
- /**
267
- * @param string $sourceName
268
- * @param string $targetName
269
- */
270
- protected function copyTable ($ sourceName , $ targetName ) {
271
- $ quotedSource = $ this ->connection ->quoteIdentifier ($ sourceName );
272
- $ quotedTarget = $ this ->connection ->quoteIdentifier ($ targetName );
273
-
274
- $ this ->connection ->exec ('CREATE TABLE ' . $ quotedTarget . ' (LIKE ' . $ quotedSource . ') ' );
275
- $ this ->connection ->exec ('INSERT INTO ' . $ quotedTarget . ' SELECT * FROM ' . $ quotedSource );
276
- }
277
-
278
- /**
279
- * @param string $name
280
- */
281
- protected function dropTable ($ name ) {
282
- $ this ->connection ->exec ('DROP TABLE ' . $ this ->connection ->quoteIdentifier ($ name ));
283
- }
284
-
285
161
/**
286
162
* @param $statement
287
163
* @return string
@@ -306,11 +182,4 @@ protected function emit($sql, $step, $max) {
306
182
}
307
183
$ this ->dispatcher ->dispatch ('\OC\DB\Migrator::executeSql ' , new GenericEvent ($ sql , [$ step + 1 , $ max ]));
308
184
}
309
-
310
- private function emitCheckStep ($ tableName , $ step , $ max ) {
311
- if (is_null ($ this ->dispatcher )) {
312
- return ;
313
- }
314
- $ this ->dispatcher ->dispatch ('\OC\DB\Migrator::checkTable ' , new GenericEvent ($ tableName , [$ step + 1 , $ max ]));
315
- }
316
185
}
0 commit comments