diff options
author | Bart Visscher <bartv@thisnet.nl> | 2014-02-17 18:02:58 +0100 |
---|---|---|
committer | Bart Visscher <bartv@thisnet.nl> | 2014-02-17 18:02:58 +0100 |
commit | bcb78e48b283def05d21defaf554f5ce7c578c56 (patch) | |
tree | 3837968bb1d1c6e6e82eefd9d138d5cc8f06fc1b /core/command | |
parent | ae525d1f125483dc14858938c5056f18d63f831e (diff) | |
download | nextcloud-server-bcb78e48b283def05d21defaf554f5ce7c578c56.tar.gz nextcloud-server-bcb78e48b283def05d21defaf554f5ce7c578c56.zip |
Split execute function into multiple functions
Diffstat (limited to 'core/command')
-rw-r--r-- | core/command/db/converttype.php | 158 |
1 files changed, 89 insertions, 69 deletions
diff --git a/core/command/db/converttype.php b/core/command/db/converttype.php index 5f59a6be828..370ace7fd0c 100644 --- a/core/command/db/converttype.php +++ b/core/command/db/converttype.php @@ -85,6 +85,54 @@ class ConvertType extends Command { $fromDB = \OC_DB::getConnection(); // connect 'to' database + $toDB = $this->getToDBConnection($input, $output); + + // Clearing schema in new database + if ($input->getOption('clear-schema')) { + $schemaManager = $toDB->getSchemaManager(); + $toTables = $schemaManager->listTableNames(); + if (!empty($toTables)) { + $output->writeln('Clearing schema in new database'); + } + foreach($toTables as $table) { + $schemaManager->dropTable($table); + } + } + + // create tables in new database + $output->writeln('Creating schema in new database'); + $schemaManager = new \OC\DB\MDB2SchemaManager($toDB); + $schemaManager->createDbFromStructure(\OC::$SERVERROOT.'/db_structure.xml'); + $apps = \OC_App::getEnabledApps(); + foreach($apps as $app) { + if(file_exists(\OC_App::getAppPath($app).'/appinfo/database.xml')) { + $schemaManager->createDbFromStructure(\OC_App::getAppPath($app).'/appinfo/database.xml'); + } + } + + // get tables from 'to' database + $toTables = $this->getTables($toDB); + // get tables from 'from' database + $fromTables = $this->getTables($fromDB); + // warn/fail if there are more tables in 'from' database + $tables = array_diff($fromTables, $toTables); + if (!empty($tables)) { + $output->writeln('<error>The following tables do NOT exist any more: '.join(', ', $tables).'</error>'); + $dialog = $this->getHelperSet()->get('dialog'); + if (!$dialog->askConfirmation( + $output, + '<question>Continue with the convertion?</question>', + false + )) { + return; + } + } + // enable maintenance mode to prevent changes + $tables = array_intersect($toTables, $fromTables); + $this->convertDB($fromDB, $toDB, $tables, $input, $output); + } + + private function getToDBConnection($input, $output) { $type = $input->getArgument('type'); $username = $input->getArgument('username'); $hostname = $input->getArgument('hostname'); @@ -103,6 +151,7 @@ class ConvertType extends Command { 'What is the database password?', false ); + $input->setOption('password', $password); } $connectionParams = array( 'driver' => self::$type2driver[$type], @@ -124,59 +173,45 @@ class ConvertType extends Command { break; } - $toDB = \Doctrine\DBAL\DriverManager::getConnection($connectionParams); + return \Doctrine\DBAL\DriverManager::getConnection($connectionParams); + } - // Clearing schema in new database - if ($input->getOption('clear-schema')) { - $schemaManager = $toDB->getSchemaManager(); - $toTables = $schemaManager->listTableNames(); - if (!empty($toTables)) { - $output->writeln('Clearing schema in new database'); - } - foreach($toTables as $table) { - $schemaManager->dropTable($table); - } - } + private function getTables($db) { + $schemaManager = $db->getSchemaManager(); + return $schemaManager->listTableNames(); + } - // create tables in new database - $output->writeln('Creating schema in new database'); - $schemaManager = new \OC\DB\MDB2SchemaManager($toDB); - $schemaManager->createDbFromStructure(\OC::$SERVERROOT.'/db_structure.xml'); - $apps = \OC_App::getEnabledApps(); - foreach($apps as $app) { - if(file_exists(\OC_App::getAppPath($app).'/appinfo/database.xml')) { - $schemaManager->createDbFromStructure(\OC_App::getAppPath($app).'/appinfo/database.xml'); + private function copyTable($fromDB, $toDB, $table, $output) { + $progress = $this->getHelperSet()->get('progress'); + $query = 'SELECT COUNT(*) FROM '.$table; + $count = $fromDB->fetchColumn($query); + $query = 'SELECT * FROM '.$table; + $statement = $fromDB->executeQuery($query); + $progress->start($output, $count); + $progress->setRedrawFrequency($count > 100 ? 5 : 1); + while($row = $statement->fetch()) { + $progress->advance(); + $data = array(); + foreach ($row as $columnName => $value) { + $data[$toDB->quoteIdentifier($columnName)] = $value; } + $toDB->insert($table, $data); } + $progress->finish(); + } - // get tables from 'to' database - $toTables = $this->getTables($toDB); - // get tables from 'from' database - $fromTables = $this->getTables($fromDB); - // warn/fail if there are more tables in 'from' database - $tables = array_diff($fromTables, $toTables); - if (!empty($tables)) { - $output->writeln('<error>The following tables do NOT exist any more: '.join(', ', $tables).'</error>'); - $dialog = $this->getHelperSet()->get('dialog'); - if (!$dialog->askConfirmation( - $output, - '<question>Continue with the convertion?</question>', - false - )) { - return; - } - } - // enable maintenance mode to prevent changes + private function convertDB($fromDB, $toDB, $tables, $input, $output) { $this->config->setValue('maintenance', true); + $type = $input->getArgument('type'); try { // copy table rows - $tables = array_intersect($toTables, $fromTables); foreach($tables as $table) { $output->writeln($table); $this->copyTable($fromDB, $toDB, $table, $output); } if ($type == 'pgsql') { $sequences = $toDB->getSchemaManager()->listSequences(); + $dbname = $input->getArgument('database'); foreach($sequences as $sequence) { $info = $toDB->fetchAssoc('SELECT table_schema, table_name, column_name ' .'FROM information_schema.columns ' @@ -188,15 +223,7 @@ class ConvertType extends Command { } } // save new database config - $dbhost = $hostname; - if ($input->getOption('port')) { - $dbhost = $hostname.':'.$input->getOption('port'); - } - $this->config->setValue('dbtype', $type); - $this->config->setValue('dbname', $dbname); - $this->config->setValue('dbhost', $dbhost); - $this->config->setValue('dbuser', $username); - $this->config->setValue('dbpassword', $password); + $this->saveDBInfo($input); } catch(\Exception $e) { $this->config->setValue('maintenance', false); throw $e; @@ -204,27 +231,20 @@ class ConvertType extends Command { $this->config->setValue('maintenance', false); } - private function getTables($db) { - $schemaManager = $db->getSchemaManager(); - return $schemaManager->listTableNames(); - } - - private function copyTable($fromDB, $toDB, $table, $output) { - $progress = $this->getHelperSet()->get('progress'); - $query = 'SELECT COUNT(*) FROM '.$table; - $count = $fromDB->fetchColumn($query); - $query = 'SELECT * FROM '.$table; - $statement = $fromDB->executeQuery($query); - $progress->start($output, $count); - $progress->setRedrawFrequency($count > 100 ? 5 : 1); - while($row = $statement->fetch()) { - $progress->advance(); - $data = array(); - foreach ($row as $columnName => $value) { - $data[$toDB->quoteIdentifier($columnName)] = $value; - } - $toDB->insert($table, $data); + private function saveDBInfo($input) { + $type = $input->getArgument('type'); + $username = $input->getArgument('username'); + $dbhost = $input->getArgument('hostname'); + $dbname = $input->getArgument('database'); + $password = $input->getOption('password'); + if ($input->getOption('port')) { + $dbhost .= ':'.$input->getOption('port'); } - $progress->finish(); + + $this->config->setValue('dbtype', $type); + $this->config->setValue('dbname', $dbname); + $this->config->setValue('dbhost', $dbhost); + $this->config->setValue('dbuser', $username); + $this->config->setValue('dbpassword', $password); } } |