Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose all attempted execution migrations #1194

Draft
wants to merge 1 commit into
base: save-skipped-errored-migrations
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 23 additions & 2 deletions lib/Doctrine/Migrations/Metadata/ExecutedMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,22 @@ final class ExecutedMigration
*/
public $executionTime;

public function __construct(Version $version, ?DateTimeImmutable $executedAt = null, ?float $executionTime = null)
{
/** @var string|null */
private $reason;

/** @var string|null */
private $reasonDescription;

public function __construct(
Version $version,
?DateTimeImmutable $executedAt = null,
?float $executionTime = null,
?string $reason
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you decide not to use

Suggested change
?string $reason
?string $reason = null

?

) {
$this->version = $version;
$this->executedAt = $executedAt;
$this->executionTime = $executionTime;
$this->reason = $reason;
}

public function getExecutionTime(): ?float
Expand All @@ -43,6 +54,16 @@ public function getExecutedAt(): ?DateTimeImmutable
return $this->executedAt;
}

public function getReason(): ?string
{
return $this->reason;
}

public function getReasonDescription(): ?string
{
return $this->reasonDescription;
}

public function getVersion(): Version
{
return $this->version;
Expand Down
9 changes: 9 additions & 0 deletions lib/Doctrine/Migrations/Metadata/Storage/MetadataStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@ interface MetadataStorage
{
public function ensureInitialized(): void;

/**
* Returns only the migrations that have been successfully executed (reason=executed)
*/
public function getExecutedMigrations(): ExecutedMigrationsList;

/**
* Returns all the for which there was an execution attempt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Returns all the for which there was an execution attempt
* Returns all the migrations for which there was an execution attempt

* Includes skipped, errored executed migrations.
*/
public function getAllExecutedMigrations(): ExecutedMigrationsList;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what would be here the best way to add such functionality to this interface in a backward compatible way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually something like

class Foo implements MetadataStorage, AllExecutedMigrationsInterfaceThingy

but I assume you're looking for something different?


public function complete(ExecutionResult $migration): void;

public function reset(): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,41 @@ public function getExecutedMigrations(): ExecutedMigrationsList
['reason' => Types::STRING]
);

return $this->hydrateExecutedMigrationsList($rows);
}

public function getAllExecutedMigrations(): ExecutedMigrationsList
{
if (! $this->isInitialized()) {
return new ExecutedMigrationsList([]);
}

$this->checkInitialization();
$rows = $this->connection->fetchAllAssociative(
sprintf(
'SELECT * FROM %s',
$this->configuration->getTableName(),
),
['reason' => self::RUN_EXECUTED],
['reason' => Types::STRING]
);

return $this->hydrateExecutedMigrationsList($rows);
}

/**
* @param array<int,array<string,mixed>> $rows
*/
private function hydrateExecutedMigrationsList(array $rows): ExecutedMigrationsList
{
$migrations = [];
foreach ($rows as $row) {
$row = array_change_key_case($row, CASE_LOWER);

$version = new Version($row[strtolower($this->configuration->getVersionColumnName())]);

$reason = $row[strtolower($this->configuration->getExecutionReasonColumnName())];

$executedAt = $row[strtolower($this->configuration->getExecutedAtColumnName())] ?? '';
$executedAt = $executedAt !== ''
? DateTimeImmutable::createFromFormat($this->platform->getDateTimeFormatString(), $executedAt)
Expand All @@ -119,7 +148,8 @@ public function getExecutedMigrations(): ExecutedMigrationsList
$migration = new ExecutedMigration(
$version,
$executedAt instanceof DateTimeImmutable ? $executedAt : null,
$executionTime
$executionTime,
$reason
);

$migrations[(string) $version] = $migration;
Expand Down