Skip to content

Commit

Permalink
Avoid timestamp collisions on bake. (#773)
Browse files Browse the repository at this point in the history
* Avoid timestamp collisions on bake.

* Fix BC topic. (#776)

* Fix BC topic.

* Fix BC topic.

* Fix Psalm.
  • Loading branch information
dereuromark authored Nov 28, 2024
1 parent 10620a6 commit 89b6331
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
24 changes: 21 additions & 3 deletions src/Command/BakeSimpleMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ abstract class BakeSimpleMigrationCommand extends SimpleBakeCommand
*/
protected ?ConsoleIo $io = null;

/**
* Arguments
*
* @var \Cake\Console\Arguments|null
*/
protected ?Arguments $args = null;

/**
* @inheritDoc
*/
Expand All @@ -58,8 +65,17 @@ public function name(): string
public function fileName($name): string
{
$name = $this->getMigrationName($name);
$timestamp = Util::getCurrentTimestamp();
$suffix = '_' . Inflector::camelize($name) . '.php';

/** @psalm-suppress PossiblyNullArgument */
$path = $this->getPath($this->args);
$offset = 0;
while (glob($path . $timestamp . '_*\\.php')) {
$timestamp = Util::getCurrentTimestamp(++$offset);
}

return Util::getCurrentTimestamp() . '_' . Inflector::camelize($name) . '.php';
return $timestamp . $suffix;
}

/**
Expand Down Expand Up @@ -100,6 +116,7 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
public function bake(string $name, Arguments $args, ConsoleIo $io): void
{
$this->io = $io;
$this->args = $args;
$migrationWithSameName = glob($this->getPath($args) . '*_' . $name . '.php');
if (!empty($migrationWithSameName)) {
$force = $args->getOption('force');
Expand Down Expand Up @@ -128,10 +145,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);
$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 89b6331

Please sign in to comment.