Skip to content

Commit c5b447e

Browse files
authored
Merge pull request #104 from kitloong/feature/json
Fallback to longText if CHECK_CONSTRAINTS is not available
2 parents a8f2fbf + db88350 commit c5b447e

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/Repositories/MariaDBRepository.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,33 @@
22

33
namespace KitLoong\MigrationsGenerator\Repositories;
44

5+
use Illuminate\Database\QueryException;
56
use Illuminate\Support\Facades\DB;
67
use KitLoong\MigrationsGenerator\Repositories\Entities\MariaDB\CheckConstraint;
78

89
class MariaDBRepository extends Repository
910
{
1011
/**
1112
* Get a check constraint definition with `json_valid` by column.
13+
* See https://mariadb.com/kb/en/information-schema-check_constraints-table/
1214
*
1315
* @param string $table Table name.
1416
* @param string $column Column name.
1517
* @return \KitLoong\MigrationsGenerator\Repositories\Entities\MariaDB\CheckConstraint|null
1618
*/
1719
public function getCheckConstraintForJson(string $table, string $column): ?CheckConstraint
1820
{
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+
}
2733
}
2834
}

0 commit comments

Comments
 (0)