summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--core/Command/Db/Migrations/GenerateCommand.php28
-rw-r--r--lib/private/DB/MigrationService.php8
2 files changed, 27 insertions, 9 deletions
diff --git a/core/Command/Db/Migrations/GenerateCommand.php b/core/Command/Db/Migrations/GenerateCommand.php
index 8614be0a507..92d8e15b66c 100644
--- a/core/Command/Db/Migrations/GenerateCommand.php
+++ b/core/Command/Db/Migrations/GenerateCommand.php
@@ -40,13 +40,14 @@ class GenerateCommand extends Command {
'<?php
namespace <namespace>;
+use Doctrine\DBAL\Schema\Schema;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;
/**
* Auto-generated migration step: Please modify to your needs!
*/
-class Version<version> extends SimpleMigrationStep {
+class <classname> extends SimpleMigrationStep {
/**
* @param IOutput $output
@@ -90,6 +91,7 @@ class Version<version> extends SimpleMigrationStep {
$this
->setName('migrations:generate')
->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on')
+ ->addArgument('version', InputArgument::REQUIRED, 'Major version of this app, to allow versions on parallel development branches')
;
parent::configure();
@@ -97,31 +99,39 @@ class Version<version> extends SimpleMigrationStep {
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;
+ }
+
$ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output));
- $version = date('YmdHis');
- $path = $this->generateMigration($ms, $version);
- $output->writeln("New migration class has been generated to <info>$path</info>");
+ $date = date('YmdHis');
+ $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date);
+ $output->writeln("New migration class has been generated to <info>$path</info>");
+ return 0;
}
/**
* @param MigrationService $ms
- * @param string $version
+ * @param string $className
* @return string
*/
- private function generateMigration(MigrationService $ms, $version) {
+ private function generateMigration(MigrationService $ms, $className) {
$placeHolders = [
'<namespace>',
- '<version>',
+ '<classname>',
];
$replacements = [
$ms->getMigrationsNamespace(),
- $version,
+ $className,
];
$code = str_replace($placeHolders, $replacements, self::$_templateSimple);
$dir = $ms->getMigrationsDirectory();
- $path = $dir . '/Version' . $version . '.php';
+ $path = $dir . '/' . $className . '.php';
if (file_put_contents($path, $code) === false) {
throw new RuntimeException('Failed to generate new migration step.');
diff --git a/lib/private/DB/MigrationService.php b/lib/private/DB/MigrationService.php
index 3cfd36b89b1..9c37a4ffa71 100644
--- a/lib/private/DB/MigrationService.php
+++ b/lib/private/DB/MigrationService.php
@@ -170,6 +170,14 @@ class MigrationService {
$files = array_keys(iterator_to_array($iterator));
uasort($files, function ($a, $b) {
+ preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($a), $matchA);
+ preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($b), $matchB);
+ if (!empty($matchA) && !empty($matchB)) {
+ if ($matchA[1] !== $matchB[1]) {
+ return ($matchA[1] < $matchB[1]) ? -1 : 1;
+ }
+ return ($matchA[2] < $matchB[2]) ? -1 : 1;
+ }
return (basename($a) < basename($b)) ? -1 : 1;
});