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

Add unknown collations for MySQL 8 and MariaDB #171

Merged
merged 2 commits into from
Mar 13, 2024
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
4 changes: 4 additions & 0 deletions config/ignition.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use Spatie\LaravelIgnition\Solutions\SolutionProviders\ViewNotFoundSolutionProvider;
use Spatie\LaravelIgnition\Solutions\SolutionProviders\OpenAiSolutionProvider;
use Spatie\LaravelIgnition\Solutions\SolutionProviders\SailNetworkSolutionProvider;
use Spatie\LaravelIgnition\Solutions\SolutionProviders\UnknownMariadbCollationSolutionProvider;
use Spatie\LaravelIgnition\Solutions\SolutionProviders\UnknownMysql8CollationSolutionProvider;

return [

Expand Down Expand Up @@ -119,6 +121,8 @@
GenericLaravelExceptionSolutionProvider::class,
OpenAiSolutionProvider::class,
SailNetworkSolutionProvider::class,
UnknownMysql8CollationSolutionProvider::class,
UnknownMariadbCollationSolutionProvider::class,
],

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Spatie\LaravelIgnition\Solutions\SolutionProviders;

use Illuminate\Database\QueryException;
use Spatie\Ignition\Contracts\HasSolutionsForThrowable;
use Spatie\LaravelIgnition\Solutions\SuggestUsingMariadbDatabaseSolution;
use Throwable;

class UnknownMariadbCollationSolutionProvider implements HasSolutionsForThrowable
{
const MYSQL_UNKNOWN_COLLATION_CODE = 1273;

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

if ($throwable->getCode() !== self::MYSQL_UNKNOWN_COLLATION_CODE) {
return false;
}

return str_contains(
$throwable->getMessage(),
'Unknown collation: \'utf8mb4_uca1400_ai_ci\''
);
}

public function getSolutions(Throwable $throwable): array
{
return [new SuggestUsingMariadbDatabaseSolution()];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Spatie\LaravelIgnition\Solutions\SolutionProviders;

use Illuminate\Database\QueryException;
use Spatie\Ignition\Contracts\HasSolutionsForThrowable;
use Spatie\LaravelIgnition\Solutions\SuggestUsingMysql8DatabaseSolution;
use Throwable;

class UnknownMysql8CollationSolutionProvider implements HasSolutionsForThrowable
{
const MYSQL_UNKNOWN_COLLATION_CODE = 1273;

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

if ($throwable->getCode() !== self::MYSQL_UNKNOWN_COLLATION_CODE) {
return false;
}

return str_contains(
$throwable->getMessage(),
'Unknown collation: \'utf8mb4_0900_ai_ci\''
);
}

public function getSolutions(Throwable $throwable): array
{
return [new SuggestUsingMysql8DatabaseSolution()];
}
}
26 changes: 26 additions & 0 deletions src/Solutions/SuggestUsingMariadbDatabaseSolution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Spatie\LaravelIgnition\Solutions;

use Spatie\Ignition\Contracts\Solution;

class SuggestUsingMariadbDatabaseSolution implements Solution
{
public function getSolutionTitle(): string
{
return 'Database is not a MariaDB database';
}

public function getSolutionDescription(): string
{
return "Laravel 11 changed the default collation for MySQL and MariaDB. It seems you are trying to use the MariaDB collation `utf8mb4_uca1400_ai_ci` with a MySQL database.\n\nEdit the `.env` file and use the correct database in the `DB_CONNECTION` key.";
}

/** @return array<string, string> */
public function getDocumentationLinks(): array
{
return [
'Database: Getting Started docs' => 'https://laravel.com/docs/master/database#configuration',
];
}
}
26 changes: 26 additions & 0 deletions src/Solutions/SuggestUsingMysql8DatabaseSolution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Spatie\LaravelIgnition\Solutions;

use Spatie\Ignition\Contracts\Solution;

class SuggestUsingMysql8DatabaseSolution implements Solution
{
public function getSolutionTitle(): string
{
return 'Database is not a MySQL 8 database';
}

public function getSolutionDescription(): string
{
return "Laravel 11 changed the default collation for MySQL and MariaDB. It seems you are trying to use the MySQL 8 collation `utf8mb4_0900_ai_ci` with a MariaDB or MySQL 5.7 database.\n\nEdit the `.env` file and use the correct database in the `DB_CONNECTION` key.";
}

/** @return array<string, string> */
public function getDocumentationLinks(): array
{
return [
'Database: Getting Started docs' => 'https://laravel.com/docs/master/database#configuration',
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\QueryException;
use Spatie\LaravelIgnition\Solutions\SolutionProviders\UnknownMariadbCollationSolutionProvider;

it('can solve an an unknown mariadb collation ', function () {
$solutionProvider = new UnknownMariadbCollationSolutionProvider();

$exception = new QueryException(
'mysql',
'select table_name as `name`, (data_length + index_length) as `size`, table_comment as `comment`, engine as `engine`, table_collation as `collation` from information_schema.tables where table_schema = \'mariadb_test\' and table_type = \'BASE TABLE\' order by table_name',
[],
new Exception('SQLSTATE[HY000]: General error: 1273 Unknown collation: \'utf8mb4_uca1400_ai_ci\'')
);

$solutions = $solutionProvider->getSolutions($exception);

$solution = $solutions[0];

expect($solution->getSolutionDescription())->toBe("Laravel 11 changed the default collation for MySQL and MariaDB. It seems you are trying to use the MariaDB collation `utf8mb4_uca1400_ai_ci` with a MySQL database.\n\nEdit the `.env` file and use the correct database in the `DB_CONNECTION` key.");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\QueryException;
use Spatie\LaravelIgnition\Solutions\SolutionProviders\UnknownMysql8CollationSolutionProvider;

it('can solve an an unknown mysql collation ', function () {
$solutionProvider = new UnknownMysql8CollationSolutionProvider();

$exception = new QueryException(
'mariadb',
'select table_name as `name`, (data_length + index_length) as `size`, table_comment as `comment`, engine as `engine`, table_collation as `collation` from information_schema.tables where table_schema = \'mariadb_test\' and table_type = \'BASE TABLE\' order by table_name',
[],
new Exception('SQLSTATE[HY000]: General error: 1273 Unknown collation: \'utf8mb4_0900_ai_ci\'')
);

$solutions = $solutionProvider->getSolutions($exception);

$solution = $solutions[0];

expect($solution->getSolutionDescription())->toBe("Laravel 11 changed the default collation for MySQL and MariaDB. It seems you are trying to use the MySQL 8 collation `utf8mb4_0900_ai_ci` with a MariaDB or MySQL 5.7 database.\n\nEdit the `.env` file and use the correct database in the `DB_CONNECTION` key.");
});