145 lines
5.9 KiB
PHP
145 lines
5.9 KiB
PHP
<?php
|
|
|
|
use Illuminate\Database\Migrations\Migration;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
return new class extends Migration
|
|
{
|
|
// Laravel's migration ledger provides idempotency; object drift fails explicitly.
|
|
public function up(): void
|
|
{
|
|
if (DB::connection()->getDriverName() !== 'pgsql') {
|
|
throw new RuntimeException('InvestMoz requires PostgreSQL with PostGIS. Use the isolated pgsql test environment.');
|
|
}
|
|
|
|
DB::statement(<<<'SQL'
|
|
CREATE TABLE public.geo_locations (
|
|
id uuid DEFAULT gen_random_uuid() NOT NULL,
|
|
source_system text NOT NULL DEFAULT 'investmoz',
|
|
external_id text,
|
|
version_no integer NOT NULL DEFAULT 1,
|
|
data_classification text NOT NULL DEFAULT 'internal',
|
|
correlation_id uuid,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
deleted_at timestamptz,
|
|
name text NOT NULL,
|
|
location_type text NOT NULL,
|
|
parent_id uuid CONSTRAINT imz_geo_locations_parent_id_fk REFERENCES public.geo_locations(id) ON DELETE RESTRICT ON UPDATE CASCADE,
|
|
code text NOT NULL,
|
|
CONSTRAINT imz_geo_locations_pk PRIMARY KEY (id),
|
|
CONSTRAINT imz_geo_locations_check_01 CHECK (version_no > 0),
|
|
CONSTRAINT imz_geo_locations_check_02 CHECK (data_classification IN ('public','internal','confidential','restricted')),
|
|
CONSTRAINT imz_geo_locations_check_03 CHECK (location_type IN ('country','province','district','municipality','site'))
|
|
);
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
CREATE TABLE public.geo_geometries (
|
|
id uuid DEFAULT gen_random_uuid() NOT NULL,
|
|
source_system text NOT NULL DEFAULT 'investmoz',
|
|
external_id text,
|
|
version_no integer NOT NULL DEFAULT 1,
|
|
data_classification text NOT NULL DEFAULT 'internal',
|
|
correlation_id uuid,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
deleted_at timestamptz,
|
|
geo_location_id uuid NOT NULL CONSTRAINT imz_geo_geometries_geo_location_id_fk REFERENCES public.geo_locations(id) ON DELETE CASCADE ON UPDATE CASCADE,
|
|
geometry_type text NOT NULL,
|
|
geom geometry(Geometry,4326) NOT NULL,
|
|
properties jsonb NOT NULL DEFAULT '{}'::jsonb,
|
|
CONSTRAINT imz_geo_geometries_pk PRIMARY KEY (id),
|
|
CONSTRAINT imz_geo_geometries_check_01 CHECK (version_no > 0),
|
|
CONSTRAINT imz_geo_geometries_check_02 CHECK (data_classification IN ('public','internal','confidential','restricted')),
|
|
CONSTRAINT imz_geo_geometries_check_03 CHECK (geometry_type IN ('point','line','polygon','multipolygon')),
|
|
CONSTRAINT imz_geo_geometries_check_04 CHECK (ST_SRID(geom) = 4326),
|
|
CONSTRAINT imz_geo_geometries_check_05 CHECK (ST_NDims(geom) = 2),
|
|
CONSTRAINT imz_geo_geometries_check_06 CHECK (NOT ST_IsEmpty(geom)),
|
|
CONSTRAINT imz_geo_geometries_check_07 CHECK (ST_IsValid(geom)),
|
|
CONSTRAINT imz_geo_geometries_check_08 CHECK ((geometry_type = 'point' AND GeometryType(geom) = 'POINT') OR (geometry_type = 'line' AND GeometryType(geom) = 'LINESTRING') OR (geometry_type = 'polygon' AND GeometryType(geom) = 'POLYGON') OR (geometry_type = 'multipolygon' AND GeometryType(geom) = 'MULTIPOLYGON')),
|
|
CONSTRAINT imz_geo_geometries_check_09 CHECK (ST_XMin(Box3D(geom)) >= -180 AND ST_XMax(Box3D(geom)) <= 180 AND ST_YMin(Box3D(geom)) >= -90 AND ST_YMax(Box3D(geom)) <= 90)
|
|
);
|
|
SQL);
|
|
}
|
|
|
|
public function down(): void
|
|
{
|
|
if (DB::connection()->getDriverName() !== 'pgsql') {
|
|
throw new RuntimeException('InvestMoz rollback requires PostgreSQL.');
|
|
}
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.geo_geometries DROP CONSTRAINT IF EXISTS imz_geo_geometries_geo_location_id_fk RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.geo_geometries DROP CONSTRAINT IF EXISTS imz_geo_geometries_pk RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.geo_geometries DROP CONSTRAINT IF EXISTS imz_geo_geometries_check_01 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.geo_geometries DROP CONSTRAINT IF EXISTS imz_geo_geometries_check_02 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.geo_geometries DROP CONSTRAINT IF EXISTS imz_geo_geometries_check_03 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.geo_geometries DROP CONSTRAINT IF EXISTS imz_geo_geometries_check_04 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.geo_geometries DROP CONSTRAINT IF EXISTS imz_geo_geometries_check_05 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.geo_geometries DROP CONSTRAINT IF EXISTS imz_geo_geometries_check_06 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.geo_geometries DROP CONSTRAINT IF EXISTS imz_geo_geometries_check_07 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.geo_geometries DROP CONSTRAINT IF EXISTS imz_geo_geometries_check_08 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.geo_geometries DROP CONSTRAINT IF EXISTS imz_geo_geometries_check_09 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
DROP TABLE IF EXISTS public.geo_geometries RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.geo_locations DROP CONSTRAINT IF EXISTS imz_geo_locations_parent_id_fk RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.geo_locations DROP CONSTRAINT IF EXISTS imz_geo_locations_pk RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.geo_locations DROP CONSTRAINT IF EXISTS imz_geo_locations_check_01 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.geo_locations DROP CONSTRAINT IF EXISTS imz_geo_locations_check_02 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.geo_locations DROP CONSTRAINT IF EXISTS imz_geo_locations_check_03 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
DROP TABLE IF EXISTS public.geo_locations RESTRICT;
|
|
SQL);
|
|
}
|
|
};
|