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

Commit 3c298d0

Browse files
authored
Merge pull request #16 from EvgenyBarinov/feature/issue15
[feature]:issue15 - add try-catch for run migrations
2 parents 79368bc + 7a47497 commit 3c298d0

File tree

4 files changed

+179
-92
lines changed

4 files changed

+179
-92
lines changed

src/Migrator.php

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
final class Migrator
2020
{
21+
private const DB_DATE_FORMAT = 'Y-m-d H:i:s';
22+
2123
/** @var MigrationConfig */
2224
private $config;
2325

@@ -116,9 +118,10 @@ public function getMigrations(): array
116118
* Execute one migration and return it's instance.
117119
*
118120
* @param CapsuleInterface $capsule
121+
*
119122
* @return null|MigrationInterface
120123
*
121-
* @throws \Throwable
124+
* @throws MigrationException
122125
*/
123126
public function run(CapsuleInterface $capsule = null): ?MigrationInterface
124127
{
@@ -131,21 +134,37 @@ public function run(CapsuleInterface $capsule = null): ?MigrationInterface
131134
continue;
132135
}
133136

134-
$capsule = $capsule ?? new Capsule($this->dbal->database($migration->getDatabase()));
135-
$capsule->getDatabase($migration->getDatabase())->transaction(
136-
static function () use ($migration, $capsule): void {
137-
$migration->withCapsule($capsule)->up();
138-
}
139-
);
140-
141-
$this->migrationTable($migration->getDatabase())->insertOne(
142-
[
143-
'migration' => $migration->getState()->getName(),
144-
'time_executed' => new \DateTime('now')
145-
]
146-
);
147-
148-
return $migration->withState($this->resolveState($migration));
137+
try {
138+
$capsule = $capsule ?? new Capsule($this->dbal->database($migration->getDatabase()));
139+
$capsule->getDatabase($migration->getDatabase())->transaction(
140+
static function () use ($migration, $capsule): void {
141+
$migration->withCapsule($capsule)->up();
142+
}
143+
);
144+
145+
$this->migrationTable($migration->getDatabase())->insertOne(
146+
[
147+
'migration' => $migration->getState()->getName(),
148+
'time_executed' => new \DateTime('now')
149+
]
150+
);
151+
152+
return $migration->withState($this->resolveState($migration));
153+
} catch (\Throwable $exception) {
154+
throw new MigrationException(
155+
\sprintf(
156+
'Error in the migration (%s) occurred: %s',
157+
\sprintf(
158+
'%s (%s)',
159+
$migration->getState()->getName(),
160+
$migration->getState()->getTimeCreated()->format(self::DB_DATE_FORMAT)
161+
),
162+
$exception->getMessage()
163+
),
164+
$exception->getCode(),
165+
$exception
166+
);
167+
}
149168
}
150169

151170
return null;

tests/Migrations/AtomizerTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,6 @@ public function testSetPrimaryKeys(): void
303303
$this->assertFalse($this->db->hasTable('sample'));
304304
}
305305

306-
/**
307-
* @expectedException \Spiral\Migrations\Exception\Operation\TableException
308-
*/
309306
public function testChangePrimaryKeys(): void
310307
{
311308
//Create thought migration
@@ -327,6 +324,13 @@ public function testChangePrimaryKeys(): void
327324
$schema->setPrimaryKeys(['id']);
328325

329326
$this->atomize('migration2', [$schema]);
327+
328+
$this->expectException(\Spiral\Migrations\Exception\MigrationException::class);
329+
$this->expectExceptionMessageMatches(
330+
"/Error in the migration \([0-9a-z_\-]+ \(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\)\) occurred: "
331+
. "Unable to set primary keys for table \'.+\'\.\'.+\', table already exists/"
332+
);
333+
330334
$this->migrator->run();
331335
}
332336

0 commit comments

Comments
 (0)