a tool for shared writing and social publishing

add function to get immutable timestamp

+15 -3
+15 -3
supabase/migrations/20260125000000_add_sort_date_column.sql
··· 2 2 -- This column stores the older of publishedAt (from JSON data) or indexed_at 3 3 -- Used for sorting feeds chronologically by when content was actually published 4 4 5 - -- Note: We use ::timestamp AT TIME ZONE 'UTC' to make the expression immutable 6 - -- (direct ::timestamptz cast is not immutable as it depends on session timezone) 5 + -- Create an immutable function to parse ISO 8601 timestamps from text 6 + -- This is needed because direct ::timestamp cast is not immutable (accepts 'now', 'today', etc.) 7 + -- The regex validates the format before casting to ensure immutability 8 + CREATE OR REPLACE FUNCTION parse_iso_timestamp(text) RETURNS timestamptz 9 + LANGUAGE sql IMMUTABLE STRICT AS $$ 10 + SELECT CASE 11 + -- Match ISO 8601 format: YYYY-MM-DDTHH:MM:SS with optional fractional seconds and Z/timezone 12 + WHEN $1 ~ '^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:?\d{2})?$' THEN 13 + $1::timestamptz 14 + ELSE 15 + NULL 16 + END 17 + $$; 18 + 7 19 ALTER TABLE documents 8 20 ADD COLUMN sort_date timestamptz GENERATED ALWAYS AS ( 9 21 LEAST( 10 - COALESCE((data->>'publishedAt')::timestamp AT TIME ZONE 'UTC', indexed_at), 22 + COALESCE(parse_iso_timestamp(data->>'publishedAt'), indexed_at), 11 23 indexed_at 12 24 ) 13 25 ) STORED;