Merge latest dashboard and Turbo setup into dev while preserving workspace features

This commit is contained in:
2026-11-04 23:16:28 +08:00
383 changed files with 3400 additions and 18527 deletions
+65
View File
@@ -0,0 +1,65 @@
BEGIN;
CREATE TABLE IF NOT EXISTS schema_migrations(version text PRIMARY KEY, applied_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE IF NOT EXISTS assets (
id uuid PRIMARY KEY, kind text NOT NULL CHECK(kind IN ('image','video','audio','text')),
filename text, storage_key text UNIQUE, mime_type text, byte_size bigint CHECK(byte_size > 0),
sha256 text, body text, width integer, height integer, duration double precision,
has_audio boolean, processing_status text NOT NULL DEFAULT 'ready' CHECK(processing_status IN ('ready','pending','processing','failed')),
processing_error text, created_at timestamptz NOT NULL DEFAULT now(),
CHECK ((kind='text' AND body IS NOT NULL AND storage_key IS NULL) OR (kind<>'text' AND storage_key IS NOT NULL AND byte_size > 0))
);
CREATE TABLE IF NOT EXISTS asset_variants (
asset_id uuid NOT NULL REFERENCES assets(id) ON DELETE CASCADE,
role text NOT NULL CHECK(role IN ('silent_video','audio_track','poster')),
storage_key text NOT NULL UNIQUE, mime_type text NOT NULL, byte_size bigint NOT NULL CHECK(byte_size>0),
PRIMARY KEY(asset_id,role)
);
CREATE TABLE IF NOT EXISTS posts (
id uuid PRIMARY KEY, slug text NOT NULL UNIQUE,
kind text NOT NULL CHECK(kind IN ('image','video','audio','article','motion')),
title text NOT NULL CHECK(length(trim(title))>0), page_key text,
cover_asset_id uuid REFERENCES assets(id), created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(),
CHECK((kind IN ('article','motion') AND page_key IS NOT NULL) OR (kind IN ('image','video','audio') AND page_key IS NULL))
);
CREATE TABLE IF NOT EXISTS post_assets (
post_id uuid NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
asset_id uuid NOT NULL REFERENCES assets(id) ON DELETE RESTRICT,
position integer NOT NULL CHECK(position>=0), role text NOT NULL DEFAULT 'content' CHECK(role IN ('content','caption')),
PRIMARY KEY(post_id,position)
);
CREATE INDEX IF NOT EXISTS post_assets_asset ON post_assets(asset_id);
CREATE TABLE IF NOT EXISTS tags (
id uuid PRIMARY KEY, parent_id uuid REFERENCES tags(id) ON DELETE RESTRICT,
slug text NOT NULL CHECK(slug ~ '^[a-z0-9][a-z0-9-]*$'), name text NOT NULL CHECK(length(trim(name))>0),
UNIQUE NULLS NOT DISTINCT(parent_id,slug), CHECK(id IS DISTINCT FROM parent_id)
);
CREATE OR REPLACE FUNCTION prevent_tag_cycle() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
-- Serialize taxonomy edits so concurrent moves cannot introduce a cycle.
PERFORM pg_advisory_xact_lock(718131);
IF NEW.parent_id IS NOT NULL AND EXISTS (
WITH RECURSIVE ancestors AS (
SELECT id,parent_id FROM tags WHERE id=NEW.parent_id
UNION ALL SELECT t.id,t.parent_id FROM tags t JOIN ancestors a ON t.id=a.parent_id
) SELECT 1 FROM ancestors WHERE id=NEW.id
) THEN RAISE EXCEPTION 'Tag hierarchy cannot contain a cycle'; END IF;
RETURN NEW;
END $$;
DROP TRIGGER IF EXISTS tags_no_cycles ON tags;
CREATE TRIGGER tags_no_cycles BEFORE INSERT OR UPDATE ON tags FOR EACH ROW EXECUTE FUNCTION prevent_tag_cycle();
CREATE TABLE IF NOT EXISTS post_tags(post_id uuid REFERENCES posts(id) ON DELETE CASCADE,tag_id uuid REFERENCES tags(id) ON DELETE CASCADE,PRIMARY KEY(post_id,tag_id));
CREATE TABLE IF NOT EXISTS asset_tags(asset_id uuid REFERENCES assets(id) ON DELETE CASCADE,tag_id uuid REFERENCES tags(id) ON DELETE CASCADE,PRIMARY KEY(asset_id,tag_id));
CREATE TABLE IF NOT EXISTS media_jobs (
id uuid PRIMARY KEY, asset_id uuid NOT NULL UNIQUE REFERENCES assets(id) ON DELETE CASCADE,
status text NOT NULL DEFAULT 'pending' CHECK(status IN ('pending','processing','ready','failed')),
attempts integer NOT NULL DEFAULT 0, available_at timestamptz NOT NULL DEFAULT now(), locked_at timestamptz, last_error text
);
CREATE TABLE IF NOT EXISTS contribution_events (
id uuid PRIMARY KEY, kind text NOT NULL, subject_type text NOT NULL, subject_id uuid,
amount integer NOT NULL DEFAULT 1 CHECK(amount>0), occurred_at timestamptz NOT NULL DEFAULT now(),
idempotency_key text UNIQUE, metadata jsonb NOT NULL DEFAULT '{}'
);
CREATE INDEX IF NOT EXISTS contribution_events_time ON contribution_events(occurred_at);
CREATE INDEX IF NOT EXISTS posts_timeline ON posts(created_at DESC,id DESC);
INSERT INTO schema_migrations(version) VALUES('001_library') ON CONFLICT DO NOTHING;
COMMIT;
+5
View File
@@ -0,0 +1,5 @@
BEGIN;
CREATE TABLE page_snapshots(post_id uuid NOT NULL REFERENCES posts(id),page_index integer NOT NULL CHECK(page_index BETWEEN 1 AND 8),asset_id uuid NOT NULL REFERENCES assets(id),rendered_at timestamptz NOT NULL DEFAULT now(),revision text NOT NULL,PRIMARY KEY(post_id,page_index));
CREATE TABLE render_jobs(post_id uuid PRIMARY KEY REFERENCES posts(id),status text NOT NULL CHECK(status IN ('pending','processing','ready','failed')),revision text NOT NULL,attempts integer NOT NULL DEFAULT 0,target_page integer CHECK(target_page BETWEEN 1 AND 8),locked_at timestamptz,error text);
INSERT INTO schema_migrations(version) VALUES('002_page_snapshots');
COMMIT;
@@ -0,0 +1,9 @@
BEGIN;
ALTER TABLE posts ADD COLUMN IF NOT EXISTS description text NOT NULL DEFAULT '';
UPDATE posts p SET description=left(COALESCE((
SELECT a.body FROM post_assets pa JOIN assets a ON a.id=pa.asset_id
WHERE pa.post_id=p.id AND a.kind='text' AND a.body IS NOT NULL
ORDER BY pa.position LIMIT 1
),p.title),200) WHERE p.description='';
INSERT INTO schema_migrations(version) VALUES('003_note_descriptions') ON CONFLICT DO NOTHING;
COMMIT;
+48
View File
@@ -0,0 +1,48 @@
BEGIN;
-- Preserve references before removing the old screenshot catalogue.
INSERT INTO post_assets(post_id,asset_id,position,role)
SELECT ps.post_id,ps.asset_id,
COALESCE((SELECT max(pa.position)+1 FROM post_assets pa WHERE pa.post_id=ps.post_id),0)
+row_number() OVER(PARTITION BY ps.post_id ORDER BY ps.page_index)::integer-1,'content'
FROM page_snapshots ps WHERE NOT EXISTS(SELECT 1 FROM post_assets pa WHERE pa.post_id=ps.post_id AND pa.asset_id=ps.asset_id);
DROP TABLE render_jobs;
DROP TABLE page_snapshots;
ALTER TABLE posts DROP COLUMN page_key;
-- Every processed file is an ordinary asset, retaining its original storage key.
ALTER TABLE assets ADD COLUMN source_asset_id uuid REFERENCES assets(id) ON DELETE RESTRICT;
ALTER TABLE assets ADD COLUMN derivation text CHECK(derivation IN ('silent_video','audio_track','poster'));
ALTER TABLE assets ADD CONSTRAINT assets_derivation_source CHECK((source_asset_id IS NULL)=(derivation IS NULL));
CREATE UNIQUE INDEX assets_source_derivation ON assets(source_asset_id,derivation) WHERE source_asset_id IS NOT NULL;
INSERT INTO assets(id,kind,filename,storage_key,mime_type,byte_size,source_asset_id,derivation)
SELECT md5(v.storage_key)::uuid,CASE v.role WHEN 'poster' THEN 'image' WHEN 'audio_track' THEN 'audio' ELSE 'video' END,
v.role,v.storage_key,v.mime_type,v.byte_size,v.asset_id,v.role FROM asset_variants v WHERE true
ON CONFLICT(storage_key) DO UPDATE SET source_asset_id=excluded.source_asset_id,derivation=excluded.derivation;
ALTER TABLE post_assets DROP CONSTRAINT post_assets_role_check;
ALTER TABLE post_assets ADD CONSTRAINT post_assets_role_check CHECK(role IN ('content','caption','silent_video','audio_track','poster'));
INSERT INTO post_assets(post_id,asset_id,position,role)
SELECT sources.post_id,a.id,
COALESCE((SELECT max(pa.position)+1 FROM post_assets pa WHERE pa.post_id=sources.post_id),0)
+row_number() OVER(PARTITION BY sources.post_id ORDER BY a.id)::integer-1,a.derivation
FROM (SELECT DISTINCT post_id,asset_id FROM post_assets WHERE role='content') sources
JOIN assets a ON a.source_asset_id=sources.asset_id
WHERE NOT EXISTS(SELECT 1 FROM post_assets pa WHERE pa.post_id=sources.post_id AND pa.asset_id=a.id);
DROP TABLE asset_variants;
-- Flatten tags, merging equal names without losing either kind of association.
DROP TRIGGER tags_no_cycles ON tags;
DROP FUNCTION prevent_tag_cycle();
CREATE TEMP TABLE tag_slugs ON COMMIT DROP AS SELECT id,slug,row_number() OVER(PARTITION BY slug ORDER BY id) AS rank FROM tags;
UPDATE tags SET slug='tag-'||replace(id::text,'-','');
UPDATE tags SET parent_id=NULL;
CREATE TEMP TABLE tag_merge ON COMMIT DROP AS
SELECT id,first_value(id) OVER(PARTITION BY lower(btrim(name)) ORDER BY id) AS keep_id FROM tags;
INSERT INTO post_tags(post_id,tag_id) SELECT pt.post_id,m.keep_id FROM post_tags pt JOIN tag_merge m ON m.id=pt.tag_id WHERE true ON CONFLICT DO NOTHING;
INSERT INTO asset_tags(asset_id,tag_id) SELECT at.asset_id,m.keep_id FROM asset_tags at JOIN tag_merge m ON m.id=at.tag_id WHERE true ON CONFLICT DO NOTHING;
DELETE FROM tags t USING tag_merge m WHERE t.id=m.id AND m.id<>m.keep_id;
ALTER TABLE tags DROP COLUMN parent_id;
UPDATE tags t SET slug=CASE WHEN s.rank=1 THEN s.slug ELSE 'tag-'||replace(t.id::text,'-','') END FROM tag_slugs s WHERE s.id=t.id;
CREATE UNIQUE INDEX tags_slug_unique ON tags(slug);
CREATE UNIQUE INDEX tags_name_unique ON tags(lower(btrim(name)));
INSERT INTO schema_migrations(version) VALUES('004_flat_notes');
COMMIT;
+15
View File
@@ -0,0 +1,15 @@
BEGIN;
ALTER TABLE posts ADD COLUMN edited_at timestamptz;
-- Code-authored Wiki pages retain their public IDs; saved edits outlive deployments.
CREATE TABLE note_edits (
page_id text PRIMARY KEY CHECK(page_id ~ '^[0-9a-f]{24}$'),
title text NOT NULL CHECK(length(btrim(title))>0), description text NOT NULL DEFAULT '',
markdown text NOT NULL DEFAULT '', updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE note_edit_tags (
page_id text NOT NULL REFERENCES note_edits(page_id) ON DELETE CASCADE,
tag_id uuid NOT NULL REFERENCES tags(id) ON DELETE RESTRICT,
PRIMARY KEY(page_id,tag_id)
);
INSERT INTO schema_migrations(version) VALUES('005_note_editing');
COMMIT;
@@ -0,0 +1,5 @@
BEGIN;
-- API-authored notes may retain a legacy 24-character public ID without code registration.
ALTER TABLE posts ADD COLUMN public_id text UNIQUE CHECK(public_id ~ '^[0-9a-f]{24}$');
INSERT INTO schema_migrations(version) VALUES('006_post_public_ids');
COMMIT;
@@ -0,0 +1,9 @@
BEGIN;
CREATE TABLE IF NOT EXISTS auth_login_state (
id text PRIMARY KEY,
attempts integer NOT NULL DEFAULT 0,
window_start timestamptz NOT NULL DEFAULT clock_timestamp(),
last_step bigint NOT NULL DEFAULT -1
);
INSERT INTO schema_migrations(version) VALUES ('007_dynamic_code_auth') ON CONFLICT DO NOTHING;
COMMIT;
+13
View File
@@ -0,0 +1,13 @@
BEGIN;
ALTER TABLE tags ADD COLUMN kind text NOT NULL DEFAULT 'normal' CHECK(kind IN ('normal','system'));
ALTER TABLE tags ADD COLUMN system_key text UNIQUE;
ALTER TABLE tags ADD CONSTRAINT tags_system_identity CHECK(
(kind='normal' AND system_key IS NULL) OR
(kind='system' AND system_key IS NOT NULL AND system_key IN ('document-entry','reference-image')));
-- Reserve system names without discarding associations if an ordinary tag already used one.
INSERT INTO tags(id,name,slug,kind,system_key) VALUES
('6f890bd2-9f64-4781-8a21-651467ab7101','文档入口','document-entry','system','document-entry'),
('6f890bd2-9f64-4781-8a21-651467ab7102','参考图','reference-image','system','reference-image')
ON CONFLICT(lower(btrim(name))) DO UPDATE SET kind='system',system_key=excluded.system_key;
INSERT INTO schema_migrations(version) VALUES('008_system_tags');
COMMIT;
+13
View File
@@ -0,0 +1,13 @@
BEGIN;
CREATE TABLE IF NOT EXISTS studio_designs (
id uuid PRIMARY KEY,
layout_id text NOT NULL CHECK (layout_id ~ '^ref-(0[1-9]|1[0-9]|2[0-7])$'),
name text NOT NULL CHECK (char_length(name) BETWEEN 1 AND 120),
parameters jsonb NOT NULL CHECK (jsonb_typeof(parameters)='object'),
revision integer NOT NULL DEFAULT 1 CHECK (revision > 0),
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS studio_designs_layout_created ON studio_designs(layout_id,created_at DESC,id DESC);
INSERT INTO schema_migrations(version) VALUES('009_studio_designs');
COMMIT;
@@ -0,0 +1,12 @@
BEGIN;
ALTER TABLE studio_designs ADD COLUMN IF NOT EXISTS post_id uuid REFERENCES posts(id) ON DELETE RESTRICT;
CREATE UNIQUE INDEX IF NOT EXISTS studio_designs_post ON studio_designs(post_id) WHERE post_id IS NOT NULL;
-- Adopt explicitly published entries, including the user's page repaired before deployment.
UPDATE studio_designs d SET post_id=p.id
FROM posts p
WHERE d.post_id IS NULL AND p.slug='studio-design-'||d.id::text
AND EXISTS(SELECT 1 FROM post_assets pa JOIN assets a ON a.id=pa.asset_id
WHERE pa.post_id=p.id AND a.kind='text'
AND strpos(a.body,'/studio/layouts/'||d.layout_id||'?id='||d.id::text)>0);
INSERT INTO schema_migrations(version) VALUES('010_studio_design_posts') ON CONFLICT DO NOTHING;
COMMIT;
+6
View File
@@ -0,0 +1,6 @@
BEGIN;
ALTER TABLE posts ADD COLUMN IF NOT EXISTS archived boolean NOT NULL DEFAULT false;
ALTER TABLE posts ADD COLUMN IF NOT EXISTS archived_at timestamptz;
CREATE INDEX IF NOT EXISTS posts_active_timeline ON posts(created_at DESC,id DESC) WHERE NOT archived;
INSERT INTO schema_migrations(version) VALUES('011_post_archiving') ON CONFLICT DO NOTHING;
COMMIT;
+7
View File
@@ -0,0 +1,7 @@
BEGIN;
ALTER TABLE studio_designs DROP CONSTRAINT studio_designs_layout_id_check;
ALTER TABLE studio_designs ADD CONSTRAINT studio_designs_layout_id_check CHECK (layout_id='standard-01' OR layout_id ~ '^ref-(0[1-9]|1[0-9]|2[0-7])$');
ALTER TABLE studio_designs ADD COLUMN archived boolean NOT NULL DEFAULT false;
ALTER TABLE studio_designs ADD COLUMN archived_at timestamptz;
INSERT INTO schema_migrations(version) VALUES('012_studio_library');
COMMIT;
+5
View File
@@ -0,0 +1,5 @@
BEGIN;
-- The cover continues to use the existing design/post identity and archival lifecycle.
ALTER TABLE studio_designs ADD COLUMN IF NOT EXISTS document jsonb CHECK(document IS NULL OR jsonb_typeof(document)='object');
INSERT INTO schema_migrations(version) VALUES('013_studio_series') ON CONFLICT DO NOTHING;
COMMIT;
+22
View File
@@ -0,0 +1,22 @@
BEGIN;
CREATE TABLE account_users (
id uuid PRIMARY KEY,
email text NOT NULL UNIQUE,
password_hash text NOT NULL,
verified_at timestamptz,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE account_tokens (
digest text PRIMARY KEY,
user_id uuid NOT NULL REFERENCES account_users(id) ON DELETE CASCADE,
kind text NOT NULL CHECK (kind IN ('verify','session')),
expires_at timestamptz NOT NULL
);
CREATE INDEX account_tokens_expiry ON account_tokens(expires_at);
CREATE TABLE account_limits (
key text PRIMARY KEY,
attempts integer NOT NULL,
window_start timestamptz NOT NULL
);
INSERT INTO schema_migrations(version) VALUES ('014_email_accounts');
COMMIT;
@@ -0,0 +1,7 @@
BEGIN;
CREATE TABLE guidance_documents(id text PRIMARY KEY CHECK(id ~ '^user-[a-z0-9-]{1,74}$'), owner_id uuid NOT NULL REFERENCES account_users(id), latest integer NOT NULL DEFAULT 0, deleted_at timestamptz, CHECK(id LIKE 'user-' || owner_id::text || '-%'));
CREATE TABLE guidance_revisions(document_id text NOT NULL REFERENCES guidance_documents(id), revision integer NOT NULL CHECK(revision>0), content jsonb NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY(document_id,revision));
CREATE FUNCTION guidance_immutable() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'Published revisions are immutable'; END $$;
CREATE TRIGGER guidance_immutable BEFORE UPDATE OR DELETE ON guidance_revisions FOR EACH ROW EXECUTE FUNCTION guidance_immutable();
INSERT INTO schema_migrations(version) VALUES('015_guidance_documents');
COMMIT;
@@ -0,0 +1,23 @@
BEGIN;
CREATE TABLE canvas_documents (
id uuid PRIMARY KEY, owner_id uuid NOT NULL REFERENCES account_users(id) ON DELETE CASCADE,
name text NOT NULL DEFAULT '未命名画布' CHECK(length(name) BETWEEN 1 AND 200),
revision integer NOT NULL DEFAULT 1 CHECK(revision>0), document_key text NOT NULL,
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX canvas_owner_updated ON canvas_documents(owner_id,updated_at DESC);
CREATE TABLE canvas_agent_links (
digest text PRIMARY KEY, canvas_id uuid NOT NULL REFERENCES canvas_documents(id) ON DELETE CASCADE,
expires_at timestamptz NOT NULL, last_seen_at timestamptz
);
CREATE INDEX canvas_link_expiry ON canvas_agent_links(canvas_id,expires_at);
CREATE TABLE canvas_tasks (
id uuid PRIMARY KEY, canvas_id uuid NOT NULL REFERENCES canvas_documents(id) ON DELETE CASCADE,
target_id text NOT NULL, source_revision integer NOT NULL,
status text NOT NULL DEFAULT 'queued' CHECK(status IN ('queued','running','succeeded','failed')),
request_key text NOT NULL, result_digest text, error text,
created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE UNIQUE INDEX canvas_target_active_task ON canvas_tasks(canvas_id,target_id) WHERE status IN ('queued','running');
INSERT INTO schema_migrations(version) VALUES ('016_canvas_workspace');
COMMIT;
+5
View File
@@ -0,0 +1,5 @@
BEGIN;
ALTER TABLE account_tokens DROP CONSTRAINT account_tokens_kind_check;
ALTER TABLE account_tokens ADD CONSTRAINT account_tokens_kind_check CHECK (kind IN ('verify','session','reset'));
INSERT INTO schema_migrations(version) VALUES ('017_password_reset');
COMMIT;
+22
View File
@@ -0,0 +1,22 @@
BEGIN;
CREATE TABLE workspaces (
id uuid PRIMARY KEY,
owner_id uuid NOT NULL REFERENCES account_users(id),
name text NOT NULL CHECK (char_length(name) BETWEEN 1 AND 40),
purpose text NOT NULL CHECK (purpose IN ('工作与团队','个人创作','学习与研究')),
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE workspace_members (
workspace_id uuid NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
user_id uuid NOT NULL REFERENCES account_users(id) ON DELETE CASCADE,
PRIMARY KEY (workspace_id, user_id)
);
CREATE INDEX workspace_members_user ON workspace_members(user_id);
CREATE TABLE workspace_invites (
digest text PRIMARY KEY,
workspace_id uuid NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
expires_at timestamptz NOT NULL
);
CREATE INDEX workspace_invites_workspace ON workspace_invites(workspace_id);
INSERT INTO schema_migrations(version) VALUES ('018_workspaces');
COMMIT;