A2: schema core (6 tables, migration, idempotent seed)

- users/roles/services/service_instances/slots/assignments with spec rules:
  - chk_slots_single_parent (Rule 26): slot has exactly one parent
  - uq_active_assignment (Rule 27): partial unique, one active assignment per slot
  - slug immutability, deactivate-only, UTC storage, America/New_York default
- uuidv7 (src/lib/uuid.ts), DST-safe tz helpers (src/lib/tz.ts)
- idempotent seed: 3 roles, 2 services, next 2 Sundays x services, 12 slots, 6 users
- migration drizzle/0000 + 0001
Verified: typecheck/lint/test/build + db:migrate + db:seed all exit 0;
service_instances=4; re-run seed idempotent; CHECK + partial unique enforced.
This commit is contained in:
Tiel Coder
2026-11-17 18:52:35 +00:00
parent c595865496
commit a68e1c6277
12 changed files with 2437 additions and 11 deletions
+109
View File
@@ -0,0 +1,109 @@
CREATE TABLE "assignments" (
"id" text PRIMARY KEY NOT NULL,
"slot_id" text NOT NULL,
"user_id" text NOT NULL,
"status" text DEFAULT 'proposed' NOT NULL,
"source" text DEFAULT 'manual' NOT NULL,
"assigned_by" text,
"assigned_at" timestamp with time zone,
"responded_at" timestamp with time zone,
"decline_reason" text,
"confirmed_via" text,
"reminder_count" integer DEFAULT 0 NOT NULL
);
--> statement-breakpoint
CREATE TABLE "roles" (
"id" text PRIMARY KEY NOT NULL,
"slug" text NOT NULL,
"name" text NOT NULL,
"description" text,
"color_hex" text,
"call_time_offset_minutes" integer,
"requires_certification" boolean DEFAULT true NOT NULL,
"default_headcount" integer DEFAULT 1 NOT NULL,
"sort_order" integer DEFAULT 0 NOT NULL,
CONSTRAINT "roles_slug_unique" UNIQUE("slug")
);
--> statement-breakpoint
CREATE TABLE "service_instances" (
"id" text PRIMARY KEY NOT NULL,
"service_id" text NOT NULL,
"occurs_on" date,
"starts_at" timestamp with time zone,
"ends_at" timestamp with time zone,
"is_special" boolean DEFAULT false NOT NULL,
"title_override" text,
"status" text DEFAULT 'draft' NOT NULL,
"published_at" timestamp with time zone,
"published_by" text,
"notes" text
);
--> statement-breakpoint
CREATE TABLE "services" (
"id" text PRIMARY KEY NOT NULL,
"name" text NOT NULL,
"location" text,
"timezone" text DEFAULT 'America/New_York' NOT NULL,
"rrule" text,
"start_time_local" text NOT NULL,
"duration_minutes" integer DEFAULT 75 NOT NULL,
"doors_open_minutes_before" integer DEFAULT 0 NOT NULL,
"active" boolean DEFAULT true NOT NULL,
"notes" text
);
--> statement-breakpoint
CREATE TABLE "slots" (
"id" text PRIMARY KEY NOT NULL,
"service_instance_id" text,
"event_id" text,
"role_id" text NOT NULL,
"slot_index" integer NOT NULL,
"call_time" timestamp with time zone,
"status" text DEFAULT 'open' NOT NULL,
"slot_note" text,
"is_trainee_slot" boolean DEFAULT false NOT NULL,
"paired_with_user_id" text,
CONSTRAINT "chk_slots_single_parent" CHECK ((service_instance_id IS NOT NULL) <> (event_id IS NOT NULL))
);
--> statement-breakpoint
CREATE TABLE "users" (
"id" text PRIMARY KEY NOT NULL,
"email" text NOT NULL,
"phone_e164" text,
"first_name" text NOT NULL,
"last_name" text NOT NULL,
"display_name" text,
"system_role" text DEFAULT 'volunteer' NOT NULL,
"status" text DEFAULT 'pending_approval' NOT NULL,
"avatar_url" text,
"timezone" text DEFAULT 'America/New_York' NOT NULL,
"emergency_contact" text,
"approved_by" text,
"approved_at" timestamp with time zone,
"last_served_at" timestamp with time zone,
"reliability_score" numeric(5, 2),
"notes" text,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone,
"deactivated_at" timestamp with time zone,
CONSTRAINT "users_email_unique" UNIQUE("email")
);
--> statement-breakpoint
ALTER TABLE "assignments" ADD CONSTRAINT "assignments_slot_id_slots_id_fk" FOREIGN KEY ("slot_id") REFERENCES "public"."slots"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "assignments" ADD CONSTRAINT "assignments_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "service_instances" ADD CONSTRAINT "service_instances_service_id_services_id_fk" FOREIGN KEY ("service_id") REFERENCES "public"."services"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "slots" ADD CONSTRAINT "slots_service_instance_id_service_instances_id_fk" FOREIGN KEY ("service_instance_id") REFERENCES "public"."service_instances"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "slots" ADD CONSTRAINT "slots_role_id_roles_id_fk" FOREIGN KEY ("role_id") REFERENCES "public"."roles"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "slots" ADD CONSTRAINT "slots_paired_with_user_id_users_id_fk" FOREIGN KEY ("paired_with_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "uq_active_assignment" ON "assignments" USING btree ("slot_id") WHERE "assignments"."status" IN ('proposed','confirmed','completed');--> statement-breakpoint
CREATE INDEX "assignments_user_idx" ON "assignments" USING btree ("user_id");--> statement-breakpoint
CREATE INDEX "roles_slug_idx" ON "roles" USING btree ("slug");--> statement-breakpoint
CREATE UNIQUE INDEX "si_service_starts_at_idx" ON "service_instances" USING btree ("service_id","starts_at");--> statement-breakpoint
CREATE INDEX "si_occurs_on_idx" ON "service_instances" USING btree ("occurs_on");--> statement-breakpoint
CREATE INDEX "si_status_idx" ON "service_instances" USING btree ("status");--> statement-breakpoint
CREATE INDEX "services_active_idx" ON "services" USING btree ("active");--> statement-breakpoint
CREATE INDEX "slots_instance_role_idx" ON "slots" USING btree ("service_instance_id","role_id");--> statement-breakpoint
CREATE INDEX "slots_status_idx" ON "slots" USING btree ("status");--> statement-breakpoint
CREATE INDEX "users_email_lower_idx" ON "users" USING btree (lower("email"));--> statement-breakpoint
CREATE INDEX "users_status_idx" ON "users" USING btree ("status");--> statement-breakpoint
CREATE INDEX "users_system_role_idx" ON "users" USING btree ("system_role");