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 3 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
37 changes: 37 additions & 0 deletions src/SolutionProviders/MissingColumnSolutionProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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_TABLE_CODE = '42S22';
MrRio marked this conversation as resolved.
Show resolved Hide resolved

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_TABLE_CODE;
}

public function getSolutions(Throwable $throwable): array
{
$solution = new RunMigrationsSolution('A column was not found');

return [$solution];
}
}
11 changes: 11 additions & 0 deletions src/Solutions/RunMigrationsSolution.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@

class RunMigrationsSolution implements RunnableSolution
{
private $customTitle;

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

public function getSolutionTitle(): string
{
if (isset($this->customTitle)) {
return $this->customTitle;
}

return 'A table was not found';
MrRio marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down