diff --git a/core/Command/Db/Migrations/GenerateCommand.php b/core/Command/Db/Migrations/GenerateCommand.php index 8614be0a50..92d8e15b66 100644 --- a/core/Command/Db/Migrations/GenerateCommand.php +++ b/core/Command/Db/Migrations/GenerateCommand.php @@ -40,13 +40,14 @@ class GenerateCommand extends Command { '; +use Doctrine\DBAL\Schema\Schema; use OCP\Migration\SimpleMigrationStep; use OCP\Migration\IOutput; /** * Auto-generated migration step: Please modify to your needs! */ -class Version extends SimpleMigrationStep { +class extends SimpleMigrationStep { /** * @param IOutput $output @@ -90,6 +91,7 @@ class 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 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('The given version is invalid. Only 0-9 are allowed (max. 16 digits)'); + return 1; + } + $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); - $version = date('YmdHis'); - $path = $this->generateMigration($ms, $version); + + $date = date('YmdHis'); + $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date); $output->writeln("New migration class has been generated to $path"); - + 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 = [ '', - '', + '', ]; $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 3cfd36b89b..9c37a4ffa7 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; });