2017-06-01 17:56:34 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2017-06-02 15:45:34 +03:00
|
|
|
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
|
2017-06-01 17:56:34 +03:00
|
|
|
* @copyright Copyright (c) 2017, ownCloud GmbH
|
2017-06-02 15:45:34 +03:00
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
*
|
2017-06-01 17:56:34 +03:00
|
|
|
* @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\Migrations;
|
|
|
|
|
|
|
|
|
|
|
|
use OC\DB\MigrationService;
|
|
|
|
use OC\Migration\ConsoleOutput;
|
2018-05-03 17:52:56 +03:00
|
|
|
use OCP\App\IAppManager;
|
2017-06-01 17:56:34 +03:00
|
|
|
use OCP\IDBConnection;
|
2018-05-03 17:52:56 +03:00
|
|
|
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
|
|
|
|
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
|
2017-06-01 17:56:34 +03:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Exception\RuntimeException;
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
2018-05-03 17:52:56 +03:00
|
|
|
class GenerateCommand extends Command implements CompletionAwareInterface {
|
2017-06-01 17:56:34 +03:00
|
|
|
|
2017-07-18 14:37:01 +03:00
|
|
|
protected static $_templateSimple =
|
2017-06-01 17:56:34 +03:00
|
|
|
'<?php
|
2018-09-07 10:19:55 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-08-09 12:01:08 +03:00
|
|
|
namespace {{namespace}};
|
2017-06-01 17:56:34 +03:00
|
|
|
|
2018-09-07 10:19:55 +03:00
|
|
|
use Closure;
|
2018-01-17 13:35:20 +03:00
|
|
|
use OCP\DB\ISchemaWrapper;
|
2017-06-02 14:54:09 +03:00
|
|
|
use OCP\Migration\SimpleMigrationStep;
|
2017-06-01 17:56:34 +03:00
|
|
|
use OCP\Migration\IOutput;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Auto-generated migration step: Please modify to your needs!
|
|
|
|
*/
|
2017-07-19 14:23:14 +03:00
|
|
|
class {{classname}} extends SimpleMigrationStep {
|
2017-06-01 17:56:34 +03:00
|
|
|
|
2017-06-02 14:54:09 +03:00
|
|
|
/**
|
|
|
|
* @param IOutput $output
|
2018-09-07 10:19:55 +03:00
|
|
|
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
|
2017-06-07 14:51:54 +03:00
|
|
|
* @param array $options
|
2017-06-02 14:54:09 +03:00
|
|
|
*/
|
2018-09-07 10:19:55 +03:00
|
|
|
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
|
2017-06-02 14:54:09 +03:00
|
|
|
}
|
2017-06-01 17:56:34 +03:00
|
|
|
|
2017-06-02 14:54:09 +03:00
|
|
|
/**
|
2017-06-07 14:51:54 +03:00
|
|
|
* @param IOutput $output
|
2018-09-07 10:19:55 +03:00
|
|
|
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
|
2017-06-02 14:54:09 +03:00
|
|
|
* @param array $options
|
2018-01-17 13:35:20 +03:00
|
|
|
* @return null|ISchemaWrapper
|
2017-06-02 14:54:09 +03:00
|
|
|
*/
|
2018-09-07 10:19:55 +03:00
|
|
|
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
|
2017-07-19 14:23:14 +03:00
|
|
|
{{schemabody}}
|
2017-06-02 14:54:09 +03:00
|
|
|
}
|
2017-06-01 17:56:34 +03:00
|
|
|
|
2017-06-02 14:54:09 +03:00
|
|
|
/**
|
|
|
|
* @param IOutput $output
|
2018-09-07 10:19:55 +03:00
|
|
|
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
|
2017-06-07 14:51:54 +03:00
|
|
|
* @param array $options
|
2017-06-02 14:54:09 +03:00
|
|
|
*/
|
2018-09-07 10:19:55 +03:00
|
|
|
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
|
2017-06-02 14:54:09 +03:00
|
|
|
}
|
2017-06-01 17:56:34 +03:00
|
|
|
}
|
|
|
|
';
|
|
|
|
|
|
|
|
/** @var IDBConnection */
|
2017-07-18 14:37:01 +03:00
|
|
|
protected $connection;
|
2017-06-01 17:56:34 +03:00
|
|
|
|
2018-05-03 17:52:56 +03:00
|
|
|
/** @var IAppManager */
|
|
|
|
protected $appManager;
|
|
|
|
|
2017-06-01 17:56:34 +03:00
|
|
|
/**
|
|
|
|
* @param IDBConnection $connection
|
2018-05-03 17:52:56 +03:00
|
|
|
* @param IAppManager $appManager
|
2017-06-01 17:56:34 +03:00
|
|
|
*/
|
2018-05-03 17:52:56 +03:00
|
|
|
public function __construct(IDBConnection $connection, IAppManager $appManager) {
|
2017-06-01 17:56:34 +03:00
|
|
|
$this->connection = $connection;
|
2018-05-03 17:52:56 +03:00
|
|
|
$this->appManager = $appManager;
|
2017-06-01 17:56:34 +03:00
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function configure() {
|
|
|
|
$this
|
|
|
|
->setName('migrations:generate')
|
|
|
|
->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on')
|
2017-06-07 13:27:16 +03:00
|
|
|
->addArgument('version', InputArgument::REQUIRED, 'Major version of this app, to allow versions on parallel development branches')
|
2017-06-02 14:54:09 +03:00
|
|
|
;
|
2017-06-01 17:56:34 +03:00
|
|
|
|
|
|
|
parent::configure();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute(InputInterface $input, OutputInterface $output) {
|
|
|
|
$appName = $input->getArgument('app');
|
2017-06-07 13:27:16 +03:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2017-06-01 17:56:34 +03:00
|
|
|
$ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output));
|
|
|
|
|
2017-06-07 13:27:16 +03:00
|
|
|
$date = date('YmdHis');
|
|
|
|
$path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date);
|
2017-06-01 17:56:34 +03:00
|
|
|
|
2017-06-07 13:27:16 +03:00
|
|
|
$output->writeln("New migration class has been generated to <info>$path</info>");
|
|
|
|
return 0;
|
2017-06-01 17:56:34 +03:00
|
|
|
}
|
|
|
|
|
2018-05-03 17:52:56 +03:00
|
|
|
/**
|
|
|
|
* @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 === 'app') {
|
|
|
|
$allApps = \OC_App::getAllApps();
|
|
|
|
return array_diff($allApps, \OC_App::getEnabledApps(true, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($argumentName === 'version') {
|
|
|
|
$appName = $context->getWordAtIndex($context->getWordIndex() - 1);
|
|
|
|
|
|
|
|
$version = explode('.', $this->appManager->getAppVersion($appName));
|
|
|
|
return [$version[0] . sprintf('%1$03d', $version[1])];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-06-01 17:56:34 +03:00
|
|
|
/**
|
|
|
|
* @param MigrationService $ms
|
2017-06-07 13:27:16 +03:00
|
|
|
* @param string $className
|
2017-07-18 14:37:01 +03:00
|
|
|
* @param string $schemaBody
|
2017-06-01 17:56:34 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-07-18 14:37:01 +03:00
|
|
|
protected function generateMigration(MigrationService $ms, $className, $schemaBody = '') {
|
|
|
|
if ($schemaBody === '') {
|
|
|
|
$schemaBody = "\t\t" . 'return null;';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-01 17:56:34 +03:00
|
|
|
$placeHolders = [
|
2017-07-19 14:23:14 +03:00
|
|
|
'{{namespace}}',
|
|
|
|
'{{classname}}',
|
|
|
|
'{{schemabody}}',
|
2017-06-01 17:56:34 +03:00
|
|
|
];
|
|
|
|
$replacements = [
|
|
|
|
$ms->getMigrationsNamespace(),
|
2017-06-07 13:27:16 +03:00
|
|
|
$className,
|
2017-07-18 14:37:01 +03:00
|
|
|
$schemaBody,
|
2017-06-01 17:56:34 +03:00
|
|
|
];
|
2017-06-02 14:54:09 +03:00
|
|
|
$code = str_replace($placeHolders, $replacements, self::$_templateSimple);
|
2017-06-01 17:56:34 +03:00
|
|
|
$dir = $ms->getMigrationsDirectory();
|
2017-07-06 10:58:39 +03:00
|
|
|
|
|
|
|
$this->ensureMigrationDirExists($dir);
|
2017-06-07 13:27:16 +03:00
|
|
|
$path = $dir . '/' . $className . '.php';
|
2017-06-01 17:56:34 +03:00
|
|
|
|
|
|
|
if (file_put_contents($path, $code) === false) {
|
|
|
|
throw new RuntimeException('Failed to generate new migration step.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
2017-07-18 14:37:01 +03:00
|
|
|
protected function ensureMigrationDirExists($directory) {
|
2017-07-06 10:58:39 +03:00
|
|
|
if (file_exists($directory) && is_dir($directory)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists($directory)) {
|
|
|
|
throw new \RuntimeException("Could not create folder \"$directory\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->ensureMigrationDirExists(dirname($directory));
|
|
|
|
|
|
|
|
if (!@mkdir($directory) && !is_dir($directory)) {
|
|
|
|
throw new \RuntimeException("Could not create folder \"$directory\"");
|
|
|
|
}
|
|
|
|
}
|
2017-06-01 17:56:34 +03:00
|
|
|
}
|