Skip to content

Commit

Permalink
Merge pull request #172 from kitloong/feature/column
Browse files Browse the repository at this point in the history
Check the existence of `GENERATION_EXPRESSION` column
  • Loading branch information
kitloong committed Mar 25, 2023
2 parents 30cf900 + 4e9b7ab commit 7669a1d
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/Repositories/MySQLRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace KitLoong\MigrationsGenerator\Repositories;

use Illuminate\Database\QueryException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use KitLoong\MigrationsGenerator\Repositories\Entities\MySQL\ShowColumn;
use KitLoong\MigrationsGenerator\Repositories\Entities\ProcedureDefinition;

Expand Down Expand Up @@ -153,13 +155,31 @@ private function getProcedure(string $procedure)
*/
private function getGenerationExpression(string $table, string $column, string $extra): ?string
{
$definition = DB::selectOne(
"SELECT GENERATION_EXPRESSION
try {
$definition = DB::selectOne(
"SELECT GENERATION_EXPRESSION
FROM information_schema.COLUMNS
WHERE TABLE_NAME = '$table'
AND COLUMN_NAME = '$column'
AND EXTRA = '$extra'"
);
);
} catch (QueryException $exception) {
// Check if error caused by missing column 'GENERATION_EXPRESSION'.
// The column is introduced since MySQL 5.7 and MariaDB 10.2.5.
// @see https://mariadb.com/kb/en/information-schema-columns-table/
// @see https://dev.mysql.com/doc/refman/5.7/en/information-schema-columns-table.html
if (
Str::contains(
$exception->getMessage(),
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'GENERATION_EXPRESSION'",
true
)
) {
return null;
}

throw $exception;
}

if ($definition === null) {
return null;
Expand Down

0 comments on commit 7669a1d

Please sign in to comment.