1
0
Fork 0
polaris-task-force/src/migrations/20260918_194842_add_operation_allocation.ts
Z8MB1E 67c993ca10 feat(operations): mission reservations and deterministic settlement
Reserve personnel, vehicles, cargo, and budget with idempotent keys and exactly-once settlement; extraction effects preflight before any write so validated HQ deposits and locker credits are final and survive mission failure. Failure sweeps touch only unextracted reservations.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-09-20 02:43:47 -04:00

116 lines
16 KiB
TypeScript

import { MigrateUpArgs, MigrateDownArgs, sql } from '@payloadcms/db-postgres'
export async function up({ db, payload, req }: MigrateUpArgs): Promise<void> {
await db.execute(sql`
CREATE TYPE "public"."enum_operation_reservations_status" AS ENUM('reserved', 'settled', 'cancelled', 'expired', 'no-show');
ALTER TYPE "public"."enum_roles_permissions" ADD VALUE 'operation-reservations:create' BEFORE 'game-rules:read';
ALTER TYPE "public"."enum_roles_permissions" ADD VALUE 'operation-reservations:read' BEFORE 'game-rules:read';
ALTER TYPE "public"."enum_roles_permissions" ADD VALUE 'operation-reservations:update' BEFORE 'game-rules:read';
ALTER TYPE "public"."enum_roles_permissions" ADD VALUE 'operation-reservations:delete' BEFORE 'game-rules:read';
ALTER TYPE "public"."enum_roles_permissions" ADD VALUE 'admin:operation-reservations:manage' BEFORE 'game-rules:read';
CREATE TABLE "operation_reservations_personnel" (
"_order" integer NOT NULL,
"_parent_id" integer NOT NULL,
"id" varchar PRIMARY KEY NOT NULL,
"user_id" integer NOT NULL,
"slot" varchar
);
CREATE TABLE "operation_reservations_vehicles" (
"_order" integer NOT NULL,
"_parent_id" integer NOT NULL,
"id" varchar PRIMARY KEY NOT NULL,
"vehicle_id" integer NOT NULL
);
CREATE TABLE "operation_reservations_cargo" (
"_order" integer NOT NULL,
"_parent_id" integer NOT NULL,
"id" varchar PRIMARY KEY NOT NULL,
"resource_id" integer NOT NULL,
"amount" numeric NOT NULL
);
CREATE TABLE "operation_reservations" (
"id" serial PRIMARY KEY NOT NULL,
"reservation_key" varchar NOT NULL,
"operation_id" varchar NOT NULL,
"mission_id" integer,
"status" "enum_operation_reservations_status" DEFAULT 'reserved' NOT NULL,
"budget_account_id" integer,
"budget_amount" numeric,
"origin_id" integer,
"destination_id" integer,
"expires_at" timestamp(3) with time zone,
"created_by_id" integer,
"settled_at" timestamp(3) with time zone,
"settled_by_effect_id" integer,
"settlement" jsonb,
"updated_at" timestamp(3) with time zone DEFAULT now() NOT NULL,
"created_at" timestamp(3) with time zone DEFAULT now() NOT NULL
);
ALTER TABLE "payload_mcp_api_keys" ADD COLUMN "operation_reservations_find" boolean DEFAULT false;
ALTER TABLE "payload_mcp_api_keys" ADD COLUMN "operation_reservations_create" boolean DEFAULT false;
ALTER TABLE "payload_mcp_api_keys" ADD COLUMN "operation_reservations_update" boolean DEFAULT false;
ALTER TABLE "payload_mcp_api_keys" ADD COLUMN "operation_reservations_delete" boolean DEFAULT false;
ALTER TABLE "payload_locked_documents_rels" ADD COLUMN "operation_reservations_id" integer;
ALTER TABLE "operation_reservations_personnel" ADD CONSTRAINT "operation_reservations_personnel_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;
ALTER TABLE "operation_reservations_personnel" ADD CONSTRAINT "operation_reservations_personnel_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."operation_reservations"("id") ON DELETE cascade ON UPDATE no action;
ALTER TABLE "operation_reservations_vehicles" ADD CONSTRAINT "operation_reservations_vehicles_vehicle_id_game_vehicles_id_fk" FOREIGN KEY ("vehicle_id") REFERENCES "public"."game_vehicles"("id") ON DELETE set null ON UPDATE no action;
ALTER TABLE "operation_reservations_vehicles" ADD CONSTRAINT "operation_reservations_vehicles_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."operation_reservations"("id") ON DELETE cascade ON UPDATE no action;
ALTER TABLE "operation_reservations_cargo" ADD CONSTRAINT "operation_reservations_cargo_resource_id_resources_id_fk" FOREIGN KEY ("resource_id") REFERENCES "public"."resources"("id") ON DELETE set null ON UPDATE no action;
ALTER TABLE "operation_reservations_cargo" ADD CONSTRAINT "operation_reservations_cargo_parent_id_fk" FOREIGN KEY ("_parent_id") REFERENCES "public"."operation_reservations"("id") ON DELETE cascade ON UPDATE no action;
ALTER TABLE "operation_reservations" ADD CONSTRAINT "operation_reservations_mission_id_missions_id_fk" FOREIGN KEY ("mission_id") REFERENCES "public"."missions"("id") ON DELETE set null ON UPDATE no action;
ALTER TABLE "operation_reservations" ADD CONSTRAINT "operation_reservations_budget_account_id_bank_accounts_id_fk" FOREIGN KEY ("budget_account_id") REFERENCES "public"."bank_accounts"("id") ON DELETE set null ON UPDATE no action;
ALTER TABLE "operation_reservations" ADD CONSTRAINT "operation_reservations_origin_id_game_structures_id_fk" FOREIGN KEY ("origin_id") REFERENCES "public"."game_structures"("id") ON DELETE set null ON UPDATE no action;
ALTER TABLE "operation_reservations" ADD CONSTRAINT "operation_reservations_destination_id_game_structures_id_fk" FOREIGN KEY ("destination_id") REFERENCES "public"."game_structures"("id") ON DELETE set null ON UPDATE no action;
ALTER TABLE "operation_reservations" ADD CONSTRAINT "operation_reservations_created_by_id_users_id_fk" FOREIGN KEY ("created_by_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;
ALTER TABLE "operation_reservations" ADD CONSTRAINT "operation_reservations_settled_by_effect_id_operation_effects_id_fk" FOREIGN KEY ("settled_by_effect_id") REFERENCES "public"."operation_effects"("id") ON DELETE set null ON UPDATE no action;
CREATE INDEX "operation_reservations_personnel_order_idx" ON "operation_reservations_personnel" USING btree ("_order");
CREATE INDEX "operation_reservations_personnel_parent_id_idx" ON "operation_reservations_personnel" USING btree ("_parent_id");
CREATE INDEX "operation_reservations_personnel_user_idx" ON "operation_reservations_personnel" USING btree ("user_id");
CREATE INDEX "operation_reservations_vehicles_order_idx" ON "operation_reservations_vehicles" USING btree ("_order");
CREATE INDEX "operation_reservations_vehicles_parent_id_idx" ON "operation_reservations_vehicles" USING btree ("_parent_id");
CREATE INDEX "operation_reservations_vehicles_vehicle_idx" ON "operation_reservations_vehicles" USING btree ("vehicle_id");
CREATE INDEX "operation_reservations_cargo_order_idx" ON "operation_reservations_cargo" USING btree ("_order");
CREATE INDEX "operation_reservations_cargo_parent_id_idx" ON "operation_reservations_cargo" USING btree ("_parent_id");
CREATE INDEX "operation_reservations_cargo_resource_idx" ON "operation_reservations_cargo" USING btree ("resource_id");
CREATE UNIQUE INDEX "operation_reservations_reservation_key_idx" ON "operation_reservations" USING btree ("reservation_key");
CREATE INDEX "operation_reservations_operation_id_idx" ON "operation_reservations" USING btree ("operation_id");
CREATE INDEX "operation_reservations_mission_idx" ON "operation_reservations" USING btree ("mission_id");
CREATE INDEX "operation_reservations_budget_budget_account_idx" ON "operation_reservations" USING btree ("budget_account_id");
CREATE INDEX "operation_reservations_origin_idx" ON "operation_reservations" USING btree ("origin_id");
CREATE INDEX "operation_reservations_destination_idx" ON "operation_reservations" USING btree ("destination_id");
CREATE INDEX "operation_reservations_created_by_idx" ON "operation_reservations" USING btree ("created_by_id");
CREATE INDEX "operation_reservations_settled_by_effect_idx" ON "operation_reservations" USING btree ("settled_by_effect_id");
CREATE INDEX "operation_reservations_updated_at_idx" ON "operation_reservations" USING btree ("updated_at");
CREATE INDEX "operation_reservations_created_at_idx" ON "operation_reservations" USING btree ("created_at");
ALTER TABLE "payload_locked_documents_rels" ADD CONSTRAINT "payload_locked_documents_rels_operation_reservations_fk" FOREIGN KEY ("operation_reservations_id") REFERENCES "public"."operation_reservations"("id") ON DELETE cascade ON UPDATE no action;
CREATE INDEX "payload_locked_documents_rels_operation_reservations_id_idx" ON "payload_locked_documents_rels" USING btree ("operation_reservations_id");`)
}
export async function down({ db, payload, req }: MigrateDownArgs): Promise<void> {
await db.execute(sql`
ALTER TABLE "operation_reservations_personnel" DISABLE ROW LEVEL SECURITY;
ALTER TABLE "operation_reservations_vehicles" DISABLE ROW LEVEL SECURITY;
ALTER TABLE "operation_reservations_cargo" DISABLE ROW LEVEL SECURITY;
ALTER TABLE "operation_reservations" DISABLE ROW LEVEL SECURITY;
DROP TABLE "operation_reservations_personnel" CASCADE;
DROP TABLE "operation_reservations_vehicles" CASCADE;
DROP TABLE "operation_reservations_cargo" CASCADE;
DROP TABLE "operation_reservations" CASCADE;
ALTER TABLE "payload_locked_documents_rels" DROP CONSTRAINT "payload_locked_documents_rels_operation_reservations_fk";
ALTER TABLE "roles_permissions" ALTER COLUMN "value" SET DATA TYPE text;
DROP TYPE "public"."enum_roles_permissions";
CREATE TYPE "public"."enum_roles_permissions" AS ENUM('system:admin-access', 'media:create', 'media:read', 'media:update', 'media:delete', 'admin:media:manage', 'users:create', 'users:read', 'users:update', 'users:delete', 'users:unlock', 'users:assign-roles', 'admin:users:manage', 'roles:create', 'roles:read', 'roles:update', 'roles:delete', 'admin:roles:manage', 'ranks:create', 'ranks:read', 'ranks:update', 'ranks:delete', 'admin:ranks:manage', 'profiles:create', 'profiles:read', 'profiles:update', 'profiles:delete', 'admin:profiles:manage', 'awards:create', 'awards:read', 'awards:update', 'awards:delete', 'admin:awards:manage', 'qualifications:create', 'qualifications:read', 'qualifications:update', 'qualifications:delete', 'admin:qualifications:manage', 'assignments:create', 'assignments:read', 'assignments:update', 'assignments:delete', 'admin:assignments:manage', 'assignment-transfers:create', 'assignment-transfers:read', 'assignment-transfers:update', 'assignment-transfers:delete', 'admin:assignment-transfers:manage', 'experience:create', 'experience:read', 'experience:update', 'experience:delete', 'admin:experience:manage', 'user-notifications:create', 'user-notifications:read', 'user-notifications:update', 'user-notifications:delete', 'admin:user-notifications:manage', 'evaluations:create', 'evaluations:read', 'evaluations:update', 'evaluations:delete', 'admin:evaluations:manage', 'missions:create', 'missions:read', 'missions:update', 'missions:delete', 'missions:read-sensitive', 'admin:missions:manage', 'mission-attendances:create', 'mission-attendances:read', 'mission-attendances:update', 'mission-attendances:delete', 'admin:mission-attendances:manage', 'campaigns:create', 'campaigns:read', 'campaigns:update', 'campaigns:delete', 'admin:campaigns:manage', 'campaigns:manage', 'factions:create', 'factions:read', 'factions:update', 'factions:delete', 'admin:factions:manage', 'technologies:create', 'technologies:read', 'technologies:update', 'technologies:delete', 'admin:technologies:manage', 'assets:create', 'assets:read', 'assets:update', 'assets:delete', 'admin:assets:manage', 'resources:create', 'resources:read', 'resources:update', 'resources:delete', 'admin:resources:manage', 'vehicles:create', 'vehicles:read', 'vehicles:update', 'vehicles:delete', 'admin:vehicles:manage', 'structures:create', 'structures:read', 'structures:update', 'structures:delete', 'admin:structures:manage', 'shipments:create', 'shipments:read', 'shipments:update', 'shipments:delete', 'admin:shipments:manage', 'bank-accounts:create', 'bank-accounts:read', 'bank-accounts:update', 'bank-accounts:delete', 'admin:bank-accounts:manage', 'bank-transactions:create', 'bank-transactions:read', 'bank-transactions:update', 'bank-transactions:delete', 'admin:bank-transactions:manage', 'ledger-entries:create', 'ledger-entries:read', 'ledger-entries:update', 'ledger-entries:delete', 'admin:ledger-entries:manage', 'locker-storages:create', 'locker-storages:read', 'locker-storages:update', 'locker-storages:delete', 'admin:locker-storages:manage', 'loadouts:create', 'loadouts:read', 'loadouts:update', 'loadouts:delete', 'admin:loadouts:manage', 'market-listings:create', 'market-listings:read', 'market-listings:update', 'market-listings:delete', 'admin:market-listings:manage', 'market-negotiations:create', 'market-negotiations:read', 'market-negotiations:update', 'market-negotiations:delete', 'admin:market-negotiations:manage', 'market-state:create', 'market-state:read', 'market-state:update', 'market-state:delete', 'admin:market-state:manage', 'projects:create', 'projects:read', 'projects:update', 'projects:delete', 'admin:projects:manage', 'labels:create', 'labels:read', 'labels:update', 'labels:delete', 'admin:labels:manage', 'sprints:create', 'sprints:read', 'sprints:update', 'sprints:delete', 'admin:sprints:manage', 'releases:create', 'releases:read', 'releases:update', 'releases:delete', 'admin:releases:manage', 'tickets:create', 'tickets:read', 'tickets:update', 'tickets:delete', 'admin:tickets:manage', 'ticket-votes:create', 'ticket-votes:read', 'ticket-votes:update', 'ticket-votes:delete', 'admin:ticket-votes:manage', 'game-structures:create', 'game-structures:read', 'game-structures:update', 'game-structures:delete', 'admin:game-structures:manage', 'game-vehicles:create', 'game-vehicles:read', 'game-vehicles:update', 'game-vehicles:delete', 'admin:game-vehicles:manage', 'game-npcs:create', 'game-npcs:read', 'game-npcs:update', 'game-npcs:delete', 'admin:game-npcs:manage', 'game-hard-resources:create', 'game-hard-resources:read', 'game-hard-resources:update', 'game-hard-resources:delete', 'admin:game-hard-resources:manage', 'game-event-logs:create', 'game-event-logs:read', 'game-event-logs:update', 'game-event-logs:delete', 'admin:game-event-logs:manage', 'maps:create', 'maps:read', 'maps:update', 'maps:delete', 'admin:maps:manage', 'map-roads:read', 'admin:map-roads:manage', 'map-zones:read', 'admin:map-zones:manage', 'resource-nodes:read', 'admin:resource-nodes:manage', 'narrative-events:create', 'narrative-events:read', 'narrative-events:update', 'narrative-events:delete', 'admin:narrative-events:manage', 'mission-files:create', 'mission-files:read', 'mission-files:update', 'mission-files:delete', 'admin:mission-files:manage', 'mod-lists:create', 'mod-lists:read', 'mod-lists:update', 'mod-lists:delete', 'admin:mod-lists:manage', 'game-servers:create', 'game-servers:read', 'game-servers:update', 'game-servers:delete', 'admin:game-servers:manage', 'arma-sync-events:create', 'arma-sync-events:read', 'arma-sync-events:update', 'arma-sync-events:delete', 'admin:arma-sync-events:manage', 'arma-commands:create', 'arma-commands:read', 'arma-commands:update', 'arma-commands:delete', 'admin:arma-commands:manage', 'operation-events:create', 'operation-events:read', 'operation-events:update', 'operation-events:delete', 'admin:operation-events:manage', 'operation-effects:create', 'operation-effects:read', 'operation-effects:update', 'operation-effects:delete', 'admin:operation-effects:manage', 'game-rules:read', 'game-rules:update', 'shims:read', 'shims:update', 'logistics:manage', 'intelligence:manage', 'profiles:intel_excerpt:update', 'banking:manage', 'tickets:staff', 'discord:staff', 'discord:announce', 'custom-minefields:update', 'custom-minefields:delete', 'custom-minefield-scores:delete', 'custom-radio-tests:update', 'custom-radio-tests:delete', 'custom-radio-test-scores:delete');
ALTER TABLE "roles_permissions" ALTER COLUMN "value" SET DATA TYPE "public"."enum_roles_permissions" USING "value"::"public"."enum_roles_permissions";
DROP INDEX "payload_locked_documents_rels_operation_reservations_id_idx";
ALTER TABLE "payload_mcp_api_keys" DROP COLUMN "operation_reservations_find";
ALTER TABLE "payload_mcp_api_keys" DROP COLUMN "operation_reservations_create";
ALTER TABLE "payload_mcp_api_keys" DROP COLUMN "operation_reservations_update";
ALTER TABLE "payload_mcp_api_keys" DROP COLUMN "operation_reservations_delete";
ALTER TABLE "payload_locked_documents_rels" DROP COLUMN "operation_reservations_id";
DROP TYPE "public"."enum_operation_reservations_status";`)
}