Skip to content

Commit

Permalink
Merge pull request #104 from kitloong/feature/json
Browse files Browse the repository at this point in the history
Fallback to longText if CHECK_CONSTRAINTS is not available
  • Loading branch information
kitloong authored Jul 23, 2022
2 parents a8f2fbf + db88350 commit c5b447e
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/Repositories/MariaDBRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,33 @@

namespace KitLoong\MigrationsGenerator\Repositories;

use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use KitLoong\MigrationsGenerator\Repositories\Entities\MariaDB\CheckConstraint;

class MariaDBRepository extends Repository
{
/**
* Get a check constraint definition with `json_valid` by column.
* See https://mariadb.com/kb/en/information-schema-check_constraints-table/
*
* @param string $table Table name.
* @param string $column Column name.
* @return \KitLoong\MigrationsGenerator\Repositories\Entities\MariaDB\CheckConstraint|null
*/
public function getCheckConstraintForJson(string $table, string $column): ?CheckConstraint
{
$column = DB::selectOne(
"SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS
WHERE TABLE_NAME = '$table'
AND CONSTRAINT_SCHEMA = '" . DB::getDatabaseName() . "'
AND LEVEL = 'Column'
AND CHECK_CLAUSE LIKE '%json_valid(`$column`)%'"
);
return $column === null ? null : new CheckConstraint($column);
try {
// CHECK_CONSTRAINTS available MariaDB starting with 10.2.22
$column = DB::selectOne(
"SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS
WHERE TABLE_NAME = '$table'
AND CONSTRAINT_SCHEMA = '" . DB::getDatabaseName() . "'
AND CHECK_CLAUSE LIKE '%json_valid(`$column`)%'"
);
return $column === null ? null : new CheckConstraint($column);
} catch (QueryException $exception) {
return null;
}
}
}

0 comments on commit c5b447e

Please sign in to comment.