Skip to content

Commit

Permalink
Merge pull request #83 from parallax/feature/missing-column
Browse files Browse the repository at this point in the history
Suggest running migrations when a column is missing
  • Loading branch information
freekmurze authored Sep 9, 2019
2 parents bbd4b61 + 1d56350 commit 60f8f0a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/IgnitionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use Facade\Ignition\SolutionProviders\DefaultDbNameSolutionProvider;
use Facade\Ignition\SolutionProviders\MergeConflictSolutionProvider;
use Facade\Ignition\SolutionProviders\MissingAppKeySolutionProvider;
use Facade\Ignition\SolutionProviders\MissingColumnSolutionProvider;
use Facade\Ignition\SolutionProviders\MissingImportSolutionProvider;
use Facade\Ignition\SolutionProviders\TableNotFoundSolutionProvider;
use Illuminate\View\Engines\CompilerEngine as LaravelCompilerEngine;
Expand Down Expand Up @@ -307,6 +308,7 @@ protected function getDefaultSolutions(): array
InvalidRouteActionSolutionProvider::class,
ViewNotFoundSolutionProvider::class,
MergeConflictSolutionProvider::class,
MissingColumnSolutionProvider::class,
];
}

Expand Down
35 changes: 35 additions & 0 deletions src/SolutionProviders/MissingColumnSolutionProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Facade\Ignition\SolutionProviders;

use Throwable;
use Illuminate\Database\QueryException;
use Facade\Ignition\Solutions\RunMigrationsSolution;
use Facade\IgnitionContracts\HasSolutionsForThrowable;

class MissingColumnSolutionProvider implements HasSolutionsForThrowable
{
/**
* See https://dev.mysql.com/doc/refman/8.0/en/server-error-reference.html#error_er_bad_field_error.
*/
const MYSQL_BAD_FIELD_CODE = '42S22';

public function canSolve(Throwable $throwable): bool
{
if (! $throwable instanceof QueryException) {
return false;
}

return $this->isBadTableErrorCode($throwable->getCode());
}

protected function isBadTableErrorCode($code): bool
{
return $code === static::MYSQL_BAD_FIELD_CODE;
}

public function getSolutions(Throwable $throwable): array
{
return [new RunMigrationsSolution('A column was not found')];
}
}
2 changes: 1 addition & 1 deletion src/SolutionProviders/TableNotFoundSolutionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ protected function isBadTableErrorCode($code): bool

public function getSolutions(Throwable $throwable): array
{
return [new RunMigrationsSolution()];
return [new RunMigrationsSolution('A table was not found')];
}
}
9 changes: 8 additions & 1 deletion src/Solutions/RunMigrationsSolution.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@

class RunMigrationsSolution implements RunnableSolution
{
private $customTitle;

public function __construct($customTitle = '')
{
$this->customTitle = $customTitle;
}

public function getSolutionTitle(): string
{
return 'A table was not found';
return $this->customTitle;
}

public function getSolutionDescription(): string
Expand Down

0 comments on commit 60f8f0a

Please sign in to comment.