|
| 1 | +ALTER TABLE "workspace_files" ALTER COLUMN "content_updated_at" SET DEFAULT date_trunc('milliseconds', now());--> statement-breakpoint |
| 2 | +-- The trigger is the invariant; the truncating default above only keeps the common insert path from |
| 3 | +-- paying for it. A default cannot cover the writers that matter here: explicit `CURRENT_TIMESTAMP` |
| 4 | +-- expressions and raw SQL inserts. |
| 5 | +-- |
| 6 | +-- It fires on every UPDATE rather than `UPDATE OF content_updated_at`, because a row can enter the |
| 7 | +-- search index without its revision being written: materializing a chat upload sets `context` alone, and |
| 8 | +-- `workspace_files_search_index_pending` then indexes whatever revision the row already carried. `UPDATE |
| 9 | +-- OF` is evaluated against the statement's target columns, so widening it does not make |
| 10 | +-- `workspace_files_secret_provenance_demote` fire on those metadata writes — normalizing a legacy row |
| 11 | +-- keeps its tracked provenance. The WHEN guard keeps the plpgsql call off the hot path for the compliant |
| 12 | +-- writes that are the norm, and the name sorts before both other triggers so they see the normalized |
| 13 | +-- value. Rows minted before this runs are retired by script migration |
| 14 | +-- `0018_repair_workspace_file_content_revision`, which the runner applies after every SQL migration. |
| 15 | +CREATE OR REPLACE FUNCTION workspace_file_content_version_millisecond() |
| 16 | +RETURNS trigger |
| 17 | +LANGUAGE plpgsql |
| 18 | +AS $$ |
| 19 | +BEGIN |
| 20 | + NEW.content_updated_at := date_trunc('milliseconds', NEW.content_updated_at); |
| 21 | + RETURN NEW; |
| 22 | +END; |
| 23 | +$$;--> statement-breakpoint |
| 24 | +DROP TRIGGER IF EXISTS workspace_files_content_version_millisecond ON workspace_files;--> statement-breakpoint |
| 25 | +CREATE TRIGGER workspace_files_content_version_millisecond |
| 26 | +BEFORE INSERT OR UPDATE ON workspace_files |
| 27 | +FOR EACH ROW |
| 28 | +WHEN (NEW.content_updated_at <> date_trunc('milliseconds', NEW.content_updated_at)) |
| 29 | +EXECUTE FUNCTION workspace_file_content_version_millisecond(); |
0 commit comments