diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/Command/Db/ConvertType.php | 90 | ||||
-rw-r--r-- | core/Command/Db/GenerateChangeScript.php | 89 | ||||
-rw-r--r-- | core/Command/Db/Migrations/GenerateCommand.php | 26 | ||||
-rw-r--r-- | core/Command/Db/Migrations/GenerateFromSchemaFileCommand.php | 199 | ||||
-rw-r--r-- | core/Migrations/Version13000Date20170718121200.php | 897 | ||||
-rw-r--r-- | core/register_command.php | 2 |
6 files changed, 1179 insertions, 124 deletions
diff --git a/core/Command/Db/ConvertType.php b/core/Command/Db/ConvertType.php index 16864e57dbc..a7839522934 100644 --- a/core/Command/Db/ConvertType.php +++ b/core/Command/Db/ConvertType.php @@ -28,6 +28,10 @@ namespace OC\Core\Command\Db; +use Doctrine\DBAL\DBALException; +use Doctrine\DBAL\Schema\Table; +use Doctrine\DBAL\Types\Type; +use OC\DB\MigrationService; use OCP\DB\QueryBuilder\IQueryBuilder; use \OCP\IConfig; use OC\DB\Connection; @@ -190,7 +194,7 @@ class ConvertType extends Command implements CompletionAwareInterface { $this->clearSchema($toDB, $input, $output); } - $this->createSchema($toDB, $input, $output); + $this->createSchema($fromDB, $toDB, $input, $output); $toTables = $this->getTables($toDB); $fromTables = $this->getTables($fromDB); @@ -217,27 +221,43 @@ class ConvertType extends Command implements CompletionAwareInterface { $this->convertDB($fromDB, $toDB, $intersectingTables, $input, $output); } - protected function createSchema(Connection $toDB, InputInterface $input, OutputInterface $output) { + protected function createSchema(Connection $fromDB, Connection $toDB, InputInterface $input, OutputInterface $output) { $output->writeln('<info>Creating schema in new database</info>'); + + $fromMS = new MigrationService('core', $fromDB); + $currentMigration = $fromMS->getMigration('current'); + if ($currentMigration !== '0') { + $toMS = new MigrationService('core', $toDB); + $toMS->migrate($currentMigration); + } + $schemaManager = new \OC\DB\MDB2SchemaManager($toDB); - $schemaManager->createDbFromStructure(\OC::$SERVERROOT.'/db_structure.xml'); $apps = $input->getOption('all-apps') ? \OC_App::getAllApps() : \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'); + } else { + // Make sure autoloading works... + \OC_App::loadApp($app); + $fromMS = new MigrationService($app, $fromDB); + $currentMigration = $fromMS->getMigration('current'); + if ($currentMigration !== '0') { + $toMS = new MigrationService($app, $toDB); + $toMS->migrate($currentMigration); + } } } } protected function getToDBConnection(InputInterface $input, OutputInterface $output) { $type = $input->getArgument('type'); - $connectionParams = array( + $connectionParams = $this->connectionFactory->createConnectionParams(); + $connectionParams = array_merge($connectionParams, [ 'host' => $input->getArgument('hostname'), 'user' => $input->getArgument('username'), 'password' => $input->getOption('password'), 'dbname' => $input->getArgument('database'), - 'tablePrefix' => $this->config->getSystemValue('dbtableprefix', 'oc_'), - ); + ]); if ($input->getOption('port')) { $connectionParams['port'] = $input->getOption('port'); } @@ -264,18 +284,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) { + protected function copyTable(Connection $fromDB, Connection $toDB, Table $table, InputInterface $input, OutputInterface $output) { + if ($table->getName() === $toDB->getPrefix() . 'migrations') { + $output->writeln('<comment>Skipping migrations table because it was already filled by running the migrations</comment>'); + 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(); @@ -293,12 +318,25 @@ 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->getColumns() as $column) { + $orderColumns[] = $column->getName(); + } + } + + 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++) { @@ -330,33 +368,35 @@ class ConvertType extends Command implements CompletionAwareInterface { $progress->finish(); } - protected function getColumnType($table, $column) { - if (isset($this->columnTypes[$table][$column])) { - return $this->columnTypes[$table][$column]; + protected function getColumnType(Table $table, $columnName) { + $tableName = $table->getName(); + if (isset($this->columnTypes[$tableName][$columnName])) { + return $this->columnTypes[$tableName][$columnName]; } - $prefix = $this->config->getSystemValue('dbtableprefix', 'oc_'); - $this->columnTypes[$table][$column] = false; + $type = $table->getColumn($columnName)->getType()->getName(); - if ($table === $prefix . 'cards' && $column === 'carddata') { - $this->columnTypes[$table][$column] = IQueryBuilder::PARAM_LOB; - } else if ($column === 'calendardata') { - if ($table === $prefix . 'calendarobjects' || - $table === $prefix . 'schedulingobjects') { - $this->columnTypes[$table][$column] = IQueryBuilder::PARAM_LOB; - } + switch ($type) { + case Type::BLOB: + case Type::TEXT: + $this->columnTypes[$tableName][$columnName] = IQueryBuilder::PARAM_LOB; + break; + default: + $this->columnTypes[$tableName][$columnName] = false; } - return $this->columnTypes[$table][$column]; + return $this->columnTypes[$tableName][$columnName]; } 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/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 @@ -<?php -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Morris Jobke <hey@morrisjobke.de> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @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 <http://www.gnu.org/licenses/> - * - */ - -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/Command/Db/Migrations/GenerateCommand.php b/core/Command/Db/Migrations/GenerateCommand.php index e6c38d06e5d..4db82d6eff4 100644 --- a/core/Command/Db/Migrations/GenerateCommand.php +++ b/core/Command/Db/Migrations/GenerateCommand.php @@ -36,9 +36,9 @@ use Symfony\Component\Console\Output\OutputInterface; class GenerateCommand extends Command { - private static $_templateSimple = + protected static $_templateSimple = '<?php -namespace <namespace>; +namespace {{<namespace}}; use Doctrine\DBAL\Schema\Schema; use OCP\Migration\SimpleMigrationStep; @@ -47,7 +47,7 @@ use OCP\Migration\IOutput; /** * Auto-generated migration step: Please modify to your needs! */ -class <classname> extends SimpleMigrationStep { +class {{classname}} extends SimpleMigrationStep { /** * @param IOutput $output @@ -66,7 +66,7 @@ class <classname> extends SimpleMigrationStep { * @since 13.0.0 */ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { - return null; +{{schemabody}} } /** @@ -81,7 +81,7 @@ class <classname> extends SimpleMigrationStep { '; /** @var IDBConnection */ - private $connection; + protected $connection; /** * @param IDBConnection $connection @@ -123,16 +123,24 @@ class <classname> extends SimpleMigrationStep { /** * @param MigrationService $ms * @param string $className + * @param string $schemaBody * @return string */ - private function generateMigration(MigrationService $ms, $className) { + protected function generateMigration(MigrationService $ms, $className, $schemaBody = '') { + if ($schemaBody === '') { + $schemaBody = "\t\t" . 'return null;'; + } + + $placeHolders = [ - '<namespace>', - '<classname>', + '{{namespace}}', + '{{classname}}', + '{{schemabody}}', ]; $replacements = [ $ms->getMigrationsNamespace(), $className, + $schemaBody, ]; $code = str_replace($placeHolders, $replacements, self::$_templateSimple); $dir = $ms->getMigrationsDirectory(); @@ -147,7 +155,7 @@ class <classname> extends SimpleMigrationStep { return $path; } - private function ensureMigrationDirExists($directory) { + protected function ensureMigrationDirExists($directory) { if (file_exists($directory) && is_dir($directory)) { return; } diff --git a/core/Command/Db/Migrations/GenerateFromSchemaFileCommand.php b/core/Command/Db/Migrations/GenerateFromSchemaFileCommand.php new file mode 100644 index 00000000000..54a4d95738e --- /dev/null +++ b/core/Command/Db/Migrations/GenerateFromSchemaFileCommand.php @@ -0,0 +1,199 @@ +<?php +/** + * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com> + * + * @author Joas Schilling <coding@schilljs.com> + * @author Julius Haertl <jus@bitgrid.net> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Core\Command\Db\Migrations; + + +use Doctrine\DBAL\Schema\Schema; +use OC\DB\MDB2SchemaReader; +use OC\DB\MigrationService; +use OC\Migration\ConsoleOutput; +use OCP\App\IAppManager; +use OCP\IConfig; +use OCP\IDBConnection; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class GenerateFromSchemaFileCommand extends GenerateCommand { + + /** @var IConfig */ + protected $config; + + /** @var IAppManager */ + protected $appManager; + + public function __construct(IConfig $config, IAppManager $appManager, IDBConnection $connection) { + parent::__construct($connection); + $this->config = $config; + $this->appManager = $appManager; + } + + + protected function configure() { + parent::configure(); + + $this->setName('migrations:generate-from-schema'); + } + + public function execute(InputInterface $input, OutputInterface $output) { + $appName = $input->getArgument('app'); + $version = $input->getArgument('version'); + + if (!preg_match('/^\d{1,16}$/',$version)) { + $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); + return 1; + } + + $schemaFile = $this->appManager->getAppPath($appName) . '/appinfo/database.xml'; + if (!file_exists($schemaFile)) { + $output->writeln('<error>App ' . $appName . ' does not have a database.xml file</error>'); + return 2; + } + + $reader = new MDB2SchemaReader($this->config, $this->connection->getDatabasePlatform()); + $schema = new Schema(); + $reader->loadSchemaFromFile($schemaFile, $schema); + + $schemaBody = $this->schemaToMigration($schema); + + $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); + + $date = date('YmdHis'); + $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date, $schemaBody); + + $output->writeln("New migration class has been generated to <info>$path</info>"); + return 0; + } + + /** + * @param Schema $schema + * @return string + */ + protected function schemaToMigration(Schema $schema) { + $content = <<<'EOT' + /** @var Schema $schema */ + $schema = $schemaClosure(); + +EOT; + + foreach ($schema->getTables() as $table) { + $content .= str_replace('{{table-name}}', substr($table->getName(), 3), <<<'EOT' + + if (!$schema->hasTable('{{table-name}}')) { + $table = $schema->createTable('{{table-name}}'); + +EOT + ); + + foreach ($table->getColumns() as $column) { + $content .= str_replace(['{{name}}', '{{type}}'], [$column->getName(), $column->getType()->getName()], <<<'EOT' + $table->addColumn('{{name}}', '{{type}}', [ + +EOT + ); + if ($column->getAutoincrement()) { + $content .= <<<'EOT' + 'autoincrement' => true, + +EOT; + } + $content .= str_replace('{{notnull}}', $column->getNotnull() ? 'true' : 'false', <<<'EOT' + 'notnull' => {{notnull}}, + +EOT + ); + if ($column->getLength() !== null) { + $content .= str_replace('{{length}}', $column->getLength(), <<<'EOT' + 'length' => {{length}}, + +EOT + ); + } + $default = $column->getDefault(); + if ($default !== null) { + $default = is_numeric($default) ? $default : "'$default'"; + $content .= str_replace('{{default}}', $default, <<<'EOT' + 'default' => {{default}}, + +EOT + ); + } + $content .= <<<'EOT' + ]); + +EOT; + } + + $content .= <<<'EOT' + +EOT; + + $primaryKey = $table->getPrimaryKey(); + if ($primaryKey !== null) { + $content .= str_replace('{{columns}}', implode('\', \'', $primaryKey->getUnquotedColumns()), <<<'EOT' + $table->setPrimaryKey(['{{columns}}']); + +EOT + ); + } + + foreach ($table->getIndexes() as $index) { + if ($index->isPrimary()) { + continue; + } + + if ($index->isUnique()) { + $content .= str_replace( + ['{{columns}}', '{{name}}'], + [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], + <<<'EOT' + $table->addUniqueIndex(['{{columns}}'], '{{name}}'); + +EOT + ); + } else { + $content .= str_replace( + ['{{columns}}', '{{name}}'], + [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], + <<<'EOT' + $table->addIndex(['{{columns}}'], '{{name}}'); + +EOT + ); + } + } + + $content .= <<<'EOT' + } + +EOT; + } + + $content .= <<<'EOT' + return $schema; +EOT; + + return $content; + } +} diff --git a/core/Migrations/Version13000Date20170718121200.php b/core/Migrations/Version13000Date20170718121200.php new file mode 100644 index 00000000000..81743bd203f --- /dev/null +++ b/core/Migrations/Version13000Date20170718121200.php @@ -0,0 +1,897 @@ +<?php +namespace OC\Core\Migrations; + +use Doctrine\DBAL\Schema\Schema; +use OCP\Migration\SimpleMigrationStep; +use OCP\Migration\IOutput; + +/** + * Auto-generated migration step: Please modify to your needs! + */ +class Version13000Date20170718121200 extends SimpleMigrationStep { + + /** + * @param IOutput $output + * @param \Closure $schemaClosure The `\Closure` returns a `Schema` + * @param array $options + * @since 13.0.0 + */ + public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { + } + + /** + * @param IOutput $output + * @param \Closure $schemaClosure The `\Closure` returns a `Schema` + * @param array $options + * @return null|Schema + * @since 13.0.0 + */ + public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { + /** @var Schema $schema */ + $schema = $schemaClosure(); + + if (!$schema->hasTable('appconfig')) { + $table = $schema->createTable('appconfig'); + $table->addColumn('appid', 'string', [ + 'notnull' => true, + 'length' => 32, + 'default' => '', + ]); + $table->addColumn('configkey', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('configvalue', 'text', [ + 'notnull' => false, + ]); + $table->setPrimaryKey(['appid', 'configkey']); + $table->addIndex(['configkey'], 'appconfig_config_key_index'); + $table->addIndex(['appid'], 'appconfig_appid_key'); + } + + if (!$schema->hasTable('storages')) { + $table = $schema->createTable('storages'); + $table->addColumn('id', 'string', [ + 'notnull' => false, + 'length' => 64, + ]); + $table->addColumn('numeric_id', 'integer', [ + 'autoincrement' => true, + 'notnull' => true, + 'length' => 4, + ]); + $table->addColumn('available', 'integer', [ + 'notnull' => true, + 'default' => 1, + ]); + $table->addColumn('last_checked', 'integer', [ + 'notnull' => false, + ]); + $table->setPrimaryKey(['numeric_id']); + $table->addUniqueIndex(['id'], 'storages_id_index'); + } + + if (!$schema->hasTable('mounts')) { + $table = $schema->createTable('mounts'); + $table->addColumn('id', 'integer', [ + 'autoincrement' => true, + 'notnull' => true, + 'length' => 4, + ]); + $table->addColumn('storage_id', 'integer', [ + 'notnull' => true, + ]); + $table->addColumn('root_id', 'integer', [ + 'notnull' => true, + ]); + $table->addColumn('user_id', 'string', [ + 'notnull' => true, + 'length' => 64, + ]); + $table->addColumn('mount_point', 'string', [ + 'notnull' => true, + 'length' => 4000, + ]); + $table->addColumn('mount_id', 'integer', [ + 'notnull' => false, + ]); + $table->setPrimaryKey(['id']); + $table->addIndex(['user_id'], 'mounts_user_index'); + $table->addIndex(['storage_id'], 'mounts_storage_index'); + $table->addIndex(['root_id'], 'mounts_root_index'); + $table->addIndex(['mount_id'], 'mounts_mount_id_index'); + $table->addUniqueIndex(['user_id', 'root_id'], 'mounts_user_root_index'); + } + + if (!$schema->hasTable('mimetypes')) { + $table = $schema->createTable('mimetypes'); + $table->addColumn('id', 'integer', [ + 'autoincrement' => true, + 'notnull' => true, + 'length' => 4, + ]); + $table->addColumn('mimetype', 'string', [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table->setPrimaryKey(['id']); + $table->addUniqueIndex(['mimetype'], 'mimetype_id_index'); + } + + if (!$schema->hasTable('filecache')) { + $table = $schema->createTable('filecache'); + $table->addColumn('fileid', 'integer', [ + 'autoincrement' => true, + 'notnull' => true, + 'length' => 4, + ]); + $table->addColumn('storage', 'integer', [ + 'notnull' => true, + 'length' => 4, + 'default' => 0, + ]); + $table->addColumn('path', 'string', [ + 'notnull' => false, + 'length' => 4000, + ]); + $table->addColumn('path_hash', 'string', [ + 'notnull' => true, + 'length' => 32, + 'default' => '', + ]); + $table->addColumn('parent', 'integer', [ + 'notnull' => true, + 'length' => 4, + 'default' => 0, + ]); + $table->addColumn('name', 'string', [ + 'notnull' => false, + 'length' => 250, + ]); + $table->addColumn('mimetype', 'integer', [ + 'notnull' => true, + 'length' => 4, + 'default' => 0, + ]); + $table->addColumn('mimepart', 'integer', [ + 'notnull' => true, + 'length' => 4, + 'default' => 0, + ]); + $table->addColumn('size', 'bigint', [ + 'notnull' => true, + 'length' => 8, + 'default' => 0, + ]); + $table->addColumn('mtime', 'integer', [ + 'notnull' => true, + 'length' => 4, + 'default' => 0, + ]); + $table->addColumn('storage_mtime', 'integer', [ + 'notnull' => true, + 'length' => 4, + 'default' => 0, + ]); + $table->addColumn('encrypted', 'integer', [ + 'notnull' => true, + 'length' => 4, + 'default' => 0, + ]); + $table->addColumn('unencrypted_size', 'bigint', [ + 'notnull' => true, + 'length' => 8, + 'default' => 0, + ]); + $table->addColumn('etag', 'string', [ + 'notnull' => false, + 'length' => 40, + ]); + $table->addColumn('permissions', 'integer', [ + 'notnull' => false, + 'length' => 4, + 'default' => 0, + ]); + $table->addColumn('checksum', 'string', [ + 'notnull' => false, + 'length' => 255, + ]); + $table->setPrimaryKey(['fileid']); + $table->addUniqueIndex(['storage', 'path_hash'], 'fs_storage_path_hash'); + $table->addIndex(['parent', 'name'], 'fs_parent_name_hash'); + $table->addIndex(['storage', 'mimetype'], 'fs_storage_mimetype'); + $table->addIndex(['storage', 'mimepart'], 'fs_storage_mimepart'); + $table->addIndex(['storage', 'size', 'fileid'], 'fs_storage_size'); + } + + if (!$schema->hasTable('group_user')) { + $table = $schema->createTable('group_user'); + $table->addColumn('gid', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('uid', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->setPrimaryKey(['gid', 'uid']); + $table->addIndex(['uid'], 'gu_uid_index'); + } + + if (!$schema->hasTable('group_admin')) { + $table = $schema->createTable('group_admin'); + $table->addColumn('gid', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('uid', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->setPrimaryKey(['gid', 'uid']); + $table->addIndex(['uid'], 'group_admin_uid'); + } + + if (!$schema->hasTable('groups')) { + $table = $schema->createTable('groups'); + $table->addColumn('gid', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->setPrimaryKey(['gid']); + } + + if (!$schema->hasTable('preferences')) { + $table = $schema->createTable('preferences'); + $table->addColumn('userid', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('appid', 'string', [ + 'notnull' => true, + 'length' => 32, + 'default' => '', + ]); + $table->addColumn('configkey', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('configvalue', 'text', [ + 'notnull' => false, + ]); + $table->setPrimaryKey(['userid', 'appid', 'configkey']); + } + + if (!$schema->hasTable('properties')) { + $table = $schema->createTable('properties'); + $table->addColumn('id', 'integer', [ + 'autoincrement' => true, + 'notnull' => true, + 'length' => 4, + ]); + $table->addColumn('userid', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('propertypath', 'string', [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table->addColumn('propertyname', 'string', [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table->addColumn('propertyvalue', 'text', [ + 'notnull' => true, + ]); + $table->setPrimaryKey(['id']); + $table->addIndex(['userid'], 'property_index'); + } + + if (!$schema->hasTable('share')) { + $table = $schema->createTable('share'); + $table->addColumn('id', 'integer', [ + 'autoincrement' => true, + 'notnull' => true, + 'length' => 4, + ]); + $table->addColumn('share_type', 'smallint', [ + 'notnull' => true, + 'length' => 1, + 'default' => 0, + ]); + $table->addColumn('share_with', 'string', [ + 'notnull' => false, + 'length' => 255, + ]); + $table->addColumn('password', 'string', [ + 'notnull' => false, + 'length' => 255, + ]); + $table->addColumn('uid_owner', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('uid_initiator', 'string', [ + 'notnull' => false, + 'length' => 64, + ]); + $table->addColumn('parent', 'integer', [ + 'notnull' => false, + 'length' => 4, + ]); + $table->addColumn('item_type', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('item_source', 'string', [ + 'notnull' => false, + 'length' => 255, + ]); + $table->addColumn('item_target', 'string', [ + 'notnull' => false, + 'length' => 255, + ]); + $table->addColumn('file_source', 'integer', [ + 'notnull' => false, + 'length' => 4, + ]); + $table->addColumn('file_target', 'string', [ + 'notnull' => false, + 'length' => 512, + ]); + $table->addColumn('permissions', 'smallint', [ + 'notnull' => true, + 'length' => 1, + 'default' => 0, + ]); + $table->addColumn('stime', 'bigint', [ + 'notnull' => true, + 'length' => 8, + 'default' => 0, + ]); + $table->addColumn('accepted', 'smallint', [ + 'notnull' => true, + 'length' => 1, + 'default' => 0, + ]); + $table->addColumn('expiration', 'datetime', [ + 'notnull' => false, + ]); + $table->addColumn('token', 'string', [ + 'notnull' => false, + 'length' => 32, + ]); + $table->addColumn('mail_send', 'smallint', [ + 'notnull' => true, + 'length' => 1, + 'default' => 0, + ]); + $table->addColumn('share_name', 'string', [ + 'notnull' => false, + 'length' => 64, + ]); + $table->setPrimaryKey(['id']); + $table->addIndex(['item_type', 'share_type'], 'item_share_type_index'); + $table->addIndex(['file_source'], 'file_source_index'); + $table->addIndex(['token'], 'token_index'); + } + + if (!$schema->hasTable('jobs')) { + $table = $schema->createTable('jobs'); + $table->addColumn('id', 'integer', [ + 'autoincrement' => true, + 'notnull' => true, + 'length' => 4, + ]); + $table->addColumn('class', 'string', [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table->addColumn('argument', 'string', [ + 'notnull' => true, + 'length' => 4000, + 'default' => '', + ]); + $table->addColumn('last_run', 'integer', [ + 'notnull' => false, + 'default' => 0, + ]); + $table->addColumn('last_checked', 'integer', [ + 'notnull' => false, + 'default' => 0, + ]); + $table->addColumn('reserved_at', 'integer', [ + 'notnull' => false, + 'default' => 0, + ]); + $table->addColumn('execution_duration', 'integer', [ + 'notnull' => true, + 'default' => 0, + ]); + $table->setPrimaryKey(['id']); + $table->addIndex(['class'], 'job_class_index'); + } + + if (!$schema->hasTable('users')) { + $table = $schema->createTable('users'); + $table->addColumn('uid', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('displayname', 'string', [ + 'notnull' => false, + 'length' => 64, + ]); + $table->addColumn('password', 'string', [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table->setPrimaryKey(['uid']); + } + + if (!$schema->hasTable('authtoken')) { + $table = $schema->createTable('authtoken'); + $table->addColumn('id', 'integer', [ + 'autoincrement' => true, + 'notnull' => true, + 'length' => 4, + ]); + $table->addColumn('uid', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('login_name', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('password', 'text', [ + 'notnull' => false, + ]); + $table->addColumn('name', 'text', [ + 'notnull' => true, + 'default' => '', + ]); + $table->addColumn('token', 'string', [ + 'notnull' => true, + 'length' => 200, + 'default' => '', + ]); + $table->addColumn('type', 'smallint', [ + 'notnull' => true, + 'length' => 2, + 'default' => 0, + ]); + $table->addColumn('remember', 'smallint', [ + 'notnull' => true, + 'length' => 1, + 'default' => 0, + ]); + $table->addColumn('last_activity', 'integer', [ + 'notnull' => true, + 'length' => 4, + 'default' => 0, + ]); + $table->addColumn('last_check', 'integer', [ + 'notnull' => true, + 'length' => 4, + 'default' => 0, + ]); + $table->addColumn('scope', 'text', [ + 'notnull' => false, + ]); + $table->setPrimaryKey(['id']); + $table->addUniqueIndex(['token'], 'authtoken_token_index'); + $table->addIndex(['last_activity'], 'authtoken_last_activity_index'); + } + + if (!$schema->hasTable('bruteforce_attempts')) { + $table = $schema->createTable('bruteforce_attempts'); + $table->addColumn('id', 'integer', [ + 'autoincrement' => true, + 'notnull' => true, + 'length' => 4, + ]); + $table->addColumn('action', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('occurred', 'integer', [ + 'notnull' => true, + 'length' => 4, + 'default' => 0, + ]); + $table->addColumn('ip', 'string', [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table->addColumn('subnet', 'string', [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table->addColumn('metadata', 'string', [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table->setPrimaryKey(['id']); + $table->addIndex(['ip'], 'bruteforce_attempts_ip'); + $table->addIndex(['subnet'], 'bruteforce_attempts_subnet'); + } + + if (!$schema->hasTable('vcategory')) { + $table = $schema->createTable('vcategory'); + $table->addColumn('id', 'integer', [ + 'autoincrement' => true, + 'notnull' => true, + 'length' => 4, + ]); + $table->addColumn('uid', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('type', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('category', 'string', [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table->setPrimaryKey(['id']); + $table->addIndex(['uid'], 'uid_index'); + $table->addIndex(['type'], 'type_index'); + $table->addIndex(['category'], 'category_index'); + } + + if (!$schema->hasTable('vcategory_to_object')) { + $table = $schema->createTable('vcategory_to_object'); + $table->addColumn('objid', 'integer', [ + 'notnull' => true, + 'length' => 4, + 'default' => 0, + ]); + $table->addColumn('categoryid', 'integer', [ + 'notnull' => true, + 'length' => 4, + 'default' => 0, + ]); + $table->addColumn('type', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->setPrimaryKey(['categoryid', 'objid', 'type']); + $table->addIndex(['objid', 'type'], 'vcategory_objectd_index'); + } + + if (!$schema->hasTable('systemtag')) { + $table = $schema->createTable('systemtag'); + $table->addColumn('id', 'integer', [ + 'autoincrement' => true, + 'notnull' => true, + 'length' => 4, + ]); + $table->addColumn('name', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('visibility', 'smallint', [ + 'notnull' => true, + 'length' => 1, + 'default' => 1, + ]); + $table->addColumn('editable', 'smallint', [ + 'notnull' => true, + 'length' => 1, + 'default' => 1, + ]); + $table->setPrimaryKey(['id']); + $table->addUniqueIndex(['name', 'visibility', 'editable'], 'tag_ident'); + } + + if (!$schema->hasTable('systemtag_object_mapping')) { + $table = $schema->createTable('systemtag_object_mapping'); + $table->addColumn('objectid', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('objecttype', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('systemtagid', 'integer', [ + 'notnull' => true, + 'length' => 4, + 'default' => 0, + ]); + $table->addUniqueIndex(['objecttype', 'objectid', 'systemtagid'], 'mapping'); + } + + if (!$schema->hasTable('systemtag_group')) { + $table = $schema->createTable('systemtag_group'); + $table->addColumn('systemtagid', 'integer', [ + 'notnull' => true, + 'length' => 4, + 'default' => 0, + ]); + $table->addColumn('gid', 'string', [ + 'notnull' => true, + ]); + $table->setPrimaryKey(['gid', 'systemtagid']); + } + + if (!$schema->hasTable('file_locks')) { + $table = $schema->createTable('file_locks'); + $table->addColumn('id', 'integer', [ + 'autoincrement' => true, + 'notnull' => true, + 'length' => 4, + ]); + $table->addColumn('lock', 'integer', [ + 'notnull' => true, + 'length' => 4, + 'default' => 0, + ]); + $table->addColumn('key', 'string', [ + 'notnull' => true, + 'length' => 64, + ]); + $table->addColumn('ttl', 'integer', [ + 'notnull' => true, + 'length' => 4, + 'default' => -1, + ]); + $table->setPrimaryKey(['id']); + $table->addUniqueIndex(['key'], 'lock_key_index'); + $table->addIndex(['ttl'], 'lock_ttl_index'); + } + + if (!$schema->hasTable('comments')) { + $table = $schema->createTable('comments'); + $table->addColumn('id', 'integer', [ + 'autoincrement' => true, + 'notnull' => true, + 'length' => 4, + ]); + $table->addColumn('parent_id', 'integer', [ + 'notnull' => true, + 'length' => 4, + 'default' => 0, + ]); + $table->addColumn('topmost_parent_id', 'integer', [ + 'notnull' => true, + 'length' => 4, + 'default' => 0, + ]); + $table->addColumn('children_count', 'integer', [ + 'notnull' => true, + 'length' => 4, + 'default' => 0, + ]); + $table->addColumn('actor_type', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('actor_id', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('message', 'text', [ + 'notnull' => false, + ]); + $table->addColumn('verb', 'string', [ + 'notnull' => false, + 'length' => 64, + ]); + $table->addColumn('creation_timestamp', 'datetime', [ + 'notnull' => false, + ]); + $table->addColumn('latest_child_timestamp', 'datetime', [ + 'notnull' => false, + ]); + $table->addColumn('object_type', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('object_id', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->setPrimaryKey(['id']); + $table->addIndex(['parent_id'], 'comments_parent_id_index'); + $table->addIndex(['topmost_parent_id'], 'comments_topmost_parent_id_idx'); + $table->addIndex(['object_type', 'object_id', 'creation_timestamp'], 'comments_object_index'); + $table->addIndex(['actor_type', 'actor_id'], 'comments_actor_index'); + } + + if (!$schema->hasTable('comments_read_markers')) { + $table = $schema->createTable('comments_read_markers'); + $table->addColumn('user_id', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('marker_datetime', 'datetime', [ + 'notnull' => false, + ]); + $table->addColumn('object_type', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('object_id', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addIndex(['object_type', 'object_id'], 'comments_marker_object_index'); + $table->addUniqueIndex(['user_id', 'object_type', 'object_id'], 'comments_marker_index'); + } + + if (!$schema->hasTable('credentials')) { + $table = $schema->createTable('credentials'); + $table->addColumn('user', 'string', [ + 'notnull' => true, + 'length' => 64, + ]); + $table->addColumn('identifier', 'string', [ + 'notnull' => true, + 'length' => 64, + ]); + $table->addColumn('credentials', 'text', [ + 'notnull' => false, + ]); + $table->setPrimaryKey(['user', 'identifier']); + $table->addIndex(['user'], 'credentials_user'); + } + + if (!$schema->hasTable('admin_sections')) { + $table = $schema->createTable('admin_sections'); + $table->addColumn('id', 'string', [ + 'notnull' => true, + 'length' => 64, + ]); + $table->addColumn('class', 'string', [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table->addColumn('priority', 'smallint', [ + 'notnull' => true, + 'length' => 1, + 'default' => 0, + ]); + $table->setPrimaryKey(['id']); + $table->addUniqueIndex(['class'], 'admin_sections_class'); + } + + if (!$schema->hasTable('admin_settings')) { + $table = $schema->createTable('admin_settings'); + $table->addColumn('id', 'integer', [ + 'autoincrement' => true, + 'notnull' => true, + 'length' => 4, + ]); + $table->addColumn('class', 'string', [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table->addColumn('section', 'string', [ + 'notnull' => false, + 'length' => 64, + ]); + $table->addColumn('priority', 'smallint', [ + 'notnull' => true, + 'length' => 1, + 'default' => 0, + ]); + $table->setPrimaryKey(['id']); + $table->addUniqueIndex(['class'], 'admin_settings_class'); + $table->addIndex(['section'], 'admin_settings_section'); + } + + if (!$schema->hasTable('personal_sections')) { + $table = $schema->createTable('personal_sections'); + $table->addColumn('id', 'string', [ + 'notnull' => true, + 'length' => 64, + ]); + $table->addColumn('class', 'string', [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table->addColumn('priority', 'smallint', [ + 'notnull' => true, + 'length' => 1, + 'default' => 0, + ]); + $table->setPrimaryKey(['id']); + $table->addUniqueIndex(['class'], 'personal_sections_class'); + } + + if (!$schema->hasTable('personal_settings')) { + $table = $schema->createTable('personal_settings'); + $table->addColumn('id', 'integer', [ + 'autoincrement' => true, + 'notnull' => true, + 'length' => 4, + ]); + $table->addColumn('class', 'string', [ + 'notnull' => true, + 'length' => 255, + 'default' => '', + ]); + $table->addColumn('section', 'string', [ + 'notnull' => false, + 'length' => 64, + ]); + $table->addColumn('priority', 'smallint', [ + 'notnull' => true, + 'length' => 1, + 'default' => 0, + ]); + $table->setPrimaryKey(['id']); + $table->addUniqueIndex(['class'], 'personal_settings_class'); + $table->addIndex(['section'], 'personal_settings_section'); + } + + if (!$schema->hasTable('accounts')) { + $table = $schema->createTable('accounts'); + $table->addColumn('uid', 'string', [ + 'notnull' => true, + 'length' => 64, + 'default' => '', + ]); + $table->addColumn('data', 'text', [ + 'notnull' => true, + 'default' => '', + ]); + $table->setPrimaryKey(['uid']); + } + return $schema; + } + + /** + * @param IOutput $output + * @param \Closure $schemaClosure The `\Closure` returns a `Schema` + * @param array $options + * @since 13.0.0 + */ + public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { + } +} diff --git a/core/register_command.php b/core/register_command.php index bfb1138c5e3..fd693729a72 100644 --- a/core/register_command.php +++ b/core/register_command.php @@ -82,12 +82,12 @@ 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())); $application->add(new OC\Core\Command\Db\Migrations\MigrateCommand(\OC::$server->getDatabaseConnection())); $application->add(new OC\Core\Command\Db\Migrations\GenerateCommand(\OC::$server->getDatabaseConnection())); + $application->add(new OC\Core\Command\Db\Migrations\GenerateFromSchemaFileCommand(\OC::$server->getConfig(), \OC::$server->getAppManager(), \OC::$server->getDatabaseConnection())); $application->add(new OC\Core\Command\Db\Migrations\ExecuteCommand(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig())); $application->add(new OC\Core\Command\Encryption\Disable(\OC::$server->getConfig())); |