kitloong / laravel-migrations-generator

Laravel Migrations Generator: Automatically generate your migrations from an existing database schema.
MIT License
2.5k stars 276 forks source link

Bug for the geography types in the PostGIS extension of PostgreSQL #222

Closed creyeschaponan closed 3 months ago

creyeschaponan commented 3 months ago

Describe the bug There is an error when performing a reverse migration on a field of type geography , which is enabled by the PostGIS plugin in PostgreSQL.

To Reproduce Table in postgrest public.profile_locations ( id bigint generated by default as identity, location geography not null, is_active smallint not null default '1'::smallint, profile_id uuid not null, created_at timestamp with time zone not null default now(), updated_at timestamp without time zone null, deleted_at timestamp without time zone null, constraint profile_locations_pkey primary key (id), constraint profile_locations_profile_id_fkey foreign key (profile_id) references profiles (id) ) tablespace pg_default;

ERROR: Undefined array key 2

at vendor\kitloong\laravel-migrations-generator\src\Database\Models\PgSQL\PgSQLColumn.php:165 161▕ if (!preg_match('/(\w+)(?:((\w+)(?:,\s*(\w+))?))?/', $dataType, $matches)) { 162▕ return; 163▕ } 164▕ ➜ 165▕ $spatialSubType = $matches[2]; 166▕ $spatialSrID = isset($matches[3]) ? (int) $matches[3] : null; 167▕ 168▕ if (!$this->atLeastLaravel11()) { 169▕ $map = $this->getGeometryMap();

1 vendor\kitloong\laravel-migrations-generator\src\Database\Models\PgSQL\PgSQLColumn.php:165 Illuminate\Foundation\Bootstrap\HandleExceptions::Illuminate\Foundation\Bootstrap{closure}("Undefined array key 2", "E:\Proyectos\AyniApp\ProyectoDesarrollo\administracion\aynihogaradmin\vendor\kitloong\laravel-migrations-generator\src\Database\Models\PgSQL\PgSQLColumn.php")

2 vendor\kitloong\laravel-migrations-generator\src\Database\Models\PgSQL\PgSQLColumn.php:46 KitLoong\MigrationsGenerator\Database\Models\PgSQL\PgSQLColumn::setRealSpatialColumn("extensions.geography(Point,4326)")

Expected behavior I want it to be able to migrate special data types, such as the PostGIS extensions, so that by adding DB::statement('CREATE EXTENSION IF NOT EXISTS postgis;');, it can recognize the geography data type.

Screenshots image

Details (please complete the following information):

creyeschaponan commented 3 months ago

Hi, I have reviewed the code and identified the issue:

The problem seems to be related to how preg_match handles match patterns and captured groups. The string extensions.geography(point,4326) contains a period, which is not captured by (\w+) because \w only matches letters, numbers, and underscores.

Proposed Solution: in the file: #PgSQLColumn.php in the row: 161 remplace

to:

if (!preg_match('/([\w\.]+)(?:\(([\w\s]+)(?:,\s*(\w+))?\))?/', $dataType, $matches)) {
    return;
}

Please feel free to change it as necessary, as I am currently migrating tables from Supabase, which uses PostgreSQL, so I can incorporate the changes if needed. Thank you

https://github.com/kitloong/laravel-migrations-generator/blob/ca7d318e4922f276d2f862d22a2089bee8eb6921/src/Database/Models/PgSQL/PgSQLColumn.php#L161

kitloong commented 3 months ago

Hi @creyeschaponan Thank you for your report and sorry for the delay.

Instead of updating the regex, I have taken another approach: Strippiing the extensions. before the regex check. For the details please have a look at fcc0cdf

I will release this after development is completed.