Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

[feature]:issue15 - add try-catch for run migrations #16

Merged
merged 3 commits into from
Jun 5, 2020
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
51 changes: 35 additions & 16 deletions src/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

final class Migrator
{
private const DB_DATE_FORMAT = 'Y-m-d H:i:s';

/** @var MigrationConfig */
private $config;

Expand Down Expand Up @@ -116,9 +118,10 @@ public function getMigrations(): array
* Execute one migration and return it's instance.
*
* @param CapsuleInterface $capsule
*
* @return null|MigrationInterface
*
* @throws \Throwable
* @throws MigrationException
*/
public function run(CapsuleInterface $capsule = null): ?MigrationInterface
{
Expand All @@ -131,21 +134,37 @@ public function run(CapsuleInterface $capsule = null): ?MigrationInterface
continue;
}

$capsule = $capsule ?? new Capsule($this->dbal->database($migration->getDatabase()));
$capsule->getDatabase($migration->getDatabase())->transaction(
static function () use ($migration, $capsule): void {
$migration->withCapsule($capsule)->up();
}
);

$this->migrationTable($migration->getDatabase())->insertOne(
[
'migration' => $migration->getState()->getName(),
'time_executed' => new \DateTime('now')
]
);

return $migration->withState($this->resolveState($migration));
try {
$capsule = $capsule ?? new Capsule($this->dbal->database($migration->getDatabase()));
$capsule->getDatabase($migration->getDatabase())->transaction(
static function () use ($migration, $capsule): void {
$migration->withCapsule($capsule)->up();
}
);

$this->migrationTable($migration->getDatabase())->insertOne(
[
'migration' => $migration->getState()->getName(),
'time_executed' => new \DateTime('now')
]
);

return $migration->withState($this->resolveState($migration));
} catch (\Throwable $exception) {
throw new MigrationException(
\sprintf(
'Error in the migration (%s) occurred: %s',
\sprintf(
'%s (%s)',
$migration->getState()->getName(),
$migration->getState()->getTimeCreated()->format(self::DB_DATE_FORMAT)
),
$exception->getMessage()
),
$exception->getCode(),
$exception
);
}
}

return null;
Expand Down
10 changes: 7 additions & 3 deletions tests/Migrations/AtomizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,6 @@ public function testSetPrimaryKeys(): void
$this->assertFalse($this->db->hasTable('sample'));
}

/**
* @expectedException \Spiral\Migrations\Exception\Operation\TableException
*/
public function testChangePrimaryKeys(): void
{
//Create thought migration
Expand All @@ -327,6 +324,13 @@ public function testChangePrimaryKeys(): void
$schema->setPrimaryKeys(['id']);

$this->atomize('migration2', [$schema]);

$this->expectException(\Spiral\Migrations\Exception\MigrationException::class);
$this->expectExceptionMessageMatches(
"/Error in the migration \([0-9a-z_\-]+ \(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\)\) occurred: "
. "Unable to set primary keys for table \'.+\'\.\'.+\', table already exists/"
);

$this->migrator->run();
}

Expand Down
Loading