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