Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest running migrations when a column is missing #83

Merged
merged 6 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/IgnitionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,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 @@ -303,6 +304,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