feat: add governed territorial registry and versioned APIs
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
DB::statement('ALTER TABLE geo_locations DROP CONSTRAINT imz_geo_locations_check_03');
|
||||
DB::statement("ALTER TABLE geo_locations ADD CONSTRAINT imz_geo_locations_check_03 CHECK(location_type IN ('country','province','district','admin_post','locality','municipality','site'))");
|
||||
Schema::create('geo_sources', function (Blueprint $t) {
|
||||
$t->uuid('id')->primary();
|
||||
$t->string('name');
|
||||
$t->string('responsible_entity')->nullable();
|
||||
$t->text('reference');
|
||||
$t->text('license')->nullable();
|
||||
$t->char('checksum', 64);
|
||||
$t->string('source_version');
|
||||
$t->date('collected_at');
|
||||
$t->string('original_crs')->nullable();
|
||||
$t->string('status')->default('pending');
|
||||
$t->foreignId('editor_id')->constrained('users')->restrictOnDelete();
|
||||
$t->foreignId('reviewer_id')->nullable()->constrained('users')->restrictOnDelete();
|
||||
$t->text('approval_reference')->nullable();
|
||||
$t->timestampsTz();
|
||||
$t->unique(['checksum', 'source_version']);
|
||||
});
|
||||
DB::statement("ALTER TABLE geo_sources ADD CHECK(status IN ('pending','approved','revoked')), ADD CHECK(checksum ~ '^[0-9a-f]{64}$'), ADD CHECK(status<>'approved' OR (responsible_entity IS NOT NULL AND license IS NOT NULL))");
|
||||
Schema::create('geo_territory_profiles', function (Blueprint $t) {
|
||||
$t->foreignUuid('geo_location_id')->primary()->constrained('geo_locations')->restrictOnDelete();
|
||||
$t->foreignUuid('source_id')->constrained('geo_sources')->restrictOnDelete();
|
||||
$t->char('iso2', 2)->nullable()->unique();
|
||||
$t->char('iso3', 3)->nullable()->unique();
|
||||
$t->foreignUuid('capital_territory_id')->nullable()->constrained('geo_locations')->restrictOnDelete();
|
||||
$t->date('valid_from');
|
||||
$t->date('valid_to')->nullable();
|
||||
$t->string('status')->default('draft');
|
||||
$t->foreignId('editor_id')->constrained('users')->restrictOnDelete();
|
||||
$t->foreignId('reviewer_id')->nullable()->constrained('users')->restrictOnDelete();
|
||||
$t->foreignId('publisher_id')->nullable()->constrained('users')->restrictOnDelete();
|
||||
$t->timestampsTz();
|
||||
});
|
||||
DB::statement("ALTER TABLE geo_territory_profiles ADD CHECK(valid_to IS NULL OR valid_to>=valid_from), ADD CHECK(status IN ('draft','pending_review','approved','published','rejected','archived')), ADD CHECK(iso2 IS NULL OR iso2 ~ '^[A-Z]{2}$'), ADD CHECK(iso3 IS NULL OR iso3 ~ '^[A-Z]{3}$'), ADD CHECK(status NOT IN ('approved','published') OR (reviewer_id IS NOT NULL AND reviewer_id<>editor_id)), ADD CHECK(status<>'published' OR (publisher_id IS NOT NULL AND publisher_id<>editor_id AND publisher_id<>reviewer_id))");
|
||||
Schema::create('geo_names', function (Blueprint $t) {
|
||||
$t->uuid('id')->primary();
|
||||
$t->foreignUuid('geo_location_id')->constrained('geo_locations')->restrictOnDelete();
|
||||
$t->string('language', 10);
|
||||
$t->string('name');
|
||||
$t->string('normalized_name');
|
||||
$t->boolean('official')->default(false);
|
||||
$t->timestampsTz();
|
||||
$t->unique(['geo_location_id', 'language', 'normalized_name']);
|
||||
});
|
||||
DB::statement('CREATE UNIQUE INDEX geo_names_official_language_uq ON geo_names(geo_location_id,language) WHERE official');
|
||||
Schema::create('geo_territory_revisions', function (Blueprint $t) {
|
||||
$t->uuid('id')->primary();
|
||||
$t->foreignUuid('geo_location_id')->constrained('geo_locations')->restrictOnDelete();
|
||||
$t->unsignedInteger('version_no');
|
||||
$t->foreignId('actor_id')->constrained('users')->restrictOnDelete();
|
||||
$t->text('reason');
|
||||
$t->text('snapshot');
|
||||
$t->char('checksum', 64);
|
||||
$t->timestampTz('created_at')->useCurrent();
|
||||
$t->unique(['geo_location_id', 'version_no']);
|
||||
});
|
||||
DB::unprepared("CREATE OR REPLACE FUNCTION reject_territory_revision_change() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'Territory revisions are immutable'; END $$; CREATE TRIGGER immutable_territory_revision BEFORE UPDATE OR DELETE ON geo_territory_revisions FOR EACH ROW EXECUTE FUNCTION reject_territory_revision_change()");
|
||||
Schema::create('geo_write_receipts', function (Blueprint $t) {
|
||||
$t->uuid('id')->primary();
|
||||
$t->foreignId('actor_id')->constrained('users')->restrictOnDelete();
|
||||
$t->uuid('idempotency_key');
|
||||
$t->string('scope');
|
||||
$t->char('request_hash', 64);
|
||||
$t->text('response');
|
||||
$t->integer('http_status');
|
||||
$t->timestampTz('created_at')->useCurrent();
|
||||
$t->unique(['actor_id', 'idempotency_key', 'scope']);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
$tables = ['geo_write_receipts', 'geo_territory_revisions', 'geo_names', 'geo_territory_profiles', 'geo_sources'];
|
||||
foreach ($tables as $table) {
|
||||
if (DB::table($table)->exists()) {
|
||||
throw new RuntimeException('Territory evidence exists; destructive rollback refused.');
|
||||
}
|
||||
}
|
||||
if (DB::table('geo_locations')->whereIn('location_type', ['admin_post', 'locality'])->exists()) {
|
||||
throw new RuntimeException('New territory types exist; rollback refused.');
|
||||
}
|
||||
foreach ($tables as $table) {
|
||||
Schema::drop($table);
|
||||
}
|
||||
DB::statement('DROP FUNCTION reject_territory_revision_change()');
|
||||
DB::statement('ALTER TABLE geo_locations DROP CONSTRAINT imz_geo_locations_check_03');
|
||||
DB::statement("ALTER TABLE geo_locations ADD CONSTRAINT imz_geo_locations_check_03 CHECK(location_type IN ('country','province','district','municipality','site'))");
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user