-- B01拥有作品身份、动态档案版本及稳定章节目录。 CREATE TABLE public.muse_work ( work_id text PRIMARY KEY, author_id text NOT NULL, current_revision bigint NOT NULL CHECK(current_revision>0), directory_revision bigint NOT NULL DEFAULT 0 CHECK(directory_revision>=0) ); CREATE TABLE public.muse_work_version ( work_id text NOT NULL REFERENCES public.muse_work(work_id), revision bigint NOT NULL CHECK(revision>0), content jsonb NOT NULL, schema_binding jsonb NOT NULL, content_hash text NOT NULL, created_at timestamptz NOT NULL DEFAULT clock_timestamp(), PRIMARY KEY(work_id,revision) ); CREATE TABLE public.muse_chapter ( chapter_id text PRIMARY KEY, work_id text NOT NULL REFERENCES public.muse_work(work_id), title text NOT NULL, position integer NOT NULL CHECK(position>0), UNIQUE(work_id,position) DEFERRABLE INITIALLY DEFERRED ); CREATE TABLE public.muse_directory_version ( work_id text NOT NULL REFERENCES public.muse_work(work_id), revision bigint NOT NULL CHECK(revision>=0), chapters jsonb NOT NULL, nodes jsonb NOT NULL DEFAULT '[]', created_at timestamptz NOT NULL DEFAULT clock_timestamp(), PRIMARY KEY(work_id,revision) ); ALTER TABLE public.muse_work ADD CONSTRAINT work_current_version_fk FOREIGN KEY(work_id,current_revision) REFERENCES public.muse_work_version(work_id,revision) DEFERRABLE INITIALLY DEFERRED; ALTER TABLE public.muse_work ADD CONSTRAINT work_current_directory_fk FOREIGN KEY(work_id,directory_revision) REFERENCES public.muse_directory_version(work_id,revision) DEFERRABLE INITIALLY DEFERRED; -- 明确选定的探索来源属于B01;保留不可变快照,不将方向选择当作规划确认。 CREATE TABLE public.muse_exploration_selection ( work_id text PRIMARY KEY REFERENCES public.muse_work(work_id), current_revision bigint NOT NULL CHECK(current_revision>0) ); CREATE TABLE public.muse_exploration_selection_version ( work_id text NOT NULL REFERENCES public.muse_work(work_id), revision bigint NOT NULL CHECK(revision>0), snapshot jsonb NOT NULL, selected_by text NOT NULL, selected_at timestamptz NOT NULL DEFAULT clock_timestamp(), PRIMARY KEY(work_id,revision) ); ALTER TABLE public.muse_exploration_selection ADD CONSTRAINT selection_current_version_fk FOREIGN KEY(work_id,current_revision) REFERENCES public.muse_exploration_selection_version(work_id,revision) DEFERRABLE INITIALLY DEFERRED; CREATE TABLE public.muse_plan ( plan_id text PRIMARY KEY, work_id text NOT NULL REFERENCES public.muse_work(work_id), type_id text NOT NULL, chapter_id text REFERENCES public.muse_chapter(chapter_id), node_id text, current_revision bigint NOT NULL CHECK(current_revision>0) ); CREATE TABLE public.muse_plan_version ( plan_id text NOT NULL REFERENCES public.muse_plan(plan_id), revision bigint NOT NULL CHECK(revision>0), request jsonb NOT NULL, content jsonb NOT NULL, content_hash text NOT NULL, schema_binding jsonb NOT NULL, source_dependencies jsonb NOT NULL, author_id text NOT NULL, created_at timestamptz NOT NULL DEFAULT clock_timestamp(), PRIMARY KEY(plan_id,revision) ); ALTER TABLE public.muse_plan ADD CONSTRAINT plan_current_version_fk FOREIGN KEY(plan_id,current_revision) REFERENCES public.muse_plan_version(plan_id,revision) DEFERRABLE INITIALLY DEFERRED; CREATE TABLE public.muse_plan_candidate ( candidate_id uuid PRIMARY KEY, work_id text NOT NULL REFERENCES public.muse_work(work_id), plan_id text NOT NULL, current_revision bigint NOT NULL CHECK(current_revision>0) ); CREATE TABLE public.muse_plan_candidate_version ( candidate_id uuid NOT NULL REFERENCES public.muse_plan_candidate(candidate_id), revision bigint NOT NULL CHECK(revision>0), request jsonb NOT NULL, content jsonb NOT NULL, source_dependencies jsonb NOT NULL, candidate_hash text NOT NULL, PRIMARY KEY(candidate_id,revision) ); ALTER TABLE public.muse_plan_candidate ADD CONSTRAINT plan_candidate_current_version_fk FOREIGN KEY(candidate_id,current_revision) REFERENCES public.muse_plan_candidate_version(candidate_id,revision) DEFERRABLE INITIALLY DEFERRED;