From 5e04254d40b427f43ac0cf05f684812c4008aaf5 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 18 Jul 2017 14:20:29 +0200 Subject: Install from migrations Signed-off-by: Joas Schilling --- lib/private/Setup.php | 3 +-- lib/private/Setup/AbstractDatabase.php | 5 +---- lib/private/Setup/MySQL.php | 6 +----- lib/private/Setup/OCI.php | 9 +-------- lib/private/Setup/PostgreSQL.php | 5 ----- lib/private/Setup/Sqlite.php | 1 - 6 files changed, 4 insertions(+), 25 deletions(-) (limited to 'lib') diff --git a/lib/private/Setup.php b/lib/private/Setup.php index 5cd3c84ce92..f5bfca604a9 100644 --- a/lib/private/Setup.php +++ b/lib/private/Setup.php @@ -282,8 +282,7 @@ class Setup { $class = self::$dbSetupClasses[$dbType]; /** @var \OC\Setup\AbstractDatabase $dbSetup */ - $dbSetup = new $class($l, 'db_structure.xml', $this->config, - $this->logger, $this->random); + $dbSetup = new $class($l, $this->config, $this->logger, $this->random); $error = array_merge($error, $dbSetup->validate($options)); // validate the data directory diff --git a/lib/private/Setup/AbstractDatabase.php b/lib/private/Setup/AbstractDatabase.php index 2fbec326a5d..0e0981e0771 100644 --- a/lib/private/Setup/AbstractDatabase.php +++ b/lib/private/Setup/AbstractDatabase.php @@ -38,8 +38,6 @@ abstract class AbstractDatabase { /** @var IL10N */ protected $trans; /** @var string */ - protected $dbDefinitionFile; - /** @var string */ protected $dbUser; /** @var string */ protected $dbPassword; @@ -58,9 +56,8 @@ abstract class AbstractDatabase { /** @var ISecureRandom */ protected $random; - public function __construct(IL10N $trans, $dbDefinitionFile, SystemConfig $config, ILogger $logger, ISecureRandom $random) { + public function __construct(IL10N $trans, SystemConfig $config, ILogger $logger, ISecureRandom $random) { $this->trans = $trans; - $this->dbDefinitionFile = $dbDefinitionFile; $this->config = $config; $this->logger = $logger; $this->random = $random; diff --git a/lib/private/Setup/MySQL.php b/lib/private/Setup/MySQL.php index b4ca02d2e84..d03e4858b61 100644 --- a/lib/private/Setup/MySQL.php +++ b/lib/private/Setup/MySQL.php @@ -51,11 +51,7 @@ class MySQL extends AbstractDatabase { //fill the database if needed $query='select count(*) from information_schema.tables where table_schema=? AND table_name = ?'; - $result = $connection->executeQuery($query, [$this->dbName, $this->tablePrefix.'users']); - $row = $result->fetch(); - if (!$row or $row['count(*)'] === '0') { - \OC_DB::createDbFromStructure($this->dbDefinitionFile); - } + $connection->executeQuery($query, [$this->dbName, $this->tablePrefix.'users']); } /** diff --git a/lib/private/Setup/OCI.php b/lib/private/Setup/OCI.php index 1bc6b08117f..3051987917c 100644 --- a/lib/private/Setup/OCI.php +++ b/lib/private/Setup/OCI.php @@ -165,14 +165,7 @@ class OCI extends AbstractDatabase { $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '
'; $this->logger->warning( $entry, ['app' => 'setup.oci']); } - $result = oci_execute($stmt); - - if($result) { - $row = oci_fetch_row($stmt); - } - if(!$result or $row[0]==0) { - \OC_DB::createDbFromStructure($this->dbDefinitionFile); - } + oci_execute($stmt); } /** diff --git a/lib/private/Setup/PostgreSQL.php b/lib/private/Setup/PostgreSQL.php index 8267b065142..dbcb94d6d7f 100644 --- a/lib/private/Setup/PostgreSQL.php +++ b/lib/private/Setup/PostgreSQL.php @@ -105,11 +105,6 @@ class PostgreSQL extends AbstractDatabase { throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'), $this->trans->t('You need to enter details of an existing account.')); } - - - if (!$tablesSetup) { - \OC_DB::createDbFromStructure($this->dbDefinitionFile); - } } private function createDatabase(IDBConnection $connection) { diff --git a/lib/private/Setup/Sqlite.php b/lib/private/Setup/Sqlite.php index 87c0b82682f..d95e70c8b0c 100644 --- a/lib/private/Setup/Sqlite.php +++ b/lib/private/Setup/Sqlite.php @@ -41,6 +41,5 @@ class Sqlite extends AbstractDatabase { } //in case of sqlite, we can always fill the database error_log("creating sqlite db"); - \OC_DB::createDbFromStructure($this->dbDefinitionFile); } } -- cgit v1.2.3 From 5308bd3378c45a44a5e367a7c9d1b1f2573e62f2 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Tue, 18 Jul 2017 19:36:40 +0200 Subject: Fix autoloader Signed-off-by: Morris Jobke --- lib/composer/composer/autoload_classmap.php | 2 ++ lib/composer/composer/autoload_static.php | 2 ++ 2 files changed, 4 insertions(+) (limited to 'lib') diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 749b3949f68..c43aa6bb4f1 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -426,6 +426,7 @@ return array( 'OC\\Core\\Command\\Db\\GenerateChangeScript' => $baseDir . '/core/Command/Db/GenerateChangeScript.php', 'OC\\Core\\Command\\Db\\Migrations\\ExecuteCommand' => $baseDir . '/core/Command/Db/Migrations/ExecuteCommand.php', 'OC\\Core\\Command\\Db\\Migrations\\GenerateCommand' => $baseDir . '/core/Command/Db/Migrations/GenerateCommand.php', + 'OC\\Core\\Command\\Db\\Migrations\\GenerateFromSchemaFileCommand' => $baseDir . '/core/Command/Db/Migrations/GenerateFromSchemaFileCommand.php', 'OC\\Core\\Command\\Db\\Migrations\\MigrateCommand' => $baseDir . '/core/Command/Db/Migrations/MigrateCommand.php', 'OC\\Core\\Command\\Db\\Migrations\\StatusCommand' => $baseDir . '/core/Command/Db/Migrations/StatusCommand.php', 'OC\\Core\\Command\\Encryption\\ChangeKeyStorageRoot' => $baseDir . '/core/Command/Encryption/ChangeKeyStorageRoot.php', @@ -489,6 +490,7 @@ return array( 'OC\\Core\\Controller\\UserController' => $baseDir . '/core/Controller/UserController.php', 'OC\\Core\\Middleware\\TwoFactorMiddleware' => $baseDir . '/core/Middleware/TwoFactorMiddleware.php', 'OC\\Core\\Migrations\\Version13000Date20170705121758' => $baseDir . '/core/Migrations/Version13000Date20170705121758.php', + 'OC\\Core\\Migrations\\Version13000Date20170718121200' => $baseDir . '/core/Migrations/Version13000Date20170718121200.php', 'OC\\DB\\Adapter' => $baseDir . '/lib/private/DB/Adapter.php', 'OC\\DB\\AdapterMySQL' => $baseDir . '/lib/private/DB/AdapterMySQL.php', 'OC\\DB\\AdapterOCI8' => $baseDir . '/lib/private/DB/AdapterOCI8.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index d92045f6c53..e8ba105744f 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -456,6 +456,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\Core\\Command\\Db\\GenerateChangeScript' => __DIR__ . '/../../..' . '/core/Command/Db/GenerateChangeScript.php', 'OC\\Core\\Command\\Db\\Migrations\\ExecuteCommand' => __DIR__ . '/../../..' . '/core/Command/Db/Migrations/ExecuteCommand.php', 'OC\\Core\\Command\\Db\\Migrations\\GenerateCommand' => __DIR__ . '/../../..' . '/core/Command/Db/Migrations/GenerateCommand.php', + 'OC\\Core\\Command\\Db\\Migrations\\GenerateFromSchemaFileCommand' => __DIR__ . '/../../..' . '/core/Command/Db/Migrations/GenerateFromSchemaFileCommand.php', 'OC\\Core\\Command\\Db\\Migrations\\MigrateCommand' => __DIR__ . '/../../..' . '/core/Command/Db/Migrations/MigrateCommand.php', 'OC\\Core\\Command\\Db\\Migrations\\StatusCommand' => __DIR__ . '/../../..' . '/core/Command/Db/Migrations/StatusCommand.php', 'OC\\Core\\Command\\Encryption\\ChangeKeyStorageRoot' => __DIR__ . '/../../..' . '/core/Command/Encryption/ChangeKeyStorageRoot.php', @@ -519,6 +520,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\Core\\Controller\\UserController' => __DIR__ . '/../../..' . '/core/Controller/UserController.php', 'OC\\Core\\Middleware\\TwoFactorMiddleware' => __DIR__ . '/../../..' . '/core/Middleware/TwoFactorMiddleware.php', 'OC\\Core\\Migrations\\Version13000Date20170705121758' => __DIR__ . '/../../..' . '/core/Migrations/Version13000Date20170705121758.php', + 'OC\\Core\\Migrations\\Version13000Date20170718121200' => __DIR__ . '/../../..' . '/core/Migrations/Version13000Date20170718121200.php', 'OC\\DB\\Adapter' => __DIR__ . '/../../..' . '/lib/private/DB/Adapter.php', 'OC\\DB\\AdapterMySQL' => __DIR__ . '/../../..' . '/lib/private/DB/AdapterMySQL.php', 'OC\\DB\\AdapterOCI8' => __DIR__ . '/../../..' . '/lib/private/DB/AdapterOCI8.php', -- cgit v1.2.3 From 0a0dbbdf15d30951d909924b6e3f3b2bcc093017 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 19 Jul 2017 12:14:42 +0200 Subject: Remove generate change script which doesnt work with migrations anymore Signed-off-by: Joas Schilling --- core/Command/Db/GenerateChangeScript.php | 89 ----------------------------- core/register_command.php | 1 - lib/composer/composer/autoload_classmap.php | 1 - lib/composer/composer/autoload_static.php | 1 - 4 files changed, 92 deletions(-) delete mode 100644 core/Command/Db/GenerateChangeScript.php (limited to 'lib') diff --git a/core/Command/Db/GenerateChangeScript.php b/core/Command/Db/GenerateChangeScript.php deleted file mode 100644 index bbe8d29958e..00000000000 --- a/core/Command/Db/GenerateChangeScript.php +++ /dev/null @@ -1,89 +0,0 @@ - - * @author Thomas Müller - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see - * - */ - -namespace OC\Core\Command\Db; - -use Stecman\Component\Symfony\Console\BashCompletion\Completion; -use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface; -use Stecman\Component\Symfony\Console\BashCompletion\Completion\ShellPathCompletion; -use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; - -class GenerateChangeScript extends Command implements CompletionAwareInterface { - protected function configure() { - $this - ->setName('db:generate-change-script') - ->setDescription('generates the change script from the current connected db to db_structure.xml') - ->addArgument( - 'schema-xml', - InputArgument::OPTIONAL, - 'the schema xml to be used as target schema', - \OC::$SERVERROOT . '/db_structure.xml' - ) - ; - } - - protected function execute(InputInterface $input, OutputInterface $output) { - - $file = $input->getArgument('schema-xml'); - - $schemaManager = new \OC\DB\MDB2SchemaManager(\OC::$server->getDatabaseConnection()); - - try { - $result = $schemaManager->updateDbFromStructure($file, true); - $output->writeln($result); - } catch (\Exception $e) { - $output->writeln('Failed to update database structure ('.$e.')'); - } - - } - - /** - * @param string $optionName - * @param CompletionContext $context - * @return string[] - */ - public function completeOptionValues($optionName, CompletionContext $context) { - return []; - } - - /** - * @param string $argumentName - * @param CompletionContext $context - * @return string[] - */ - public function completeArgumentValues($argumentName, CompletionContext $context) { - if ($argumentName === 'schema-xml') { - $helper = new ShellPathCompletion( - $this->getName(), - 'schema-xml', - Completion::TYPE_ARGUMENT - ); - return $helper->run(); - } - return []; - } -} diff --git a/core/register_command.php b/core/register_command.php index c65ff8d0067..fd693729a72 100644 --- a/core/register_command.php +++ b/core/register_command.php @@ -82,7 +82,6 @@ if (\OC::$server->getConfig()->getSystemValue('installed', false)) { $application->add(new OC\Core\Command\Config\System\GetConfig(\OC::$server->getSystemConfig())); $application->add(new OC\Core\Command\Config\System\SetConfig(\OC::$server->getSystemConfig())); - $application->add(new OC\Core\Command\Db\GenerateChangeScript()); $application->add(new OC\Core\Command\Db\ConvertType(\OC::$server->getConfig(), new \OC\DB\ConnectionFactory(\OC::$server->getSystemConfig()))); $application->add(new OC\Core\Command\Db\ConvertMysqlToMB4(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection(), \OC::$server->getURLGenerator(), \OC::$server->getLogger())); $application->add(new OC\Core\Command\Db\Migrations\StatusCommand(\OC::$server->getDatabaseConnection())); diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index c43aa6bb4f1..09231824703 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -423,7 +423,6 @@ return array( 'OC\\Core\\Command\\Config\\System\\SetConfig' => $baseDir . '/core/Command/Config/System/SetConfig.php', 'OC\\Core\\Command\\Db\\ConvertMysqlToMB4' => $baseDir . '/core/Command/Db/ConvertMysqlToMB4.php', 'OC\\Core\\Command\\Db\\ConvertType' => $baseDir . '/core/Command/Db/ConvertType.php', - 'OC\\Core\\Command\\Db\\GenerateChangeScript' => $baseDir . '/core/Command/Db/GenerateChangeScript.php', 'OC\\Core\\Command\\Db\\Migrations\\ExecuteCommand' => $baseDir . '/core/Command/Db/Migrations/ExecuteCommand.php', 'OC\\Core\\Command\\Db\\Migrations\\GenerateCommand' => $baseDir . '/core/Command/Db/Migrations/GenerateCommand.php', 'OC\\Core\\Command\\Db\\Migrations\\GenerateFromSchemaFileCommand' => $baseDir . '/core/Command/Db/Migrations/GenerateFromSchemaFileCommand.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index e8ba105744f..8f23eb53ecf 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -453,7 +453,6 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\Core\\Command\\Config\\System\\SetConfig' => __DIR__ . '/../../..' . '/core/Command/Config/System/SetConfig.php', 'OC\\Core\\Command\\Db\\ConvertMysqlToMB4' => __DIR__ . '/../../..' . '/core/Command/Db/ConvertMysqlToMB4.php', 'OC\\Core\\Command\\Db\\ConvertType' => __DIR__ . '/../../..' . '/core/Command/Db/ConvertType.php', - 'OC\\Core\\Command\\Db\\GenerateChangeScript' => __DIR__ . '/../../..' . '/core/Command/Db/GenerateChangeScript.php', 'OC\\Core\\Command\\Db\\Migrations\\ExecuteCommand' => __DIR__ . '/../../..' . '/core/Command/Db/Migrations/ExecuteCommand.php', 'OC\\Core\\Command\\Db\\Migrations\\GenerateCommand' => __DIR__ . '/../../..' . '/core/Command/Db/Migrations/GenerateCommand.php', 'OC\\Core\\Command\\Db\\Migrations\\GenerateFromSchemaFileCommand' => __DIR__ . '/../../..' . '/core/Command/Db/Migrations/GenerateFromSchemaFileCommand.php', -- cgit v1.2.3 From 10d7cbb71f89b1e5e39a894bda1b0701760224a0 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 19 Jul 2017 13:15:32 +0200 Subject: Use order by to avoid problems when chunking finds a same item again... Signed-off-by: Joas Schilling --- core/Command/Db/ConvertType.php | 39 ++++++++++++++++++++++++++++++--------- lib/private/DB/SchemaWrapper.php | 2 +- 2 files changed, 31 insertions(+), 10 deletions(-) (limited to 'lib') diff --git a/core/Command/Db/ConvertType.php b/core/Command/Db/ConvertType.php index 9b8284c9307..f05014936f5 100644 --- a/core/Command/Db/ConvertType.php +++ b/core/Command/Db/ConvertType.php @@ -28,6 +28,8 @@ namespace OC\Core\Command\Db; +use Doctrine\DBAL\DBALException; +use Doctrine\DBAL\Schema\Table; use OC\DB\MigrationService; use OCP\DB\QueryBuilder\IQueryBuilder; use \OCP\IConfig; @@ -271,24 +273,23 @@ class ConvertType extends Command implements CompletionAwareInterface { /** * @param Connection $fromDB * @param Connection $toDB - * @param $table + * @param Table $table * @param InputInterface $input * @param OutputInterface $output * @suppress SqlInjectionChecker */ - protected function copyTable(Connection $fromDB, Connection $toDB, $table, InputInterface $input, OutputInterface $output) { - if ($table === $toDB->getPrefix() . 'migrations') { + protected function copyTable(Connection $fromDB, Connection $toDB, Table $table, InputInterface $input, OutputInterface $output) { + if ($table->getName() === $toDB->getPrefix() . 'migrations') { $output->writeln('Skipping migrations table because it was already filled by running the migrations'); return; } - $chunkSize = $input->getOption('chunk-size'); $query = $fromDB->getQueryBuilder(); $query->automaticTablePrefix(false); $query->selectAlias($query->createFunction('COUNT(*)'), 'num_entries') - ->from($table); + ->from($table->getName()); $result = $query->execute(); $count = $result->fetchColumn(); $result->closeCursor(); @@ -306,12 +307,30 @@ class ConvertType extends Command implements CompletionAwareInterface { $query = $fromDB->getQueryBuilder(); $query->automaticTablePrefix(false); $query->select('*') - ->from($table) + ->from($table->getName()) ->setMaxResults($chunkSize); + try { + $orderColumns = $table->getPrimaryKeyColumns(); + } catch (DBALException $e) { + $orderColumns = []; + } + foreach ($table->getIndexes() as $index) { + if ($index->isUnique()) { + $orderColumns = array_merge($orderColumns, $index->getUnquotedColumns()); + } + } + $orderColumns = array_unique($orderColumns); + + if (!empty($orderColumns)) { + foreach ($orderColumns as $column) { + $query->addOrderBy($column); + } + } + $insertQuery = $toDB->getQueryBuilder(); $insertQuery->automaticTablePrefix(false); - $insertQuery->insert($table); + $insertQuery->insert($table->getName()); $parametersCreated = false; for ($chunk = 0; $chunk < $numChunks; $chunk++) { @@ -329,7 +348,7 @@ class ConvertType extends Command implements CompletionAwareInterface { } foreach ($row as $key => $value) { - $type = $this->getColumnType($table, $key); + $type = $this->getColumnType($table->getName(), $key); if ($type !== false) { $insertQuery->setParameter($key, $value, $type); } else { @@ -365,11 +384,13 @@ class ConvertType extends Command implements CompletionAwareInterface { protected function convertDB(Connection $fromDB, Connection $toDB, array $tables, InputInterface $input, OutputInterface $output) { $this->config->setSystemValue('maintenance', true); + $schema = $fromDB->createSchema(); + try { // copy table rows foreach($tables as $table) { $output->writeln($table); - $this->copyTable($fromDB, $toDB, $table, $input, $output); + $this->copyTable($fromDB, $toDB, $schema->getTable($table), $input, $output); } if ($input->getArgument('type') === 'pgsql') { $tools = new \OC\DB\PgSqlTools($this->config); diff --git a/lib/private/DB/SchemaWrapper.php b/lib/private/DB/SchemaWrapper.php index 0be45d8d3f7..d686d7e63e5 100644 --- a/lib/private/DB/SchemaWrapper.php +++ b/lib/private/DB/SchemaWrapper.php @@ -34,7 +34,7 @@ class SchemaWrapper { protected $schema; /** @var array */ - protected $tablesToDelete; + protected $tablesToDelete = []; /** * @param IDBConnection $connection -- cgit v1.2.3 From 41fca6c852c08e7e74ae2ceea8eadeda7a1348b1 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 19 Jul 2017 16:18:11 +0200 Subject: "Only variables must be passed by reference" Signed-off-by: Joas Schilling --- lib/private/DB/MigrationService.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/private/DB/MigrationService.php b/lib/private/DB/MigrationService.php index 92041b5e324..45f6aee83f6 100644 --- a/lib/private/DB/MigrationService.php +++ b/lib/private/DB/MigrationService.php @@ -284,7 +284,8 @@ class MigrationService { case 'latest': $this->ensureMigrationsAreLoaded(); - return @end($this->getAvailableVersions()); + $migrations = $this->getAvailableVersions(); + return @end($migrations); } return '0'; } @@ -316,7 +317,8 @@ class MigrationService { if (count($m) === 0) { return '0'; } - return @end(array_values($m)); + $migrations = array_values($m); + return @end($migrations); } /** -- cgit v1.2.3