From a926e63f3586391602e509cbb02efd6d7392c997 Mon Sep 17 00:00:00 2001 From: mscherer Date: Tue, 26 Nov 2024 11:47:13 +0100 Subject: [PATCH] Avoid timestamp collisions on bake. --- src/Command/BakeSimpleMigrationCommand.php | 18 ++++++++++++++---- src/Util/Util.php | 8 ++++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Command/BakeSimpleMigrationCommand.php b/src/Command/BakeSimpleMigrationCommand.php index 879ee237..31e0db7d 100644 --- a/src/Command/BakeSimpleMigrationCommand.php +++ b/src/Command/BakeSimpleMigrationCommand.php @@ -55,11 +55,20 @@ public function name(): string /** * @inheritDoc */ - public function fileName($name): string + public function fileName($name, ?string $path = null): string { $name = $this->getMigrationName($name); + $timestamp = Util::getCurrentTimestamp(); + $suffix = '_' . Inflector::camelize($name) . '.php'; - return Util::getCurrentTimestamp() . '_' . Inflector::camelize($name) . '.php'; + if ($path) { + $offset = 0; + while (glob($path . $timestamp . '_*\\.php')) { + $timestamp = Util::getCurrentTimestamp(++$offset); + } + } + + return $timestamp . $suffix; } /** @@ -128,10 +137,11 @@ public function bake(string $name, Arguments $args, ConsoleIo $io): void $renderer->set($this->templateData($args)); $contents = $renderer->generate($this->template()); - $filename = $this->getPath($args) . $this->fileName($name); + $path = $this->getPath($args); + $filename = $path . $this->fileName($name, $path); $this->createFile($filename, $contents, $args, $io); - $emptyFile = $this->getPath($args) . '.gitkeep'; + $emptyFile = $path . '.gitkeep'; $this->deleteEmptyFile($emptyFile, $io); } diff --git a/src/Util/Util.php b/src/Util/Util.php index f24a8f86..32612b09 100644 --- a/src/Util/Util.php +++ b/src/Util/Util.php @@ -57,9 +57,13 @@ class Util * * @return string */ - public static function getCurrentTimestamp(): string + public static function getCurrentTimestamp(?int $offset = null): string { - $dt = new DateTime('now', new DateTimeZone('UTC')); + $time = 'now'; + if ($offset) { + $time = '+' . $offset . ' seconds'; + } + $dt = new DateTime($time, new DateTimeZone('UTC')); return $dt->format(static::DATE_FORMAT); }