Adjust the code to use our interface and abstract

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2017-06-02 13:54:09 +02:00 committed by Morris Jobke
parent 183b1dbde3
commit 194ef1a171
4 changed files with 99 additions and 78 deletions

View File

@ -24,7 +24,6 @@ namespace OC\Core\Command\Db\Migrations;
use OC\DB\MigrationService; use OC\DB\MigrationService;
use OC\Migration\ConsoleOutput; use OC\Migration\ConsoleOutput;
use OCP\IConfig;
use OCP\IDBConnection; use OCP\IDBConnection;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\RuntimeException; use Symfony\Component\Console\Exception\RuntimeException;
@ -38,56 +37,37 @@ class GenerateCommand extends Command {
'<?php '<?php
namespace <namespace>; namespace <namespace>;
use OCP\Migration\ISimpleMigration; use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput; use OCP\Migration\IOutput;
/** /**
* Auto-generated migration step: Please modify to your needs! * Auto-generated migration step: Please modify to your needs!
*/ */
class Version<version> implements ISimpleMigration { class Version<version> extends SimpleMigrationStep {
/** /**
* @param IOutput $out * @param IOutput $output
*/ * @since 13.0.0
public function run(IOutput $out) { */
// auto-generated - please modify it to your needs public function preSchemaChange(IOutput $output) {
} }
}
';
private static $_templateSchema = /**
'<?php * @param \Closure $schema The `\Closure` returns a `Schema`
namespace <namespace>; * @param array $options
* @return null|Schema
* @since 13.0.0
*/
public function changeSchema(\Closure $schema, array $options) {
return null;
}
use Doctrine\DBAL\Schema\Schema; /**
use OCP\Migration\ISchemaMigration; * @param IOutput $output
* @since 13.0.0
/** */
* Auto-generated migration step: Please modify to your needs! public function postSchemaChange(IOutput $output) {
*/ }
class Version<version> implements ISchemaMigration {
public function changeSchema(Schema $schema, array $options) {
// auto-generated - please modify it to your needs
}
}
';
private static $_templateSql =
'<?php
namespace <namespace>;
use OCP\IDBConnection;
use OCP\Migration\ISqlMigration;
/**
* Auto-generated migration step: Please modify to your needs!
*/
class Version<version> implements ISqlMigration {
public function sql(IDBConnection $connection) {
// auto-generated - please modify it to your needs
}
} }
'; ';
@ -107,7 +87,7 @@ class Version<version> implements ISqlMigration {
$this $this
->setName('migrations:generate') ->setName('migrations:generate')
->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on') ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on')
->addArgument('kind', InputArgument::REQUIRED, 'simple, schema or sql - defines the kind of migration to be generated'); ;
parent::configure(); parent::configure();
} }
@ -115,10 +95,8 @@ class Version<version> implements ISqlMigration {
public function execute(InputInterface $input, OutputInterface $output) { public function execute(InputInterface $input, OutputInterface $output) {
$appName = $input->getArgument('app'); $appName = $input->getArgument('app');
$ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output));
$kind = $input->getArgument('kind');
$version = date('YmdHis'); $version = date('YmdHis');
$path = $this->generateMigration($ms, $version, $kind); $path = $this->generateMigration($ms, $version);
$output->writeln("New migration class has been generated to <info>$path</info>"); $output->writeln("New migration class has been generated to <info>$path</info>");
@ -127,10 +105,9 @@ class Version<version> implements ISqlMigration {
/** /**
* @param MigrationService $ms * @param MigrationService $ms
* @param string $version * @param string $version
* @param string $kind
* @return string * @return string
*/ */
private function generateMigration(MigrationService $ms, $version, $kind) { private function generateMigration(MigrationService $ms, $version) {
$placeHolders = [ $placeHolders = [
'<namespace>', '<namespace>',
'<version>', '<version>',
@ -139,7 +116,7 @@ class Version<version> implements ISqlMigration {
$ms->getMigrationsNamespace(), $ms->getMigrationsNamespace(),
$version, $version,
]; ];
$code = str_replace($placeHolders, $replacements, $this->getTemplate($kind)); $code = str_replace($placeHolders, $replacements, self::$_templateSimple);
$dir = $ms->getMigrationsDirectory(); $dir = $ms->getMigrationsDirectory();
$path = $dir . '/Version' . $version . '.php'; $path = $dir . '/Version' . $version . '.php';
@ -150,17 +127,4 @@ class Version<version> implements ISqlMigration {
return $path; return $path;
} }
private function getTemplate($kind) {
if ($kind === 'simple') {
return self::$_templateSimple;
}
if ($kind === 'schema') {
return self::$_templateSchema;
}
if ($kind === 'sql') {
return self::$_templateSql;
}
throw new \InvalidArgumentException('Kind can only be one of the following: simple, schema or sql');
}
} }

View File

@ -22,10 +22,12 @@
namespace OC\DB; namespace OC\DB;
use Doctrine\DBAL\Schema\Schema;
use OC\IntegrityCheck\Helpers\AppLocator; use OC\IntegrityCheck\Helpers\AppLocator;
use OC\Migration\SimpleOutput; use OC\Migration\SimpleOutput;
use OCP\AppFramework\QueryException; use OCP\AppFramework\QueryException;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\Migration\IMigrationStep;
use OCP\Migration\IOutput; use OCP\Migration\IOutput;
use OCP\Migration\ISchemaMigration; use OCP\Migration\ISchemaMigration;
use OCP\Migration\ISimpleMigration; use OCP\Migration\ISimpleMigration;
@ -373,23 +375,23 @@ class MigrationService {
* @param string $version * @param string $version
*/ */
public function executeStep($version) { public function executeStep($version) {
// FIXME our interface
$instance = $this->createInstance($version); $instance = $this->createInstance($version);
if ($instance instanceof ISimpleMigration) { if (!$instance instanceof IMigrationStep) {
$instance->run($this->output); throw new \RuntimeException('Not a valid migration');
} }
if ($instance instanceof ISqlMigration) {
$sqls = $instance->sql($this->connection); $instance->preSchemaChange($this->output);
foreach ($sqls as $s) {
$this->connection->executeQuery($s); $toSchema = $instance->changeSchema(function() {
} return $this->connection->createSchema();
} }, ['tablePrefix' => $this->connection->getPrefix()]);
if ($instance instanceof ISchemaMigration) {
$toSchema = $this->connection->createSchema(); if ($toSchema instanceof Schema) {
$instance->changeSchema($toSchema, ['tablePrefix' => $this->connection->getPrefix()]);
$this->connection->migrateToSchema($toSchema); $this->connection->migrateToSchema($toSchema);
} }
$instance->postSchemaChange($this->output);
$this->markAsExecuted($version); $this->markAsExecuted($version);
} }

View File

@ -35,11 +35,12 @@ interface IMigrationStep {
public function preSchemaChange(IOutput $output); public function preSchemaChange(IOutput $output);
/** /**
* @param Schema $schema * @param \Closure $schema The `\Closure` returns a `Schema`
* @param array $options * @param array $options
* @return null|Schema
* @since 13.0.0 * @since 13.0.0
*/ */
public function changeSchema(Schema $schema, array $options); public function changeSchema(\Closure $schema, array $options);
/** /**
* @param IOutput $output * @param IOutput $output

View File

@ -0,0 +1,54 @@
<?php
/**
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
*
* @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 OCP\Migration;
use Doctrine\DBAL\Schema\Schema;
/**
* @since 13.0.0
*/
abstract class SimpleMigrationStep implements IMigrationStep {
/**
* @param IOutput $output
* @since 13.0.0
*/
public function preSchemaChange(IOutput $output) {
}
/**
* @param \Closure $schema
* @param array $options
* @return null|Schema
* @since 13.0.0
*/
public function changeSchema(\Closure $schema, array $options) {
return null;
}
/**
* @param IOutput $output
* @since 13.0.0
*/
public function postSchemaChange(IOutput $output) {
}
}