An open-source, faster Devin alternative
TypeScript
add more triggers
Brian Reardond23da193940d
diff --git a/internal/db/migrations/041_updated_at_triggers_remaining.down.sql b/internal/db/migrations/041_updated_at_triggers_remaining.down.sqlnew file mode 100644index 00000000..4c11713e--- /dev/null+++ b/internal/db/migrations/041_updated_at_triggers_remaining.down.sql@@ -0,0 +1,34 @@+-- Reverse 041: drop the set_updated_at triggers added to the four tables.+-- Leaves set_updated_at() in place (owned by 040).++DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'orgs') THEN+ DROP TRIGGER IF EXISTS orgs_set_updated_at ON orgs;+ END IF;+END $$;++DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'secret_stores') THEN+ DROP TRIGGER IF EXISTS secret_stores_set_updated_at ON secret_stores;+ END IF;+END $$;++DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'secret_store_entries') THEN+ DROP TRIGGER IF EXISTS secret_store_entries_set_updated_at ON secret_store_entries;+ END IF;+END $$;++DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'agent_subscriptions') THEN+ DROP TRIGGER IF EXISTS agent_subscriptions_set_updated_at ON agent_subscriptions;+ END IF;+END $$;diff --git a/internal/db/migrations/041_updated_at_triggers_remaining.up.sql b/internal/db/migrations/041_updated_at_triggers_remaining.up.sqlnew file mode 100644index 00000000..eaff64fb--- /dev/null+++ b/internal/db/migrations/041_updated_at_triggers_remaining.up.sql@@ -0,0 +1,71 @@+-- Add set_updated_at triggers to the source tables that already had an+-- updated_at column before migration 040 and so were skipped by it:+-- orgs, secret_stores, secret_store_entries, agent_subscriptions+--+-- Why this is needed: the PG → D1 cutover delta sync uses updated_at as its+-- watermark across every source table. Migration 040 added a BEFORE UPDATE+-- trigger to the tables that lacked the column, but skipped these four because+-- they already had it. Their updated_at is therefore only bumped by app code+-- that explicitly sets it — and at least one production path does NOT:+-- internal/db/usage.go — `UPDATE orgs SET last_usage_reported_at = $2 ...`+-- (the usage reporter, every billing tick). That column IS mirrored into D1,+-- so without a trigger it goes stale and delta sync silently misses the update.+-- A trigger makes the watermark reliable on every write path, present and+-- future. Setting now() in the trigger is harmless alongside app code that+-- also sets it.+--+-- Wrapped per-table in IF EXISTS so it's safe across schema-drifted fleets.++-- set_updated_at() is created by 040; CREATE OR REPLACE here too so this+-- migration is self-contained and idempotent.+CREATE OR REPLACE FUNCTION set_updated_at()+RETURNS TRIGGER AS $$+BEGIN+ NEW.updated_at = now();+ RETURN NEW;+END;+$$ LANGUAGE plpgsql;++DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'orgs') THEN+ DROP TRIGGER IF EXISTS orgs_set_updated_at ON orgs;+ CREATE TRIGGER orgs_set_updated_at+ BEFORE UPDATE ON orgs+ FOR EACH ROW EXECUTE FUNCTION set_updated_at();+ END IF;+END $$;++DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'secret_stores') THEN+ DROP TRIGGER IF EXISTS secret_stores_set_updated_at ON secret_stores;+ CREATE TRIGGER secret_stores_set_updated_at+ BEFORE UPDATE ON secret_stores+ FOR EACH ROW EXECUTE FUNCTION set_updated_at();+ END IF;+END $$;++DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'secret_store_entries') THEN+ DROP TRIGGER IF EXISTS secret_store_entries_set_updated_at ON secret_store_entries;+ CREATE TRIGGER secret_store_entries_set_updated_at+ BEFORE UPDATE ON secret_store_entries+ FOR EACH ROW EXECUTE FUNCTION set_updated_at();+ END IF;+END $$;++DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'agent_subscriptions') THEN+ DROP TRIGGER IF EXISTS agent_subscriptions_set_updated_at ON agent_subscriptions;+ CREATE TRIGGER agent_subscriptions_set_updated_at+ BEFORE UPDATE ON agent_subscriptions+ FOR EACH ROW EXECUTE FUNCTION set_updated_at();+ END IF;+END $$;diff --git a/internal/db/store.go b/internal/db/store.goindex 57227a72..bcee5716 100644--- a/internal/db/store.go+++ b/internal/db/store.go@@ -131,6 +131,7 @@ func (s *Store) Migrate(ctx context.Context) error {{38, "migrations/038_hibernation_upload_status.up.sql"},{39, "migrations/039_checkpoint_failure_detail.up.sql"},{40, "migrations/040_add_updated_at.up.sql"},+ {41, "migrations/041_updated_at_triggers_remaining.up.sql"},}for _, m := range migrations {