Files
Art-gallery/db/migrate-sync-timestamps.sql
T
Danila KhodjaefandCursor 8e444fa461 Fix harmonize prod DB targeting and large-hall loading hangs.
Prod env files now win over root .env so harmonize:db can open gallery_prod after gallery_dev; tour_stops gains updated_at; halls no longer block the overlay on every painting texture. Docs cover the fixes and one-artist image pull from prod; Duccio painting assets synced from TrueNAS.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-26 01:26:51 +03:00

46 lines
1.1 KiB
PL/PgSQL

-- Catalog row timestamps for dev/prod harmonize (last-write-wins merge).
-- Idempotent: safe to re-run on existing databases.
CREATE OR REPLACE FUNCTION sync_touch_updated_at()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DO $$
DECLARE
tbl TEXT;
BEGIN
FOREACH tbl IN ARRAY ARRAY[
'historical_eras',
'art_movements',
'artists',
'artist_periods',
'paintings',
'painting_influences',
'painting_influence_sources',
'painting_annotations',
'tour_stops'
]
LOOP
-- Skip tables not created yet (e.g. tour_stops before migrate-tours.sql).
IF to_regclass(format('public.%I', tbl)) IS NULL THEN
CONTINUE;
END IF;
EXECUTE format(
'ALTER TABLE %I ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ NOT NULL DEFAULT now()',
tbl
);
EXECUTE format('DROP TRIGGER IF EXISTS %I ON %I', tbl || '_updated_at', tbl);
EXECUTE format(
'CREATE TRIGGER %I BEFORE UPDATE ON %I FOR EACH ROW EXECUTE PROCEDURE sync_touch_updated_at()',
tbl || '_updated_at',
tbl
);
END LOOP;
END $$;