Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions packages/database/src/Migrations/MigrationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,9 @@ public function executeDown(MigratesDown $migration): void

try {
$this->database->execute(
new Query(
'DELETE FROM Migration WHERE name = :name',
['name' => $migration->name],
),
query(Migration::class)
->delete()
->whereRaw('name = ?', $migration->name),
);
} catch (QueryWasInvalid) { // @mago-expect lint:no-empty-catch-clause
/**
Expand Down
46 changes: 46 additions & 0 deletions tests/Integration/Database/MigrationManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@

namespace Tests\Tempest\Integration\Database;

use Tempest\Database\MigratesDown;
use Tempest\Database\MigratesUp;
use Tempest\Database\Migrations\CreateMigrationsTable;
use Tempest\Database\Migrations\Migration;
use Tempest\Database\Migrations\MigrationManager;
use Tempest\Database\QueryStatement;
use Tempest\Database\QueryStatements\CreateTableStatement;
use Tempest\Database\QueryStatements\DropTableStatement;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;

use function Tempest\Database\query;

/**
* @internal
*/
Expand All @@ -29,4 +37,42 @@ public function test_migration(): void
$this->assertNotEmpty($migrations);
$this->assertSame($oldCount, count($migrations));
}

public function test_execute_down_removes_rolled_back_migration_record(): void
{
$migrationManager = $this->container->get(MigrationManager::class);
$migration = new MigrationManagerTestRollbackMigration();

$migrationManager->executeUp(new CreateMigrationsTable());
$migrationManager->executeUp($migration);

$this->assertSame(1, $this->countMigrationRecords($migration));

$migrationManager->executeDown($migration);

$this->assertSame(0, $this->countMigrationRecords($migration));
}

private function countMigrationRecords(MigrationManagerTestRollbackMigration $migration): int
{
return query(Migration::class)
->count()
->whereRaw('name = ?', $migration->name)
->execute();
}
}

final class MigrationManagerTestRollbackMigration implements MigratesUp, MigratesDown
{
public string $name = '0000-00-00_create_migration_manager_test_table';

public function up(): QueryStatement
{
return new CreateTableStatement('migration_manager_test_table')->primary();
}

public function down(): QueryStatement
{
return new DropTableStatement('migration_manager_test_table');
}
}
Loading