An open-source, faster Devin alternative
TypeScript
make defensive
Brian Reardonb3f6430c5832
diff --git a/internal/db/migrations/040_add_updated_at.down.sql b/internal/db/migrations/040_add_updated_at.down.sqlindex 5c6518c7..b72cf01a 100644--- a/internal/db/migrations/040_add_updated_at.down.sql+++ b/internal/db/migrations/040_add_updated_at.down.sql@@ -1,19 +1,67 @@-- Reverse 040_add_updated_at: drop triggers, columns, and the shared function.+-- Wrapped per-table in IF EXISTS so partial-applied envs reverse cleanly.-DROP TRIGGER IF EXISTS users_set_updated_at ON users;-DROP TRIGGER IF EXISTS api_keys_set_updated_at ON api_keys;-DROP TRIGGER IF EXISTS org_memberships_set_updated_at ON org_memberships;-DROP TRIGGER IF EXISTS templates_set_updated_at ON templates;-DROP TRIGGER IF EXISTS sandbox_sessions_set_updated_at ON sandbox_sessions;-DROP TRIGGER IF EXISTS sandbox_checkpoints_set_updated_at ON sandbox_checkpoints;-DROP TRIGGER IF EXISTS org_subscription_items_set_updated_at ON org_subscription_items;--ALTER TABLE users DROP COLUMN IF EXISTS updated_at;-ALTER TABLE api_keys DROP COLUMN IF EXISTS updated_at;-ALTER TABLE org_memberships DROP COLUMN IF EXISTS updated_at;-ALTER TABLE templates DROP COLUMN IF EXISTS updated_at;-ALTER TABLE sandbox_sessions DROP COLUMN IF EXISTS updated_at;-ALTER TABLE sandbox_checkpoints DROP COLUMN IF EXISTS updated_at;-ALTER TABLE org_subscription_items DROP COLUMN IF EXISTS updated_at;+DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'users') THEN+ DROP TRIGGER IF EXISTS users_set_updated_at ON users;+ ALTER TABLE users DROP COLUMN IF EXISTS updated_at;+ END IF;+END $$;++DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'api_keys') THEN+ DROP TRIGGER IF EXISTS api_keys_set_updated_at ON api_keys;+ ALTER TABLE api_keys DROP COLUMN IF EXISTS updated_at;+ END IF;+END $$;++DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'org_memberships') THEN+ DROP TRIGGER IF EXISTS org_memberships_set_updated_at ON org_memberships;+ ALTER TABLE org_memberships DROP COLUMN IF EXISTS updated_at;+ END IF;+END $$;++DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'templates') THEN+ DROP TRIGGER IF EXISTS templates_set_updated_at ON templates;+ ALTER TABLE templates DROP COLUMN IF EXISTS updated_at;+ END IF;+END $$;++DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'sandbox_sessions') THEN+ DROP TRIGGER IF EXISTS sandbox_sessions_set_updated_at ON sandbox_sessions;+ ALTER TABLE sandbox_sessions DROP COLUMN IF EXISTS updated_at;+ END IF;+END $$;++DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'sandbox_checkpoints') THEN+ DROP TRIGGER IF EXISTS sandbox_checkpoints_set_updated_at ON sandbox_checkpoints;+ ALTER TABLE sandbox_checkpoints DROP COLUMN IF EXISTS updated_at;+ END IF;+END $$;++DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'org_subscription_items') THEN+ DROP TRIGGER IF EXISTS org_subscription_items_set_updated_at ON org_subscription_items;+ ALTER TABLE org_subscription_items DROP COLUMN IF EXISTS updated_at;+ END IF;+END $$;DROP FUNCTION IF EXISTS set_updated_at();diff --git a/internal/db/migrations/040_add_updated_at.up.sql b/internal/db/migrations/040_add_updated_at.up.sqlindex 37426240..797797c3 100644--- a/internal/db/migrations/040_add_updated_at.up.sql+++ b/internal/db/migrations/040_add_updated_at.up.sql@@ -18,7 +18,14 @@-- NULL + DEFAULT" keeps the schema change instant and the backfill-- well-bounded.----- Tables affected (no updated_at today):+-- Why each table is wrapped in IF EXISTS: prod and dev fleets have drifted+-- over time — some envs predate certain migrations (e.g. dev was missing+-- org_memberships at the time 040 was written). Wrapping each block in a+-- DO $$ ... IF EXISTS THEN ... END $$ makes the migration safely+-- idempotent across whatever schema actually exists, instead of failing+-- hard on the first missing table.+--+-- Tables targeted (no updated_at today):-- users, api_keys, org_memberships, templates,-- sandbox_sessions, sandbox_checkpoints, org_subscription_items--@@ -38,80 +45,122 @@ END;$$ LANGUAGE plpgsql;-- ── users ─────────────────────────────────────────────────────────────-ALTER TABLE users ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ;-UPDATE users SET updated_at = created_at WHERE updated_at IS NULL;-ALTER TABLE users ALTER COLUMN updated_at SET NOT NULL;-ALTER TABLE users ALTER COLUMN updated_at SET DEFAULT now();-DROP TRIGGER IF EXISTS users_set_updated_at ON users;-CREATE TRIGGER users_set_updated_at- BEFORE UPDATE ON users- FOR EACH ROW EXECUTE FUNCTION set_updated_at();+DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'users') THEN+ ALTER TABLE users ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ;+ UPDATE users SET updated_at = created_at WHERE updated_at IS NULL;+ ALTER TABLE users ALTER COLUMN updated_at SET NOT NULL;+ ALTER TABLE users ALTER COLUMN updated_at SET DEFAULT now();+ DROP TRIGGER IF EXISTS users_set_updated_at ON users;+ CREATE TRIGGER users_set_updated_at+ BEFORE UPDATE ON users+ FOR EACH ROW EXECUTE FUNCTION set_updated_at();+ END IF;+END $$;-- ── api_keys ──────────────────────────────────────────────────────────-ALTER TABLE api_keys ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ;-UPDATE api_keys SET updated_at = created_at WHERE updated_at IS NULL;-ALTER TABLE api_keys ALTER COLUMN updated_at SET NOT NULL;-ALTER TABLE api_keys ALTER COLUMN updated_at SET DEFAULT now();-DROP TRIGGER IF EXISTS api_keys_set_updated_at ON api_keys;-CREATE TRIGGER api_keys_set_updated_at- BEFORE UPDATE ON api_keys- FOR EACH ROW EXECUTE FUNCTION set_updated_at();+DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'api_keys') THEN+ ALTER TABLE api_keys ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ;+ UPDATE api_keys SET updated_at = created_at WHERE updated_at IS NULL;+ ALTER TABLE api_keys ALTER COLUMN updated_at SET NOT NULL;+ ALTER TABLE api_keys ALTER COLUMN updated_at SET DEFAULT now();+ DROP TRIGGER IF EXISTS api_keys_set_updated_at ON api_keys;+ CREATE TRIGGER api_keys_set_updated_at+ BEFORE UPDATE ON api_keys+ FOR EACH ROW EXECUTE FUNCTION set_updated_at();+ END IF;+END $$;-- ── org_memberships ───────────────────────────────────────────────────-ALTER TABLE org_memberships ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ;-UPDATE org_memberships SET updated_at = created_at WHERE updated_at IS NULL;-ALTER TABLE org_memberships ALTER COLUMN updated_at SET NOT NULL;-ALTER TABLE org_memberships ALTER COLUMN updated_at SET DEFAULT now();-DROP TRIGGER IF EXISTS org_memberships_set_updated_at ON org_memberships;-CREATE TRIGGER org_memberships_set_updated_at- BEFORE UPDATE ON org_memberships- FOR EACH ROW EXECUTE FUNCTION set_updated_at();+DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'org_memberships') THEN+ ALTER TABLE org_memberships ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ;+ UPDATE org_memberships SET updated_at = created_at WHERE updated_at IS NULL;+ ALTER TABLE org_memberships ALTER COLUMN updated_at SET NOT NULL;+ ALTER TABLE org_memberships ALTER COLUMN updated_at SET DEFAULT now();+ DROP TRIGGER IF EXISTS org_memberships_set_updated_at ON org_memberships;+ CREATE TRIGGER org_memberships_set_updated_at+ BEFORE UPDATE ON org_memberships+ FOR EACH ROW EXECUTE FUNCTION set_updated_at();+ END IF;+END $$;-- ── templates ─────────────────────────────────────────────────────────-ALTER TABLE templates ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ;-UPDATE templates SET updated_at = created_at WHERE updated_at IS NULL;-ALTER TABLE templates ALTER COLUMN updated_at SET NOT NULL;-ALTER TABLE templates ALTER COLUMN updated_at SET DEFAULT now();-DROP TRIGGER IF EXISTS templates_set_updated_at ON templates;-CREATE TRIGGER templates_set_updated_at- BEFORE UPDATE ON templates- FOR EACH ROW EXECUTE FUNCTION set_updated_at();+DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'templates') THEN+ ALTER TABLE templates ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ;+ UPDATE templates SET updated_at = created_at WHERE updated_at IS NULL;+ ALTER TABLE templates ALTER COLUMN updated_at SET NOT NULL;+ ALTER TABLE templates ALTER COLUMN updated_at SET DEFAULT now();+ DROP TRIGGER IF EXISTS templates_set_updated_at ON templates;+ CREATE TRIGGER templates_set_updated_at+ BEFORE UPDATE ON templates+ FOR EACH ROW EXECUTE FUNCTION set_updated_at();+ END IF;+END $$;-- ── sandbox_sessions ──────────────────────────────────────────────────-- Sandbox sessions have started_at + stopped_at but no single "modified"-- timestamp; for the backfill watermark, the most recent of the two is-- the right historical anchor (a stopped sandbox last "changed" when it-- stopped; a running one when it started).-ALTER TABLE sandbox_sessions ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ;-UPDATE sandbox_sessions- SET updated_at = COALESCE(stopped_at, started_at)- WHERE updated_at IS NULL;-ALTER TABLE sandbox_sessions ALTER COLUMN updated_at SET NOT NULL;-ALTER TABLE sandbox_sessions ALTER COLUMN updated_at SET DEFAULT now();-DROP TRIGGER IF EXISTS sandbox_sessions_set_updated_at ON sandbox_sessions;-CREATE TRIGGER sandbox_sessions_set_updated_at- BEFORE UPDATE ON sandbox_sessions- FOR EACH ROW EXECUTE FUNCTION set_updated_at();+DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'sandbox_sessions') THEN+ ALTER TABLE sandbox_sessions ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ;+ UPDATE sandbox_sessions+ SET updated_at = COALESCE(stopped_at, started_at)+ WHERE updated_at IS NULL;+ ALTER TABLE sandbox_sessions ALTER COLUMN updated_at SET NOT NULL;+ ALTER TABLE sandbox_sessions ALTER COLUMN updated_at SET DEFAULT now();+ DROP TRIGGER IF EXISTS sandbox_sessions_set_updated_at ON sandbox_sessions;+ CREATE TRIGGER sandbox_sessions_set_updated_at+ BEFORE UPDATE ON sandbox_sessions+ FOR EACH ROW EXECUTE FUNCTION set_updated_at();+ END IF;+END $$;-- ── sandbox_checkpoints ───────────────────────────────────────────────-ALTER TABLE sandbox_checkpoints ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ;-UPDATE sandbox_checkpoints SET updated_at = created_at WHERE updated_at IS NULL;-ALTER TABLE sandbox_checkpoints ALTER COLUMN updated_at SET NOT NULL;-ALTER TABLE sandbox_checkpoints ALTER COLUMN updated_at SET DEFAULT now();-DROP TRIGGER IF EXISTS sandbox_checkpoints_set_updated_at ON sandbox_checkpoints;-CREATE TRIGGER sandbox_checkpoints_set_updated_at- BEFORE UPDATE ON sandbox_checkpoints- FOR EACH ROW EXECUTE FUNCTION set_updated_at();+DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'sandbox_checkpoints') THEN+ ALTER TABLE sandbox_checkpoints ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ;+ UPDATE sandbox_checkpoints SET updated_at = created_at WHERE updated_at IS NULL;+ ALTER TABLE sandbox_checkpoints ALTER COLUMN updated_at SET NOT NULL;+ ALTER TABLE sandbox_checkpoints ALTER COLUMN updated_at SET DEFAULT now();+ DROP TRIGGER IF EXISTS sandbox_checkpoints_set_updated_at ON sandbox_checkpoints;+ CREATE TRIGGER sandbox_checkpoints_set_updated_at+ BEFORE UPDATE ON sandbox_checkpoints+ FOR EACH ROW EXECUTE FUNCTION set_updated_at();+ END IF;+END $$;-- ── org_subscription_items ────────────────────────────────────────────-- No created_at on this table — backfill existing rows to now() so they-- get a sensible historical watermark. Subsequent UPDATEs flow through-- the trigger.-ALTER TABLE org_subscription_items ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ;-UPDATE org_subscription_items SET updated_at = now() WHERE updated_at IS NULL;-ALTER TABLE org_subscription_items ALTER COLUMN updated_at SET NOT NULL;-ALTER TABLE org_subscription_items ALTER COLUMN updated_at SET DEFAULT now();-DROP TRIGGER IF EXISTS org_subscription_items_set_updated_at ON org_subscription_items;-CREATE TRIGGER org_subscription_items_set_updated_at- BEFORE UPDATE ON org_subscription_items- FOR EACH ROW EXECUTE FUNCTION set_updated_at();+DO $$+BEGIN+ IF EXISTS (SELECT 1 FROM information_schema.tables+ WHERE table_schema = 'public' AND table_name = 'org_subscription_items') THEN+ ALTER TABLE org_subscription_items ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ;+ UPDATE org_subscription_items SET updated_at = now() WHERE updated_at IS NULL;+ ALTER TABLE org_subscription_items ALTER COLUMN updated_at SET NOT NULL;+ ALTER TABLE org_subscription_items ALTER COLUMN updated_at SET DEFAULT now();+ DROP TRIGGER IF EXISTS org_subscription_items_set_updated_at ON org_subscription_items;+ CREATE TRIGGER org_subscription_items_set_updated_at+ BEFORE UPDATE ON org_subscription_items+ FOR EACH ROW EXECUTE FUNCTION set_updated_at();+ END IF;+END $$;