160 lines
5.9 KiB
PHP
160 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.organizations (
|
|
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,
|
|
organization_type text NOT NULL,
|
|
CONSTRAINT imz_organizations_pk PRIMARY KEY (id),
|
|
CONSTRAINT imz_organizations_check_01 CHECK (version_no > 0),
|
|
CONSTRAINT imz_organizations_check_02 CHECK (data_classification IN ('public','internal','confidential','restricted')),
|
|
CONSTRAINT imz_organizations_check_03 CHECK (organization_type IN ('public','private','ngo','education'))
|
|
);
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
CREATE TABLE public.memberships (
|
|
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,
|
|
user_id bigint NOT NULL CONSTRAINT imz_memberships_user_id_fk REFERENCES public.users(id) ON DELETE CASCADE ON UPDATE CASCADE,
|
|
organization_id uuid NOT NULL CONSTRAINT imz_memberships_organization_id_fk REFERENCES public.organizations(id) ON DELETE CASCADE ON UPDATE CASCADE,
|
|
role text NOT NULL,
|
|
CONSTRAINT imz_memberships_pk PRIMARY KEY (id),
|
|
CONSTRAINT imz_memberships_check_01 CHECK (version_no > 0),
|
|
CONSTRAINT imz_memberships_check_02 CHECK (data_classification IN ('public','internal','confidential','restricted')),
|
|
CONSTRAINT imz_memberships_check_03 CHECK (role IN ('admin','manager','officer','viewer'))
|
|
);
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
CREATE TABLE public.consents (
|
|
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,
|
|
user_id bigint CONSTRAINT imz_consents_user_id_fk REFERENCES public.users(id) ON DELETE SET NULL ON UPDATE CASCADE,
|
|
subject_ref text NOT NULL,
|
|
purpose text NOT NULL,
|
|
status text NOT NULL,
|
|
granted_at timestamptz,
|
|
withdrawn_at timestamptz,
|
|
CONSTRAINT imz_consents_pk PRIMARY KEY (id),
|
|
CONSTRAINT imz_consents_check_01 CHECK (version_no > 0),
|
|
CONSTRAINT imz_consents_check_02 CHECK (data_classification IN ('public','internal','confidential','restricted')),
|
|
CONSTRAINT imz_consents_check_03 CHECK (status IN ('granted','withdrawn','expired'))
|
|
);
|
|
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.consents DROP CONSTRAINT IF EXISTS imz_consents_user_id_fk RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.consents DROP CONSTRAINT IF EXISTS imz_consents_pk RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.consents DROP CONSTRAINT IF EXISTS imz_consents_check_01 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.consents DROP CONSTRAINT IF EXISTS imz_consents_check_02 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.consents DROP CONSTRAINT IF EXISTS imz_consents_check_03 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
DROP TABLE IF EXISTS public.consents RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.memberships DROP CONSTRAINT IF EXISTS imz_memberships_user_id_fk RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.memberships DROP CONSTRAINT IF EXISTS imz_memberships_organization_id_fk RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.memberships DROP CONSTRAINT IF EXISTS imz_memberships_pk RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.memberships DROP CONSTRAINT IF EXISTS imz_memberships_check_01 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.memberships DROP CONSTRAINT IF EXISTS imz_memberships_check_02 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.memberships DROP CONSTRAINT IF EXISTS imz_memberships_check_03 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
DROP TABLE IF EXISTS public.memberships RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.organizations DROP CONSTRAINT IF EXISTS imz_organizations_pk RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.organizations DROP CONSTRAINT IF EXISTS imz_organizations_check_01 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.organizations DROP CONSTRAINT IF EXISTS imz_organizations_check_02 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
ALTER TABLE IF EXISTS public.organizations DROP CONSTRAINT IF EXISTS imz_organizations_check_03 RESTRICT;
|
|
SQL);
|
|
|
|
DB::statement(<<<'SQL'
|
|
DROP TABLE IF EXISTS public.organizations RESTRICT;
|
|
SQL);
|
|
}
|
|
};
|