From 5898a50463cec436b24b2e0bd410a469441d4a13 Mon Sep 17 00:00:00 2001 From: Clark Winkelmann Date: Wed, 2 Nov 2022 15:22:21 +0100 Subject: [PATCH] Log migration path when up/down keys are missing (#3664) * Log migration path when up/down keys are missing --- .../Exception/MigrationKeyMissing.php | 30 +++++++++++++++++ framework/core/src/Database/Migrator.php | 33 ++++++++++++++----- 2 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 framework/core/src/Database/Exception/MigrationKeyMissing.php diff --git a/framework/core/src/Database/Exception/MigrationKeyMissing.php b/framework/core/src/Database/Exception/MigrationKeyMissing.php new file mode 100644 index 000000000..9d955f59f --- /dev/null +++ b/framework/core/src/Database/Exception/MigrationKeyMissing.php @@ -0,0 +1,30 @@ +direction = $direction; + + $fileNameWithSpace = $file ? ' '.realpath($file) : ''; + parent::__construct("Migration file$fileNameWithSpace should contain an array with up/down (looking for $direction)"); + } + + public function withFile(string $file = null): self + { + return new self($this->direction, $file); + } +} diff --git a/framework/core/src/Database/Migrator.php b/framework/core/src/Database/Migrator.php index 1a3119360..58d25288d 100644 --- a/framework/core/src/Database/Migrator.php +++ b/framework/core/src/Database/Migrator.php @@ -9,7 +9,7 @@ namespace Flarum\Database; -use Exception; +use Flarum\Database\Exception\MigrationKeyMissing; use Flarum\Extension\Extension; use Illuminate\Database\ConnectionInterface; use Illuminate\Database\MySqlConnection; @@ -124,9 +124,7 @@ class Migrator */ protected function runUp($path, $file, Extension $extension = null) { - $migration = $this->resolve($path, $file); - - $this->runClosureMigration($migration); + $this->resolveAndRunClosureMigration($path, $file); // Once we have run a migrations class, we will log that it was run in this // repository so that we don't try to run it next time we do a migration @@ -173,9 +171,7 @@ class Migrator */ protected function runDown($path, $file, Extension $extension = null) { - $migration = $this->resolve($path, $file); - - $this->runClosureMigration($migration, 'down'); + $this->resolveAndRunClosureMigration($path, $file, 'down'); // Once we have successfully run the migration "down" we will remove it from // the migration repository so it will be considered to have not been run @@ -190,14 +186,33 @@ class Migrator * * @param $migration * @param string $direction - * @throws Exception + * @throws MigrationKeyMissing */ protected function runClosureMigration($migration, $direction = 'up') { if (is_array($migration) && array_key_exists($direction, $migration)) { call_user_func($migration[$direction], $this->connection->getSchemaBuilder()); } else { - throw new Exception('Migration file should contain an array with up/down.'); + throw new MigrationKeyMissing($direction); + } + } + + /** + * Resolves and run a migration and assign the filename to the exception if needed. + * + * @param string $path + * @param string $file + * @param string $direction + * @throws MigrationKeyMissing + */ + protected function resolveAndRunClosureMigration(string $path, string $file, string $direction = 'up') + { + $migration = $this->resolve($path, $file); + + try { + $this->runClosureMigration($migration, $direction); + } catch (MigrationKeyMissing $exception) { + throw $exception->withFile("$path/$file.php"); } }