Skip to content

Commit

Permalink
Avoid timestamp collisions on bake.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Nov 26, 2024
1 parent 32afa21 commit a926e63
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
18 changes: 14 additions & 4 deletions src/Command/BakeSimpleMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}

Expand Down
8 changes: 6 additions & 2 deletions src/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit a926e63

Please sign in to comment.