Log migration path when up/down keys are missing (#3664)

* Log migration path when up/down keys are missing
This commit is contained in:
Clark Winkelmann 2022-11-02 15:22:21 +01:00 committed by GitHub
parent 72d9ee2010
commit 5898a50463
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 9 deletions

View File

@ -0,0 +1,30 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Database\Exception;
use Exception;
class MigrationKeyMissing extends Exception
{
protected $direction;
public function __construct(string $direction, string $file = null)
{
$this->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);
}
}

View File

@ -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");
}
}