9155ae627c
* feat(sync): decouple the incremental-pull cursor from updated_at (#4678) `books.updated_at` was overloaded as both the incremental-pull cursor (`GET /api/sync?since=…` filters `updated_at > since`, devices keep one global `max(updated_at)` watermark) and the library "date read" sort key. A server-resolved reading-status merge had to be written with a timestamp greater than every peer's global cursor to propagate, which forced `updated_at = now()` and reordered the date-read library by sync-processing time (the #4677 symptom). Introduce a server-assigned `synced_at` column on `books`, stamped by a `BEFORE INSERT OR UPDATE` trigger on every write, used only as the pull cursor. `updated_at` stays pure client event time used only for sorting. - Migration 016 + baseline schema: add `synced_at` (NOT NULL DEFAULT now()), index `(user_id, synced_at)`, trigger `set_books_synced_at`. Backfill `synced_at = updated_at` before creating the trigger so existing devices' cursors hand over without a re-sync storm. - GET: books filters/orders on `synced_at > since` (a delete bumps synced_at, so the deleted_at clause is dropped); configs/notes stay on updated_at. - POST: extract `buildStatusPropagationRow` and drop the `updated_at = now()` bump — the trigger advances synced_at so peers re-pull the status change while updated_at (the sort key) stays put. - Client `computeMaxTimestamp` keys on synced_at, falling back to updated_at/deleted_at for pre-migration servers and configs/notes. Backward-compatible: `synced_at >= updated_at` always, so `synced_at > since` is a strict superset of `updated_at > since` — old web clients and the koplugin keep working with no data loss (at worst a redundant idempotent re-pull of rare server-merged rows). The koplugin's shared pull/push cursor is left untouched; a proper split is a follow-up. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(sync): make the books synced_at backfill safe for large live tables (#4678) The single `UPDATE … WHERE synced_at IS NULL` deadlocked on a 3.8M-row production `books` table: it rewrites every row in one transaction while the live /api/sync push path upserts books rows, and the two lock rows in opposite orders. `ALTER COLUMN … SET NOT NULL` (full-table ACCESS EXCLUSIVE scan) and a plain CREATE INDEX (write-blocking SHARE lock) compounded it. Rework migration 016 as an online migration (run via psql, not in a wrapping transaction): - backfill in small autocommitted batches via a procedure, using FOR UPDATE SKIP LOCKED so it never waits on an app-locked row; - CREATE INDEX CONCURRENTLY instead of a blocking build; - install the trigger last (so it can't clobber the updated_at backfill); - drop the hard SET NOT NULL (the default + trigger + backfill keep the column populated and the client falls back to updated_at); a NOT VALID CHECK + VALIDATE alternative is included, commented, for operators who want it. The baseline schema.sql (fresh, empty installs) keeps the simple inline NOT NULL DEFAULT now() + trigger. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
134 lines
5.7 KiB
PL/PgSQL
134 lines
5.7 KiB
PL/PgSQL
ALTER FUNCTION auth.uid() OWNER TO supabase_auth_admin;
|
|
ALTER FUNCTION auth.role() OWNER TO supabase_auth_admin;
|
|
|
|
CREATE TABLE public.books (
|
|
user_id uuid NOT NULL,
|
|
book_hash text NOT NULL,
|
|
meta_hash text NULL,
|
|
format text NULL,
|
|
title text NULL,
|
|
source_title text NULL,
|
|
author text NULL,
|
|
"group" text NULL,
|
|
tags text[] NULL,
|
|
created_at timestamp with time zone NULL DEFAULT now(),
|
|
updated_at timestamp with time zone NULL DEFAULT now(),
|
|
deleted_at timestamp with time zone NULL,
|
|
uploaded_at timestamp with time zone NULL,
|
|
synced_at timestamp with time zone NOT NULL DEFAULT now(),
|
|
progress integer[] NULL,
|
|
reading_status text NULL,
|
|
reading_status_updated_at timestamp with time zone NULL,
|
|
group_id text NULL,
|
|
group_name text NULL,
|
|
metadata json NULL,
|
|
CONSTRAINT books_pkey PRIMARY KEY (user_id, book_hash),
|
|
CONSTRAINT books_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users (id) ON DELETE CASCADE
|
|
);
|
|
|
|
-- Server-assigned incremental-pull cursor, decoupled from updated_at (the
|
|
-- client event time / sort key). A trigger stamps it on every write so a
|
|
-- server-resolved merge propagates without reordering the date-read library.
|
|
-- See migration 016_add_books_synced_at.sql (issue #4678).
|
|
CREATE INDEX idx_books_user_synced ON public.books (user_id, synced_at);
|
|
|
|
CREATE OR REPLACE FUNCTION public.set_books_synced_at()
|
|
RETURNS trigger
|
|
LANGUAGE plpgsql
|
|
AS $$
|
|
BEGIN
|
|
NEW.synced_at := now();
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
|
|
CREATE TRIGGER books_set_synced_at
|
|
BEFORE INSERT OR UPDATE ON public.books
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION public.set_books_synced_at();
|
|
|
|
ALTER TABLE public.books ENABLE ROW LEVEL SECURITY;
|
|
CREATE POLICY select_books ON public.books FOR SELECT TO authenticated USING ((SELECT auth.uid()) = user_id);
|
|
CREATE POLICY insert_books ON public.books FOR INSERT TO authenticated WITH CHECK ((SELECT auth.uid()) = user_id);
|
|
CREATE POLICY update_books ON public.books FOR UPDATE TO authenticated USING ((SELECT auth.uid()) = user_id);
|
|
CREATE POLICY delete_books ON public.books FOR DELETE TO authenticated USING ((SELECT auth.uid()) = user_id);
|
|
|
|
CREATE TABLE public.book_configs (
|
|
user_id uuid NOT NULL,
|
|
book_hash text NOT NULL,
|
|
meta_hash text NULL,
|
|
location text NULL,
|
|
xpointer text NULL,
|
|
progress jsonb NULL,
|
|
rsvp_position text NULL,
|
|
search_config jsonb NULL,
|
|
view_settings jsonb NULL,
|
|
created_at timestamp with time zone NULL DEFAULT now(),
|
|
updated_at timestamp with time zone NULL DEFAULT now(),
|
|
deleted_at timestamp with time zone NULL,
|
|
CONSTRAINT book_configs_pkey PRIMARY KEY (user_id, book_hash),
|
|
CONSTRAINT book_configs_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users (id) ON DELETE CASCADE
|
|
);
|
|
|
|
ALTER TABLE public.book_configs ENABLE ROW LEVEL SECURITY;
|
|
CREATE POLICY select_book_configs ON public.book_configs FOR SELECT TO authenticated USING ((SELECT auth.uid()) = user_id);
|
|
CREATE POLICY insert_book_configs ON public.book_configs FOR INSERT TO authenticated WITH CHECK ((SELECT auth.uid()) = user_id);
|
|
CREATE POLICY update_book_configs ON public.book_configs FOR UPDATE TO authenticated USING ((SELECT auth.uid()) = user_id);
|
|
CREATE POLICY delete_book_configs ON public.book_configs FOR DELETE TO authenticated USING ((SELECT auth.uid()) = user_id);
|
|
|
|
CREATE TABLE public.book_notes (
|
|
user_id uuid NOT NULL,
|
|
book_hash text NOT NULL,
|
|
meta_hash text NULL,
|
|
id text NOT NULL,
|
|
type text NULL,
|
|
cfi text NULL,
|
|
xpointer0 text NULL,
|
|
xpointer1 text NULL,
|
|
text text NULL,
|
|
style text NULL,
|
|
color text NULL,
|
|
note text NULL,
|
|
page integer NULL,
|
|
global boolean NULL,
|
|
created_at timestamp with time zone NULL DEFAULT now(),
|
|
updated_at timestamp with time zone NULL DEFAULT now(),
|
|
deleted_at timestamp with time zone NULL,
|
|
CONSTRAINT book_notes_pkey PRIMARY KEY (user_id, book_hash, id),
|
|
CONSTRAINT book_notes_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users (id) ON DELETE CASCADE
|
|
);
|
|
|
|
ALTER TABLE public.book_notes ENABLE ROW LEVEL SECURITY;
|
|
CREATE POLICY select_book_notes ON public.book_notes FOR SELECT TO authenticated USING ((SELECT auth.uid()) = user_id);
|
|
CREATE POLICY insert_book_notes ON public.book_notes FOR INSERT TO authenticated WITH CHECK ((SELECT auth.uid()) = user_id);
|
|
CREATE POLICY update_book_notes ON public.book_notes FOR UPDATE TO authenticated USING ((SELECT auth.uid()) = user_id);
|
|
CREATE POLICY delete_book_notes ON public.book_notes FOR DELETE TO authenticated USING ((SELECT auth.uid()) = user_id);
|
|
|
|
CREATE TABLE public.files (
|
|
id uuid NOT NULL DEFAULT gen_random_uuid(),
|
|
user_id uuid NOT NULL,
|
|
book_hash text NULL,
|
|
file_key text NOT NULL,
|
|
file_size bigint NOT NULL,
|
|
created_at timestamp with time zone NULL DEFAULT now(),
|
|
updated_at timestamp with time zone NULL DEFAULT now(),
|
|
deleted_at timestamp with time zone NULL,
|
|
CONSTRAINT files_pkey PRIMARY KEY (id),
|
|
CONSTRAINT files_file_key_key UNIQUE (file_key),
|
|
CONSTRAINT files_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users (id) ON DELETE CASCADE
|
|
);
|
|
|
|
CREATE INDEX idx_files_user_id_deleted_at ON public.files (user_id, deleted_at);
|
|
CREATE INDEX idx_files_file_key ON public.files (file_key);
|
|
CREATE INDEX idx_files_file_key_deleted_at ON public.files (file_key, deleted_at);
|
|
|
|
ALTER TABLE public.files ENABLE ROW LEVEL SECURITY;
|
|
CREATE POLICY files_insert ON public.files FOR INSERT WITH CHECK (auth.uid() = user_id);
|
|
CREATE POLICY files_select ON public.files FOR SELECT USING (auth.uid() = user_id AND deleted_at IS NULL);
|
|
CREATE POLICY files_update ON public.files FOR UPDATE USING (auth.uid() = user_id) WITH CHECK (deleted_at IS NULL OR deleted_at > now());
|
|
CREATE POLICY files_delete ON public.files FOR DELETE USING (auth.uid() = user_id);
|
|
|
|
GRANT ALL ON public.books TO authenticated;
|
|
GRANT ALL ON public.book_configs TO authenticated;
|
|
GRANT ALL ON public.book_notes TO authenticated;
|
|
GRANT ALL ON public.files TO authenticated; |