3535use Doctrine \DBAL \Platforms \MySQLPlatform ;
3636use Doctrine \DBAL \Schema \AbstractAsset ;
3737use Doctrine \DBAL \Schema \Comparator ;
38- use Doctrine \DBAL \Schema \Index ;
3938use Doctrine \DBAL \Schema \Schema ;
40- use Doctrine \DBAL \Schema \SchemaConfig ;
41- use Doctrine \DBAL \Schema \Table ;
4239use Doctrine \DBAL \Types \StringType ;
4340use Doctrine \DBAL \Types \Type ;
4441use OCP \IConfig ;
45- use OCP \Security \ISecureRandom ;
4642use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
4743use Symfony \Component \EventDispatcher \GenericEvent ;
4844use function preg_match ;
@@ -52,9 +48,6 @@ class Migrator {
5248 /** @var \Doctrine\DBAL\Connection */
5349 protected $ connection ;
5450
55- /** @var ISecureRandom */
56- private $ random ;
57-
5851 /** @var IConfig */
5952 protected $ config ;
6053
@@ -66,16 +59,13 @@ class Migrator {
6659
6760 /**
6861 * @param \Doctrine\DBAL\Connection $connection
69- * @param ISecureRandom $random
7062 * @param IConfig $config
7163 * @param EventDispatcherInterface $dispatcher
7264 */
7365 public function __construct (\Doctrine \DBAL \Connection $ connection ,
74- ISecureRandom $ random ,
7566 IConfig $ config ,
7667 EventDispatcherInterface $ dispatcher = null ) {
7768 $ this ->connection = $ connection ;
78- $ this ->random = $ random ;
7969 $ this ->config = $ config ;
8070 $ this ->dispatcher = $ dispatcher ;
8171 }
@@ -106,73 +96,6 @@ public function generateChangeScript(Schema $targetSchema) {
10696 return $ script ;
10797 }
10898
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-
17699 /**
177100 * @throws Exception
178101 */
@@ -260,25 +183,6 @@ protected function applySchema(Schema $targetSchema, \Doctrine\DBAL\Connection $
260183 }
261184 }
262185
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-
282186 /**
283187 * @param $statement
284188 * @return string
@@ -303,11 +207,4 @@ protected function emit($sql, $step, $max) {
303207 }
304208 $ this ->dispatcher ->dispatch ('\OC\DB\Migrator::executeSql ' , new GenericEvent ($ sql , [$ step + 1 , $ max ]));
305209 }
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- }
313210}
0 commit comments