1818
1919final 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 ;
0 commit comments