Files
lobehub__lobehub/packages/database/migrations/0119_add_acceptances.sql
Arvin Xu 7cd62b70cf 🗃️ feat(db): add acceptance aggregates (#16899)
* 🗃️ feat(db): add acceptance aggregates

* ♻️ refactor(db): reuse verify reports for acceptances

* 🗃️ feat(db): add acceptance visual render and delivered status

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* 📝 docs(db): narrow finalVerifyRunId to write-once closure semantics

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* 🗃️ refactor(db): drop redundant acceptances.final_verify_run_id

A terminal status freezes the chain, so the closing round is always
current_verify_run_id; mid-loop rejections are per-round events on
verify_runs. The pointer would either lose the rejection trail
(overwritten) or duplicate current (write-once).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* 🗃️ feat(db): add per-round user acceptance decision to verify_runs

The user's accept/reject on a round is the human verdict that drives the
acceptance loop, so it lives top-level on verify_runs (not in metadata):

- user_decision (text, indexed) — the queryable verb; free-form, not an
  enum column, so a new decision verb needs no migration. Null until decided.
- decision_detail (jsonb) — provenance bag: comment, fileIds, decidedAt/By.

Per-round because every delivered round can be judged again; the trail
lives on each round rather than a single overwritten aggregate pointer.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* 🗃️ fix(db): require round index on acceptance-linked verify runs

The verify_runs_acceptance_round_unique index could not order a chain when
round_index was null: Postgres treats NULLs as distinct, so several
null-round rows could pile onto one acceptance_id. Add a CHECK constraint
(acceptance_id IS NULL OR round_index IS NOT NULL) so a linked run always
occupies exactly one position; both columns stay nullable for
standalone/legacy runs where neither is set.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* 🗃️ refactor(db): drop derivable acceptance run/report pointers

root/current verify run and latest report are all derivable from the
round chain via the verify_runs (acceptance_id, round_index) unique
index — root = min round, current = max round, latest report = report of
the highest round that has one. The denormalized pointers bought no read
win at this per-user, bounded list scale and only added a write-time
sync burden plus a staleness bug surface, so drop the three columns and
their indexes. The composite unique index that backs the derivation stays.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-15 19:23:58 +08:00

38 lines
3.5 KiB
SQL

CREATE TABLE IF NOT EXISTS "acceptances" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"user_id" text NOT NULL,
"workspace_id" text,
"subject_type" text NOT NULL,
"subject_id" text NOT NULL,
"status" text DEFAULT 'pending' NOT NULL,
"requirement" text,
"config" jsonb DEFAULT '{}'::jsonb,
"visual_render" jsonb,
"metadata" jsonb,
"completed_at" timestamp with time zone,
"accessed_at" timestamp with time zone DEFAULT now() NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "verify_runs" ADD COLUMN IF NOT EXISTS "acceptance_id" uuid;--> statement-breakpoint
ALTER TABLE "verify_runs" ADD COLUMN IF NOT EXISTS "round_index" integer;--> statement-breakpoint
ALTER TABLE "verify_runs" ADD COLUMN IF NOT EXISTS "user_decision" text;--> statement-breakpoint
ALTER TABLE "verify_runs" ADD COLUMN IF NOT EXISTS "decision_detail" jsonb;--> statement-breakpoint
ALTER TABLE "acceptances" DROP CONSTRAINT IF EXISTS "acceptances_user_id_users_id_fk";--> statement-breakpoint
ALTER TABLE "acceptances" ADD CONSTRAINT "acceptances_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "acceptances" DROP CONSTRAINT IF EXISTS "acceptances_workspace_id_workspaces_id_fk";--> statement-breakpoint
ALTER TABLE "acceptances" ADD CONSTRAINT "acceptances_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "acceptances_user_id_idx" ON "acceptances" USING btree ("user_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "acceptances_workspace_id_idx" ON "acceptances" USING btree ("workspace_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "acceptances_subject_idx" ON "acceptances" USING btree ("subject_type","subject_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "acceptances_status_idx" ON "acceptances" USING btree ("status");--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "acceptances_personal_subject_unique" ON "acceptances" USING btree ("user_id","subject_type","subject_id") WHERE "acceptances"."workspace_id" IS NULL;--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "acceptances_workspace_subject_unique" ON "acceptances" USING btree ("workspace_id","subject_type","subject_id") WHERE "acceptances"."workspace_id" IS NOT NULL;--> statement-breakpoint
ALTER TABLE "verify_runs" DROP CONSTRAINT IF EXISTS "verify_runs_acceptance_id_acceptances_id_fk";--> statement-breakpoint
ALTER TABLE "verify_runs" ADD CONSTRAINT "verify_runs_acceptance_id_acceptances_id_fk" FOREIGN KEY ("acceptance_id") REFERENCES "public"."acceptances"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "verify_runs_acceptance_id_idx" ON "verify_runs" USING btree ("acceptance_id");--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "verify_runs_acceptance_round_unique" ON "verify_runs" USING btree ("acceptance_id","round_index");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "verify_runs_user_decision_idx" ON "verify_runs" USING btree ("user_decision");--> statement-breakpoint
ALTER TABLE "verify_runs" DROP CONSTRAINT IF EXISTS "verify_runs_acceptance_requires_round";--> statement-breakpoint
ALTER TABLE "verify_runs" ADD CONSTRAINT "verify_runs_acceptance_requires_round" CHECK ("verify_runs"."acceptance_id" IS NULL OR "verify_runs"."round_index" IS NOT NULL);