|
2 | 2 |
|
3 | 3 | namespace KitLoong\MigrationsGenerator\Repositories;
|
4 | 4 |
|
| 5 | +use Illuminate\Database\QueryException; |
5 | 6 | use Illuminate\Support\Facades\DB;
|
6 | 7 | use KitLoong\MigrationsGenerator\Repositories\Entities\MariaDB\CheckConstraint;
|
7 | 8 |
|
8 | 9 | class MariaDBRepository extends Repository
|
9 | 10 | {
|
10 | 11 | /**
|
11 | 12 | * Get a check constraint definition with `json_valid` by column.
|
| 13 | + * See https://mariadb.com/kb/en/information-schema-check_constraints-table/ |
12 | 14 | *
|
13 | 15 | * @param string $table Table name.
|
14 | 16 | * @param string $column Column name.
|
15 | 17 | * @return \KitLoong\MigrationsGenerator\Repositories\Entities\MariaDB\CheckConstraint|null
|
16 | 18 | */
|
17 | 19 | public function getCheckConstraintForJson(string $table, string $column): ?CheckConstraint
|
18 | 20 | {
|
19 |
| - $column = DB::selectOne( |
20 |
| - "SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS |
21 |
| - WHERE TABLE_NAME = '$table' |
22 |
| - AND CONSTRAINT_SCHEMA = '" . DB::getDatabaseName() . "' |
23 |
| - AND LEVEL = 'Column' |
24 |
| - AND CHECK_CLAUSE LIKE '%json_valid(`$column`)%'" |
25 |
| - ); |
26 |
| - return $column === null ? null : new CheckConstraint($column); |
| 21 | + try { |
| 22 | + // CHECK_CONSTRAINTS available MariaDB starting with 10.2.22 |
| 23 | + $column = DB::selectOne( |
| 24 | + "SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS |
| 25 | + WHERE TABLE_NAME = '$table' |
| 26 | + AND CONSTRAINT_SCHEMA = '" . DB::getDatabaseName() . "' |
| 27 | + AND CHECK_CLAUSE LIKE '%json_valid(`$column`)%'" |
| 28 | + ); |
| 29 | + return $column === null ? null : new CheckConstraint($column); |
| 30 | + } catch (QueryException $exception) { |
| 31 | + return null; |
| 32 | + } |
27 | 33 | }
|
28 | 34 | }
|
0 commit comments