From e38a0377d5e7e584d010bdcd74b237713fa05208 Mon Sep 17 00:00:00 2001 From: Hein Date: Thu, 7 May 2026 14:52:45 +0200 Subject: [PATCH] chore(db): comment out GRANT statements for various tables * Commented out GRANT ALL ON TABLE statements for household, maintenance, family calendar, meal planning, professional CRM, stored files, project skills, and project guardrails. --- Makefile | 2 +- migrations/015_bigserial_guid_migration.sql | 818 +-- migrations/020_generated_schema.sql | 6310 +++++++++++++++++-- migrations/100_rls_and_grants.sql | 40 +- 4 files changed, 6245 insertions(+), 925 deletions(-) diff --git a/Makefile b/Makefile index 0986c7a..e8e78d0 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ VERSION_TAG ?= $(shell git describe --tags --exact-match 2>/dev/null || echo dev COMMIT_SHA ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown) BUILD_DATE ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ) RELSPEC ?= $(shell command -v relspec 2>/dev/null || echo $(HOME)/go/bin/relspec) -#RELSPEC = /home/hein/dev/relspecgo/build/relspec +#RELSPEC = /mnt/vault/relspecgo/build/relspec SCHEMA_FILES := $(sort $(wildcard schema/*.dbml)) MERGE_TARGET_TMP := $(CURDIR)/.cache/schema.merge-target.dbml GENERATED_SCHEMA_MIGRATION := migrations/020_generated_schema.sql diff --git a/migrations/015_bigserial_guid_migration.sql b/migrations/015_bigserial_guid_migration.sql index f627758..dbecda4 100644 --- a/migrations/015_bigserial_guid_migration.sql +++ b/migrations/015_bigserial_guid_migration.sql @@ -1,466 +1,466 @@ --- Migration: Convert uuid PKs to bigserial, rename old uuid id to guid, convert FK columns to bigint --- Tables with uuid PK → bigserial: agent_skills, agent_guardrails, chat_histories, learnings, plans --- Tables with serial → bigserial: thought_links, plan_dependencies, plan_related_plans, plan_skills, plan_guardrails, project_skills, project_guardrails --- FK columns: all uuid FK columns converted to bigint referencing .id --- Idempotent: safe to re-run at any point. +-- -- Migration: Convert uuid PKs to bigserial, rename old uuid id to guid, convert FK columns to bigint +-- -- Tables with uuid PK → bigserial: agent_skills, agent_guardrails, chat_histories, learnings, plans +-- -- Tables with serial → bigserial: thought_links, plan_dependencies, plan_related_plans, plan_skills, plan_guardrails, project_skills, project_guardrails +-- -- FK columns: all uuid FK columns converted to bigint referencing .id +-- -- Idempotent: safe to re-run at any point. -BEGIN; +-- BEGIN; --- ============================================================ --- STEP 1: Drop all FK constraints affected by this migration --- ============================================================ +-- -- ============================================================ +-- -- STEP 1: Drop all FK constraints affected by this migration +-- -- ============================================================ -ALTER TABLE public.thoughts DROP CONSTRAINT IF EXISTS thoughts_project_id_fkey; -ALTER TABLE public.thoughts DROP CONSTRAINT IF EXISTS fk_thoughts_project_id; -ALTER TABLE public.embeddings DROP CONSTRAINT IF EXISTS embeddings_thought_id_fkey; -ALTER TABLE public.embeddings DROP CONSTRAINT IF EXISTS fk_embeddings_thought_id; -ALTER TABLE public.stored_files DROP CONSTRAINT IF EXISTS stored_files_thought_id_fkey; -ALTER TABLE public.stored_files DROP CONSTRAINT IF EXISTS fk_stored_files_thought_id; -ALTER TABLE public.stored_files DROP CONSTRAINT IF EXISTS stored_files_project_id_fkey; -ALTER TABLE public.stored_files DROP CONSTRAINT IF EXISTS fk_stored_files_project_id; -ALTER TABLE public.project_skills DROP CONSTRAINT IF EXISTS project_skills_project_id_fkey; -ALTER TABLE public.project_skills DROP CONSTRAINT IF EXISTS fk_project_skills_project_id; -ALTER TABLE public.project_skills DROP CONSTRAINT IF EXISTS project_skills_skill_id_fkey; -ALTER TABLE public.project_skills DROP CONSTRAINT IF EXISTS fk_project_skills_skill_id; -ALTER TABLE public.project_guardrails DROP CONSTRAINT IF EXISTS project_guardrails_project_id_fkey; -ALTER TABLE public.project_guardrails DROP CONSTRAINT IF EXISTS fk_project_guardrails_project_id; -ALTER TABLE public.project_guardrails DROP CONSTRAINT IF EXISTS project_guardrails_guardrail_id_fkey; -ALTER TABLE public.project_guardrails DROP CONSTRAINT IF EXISTS fk_project_guardrails_guardrail_id; -ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS chat_histories_project_id_fkey; -ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS fk_chat_histories_project_id; -ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_project_id_fkey; -ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_project_id; -ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_related_thought_id_fkey; -ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_related_thought_id; -ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_related_skill_id_fkey; -ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_related_skill_id; -ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_duplicate_of_learning_id_fkey; -ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_duplicate_of_learning_id; -ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_supersedes_learning_id_fkey; -ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_supersedes_learning_id; -ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS plans_project_id_fkey; -ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS fk_plans_project_id; -ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS plans_supersedes_plan_id_fkey; -ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS fk_plans_supersedes_plan_id; -ALTER TABLE public.plan_dependencies DROP CONSTRAINT IF EXISTS plan_dependencies_plan_id_fkey; -ALTER TABLE public.plan_dependencies DROP CONSTRAINT IF EXISTS fk_plan_dependencies_plan_id; -ALTER TABLE public.plan_dependencies DROP CONSTRAINT IF EXISTS plan_dependencies_depends_on_plan_id_fkey; -ALTER TABLE public.plan_dependencies DROP CONSTRAINT IF EXISTS fk_plan_dependencies_depends_on_plan_id; -ALTER TABLE public.plan_related_plans DROP CONSTRAINT IF EXISTS plan_related_plans_plan_a_id_fkey; -ALTER TABLE public.plan_related_plans DROP CONSTRAINT IF EXISTS fk_plan_related_plans_plan_a_id; -ALTER TABLE public.plan_related_plans DROP CONSTRAINT IF EXISTS plan_related_plans_plan_b_id_fkey; -ALTER TABLE public.plan_related_plans DROP CONSTRAINT IF EXISTS fk_plan_related_plans_plan_b_id; -ALTER TABLE public.plan_skills DROP CONSTRAINT IF EXISTS plan_skills_plan_id_fkey; -ALTER TABLE public.plan_skills DROP CONSTRAINT IF EXISTS fk_plan_skills_plan_id; -ALTER TABLE public.plan_skills DROP CONSTRAINT IF EXISTS plan_skills_skill_id_fkey; -ALTER TABLE public.plan_skills DROP CONSTRAINT IF EXISTS fk_plan_skills_skill_id; -ALTER TABLE public.plan_guardrails DROP CONSTRAINT IF EXISTS plan_guardrails_plan_id_fkey; -ALTER TABLE public.plan_guardrails DROP CONSTRAINT IF EXISTS fk_plan_guardrails_plan_id; -ALTER TABLE public.plan_guardrails DROP CONSTRAINT IF EXISTS plan_guardrails_guardrail_id_fkey; -ALTER TABLE public.plan_guardrails DROP CONSTRAINT IF EXISTS fk_plan_guardrails_guardrail_id; +-- ALTER TABLE public.thoughts DROP CONSTRAINT IF EXISTS thoughts_project_id_fkey; +-- ALTER TABLE public.thoughts DROP CONSTRAINT IF EXISTS fk_thoughts_project_id; +-- ALTER TABLE public.embeddings DROP CONSTRAINT IF EXISTS embeddings_thought_id_fkey; +-- ALTER TABLE public.embeddings DROP CONSTRAINT IF EXISTS fk_embeddings_thought_id; +-- ALTER TABLE public.stored_files DROP CONSTRAINT IF EXISTS stored_files_thought_id_fkey; +-- ALTER TABLE public.stored_files DROP CONSTRAINT IF EXISTS fk_stored_files_thought_id; +-- ALTER TABLE public.stored_files DROP CONSTRAINT IF EXISTS stored_files_project_id_fkey; +-- ALTER TABLE public.stored_files DROP CONSTRAINT IF EXISTS fk_stored_files_project_id; +-- ALTER TABLE public.project_skills DROP CONSTRAINT IF EXISTS project_skills_project_id_fkey; +-- ALTER TABLE public.project_skills DROP CONSTRAINT IF EXISTS fk_project_skills_project_id; +-- ALTER TABLE public.project_skills DROP CONSTRAINT IF EXISTS project_skills_skill_id_fkey; +-- ALTER TABLE public.project_skills DROP CONSTRAINT IF EXISTS fk_project_skills_skill_id; +-- ALTER TABLE public.project_guardrails DROP CONSTRAINT IF EXISTS project_guardrails_project_id_fkey; +-- ALTER TABLE public.project_guardrails DROP CONSTRAINT IF EXISTS fk_project_guardrails_project_id; +-- ALTER TABLE public.project_guardrails DROP CONSTRAINT IF EXISTS project_guardrails_guardrail_id_fkey; +-- ALTER TABLE public.project_guardrails DROP CONSTRAINT IF EXISTS fk_project_guardrails_guardrail_id; +-- ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS chat_histories_project_id_fkey; +-- ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS fk_chat_histories_project_id; +-- ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_project_id_fkey; +-- ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_project_id; +-- ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_related_thought_id_fkey; +-- ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_related_thought_id; +-- ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_related_skill_id_fkey; +-- ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_related_skill_id; +-- ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_duplicate_of_learning_id_fkey; +-- ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_duplicate_of_learning_id; +-- ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_supersedes_learning_id_fkey; +-- ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_supersedes_learning_id; +-- ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS plans_project_id_fkey; +-- ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS fk_plans_project_id; +-- ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS plans_supersedes_plan_id_fkey; +-- ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS fk_plans_supersedes_plan_id; +-- ALTER TABLE public.plan_dependencies DROP CONSTRAINT IF EXISTS plan_dependencies_plan_id_fkey; +-- ALTER TABLE public.plan_dependencies DROP CONSTRAINT IF EXISTS fk_plan_dependencies_plan_id; +-- ALTER TABLE public.plan_dependencies DROP CONSTRAINT IF EXISTS plan_dependencies_depends_on_plan_id_fkey; +-- ALTER TABLE public.plan_dependencies DROP CONSTRAINT IF EXISTS fk_plan_dependencies_depends_on_plan_id; +-- ALTER TABLE public.plan_related_plans DROP CONSTRAINT IF EXISTS plan_related_plans_plan_a_id_fkey; +-- ALTER TABLE public.plan_related_plans DROP CONSTRAINT IF EXISTS fk_plan_related_plans_plan_a_id; +-- ALTER TABLE public.plan_related_plans DROP CONSTRAINT IF EXISTS plan_related_plans_plan_b_id_fkey; +-- ALTER TABLE public.plan_related_plans DROP CONSTRAINT IF EXISTS fk_plan_related_plans_plan_b_id; +-- ALTER TABLE public.plan_skills DROP CONSTRAINT IF EXISTS plan_skills_plan_id_fkey; +-- ALTER TABLE public.plan_skills DROP CONSTRAINT IF EXISTS fk_plan_skills_plan_id; +-- ALTER TABLE public.plan_skills DROP CONSTRAINT IF EXISTS plan_skills_skill_id_fkey; +-- ALTER TABLE public.plan_skills DROP CONSTRAINT IF EXISTS fk_plan_skills_skill_id; +-- ALTER TABLE public.plan_guardrails DROP CONSTRAINT IF EXISTS plan_guardrails_plan_id_fkey; +-- ALTER TABLE public.plan_guardrails DROP CONSTRAINT IF EXISTS fk_plan_guardrails_plan_id; +-- ALTER TABLE public.plan_guardrails DROP CONSTRAINT IF EXISTS plan_guardrails_guardrail_id_fkey; +-- ALTER TABLE public.plan_guardrails DROP CONSTRAINT IF EXISTS fk_plan_guardrails_guardrail_id; --- ============================================================ --- STEP 2: Drop PK constraints on uuid-pk tables --- ============================================================ +-- -- ============================================================ +-- -- STEP 2: Drop PK constraints on uuid-pk tables +-- -- ============================================================ -ALTER TABLE public.agent_skills DROP CONSTRAINT IF EXISTS agent_skills_pkey; -ALTER TABLE public.agent_skills DROP CONSTRAINT IF EXISTS pk_public_agent_skills; -ALTER TABLE public.agent_guardrails DROP CONSTRAINT IF EXISTS agent_guardrails_pkey; -ALTER TABLE public.agent_guardrails DROP CONSTRAINT IF EXISTS pk_public_agent_guardrails; -ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS chat_histories_pkey; -ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS pk_public_chat_histories; -ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_pkey; -ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS pk_public_learnings; -ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS plans_pkey; -ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS pk_public_plans; +-- ALTER TABLE public.agent_skills DROP CONSTRAINT IF EXISTS agent_skills_pkey; +-- ALTER TABLE public.agent_skills DROP CONSTRAINT IF EXISTS pk_public_agent_skills; +-- ALTER TABLE public.agent_guardrails DROP CONSTRAINT IF EXISTS agent_guardrails_pkey; +-- ALTER TABLE public.agent_guardrails DROP CONSTRAINT IF EXISTS pk_public_agent_guardrails; +-- ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS chat_histories_pkey; +-- ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS pk_public_chat_histories; +-- ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_pkey; +-- ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS pk_public_learnings; +-- ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS plans_pkey; +-- ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS pk_public_plans; --- ============================================================ --- STEP 3: Rename uuid id → guid on formerly uuid-pk tables --- Only renames if id exists and guid does not yet exist. --- ============================================================ +-- -- ============================================================ +-- -- STEP 3: Rename uuid id → guid on formerly uuid-pk tables +-- -- Only renames if id exists and guid does not yet exist. +-- -- ============================================================ -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='agent_skills' AND column_name='id') - AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='agent_skills' AND column_name='guid') THEN - ALTER TABLE public.agent_skills RENAME COLUMN id TO guid; - END IF; -END $$; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='agent_skills' AND column_name='id') +-- AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='agent_skills' AND column_name='guid') THEN +-- ALTER TABLE public.agent_skills RENAME COLUMN id TO guid; +-- END IF; +-- END $$; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='agent_guardrails' AND column_name='id') - AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='agent_guardrails' AND column_name='guid') THEN - ALTER TABLE public.agent_guardrails RENAME COLUMN id TO guid; - END IF; -END $$; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='agent_guardrails' AND column_name='id') +-- AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='agent_guardrails' AND column_name='guid') THEN +-- ALTER TABLE public.agent_guardrails RENAME COLUMN id TO guid; +-- END IF; +-- END $$; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='chat_histories' AND column_name='id') - AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='chat_histories' AND column_name='guid') THEN - ALTER TABLE public.chat_histories RENAME COLUMN id TO guid; - END IF; -END $$; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='chat_histories' AND column_name='id') +-- AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='chat_histories' AND column_name='guid') THEN +-- ALTER TABLE public.chat_histories RENAME COLUMN id TO guid; +-- END IF; +-- END $$; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='id') - AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='guid') THEN - ALTER TABLE public.learnings RENAME COLUMN id TO guid; - END IF; -END $$; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='id') +-- AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='guid') THEN +-- ALTER TABLE public.learnings RENAME COLUMN id TO guid; +-- END IF; +-- END $$; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='id') - AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='guid') THEN - ALTER TABLE public.plans RENAME COLUMN id TO guid; - END IF; -END $$; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='id') +-- AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='guid') THEN +-- ALTER TABLE public.plans RENAME COLUMN id TO guid; +-- END IF; +-- END $$; --- ============================================================ --- STEP 4: Add bigserial id columns to formerly uuid-pk tables --- ============================================================ +-- -- ============================================================ +-- -- STEP 4: Add bigserial id columns to formerly uuid-pk tables +-- -- ============================================================ -ALTER TABLE public.agent_skills ADD COLUMN IF NOT EXISTS id bigserial; -ALTER TABLE public.agent_guardrails ADD COLUMN IF NOT EXISTS id bigserial; -ALTER TABLE public.chat_histories ADD COLUMN IF NOT EXISTS id bigserial; -ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS id bigserial; -ALTER TABLE public.plans ADD COLUMN IF NOT EXISTS id bigserial; +-- ALTER TABLE public.agent_skills ADD COLUMN IF NOT EXISTS id bigserial; +-- ALTER TABLE public.agent_guardrails ADD COLUMN IF NOT EXISTS id bigserial; +-- ALTER TABLE public.chat_histories ADD COLUMN IF NOT EXISTS id bigserial; +-- ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS id bigserial; +-- ALTER TABLE public.plans ADD COLUMN IF NOT EXISTS id bigserial; --- ============================================================ --- STEP 5: Add new bigint FK columns and migrate data from uuid columns. --- Each update is guarded: only runs when the source column is still uuid type. --- ============================================================ +-- -- ============================================================ +-- -- STEP 5: Add new bigint FK columns and migrate data from uuid columns. +-- -- Each update is guarded: only runs when the source column is still uuid type. +-- -- ============================================================ --- thoughts.project_id (uuid → bigint via projects.guid) -ALTER TABLE public.thoughts ADD COLUMN IF NOT EXISTS project_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='thoughts' AND column_name='project_id' AND data_type='uuid') THEN - UPDATE public.thoughts t SET project_id_new = p.id FROM public.projects p WHERE p.guid = t.project_id AND t.project_id_new IS NULL; - END IF; -END $$; +-- -- thoughts.project_id (uuid → bigint via projects.guid) +-- ALTER TABLE public.thoughts ADD COLUMN IF NOT EXISTS project_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='thoughts' AND column_name='project_id' AND data_type='uuid') THEN +-- UPDATE public.thoughts t SET project_id_new = p.id FROM public.projects p WHERE p.guid = t.project_id AND t.project_id_new IS NULL; +-- END IF; +-- END $$; --- embeddings.thought_id (uuid → bigint via thoughts.guid) -ALTER TABLE public.embeddings ADD COLUMN IF NOT EXISTS thought_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='embeddings' AND column_name='thought_id' AND data_type='uuid') THEN - UPDATE public.embeddings e SET thought_id_new = t.id FROM public.thoughts t WHERE t.guid = e.thought_id AND e.thought_id_new IS NULL; - END IF; -END $$; +-- -- embeddings.thought_id (uuid → bigint via thoughts.guid) +-- ALTER TABLE public.embeddings ADD COLUMN IF NOT EXISTS thought_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='embeddings' AND column_name='thought_id' AND data_type='uuid') THEN +-- UPDATE public.embeddings e SET thought_id_new = t.id FROM public.thoughts t WHERE t.guid = e.thought_id AND e.thought_id_new IS NULL; +-- END IF; +-- END $$; --- stored_files.thought_id (uuid → bigint via thoughts.guid) -ALTER TABLE public.stored_files ADD COLUMN IF NOT EXISTS thought_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='stored_files' AND column_name='thought_id' AND data_type='uuid') THEN - UPDATE public.stored_files f SET thought_id_new = t.id FROM public.thoughts t WHERE t.guid = f.thought_id AND f.thought_id_new IS NULL; - END IF; -END $$; +-- -- stored_files.thought_id (uuid → bigint via thoughts.guid) +-- ALTER TABLE public.stored_files ADD COLUMN IF NOT EXISTS thought_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='stored_files' AND column_name='thought_id' AND data_type='uuid') THEN +-- UPDATE public.stored_files f SET thought_id_new = t.id FROM public.thoughts t WHERE t.guid = f.thought_id AND f.thought_id_new IS NULL; +-- END IF; +-- END $$; --- stored_files.project_id (uuid → bigint via projects.guid) -ALTER TABLE public.stored_files ADD COLUMN IF NOT EXISTS project_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='stored_files' AND column_name='project_id' AND data_type='uuid') THEN - UPDATE public.stored_files f SET project_id_new = p.id FROM public.projects p WHERE p.guid = f.project_id AND f.project_id_new IS NULL; - END IF; -END $$; +-- -- stored_files.project_id (uuid → bigint via projects.guid) +-- ALTER TABLE public.stored_files ADD COLUMN IF NOT EXISTS project_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='stored_files' AND column_name='project_id' AND data_type='uuid') THEN +-- UPDATE public.stored_files f SET project_id_new = p.id FROM public.projects p WHERE p.guid = f.project_id AND f.project_id_new IS NULL; +-- END IF; +-- END $$; --- project_skills.project_id (uuid → bigint via projects.guid) -ALTER TABLE public.project_skills ADD COLUMN IF NOT EXISTS project_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_skills' AND column_name='project_id' AND data_type='uuid') THEN - UPDATE public.project_skills ps SET project_id_new = p.id FROM public.projects p WHERE p.guid = ps.project_id AND ps.project_id_new IS NULL; - END IF; -END $$; +-- -- project_skills.project_id (uuid → bigint via projects.guid) +-- ALTER TABLE public.project_skills ADD COLUMN IF NOT EXISTS project_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_skills' AND column_name='project_id' AND data_type='uuid') THEN +-- UPDATE public.project_skills ps SET project_id_new = p.id FROM public.projects p WHERE p.guid = ps.project_id AND ps.project_id_new IS NULL; +-- END IF; +-- END $$; --- project_skills.skill_id (uuid → bigint via agent_skills.guid) -ALTER TABLE public.project_skills ADD COLUMN IF NOT EXISTS skill_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_skills' AND column_name='skill_id' AND data_type='uuid') THEN - UPDATE public.project_skills ps SET skill_id_new = s.id FROM public.agent_skills s WHERE s.guid = ps.skill_id AND ps.skill_id_new IS NULL; - END IF; -END $$; +-- -- project_skills.skill_id (uuid → bigint via agent_skills.guid) +-- ALTER TABLE public.project_skills ADD COLUMN IF NOT EXISTS skill_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_skills' AND column_name='skill_id' AND data_type='uuid') THEN +-- UPDATE public.project_skills ps SET skill_id_new = s.id FROM public.agent_skills s WHERE s.guid = ps.skill_id AND ps.skill_id_new IS NULL; +-- END IF; +-- END $$; --- project_guardrails.project_id (uuid → bigint via projects.guid) -ALTER TABLE public.project_guardrails ADD COLUMN IF NOT EXISTS project_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_guardrails' AND column_name='project_id' AND data_type='uuid') THEN - UPDATE public.project_guardrails pg SET project_id_new = p.id FROM public.projects p WHERE p.guid = pg.project_id AND pg.project_id_new IS NULL; - END IF; -END $$; +-- -- project_guardrails.project_id (uuid → bigint via projects.guid) +-- ALTER TABLE public.project_guardrails ADD COLUMN IF NOT EXISTS project_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_guardrails' AND column_name='project_id' AND data_type='uuid') THEN +-- UPDATE public.project_guardrails pg SET project_id_new = p.id FROM public.projects p WHERE p.guid = pg.project_id AND pg.project_id_new IS NULL; +-- END IF; +-- END $$; --- project_guardrails.guardrail_id (uuid → bigint via agent_guardrails.guid) -ALTER TABLE public.project_guardrails ADD COLUMN IF NOT EXISTS guardrail_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_guardrails' AND column_name='guardrail_id' AND data_type='uuid') THEN - UPDATE public.project_guardrails pg SET guardrail_id_new = g.id FROM public.agent_guardrails g WHERE g.guid = pg.guardrail_id AND pg.guardrail_id_new IS NULL; - END IF; -END $$; +-- -- project_guardrails.guardrail_id (uuid → bigint via agent_guardrails.guid) +-- ALTER TABLE public.project_guardrails ADD COLUMN IF NOT EXISTS guardrail_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_guardrails' AND column_name='guardrail_id' AND data_type='uuid') THEN +-- UPDATE public.project_guardrails pg SET guardrail_id_new = g.id FROM public.agent_guardrails g WHERE g.guid = pg.guardrail_id AND pg.guardrail_id_new IS NULL; +-- END IF; +-- END $$; --- chat_histories.project_id (uuid → bigint via projects.guid) -ALTER TABLE public.chat_histories ADD COLUMN IF NOT EXISTS project_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='chat_histories' AND column_name='project_id' AND data_type='uuid') THEN - UPDATE public.chat_histories ch SET project_id_new = p.id FROM public.projects p WHERE p.guid = ch.project_id AND ch.project_id_new IS NULL; - END IF; -END $$; +-- -- chat_histories.project_id (uuid → bigint via projects.guid) +-- ALTER TABLE public.chat_histories ADD COLUMN IF NOT EXISTS project_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='chat_histories' AND column_name='project_id' AND data_type='uuid') THEN +-- UPDATE public.chat_histories ch SET project_id_new = p.id FROM public.projects p WHERE p.guid = ch.project_id AND ch.project_id_new IS NULL; +-- END IF; +-- END $$; --- learnings.project_id (uuid → bigint via projects.guid) -ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS project_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='project_id' AND data_type='uuid') THEN - UPDATE public.learnings l SET project_id_new = p.id FROM public.projects p WHERE p.guid = l.project_id AND l.project_id_new IS NULL; - END IF; -END $$; +-- -- learnings.project_id (uuid → bigint via projects.guid) +-- ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS project_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='project_id' AND data_type='uuid') THEN +-- UPDATE public.learnings l SET project_id_new = p.id FROM public.projects p WHERE p.guid = l.project_id AND l.project_id_new IS NULL; +-- END IF; +-- END $$; --- learnings.related_thought_id (uuid → bigint via thoughts.guid) -ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS related_thought_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='related_thought_id' AND data_type='uuid') THEN - UPDATE public.learnings l SET related_thought_id_new = t.id FROM public.thoughts t WHERE t.guid = l.related_thought_id AND l.related_thought_id_new IS NULL; - END IF; -END $$; +-- -- learnings.related_thought_id (uuid → bigint via thoughts.guid) +-- ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS related_thought_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='related_thought_id' AND data_type='uuid') THEN +-- UPDATE public.learnings l SET related_thought_id_new = t.id FROM public.thoughts t WHERE t.guid = l.related_thought_id AND l.related_thought_id_new IS NULL; +-- END IF; +-- END $$; --- learnings.related_skill_id (uuid → bigint via agent_skills.guid) -ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS related_skill_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='related_skill_id' AND data_type='uuid') THEN - UPDATE public.learnings l SET related_skill_id_new = s.id FROM public.agent_skills s WHERE s.guid = l.related_skill_id AND l.related_skill_id_new IS NULL; - END IF; -END $$; +-- -- learnings.related_skill_id (uuid → bigint via agent_skills.guid) +-- ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS related_skill_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='related_skill_id' AND data_type='uuid') THEN +-- UPDATE public.learnings l SET related_skill_id_new = s.id FROM public.agent_skills s WHERE s.guid = l.related_skill_id AND l.related_skill_id_new IS NULL; +-- END IF; +-- END $$; --- learnings.duplicate_of_learning_id (uuid → bigint via learnings.guid) -ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS duplicate_of_learning_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='duplicate_of_learning_id' AND data_type='uuid') THEN - UPDATE public.learnings l SET duplicate_of_learning_id_new = l2.id FROM public.learnings l2 WHERE l2.guid = l.duplicate_of_learning_id AND l.duplicate_of_learning_id_new IS NULL; - END IF; -END $$; +-- -- learnings.duplicate_of_learning_id (uuid → bigint via learnings.guid) +-- ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS duplicate_of_learning_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='duplicate_of_learning_id' AND data_type='uuid') THEN +-- UPDATE public.learnings l SET duplicate_of_learning_id_new = l2.id FROM public.learnings l2 WHERE l2.guid = l.duplicate_of_learning_id AND l.duplicate_of_learning_id_new IS NULL; +-- END IF; +-- END $$; --- learnings.supersedes_learning_id (uuid → bigint via learnings.guid) -ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS supersedes_learning_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='supersedes_learning_id' AND data_type='uuid') THEN - UPDATE public.learnings l SET supersedes_learning_id_new = l2.id FROM public.learnings l2 WHERE l2.guid = l.supersedes_learning_id AND l.supersedes_learning_id_new IS NULL; - END IF; -END $$; +-- -- learnings.supersedes_learning_id (uuid → bigint via learnings.guid) +-- ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS supersedes_learning_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='supersedes_learning_id' AND data_type='uuid') THEN +-- UPDATE public.learnings l SET supersedes_learning_id_new = l2.id FROM public.learnings l2 WHERE l2.guid = l.supersedes_learning_id AND l.supersedes_learning_id_new IS NULL; +-- END IF; +-- END $$; --- plans.project_id (uuid → bigint via projects.guid) -ALTER TABLE public.plans ADD COLUMN IF NOT EXISTS project_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='project_id' AND data_type='uuid') THEN - UPDATE public.plans pl SET project_id_new = p.id FROM public.projects p WHERE p.guid = pl.project_id AND pl.project_id_new IS NULL; - END IF; -END $$; +-- -- plans.project_id (uuid → bigint via projects.guid) +-- ALTER TABLE public.plans ADD COLUMN IF NOT EXISTS project_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='project_id' AND data_type='uuid') THEN +-- UPDATE public.plans pl SET project_id_new = p.id FROM public.projects p WHERE p.guid = pl.project_id AND pl.project_id_new IS NULL; +-- END IF; +-- END $$; --- plans.supersedes_plan_id (uuid → bigint via plans.guid) -ALTER TABLE public.plans ADD COLUMN IF NOT EXISTS supersedes_plan_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='supersedes_plan_id' AND data_type='uuid') THEN - UPDATE public.plans pl SET supersedes_plan_id_new = pl2.id FROM public.plans pl2 WHERE pl2.guid = pl.supersedes_plan_id AND pl.supersedes_plan_id_new IS NULL; - END IF; -END $$; +-- -- plans.supersedes_plan_id (uuid → bigint via plans.guid) +-- ALTER TABLE public.plans ADD COLUMN IF NOT EXISTS supersedes_plan_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='supersedes_plan_id' AND data_type='uuid') THEN +-- UPDATE public.plans pl SET supersedes_plan_id_new = pl2.id FROM public.plans pl2 WHERE pl2.guid = pl.supersedes_plan_id AND pl.supersedes_plan_id_new IS NULL; +-- END IF; +-- END $$; --- plan_dependencies.plan_id (uuid → bigint via plans.guid) -ALTER TABLE public.plan_dependencies ADD COLUMN IF NOT EXISTS plan_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_dependencies' AND column_name='plan_id' AND data_type='uuid') THEN - UPDATE public.plan_dependencies pd SET plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = pd.plan_id AND pd.plan_id_new IS NULL; - END IF; -END $$; +-- -- plan_dependencies.plan_id (uuid → bigint via plans.guid) +-- ALTER TABLE public.plan_dependencies ADD COLUMN IF NOT EXISTS plan_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_dependencies' AND column_name='plan_id' AND data_type='uuid') THEN +-- UPDATE public.plan_dependencies pd SET plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = pd.plan_id AND pd.plan_id_new IS NULL; +-- END IF; +-- END $$; --- plan_dependencies.depends_on_plan_id (uuid → bigint via plans.guid) -ALTER TABLE public.plan_dependencies ADD COLUMN IF NOT EXISTS depends_on_plan_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_dependencies' AND column_name='depends_on_plan_id' AND data_type='uuid') THEN - UPDATE public.plan_dependencies pd SET depends_on_plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = pd.depends_on_plan_id AND pd.depends_on_plan_id_new IS NULL; - END IF; -END $$; +-- -- plan_dependencies.depends_on_plan_id (uuid → bigint via plans.guid) +-- ALTER TABLE public.plan_dependencies ADD COLUMN IF NOT EXISTS depends_on_plan_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_dependencies' AND column_name='depends_on_plan_id' AND data_type='uuid') THEN +-- UPDATE public.plan_dependencies pd SET depends_on_plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = pd.depends_on_plan_id AND pd.depends_on_plan_id_new IS NULL; +-- END IF; +-- END $$; --- plan_related_plans.plan_a_id (uuid → bigint via plans.guid) -ALTER TABLE public.plan_related_plans ADD COLUMN IF NOT EXISTS plan_a_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_related_plans' AND column_name='plan_a_id' AND data_type='uuid') THEN - UPDATE public.plan_related_plans pr SET plan_a_id_new = pl.id FROM public.plans pl WHERE pl.guid = pr.plan_a_id AND pr.plan_a_id_new IS NULL; - END IF; -END $$; +-- -- plan_related_plans.plan_a_id (uuid → bigint via plans.guid) +-- ALTER TABLE public.plan_related_plans ADD COLUMN IF NOT EXISTS plan_a_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_related_plans' AND column_name='plan_a_id' AND data_type='uuid') THEN +-- UPDATE public.plan_related_plans pr SET plan_a_id_new = pl.id FROM public.plans pl WHERE pl.guid = pr.plan_a_id AND pr.plan_a_id_new IS NULL; +-- END IF; +-- END $$; --- plan_related_plans.plan_b_id (uuid → bigint via plans.guid) -ALTER TABLE public.plan_related_plans ADD COLUMN IF NOT EXISTS plan_b_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_related_plans' AND column_name='plan_b_id' AND data_type='uuid') THEN - UPDATE public.plan_related_plans pr SET plan_b_id_new = pl.id FROM public.plans pl WHERE pl.guid = pr.plan_b_id AND pr.plan_b_id_new IS NULL; - END IF; -END $$; +-- -- plan_related_plans.plan_b_id (uuid → bigint via plans.guid) +-- ALTER TABLE public.plan_related_plans ADD COLUMN IF NOT EXISTS plan_b_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_related_plans' AND column_name='plan_b_id' AND data_type='uuid') THEN +-- UPDATE public.plan_related_plans pr SET plan_b_id_new = pl.id FROM public.plans pl WHERE pl.guid = pr.plan_b_id AND pr.plan_b_id_new IS NULL; +-- END IF; +-- END $$; --- plan_skills.plan_id (uuid → bigint via plans.guid) -ALTER TABLE public.plan_skills ADD COLUMN IF NOT EXISTS plan_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_skills' AND column_name='plan_id' AND data_type='uuid') THEN - UPDATE public.plan_skills ps SET plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = ps.plan_id AND ps.plan_id_new IS NULL; - END IF; -END $$; +-- -- plan_skills.plan_id (uuid → bigint via plans.guid) +-- ALTER TABLE public.plan_skills ADD COLUMN IF NOT EXISTS plan_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_skills' AND column_name='plan_id' AND data_type='uuid') THEN +-- UPDATE public.plan_skills ps SET plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = ps.plan_id AND ps.plan_id_new IS NULL; +-- END IF; +-- END $$; --- plan_skills.skill_id (uuid → bigint via agent_skills.guid) -ALTER TABLE public.plan_skills ADD COLUMN IF NOT EXISTS skill_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_skills' AND column_name='skill_id' AND data_type='uuid') THEN - UPDATE public.plan_skills ps SET skill_id_new = s.id FROM public.agent_skills s WHERE s.guid = ps.skill_id AND ps.skill_id_new IS NULL; - END IF; -END $$; +-- -- plan_skills.skill_id (uuid → bigint via agent_skills.guid) +-- ALTER TABLE public.plan_skills ADD COLUMN IF NOT EXISTS skill_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_skills' AND column_name='skill_id' AND data_type='uuid') THEN +-- UPDATE public.plan_skills ps SET skill_id_new = s.id FROM public.agent_skills s WHERE s.guid = ps.skill_id AND ps.skill_id_new IS NULL; +-- END IF; +-- END $$; --- plan_guardrails.plan_id (uuid → bigint via plans.guid) -ALTER TABLE public.plan_guardrails ADD COLUMN IF NOT EXISTS plan_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_guardrails' AND column_name='plan_id' AND data_type='uuid') THEN - UPDATE public.plan_guardrails pg SET plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = pg.plan_id AND pg.plan_id_new IS NULL; - END IF; -END $$; +-- -- plan_guardrails.plan_id (uuid → bigint via plans.guid) +-- ALTER TABLE public.plan_guardrails ADD COLUMN IF NOT EXISTS plan_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_guardrails' AND column_name='plan_id' AND data_type='uuid') THEN +-- UPDATE public.plan_guardrails pg SET plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = pg.plan_id AND pg.plan_id_new IS NULL; +-- END IF; +-- END $$; --- plan_guardrails.guardrail_id (uuid → bigint via agent_guardrails.guid) -ALTER TABLE public.plan_guardrails ADD COLUMN IF NOT EXISTS guardrail_id_new bigint; -DO $$ BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_guardrails' AND column_name='guardrail_id' AND data_type='uuid') THEN - UPDATE public.plan_guardrails pg SET guardrail_id_new = g.id FROM public.agent_guardrails g WHERE g.guid = pg.guardrail_id AND pg.guardrail_id_new IS NULL; - END IF; -END $$; +-- -- plan_guardrails.guardrail_id (uuid → bigint via agent_guardrails.guid) +-- ALTER TABLE public.plan_guardrails ADD COLUMN IF NOT EXISTS guardrail_id_new bigint; +-- DO $$ BEGIN +-- IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_guardrails' AND column_name='guardrail_id' AND data_type='uuid') THEN +-- UPDATE public.plan_guardrails pg SET guardrail_id_new = g.id FROM public.agent_guardrails g WHERE g.guid = pg.guardrail_id AND pg.guardrail_id_new IS NULL; +-- END IF; +-- END $$; --- ============================================================ --- STEP 6: Drop old uuid FK columns --- ============================================================ +-- -- ============================================================ +-- -- STEP 6: Drop old uuid FK columns +-- -- ============================================================ -ALTER TABLE public.thoughts DROP COLUMN IF EXISTS project_id; -ALTER TABLE public.embeddings DROP COLUMN IF EXISTS thought_id; -ALTER TABLE public.stored_files DROP COLUMN IF EXISTS thought_id; -ALTER TABLE public.stored_files DROP COLUMN IF EXISTS project_id; -ALTER TABLE public.project_skills DROP COLUMN IF EXISTS project_id; -ALTER TABLE public.project_skills DROP COLUMN IF EXISTS skill_id; -ALTER TABLE public.project_guardrails DROP COLUMN IF EXISTS project_id; -ALTER TABLE public.project_guardrails DROP COLUMN IF EXISTS guardrail_id; -ALTER TABLE public.chat_histories DROP COLUMN IF EXISTS project_id; -ALTER TABLE public.learnings DROP COLUMN IF EXISTS project_id; -ALTER TABLE public.learnings DROP COLUMN IF EXISTS related_thought_id; -ALTER TABLE public.learnings DROP COLUMN IF EXISTS related_skill_id; -ALTER TABLE public.learnings DROP COLUMN IF EXISTS duplicate_of_learning_id; -ALTER TABLE public.learnings DROP COLUMN IF EXISTS supersedes_learning_id; -ALTER TABLE public.plans DROP COLUMN IF EXISTS project_id; -ALTER TABLE public.plans DROP COLUMN IF EXISTS supersedes_plan_id; -ALTER TABLE public.plan_dependencies DROP COLUMN IF EXISTS plan_id; -ALTER TABLE public.plan_dependencies DROP COLUMN IF EXISTS depends_on_plan_id; -ALTER TABLE public.plan_related_plans DROP COLUMN IF EXISTS plan_a_id; -ALTER TABLE public.plan_related_plans DROP COLUMN IF EXISTS plan_b_id; -ALTER TABLE public.plan_skills DROP COLUMN IF EXISTS plan_id; -ALTER TABLE public.plan_skills DROP COLUMN IF EXISTS skill_id; -ALTER TABLE public.plan_guardrails DROP COLUMN IF EXISTS plan_id; -ALTER TABLE public.plan_guardrails DROP COLUMN IF EXISTS guardrail_id; +-- ALTER TABLE public.thoughts DROP COLUMN IF EXISTS project_id; +-- ALTER TABLE public.embeddings DROP COLUMN IF EXISTS thought_id; +-- ALTER TABLE public.stored_files DROP COLUMN IF EXISTS thought_id; +-- ALTER TABLE public.stored_files DROP COLUMN IF EXISTS project_id; +-- ALTER TABLE public.project_skills DROP COLUMN IF EXISTS project_id; +-- ALTER TABLE public.project_skills DROP COLUMN IF EXISTS skill_id; +-- ALTER TABLE public.project_guardrails DROP COLUMN IF EXISTS project_id; +-- ALTER TABLE public.project_guardrails DROP COLUMN IF EXISTS guardrail_id; +-- ALTER TABLE public.chat_histories DROP COLUMN IF EXISTS project_id; +-- ALTER TABLE public.learnings DROP COLUMN IF EXISTS project_id; +-- ALTER TABLE public.learnings DROP COLUMN IF EXISTS related_thought_id; +-- ALTER TABLE public.learnings DROP COLUMN IF EXISTS related_skill_id; +-- ALTER TABLE public.learnings DROP COLUMN IF EXISTS duplicate_of_learning_id; +-- ALTER TABLE public.learnings DROP COLUMN IF EXISTS supersedes_learning_id; +-- ALTER TABLE public.plans DROP COLUMN IF EXISTS project_id; +-- ALTER TABLE public.plans DROP COLUMN IF EXISTS supersedes_plan_id; +-- ALTER TABLE public.plan_dependencies DROP COLUMN IF EXISTS plan_id; +-- ALTER TABLE public.plan_dependencies DROP COLUMN IF EXISTS depends_on_plan_id; +-- ALTER TABLE public.plan_related_plans DROP COLUMN IF EXISTS plan_a_id; +-- ALTER TABLE public.plan_related_plans DROP COLUMN IF EXISTS plan_b_id; +-- ALTER TABLE public.plan_skills DROP COLUMN IF EXISTS plan_id; +-- ALTER TABLE public.plan_skills DROP COLUMN IF EXISTS skill_id; +-- ALTER TABLE public.plan_guardrails DROP COLUMN IF EXISTS plan_id; +-- ALTER TABLE public.plan_guardrails DROP COLUMN IF EXISTS guardrail_id; --- ============================================================ --- STEP 7: Rename _new columns to final names (idempotent) --- ============================================================ +-- -- ============================================================ +-- -- STEP 7: Rename _new columns to final names (idempotent) +-- -- ============================================================ -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='thoughts' AND column_name='project_id_new') THEN ALTER TABLE public.thoughts RENAME COLUMN project_id_new TO project_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='embeddings' AND column_name='thought_id_new') THEN ALTER TABLE public.embeddings RENAME COLUMN thought_id_new TO thought_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='stored_files' AND column_name='thought_id_new') THEN ALTER TABLE public.stored_files RENAME COLUMN thought_id_new TO thought_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='stored_files' AND column_name='project_id_new') THEN ALTER TABLE public.stored_files RENAME COLUMN project_id_new TO project_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_skills' AND column_name='project_id_new') THEN ALTER TABLE public.project_skills RENAME COLUMN project_id_new TO project_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_skills' AND column_name='skill_id_new') THEN ALTER TABLE public.project_skills RENAME COLUMN skill_id_new TO skill_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_guardrails' AND column_name='project_id_new') THEN ALTER TABLE public.project_guardrails RENAME COLUMN project_id_new TO project_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_guardrails' AND column_name='guardrail_id_new') THEN ALTER TABLE public.project_guardrails RENAME COLUMN guardrail_id_new TO guardrail_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='chat_histories' AND column_name='project_id_new') THEN ALTER TABLE public.chat_histories RENAME COLUMN project_id_new TO project_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='project_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN project_id_new TO project_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='related_thought_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN related_thought_id_new TO related_thought_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='related_skill_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN related_skill_id_new TO related_skill_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='duplicate_of_learning_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN duplicate_of_learning_id_new TO duplicate_of_learning_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='supersedes_learning_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN supersedes_learning_id_new TO supersedes_learning_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='project_id_new') THEN ALTER TABLE public.plans RENAME COLUMN project_id_new TO project_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='supersedes_plan_id_new') THEN ALTER TABLE public.plans RENAME COLUMN supersedes_plan_id_new TO supersedes_plan_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_dependencies' AND column_name='plan_id_new') THEN ALTER TABLE public.plan_dependencies RENAME COLUMN plan_id_new TO plan_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_dependencies' AND column_name='depends_on_plan_id_new') THEN ALTER TABLE public.plan_dependencies RENAME COLUMN depends_on_plan_id_new TO depends_on_plan_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_related_plans' AND column_name='plan_a_id_new') THEN ALTER TABLE public.plan_related_plans RENAME COLUMN plan_a_id_new TO plan_a_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_related_plans' AND column_name='plan_b_id_new') THEN ALTER TABLE public.plan_related_plans RENAME COLUMN plan_b_id_new TO plan_b_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_skills' AND column_name='plan_id_new') THEN ALTER TABLE public.plan_skills RENAME COLUMN plan_id_new TO plan_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_skills' AND column_name='skill_id_new') THEN ALTER TABLE public.plan_skills RENAME COLUMN skill_id_new TO skill_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_guardrails' AND column_name='plan_id_new') THEN ALTER TABLE public.plan_guardrails RENAME COLUMN plan_id_new TO plan_id; END IF; END $$; -DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_guardrails' AND column_name='guardrail_id_new') THEN ALTER TABLE public.plan_guardrails RENAME COLUMN guardrail_id_new TO guardrail_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='thoughts' AND column_name='project_id_new') THEN ALTER TABLE public.thoughts RENAME COLUMN project_id_new TO project_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='embeddings' AND column_name='thought_id_new') THEN ALTER TABLE public.embeddings RENAME COLUMN thought_id_new TO thought_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='stored_files' AND column_name='thought_id_new') THEN ALTER TABLE public.stored_files RENAME COLUMN thought_id_new TO thought_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='stored_files' AND column_name='project_id_new') THEN ALTER TABLE public.stored_files RENAME COLUMN project_id_new TO project_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_skills' AND column_name='project_id_new') THEN ALTER TABLE public.project_skills RENAME COLUMN project_id_new TO project_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_skills' AND column_name='skill_id_new') THEN ALTER TABLE public.project_skills RENAME COLUMN skill_id_new TO skill_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_guardrails' AND column_name='project_id_new') THEN ALTER TABLE public.project_guardrails RENAME COLUMN project_id_new TO project_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_guardrails' AND column_name='guardrail_id_new') THEN ALTER TABLE public.project_guardrails RENAME COLUMN guardrail_id_new TO guardrail_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='chat_histories' AND column_name='project_id_new') THEN ALTER TABLE public.chat_histories RENAME COLUMN project_id_new TO project_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='project_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN project_id_new TO project_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='related_thought_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN related_thought_id_new TO related_thought_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='related_skill_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN related_skill_id_new TO related_skill_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='duplicate_of_learning_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN duplicate_of_learning_id_new TO duplicate_of_learning_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='supersedes_learning_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN supersedes_learning_id_new TO supersedes_learning_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='project_id_new') THEN ALTER TABLE public.plans RENAME COLUMN project_id_new TO project_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='supersedes_plan_id_new') THEN ALTER TABLE public.plans RENAME COLUMN supersedes_plan_id_new TO supersedes_plan_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_dependencies' AND column_name='plan_id_new') THEN ALTER TABLE public.plan_dependencies RENAME COLUMN plan_id_new TO plan_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_dependencies' AND column_name='depends_on_plan_id_new') THEN ALTER TABLE public.plan_dependencies RENAME COLUMN depends_on_plan_id_new TO depends_on_plan_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_related_plans' AND column_name='plan_a_id_new') THEN ALTER TABLE public.plan_related_plans RENAME COLUMN plan_a_id_new TO plan_a_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_related_plans' AND column_name='plan_b_id_new') THEN ALTER TABLE public.plan_related_plans RENAME COLUMN plan_b_id_new TO plan_b_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_skills' AND column_name='plan_id_new') THEN ALTER TABLE public.plan_skills RENAME COLUMN plan_id_new TO plan_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_skills' AND column_name='skill_id_new') THEN ALTER TABLE public.plan_skills RENAME COLUMN skill_id_new TO skill_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_guardrails' AND column_name='plan_id_new') THEN ALTER TABLE public.plan_guardrails RENAME COLUMN plan_id_new TO plan_id; END IF; END $$; +-- DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_guardrails' AND column_name='guardrail_id_new') THEN ALTER TABLE public.plan_guardrails RENAME COLUMN guardrail_id_new TO guardrail_id; END IF; END $$; --- ============================================================ --- STEP 8: Upgrade serial → bigserial (alter sequence type) --- ============================================================ +-- -- ============================================================ +-- -- STEP 8: Upgrade serial → bigserial (alter sequence type) +-- -- ============================================================ --- thought_links -ALTER SEQUENCE IF EXISTS thought_links_id_seq AS bigint; -ALTER TABLE public.thought_links ALTER COLUMN id TYPE bigint; +-- -- thought_links +-- ALTER SEQUENCE IF EXISTS thought_links_id_seq AS bigint; +-- ALTER TABLE public.thought_links ALTER COLUMN id TYPE bigint; --- plan_dependencies -ALTER SEQUENCE IF EXISTS plan_dependencies_id_seq AS bigint; -ALTER TABLE public.plan_dependencies ALTER COLUMN id TYPE bigint; +-- -- plan_dependencies +-- ALTER SEQUENCE IF EXISTS plan_dependencies_id_seq AS bigint; +-- ALTER TABLE public.plan_dependencies ALTER COLUMN id TYPE bigint; --- plan_related_plans -ALTER SEQUENCE IF EXISTS plan_related_plans_id_seq AS bigint; -ALTER TABLE public.plan_related_plans ALTER COLUMN id TYPE bigint; +-- -- plan_related_plans +-- ALTER SEQUENCE IF EXISTS plan_related_plans_id_seq AS bigint; +-- ALTER TABLE public.plan_related_plans ALTER COLUMN id TYPE bigint; --- plan_skills -ALTER SEQUENCE IF EXISTS plan_skills_id_seq AS bigint; -ALTER TABLE public.plan_skills ALTER COLUMN id TYPE bigint; +-- -- plan_skills +-- ALTER SEQUENCE IF EXISTS plan_skills_id_seq AS bigint; +-- ALTER TABLE public.plan_skills ALTER COLUMN id TYPE bigint; --- plan_guardrails -ALTER SEQUENCE IF EXISTS plan_guardrails_id_seq AS bigint; -ALTER TABLE public.plan_guardrails ALTER COLUMN id TYPE bigint; +-- -- plan_guardrails +-- ALTER SEQUENCE IF EXISTS plan_guardrails_id_seq AS bigint; +-- ALTER TABLE public.plan_guardrails ALTER COLUMN id TYPE bigint; --- project_skills -ALTER SEQUENCE IF EXISTS project_skills_id_seq AS bigint; -ALTER TABLE public.project_skills ALTER COLUMN id TYPE bigint; +-- -- project_skills +-- ALTER SEQUENCE IF EXISTS project_skills_id_seq AS bigint; +-- ALTER TABLE public.project_skills ALTER COLUMN id TYPE bigint; --- project_guardrails -ALTER SEQUENCE IF EXISTS project_guardrails_id_seq AS bigint; -ALTER TABLE public.project_guardrails ALTER COLUMN id TYPE bigint; +-- -- project_guardrails +-- ALTER SEQUENCE IF EXISTS project_guardrails_id_seq AS bigint; +-- ALTER TABLE public.project_guardrails ALTER COLUMN id TYPE bigint; --- ============================================================ --- STEP 9: Add PK constraints to formerly uuid-pk tables --- ============================================================ +-- -- ============================================================ +-- -- STEP 9: Add PK constraints to formerly uuid-pk tables +-- -- ============================================================ -ALTER TABLE public.agent_skills DROP CONSTRAINT IF EXISTS agent_skills_pkey; -ALTER TABLE public.agent_guardrails DROP CONSTRAINT IF EXISTS agent_guardrails_pkey; -ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS chat_histories_pkey; -ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_pkey; -ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS plans_pkey; +-- ALTER TABLE public.agent_skills DROP CONSTRAINT IF EXISTS agent_skills_pkey; +-- ALTER TABLE public.agent_guardrails DROP CONSTRAINT IF EXISTS agent_guardrails_pkey; +-- ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS chat_histories_pkey; +-- ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_pkey; +-- ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS plans_pkey; -ALTER TABLE public.agent_skills ADD CONSTRAINT agent_skills_pkey PRIMARY KEY (id); -ALTER TABLE public.agent_guardrails ADD CONSTRAINT agent_guardrails_pkey PRIMARY KEY (id); -ALTER TABLE public.chat_histories ADD CONSTRAINT chat_histories_pkey PRIMARY KEY (id); -ALTER TABLE public.learnings ADD CONSTRAINT learnings_pkey PRIMARY KEY (id); -ALTER TABLE public.plans ADD CONSTRAINT plans_pkey PRIMARY KEY (id); +-- ALTER TABLE public.agent_skills ADD CONSTRAINT agent_skills_pkey PRIMARY KEY (id); +-- ALTER TABLE public.agent_guardrails ADD CONSTRAINT agent_guardrails_pkey PRIMARY KEY (id); +-- ALTER TABLE public.chat_histories ADD CONSTRAINT chat_histories_pkey PRIMARY KEY (id); +-- ALTER TABLE public.learnings ADD CONSTRAINT learnings_pkey PRIMARY KEY (id); +-- ALTER TABLE public.plans ADD CONSTRAINT plans_pkey PRIMARY KEY (id); --- Add unique constraint on guid columns -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='agent_skills' AND constraint_name='agent_skills_guid_key') THEN ALTER TABLE public.agent_skills ADD CONSTRAINT agent_skills_guid_key UNIQUE (guid); END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='agent_guardrails' AND constraint_name='agent_guardrails_guid_key') THEN ALTER TABLE public.agent_guardrails ADD CONSTRAINT agent_guardrails_guid_key UNIQUE (guid); END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='chat_histories' AND constraint_name='chat_histories_guid_key') THEN ALTER TABLE public.chat_histories ADD CONSTRAINT chat_histories_guid_key UNIQUE (guid); END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_guid_key') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_guid_key UNIQUE (guid); END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plans' AND constraint_name='plans_guid_key') THEN ALTER TABLE public.plans ADD CONSTRAINT plans_guid_key UNIQUE (guid); END IF; END $$; +-- -- Add unique constraint on guid columns +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='agent_skills' AND constraint_name='agent_skills_guid_key') THEN ALTER TABLE public.agent_skills ADD CONSTRAINT agent_skills_guid_key UNIQUE (guid); END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='agent_guardrails' AND constraint_name='agent_guardrails_guid_key') THEN ALTER TABLE public.agent_guardrails ADD CONSTRAINT agent_guardrails_guid_key UNIQUE (guid); END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='chat_histories' AND constraint_name='chat_histories_guid_key') THEN ALTER TABLE public.chat_histories ADD CONSTRAINT chat_histories_guid_key UNIQUE (guid); END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_guid_key') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_guid_key UNIQUE (guid); END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plans' AND constraint_name='plans_guid_key') THEN ALTER TABLE public.plans ADD CONSTRAINT plans_guid_key UNIQUE (guid); END IF; END $$; --- ============================================================ --- STEP 10: Re-add FK constraints --- ============================================================ +-- -- ============================================================ +-- -- STEP 10: Re-add FK constraints +-- -- ============================================================ -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='thoughts' AND constraint_name='thoughts_project_id_fkey') THEN ALTER TABLE public.thoughts ADD CONSTRAINT thoughts_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id); END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='embeddings' AND constraint_name='embeddings_thought_id_fkey') THEN ALTER TABLE public.embeddings ADD CONSTRAINT embeddings_thought_id_fkey FOREIGN KEY (thought_id) REFERENCES public.thoughts(id); END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='stored_files' AND constraint_name='stored_files_thought_id_fkey') THEN ALTER TABLE public.stored_files ADD CONSTRAINT stored_files_thought_id_fkey FOREIGN KEY (thought_id) REFERENCES public.thoughts(id); END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='stored_files' AND constraint_name='stored_files_project_id_fkey') THEN ALTER TABLE public.stored_files ADD CONSTRAINT stored_files_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id); END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='project_skills' AND constraint_name='project_skills_project_id_fkey') THEN ALTER TABLE public.project_skills ADD CONSTRAINT project_skills_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE; END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='project_skills' AND constraint_name='project_skills_skill_id_fkey') THEN ALTER TABLE public.project_skills ADD CONSTRAINT project_skills_skill_id_fkey FOREIGN KEY (skill_id) REFERENCES public.agent_skills(id) ON DELETE CASCADE; END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='project_guardrails' AND constraint_name='project_guardrails_project_id_fkey') THEN ALTER TABLE public.project_guardrails ADD CONSTRAINT project_guardrails_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE; END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='project_guardrails' AND constraint_name='project_guardrails_guardrail_id_fkey') THEN ALTER TABLE public.project_guardrails ADD CONSTRAINT project_guardrails_guardrail_id_fkey FOREIGN KEY (guardrail_id) REFERENCES public.agent_guardrails(id) ON DELETE CASCADE; END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='chat_histories' AND constraint_name='chat_histories_project_id_fkey') THEN ALTER TABLE public.chat_histories ADD CONSTRAINT chat_histories_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE SET NULL; END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_project_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE SET NULL; END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_related_thought_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_related_thought_id_fkey FOREIGN KEY (related_thought_id) REFERENCES public.thoughts(id) ON DELETE SET NULL; END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_related_skill_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_related_skill_id_fkey FOREIGN KEY (related_skill_id) REFERENCES public.agent_skills(id) ON DELETE SET NULL; END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_duplicate_of_learning_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_duplicate_of_learning_id_fkey FOREIGN KEY (duplicate_of_learning_id) REFERENCES public.learnings(id) ON DELETE SET NULL; END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_supersedes_learning_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_supersedes_learning_id_fkey FOREIGN KEY (supersedes_learning_id) REFERENCES public.learnings(id) ON DELETE SET NULL; END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plans' AND constraint_name='plans_project_id_fkey') THEN ALTER TABLE public.plans ADD CONSTRAINT plans_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE SET NULL; END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plans' AND constraint_name='plans_supersedes_plan_id_fkey') THEN ALTER TABLE public.plans ADD CONSTRAINT plans_supersedes_plan_id_fkey FOREIGN KEY (supersedes_plan_id) REFERENCES public.plans(id) ON DELETE SET NULL; END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_dependencies' AND constraint_name='plan_dependencies_plan_id_fkey') THEN ALTER TABLE public.plan_dependencies ADD CONSTRAINT plan_dependencies_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_dependencies' AND constraint_name='plan_dependencies_depends_on_plan_id_fkey') THEN ALTER TABLE public.plan_dependencies ADD CONSTRAINT plan_dependencies_depends_on_plan_id_fkey FOREIGN KEY (depends_on_plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_related_plans' AND constraint_name='plan_related_plans_plan_a_id_fkey') THEN ALTER TABLE public.plan_related_plans ADD CONSTRAINT plan_related_plans_plan_a_id_fkey FOREIGN KEY (plan_a_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_related_plans' AND constraint_name='plan_related_plans_plan_b_id_fkey') THEN ALTER TABLE public.plan_related_plans ADD CONSTRAINT plan_related_plans_plan_b_id_fkey FOREIGN KEY (plan_b_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_skills' AND constraint_name='plan_skills_plan_id_fkey') THEN ALTER TABLE public.plan_skills ADD CONSTRAINT plan_skills_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_skills' AND constraint_name='plan_skills_skill_id_fkey') THEN ALTER TABLE public.plan_skills ADD CONSTRAINT plan_skills_skill_id_fkey FOREIGN KEY (skill_id) REFERENCES public.agent_skills(id) ON DELETE CASCADE; END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_guardrails' AND constraint_name='plan_guardrails_plan_id_fkey') THEN ALTER TABLE public.plan_guardrails ADD CONSTRAINT plan_guardrails_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$; -DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_guardrails' AND constraint_name='plan_guardrails_guardrail_id_fkey') THEN ALTER TABLE public.plan_guardrails ADD CONSTRAINT plan_guardrails_guardrail_id_fkey FOREIGN KEY (guardrail_id) REFERENCES public.agent_guardrails(id) ON DELETE CASCADE; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='thoughts' AND constraint_name='thoughts_project_id_fkey') THEN ALTER TABLE public.thoughts ADD CONSTRAINT thoughts_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id); END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='embeddings' AND constraint_name='embeddings_thought_id_fkey') THEN ALTER TABLE public.embeddings ADD CONSTRAINT embeddings_thought_id_fkey FOREIGN KEY (thought_id) REFERENCES public.thoughts(id); END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='stored_files' AND constraint_name='stored_files_thought_id_fkey') THEN ALTER TABLE public.stored_files ADD CONSTRAINT stored_files_thought_id_fkey FOREIGN KEY (thought_id) REFERENCES public.thoughts(id); END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='stored_files' AND constraint_name='stored_files_project_id_fkey') THEN ALTER TABLE public.stored_files ADD CONSTRAINT stored_files_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id); END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='project_skills' AND constraint_name='project_skills_project_id_fkey') THEN ALTER TABLE public.project_skills ADD CONSTRAINT project_skills_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='project_skills' AND constraint_name='project_skills_skill_id_fkey') THEN ALTER TABLE public.project_skills ADD CONSTRAINT project_skills_skill_id_fkey FOREIGN KEY (skill_id) REFERENCES public.agent_skills(id) ON DELETE CASCADE; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='project_guardrails' AND constraint_name='project_guardrails_project_id_fkey') THEN ALTER TABLE public.project_guardrails ADD CONSTRAINT project_guardrails_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='project_guardrails' AND constraint_name='project_guardrails_guardrail_id_fkey') THEN ALTER TABLE public.project_guardrails ADD CONSTRAINT project_guardrails_guardrail_id_fkey FOREIGN KEY (guardrail_id) REFERENCES public.agent_guardrails(id) ON DELETE CASCADE; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='chat_histories' AND constraint_name='chat_histories_project_id_fkey') THEN ALTER TABLE public.chat_histories ADD CONSTRAINT chat_histories_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE SET NULL; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_project_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE SET NULL; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_related_thought_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_related_thought_id_fkey FOREIGN KEY (related_thought_id) REFERENCES public.thoughts(id) ON DELETE SET NULL; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_related_skill_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_related_skill_id_fkey FOREIGN KEY (related_skill_id) REFERENCES public.agent_skills(id) ON DELETE SET NULL; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_duplicate_of_learning_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_duplicate_of_learning_id_fkey FOREIGN KEY (duplicate_of_learning_id) REFERENCES public.learnings(id) ON DELETE SET NULL; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_supersedes_learning_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_supersedes_learning_id_fkey FOREIGN KEY (supersedes_learning_id) REFERENCES public.learnings(id) ON DELETE SET NULL; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plans' AND constraint_name='plans_project_id_fkey') THEN ALTER TABLE public.plans ADD CONSTRAINT plans_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE SET NULL; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plans' AND constraint_name='plans_supersedes_plan_id_fkey') THEN ALTER TABLE public.plans ADD CONSTRAINT plans_supersedes_plan_id_fkey FOREIGN KEY (supersedes_plan_id) REFERENCES public.plans(id) ON DELETE SET NULL; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_dependencies' AND constraint_name='plan_dependencies_plan_id_fkey') THEN ALTER TABLE public.plan_dependencies ADD CONSTRAINT plan_dependencies_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_dependencies' AND constraint_name='plan_dependencies_depends_on_plan_id_fkey') THEN ALTER TABLE public.plan_dependencies ADD CONSTRAINT plan_dependencies_depends_on_plan_id_fkey FOREIGN KEY (depends_on_plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_related_plans' AND constraint_name='plan_related_plans_plan_a_id_fkey') THEN ALTER TABLE public.plan_related_plans ADD CONSTRAINT plan_related_plans_plan_a_id_fkey FOREIGN KEY (plan_a_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_related_plans' AND constraint_name='plan_related_plans_plan_b_id_fkey') THEN ALTER TABLE public.plan_related_plans ADD CONSTRAINT plan_related_plans_plan_b_id_fkey FOREIGN KEY (plan_b_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_skills' AND constraint_name='plan_skills_plan_id_fkey') THEN ALTER TABLE public.plan_skills ADD CONSTRAINT plan_skills_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_skills' AND constraint_name='plan_skills_skill_id_fkey') THEN ALTER TABLE public.plan_skills ADD CONSTRAINT plan_skills_skill_id_fkey FOREIGN KEY (skill_id) REFERENCES public.agent_skills(id) ON DELETE CASCADE; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_guardrails' AND constraint_name='plan_guardrails_plan_id_fkey') THEN ALTER TABLE public.plan_guardrails ADD CONSTRAINT plan_guardrails_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$; +-- DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_guardrails' AND constraint_name='plan_guardrails_guardrail_id_fkey') THEN ALTER TABLE public.plan_guardrails ADD CONSTRAINT plan_guardrails_guardrail_id_fkey FOREIGN KEY (guardrail_id) REFERENCES public.agent_guardrails(id) ON DELETE CASCADE; END IF; END $$; -COMMIT; +-- COMMIT; diff --git a/migrations/020_generated_schema.sql b/migrations/020_generated_schema.sql index da55c3b..da0ca0a 100644 --- a/migrations/020_generated_schema.sql +++ b/migrations/020_generated_schema.sql @@ -2,6 +2,8 @@ -- Database: database -- Generated by RelSpec +CREATE EXTENSION IF NOT EXISTS pg_trgm; + -- Sequences for schema: public CREATE SEQUENCE IF NOT EXISTS public.identity_agent_personas_id INCREMENT 1 @@ -3249,30 +3251,4900 @@ BEGIN END; $$; +-- Alter column types for schema: public +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_personas' + AND a.attname = 'compiled_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.agent_personas + ALTER COLUMN compiled_at TYPE timestamptz USING compiled_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_personas' + AND a.attname = 'compiled_detail' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_personas + ALTER COLUMN compiled_detail TYPE text USING compiled_detail::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_personas' + AND a.attname = 'compiled_summary' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_personas + ALTER COLUMN compiled_summary TYPE text USING compiled_summary::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_personas' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.agent_personas + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_personas' + AND a.attname = 'description' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_personas + ALTER COLUMN description TYPE text USING description::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_personas' + AND a.attname = 'detail' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_personas + ALTER COLUMN detail TYPE text USING detail::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_personas' + AND a.attname = 'guid' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['uuid']) THEN + ALTER TABLE public.agent_personas + ALTER COLUMN guid TYPE uuid USING guid::uuid; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_personas' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.agent_personas + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_personas' + AND a.attname = 'name' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_personas + ALTER COLUMN name TYPE text USING name::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_personas' + AND a.attname = 'summary' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_personas + ALTER COLUMN summary TYPE text USING summary::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_personas' + AND a.attname = 'tags' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text[]']) THEN + ALTER TABLE public.agent_personas + ALTER COLUMN tags TYPE text[] USING tags::text[]; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_personas' + AND a.attname = 'updated_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.agent_personas + ALTER COLUMN updated_at TYPE timestamptz USING updated_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_parts' + AND a.attname = 'content' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_parts + ALTER COLUMN content TYPE text USING content::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_parts' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.agent_parts + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_parts' + AND a.attname = 'description' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_parts + ALTER COLUMN description TYPE text USING description::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_parts' + AND a.attname = 'guid' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['uuid']) THEN + ALTER TABLE public.agent_parts + ALTER COLUMN guid TYPE uuid USING guid::uuid; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_parts' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.agent_parts + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_parts' + AND a.attname = 'name' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_parts + ALTER COLUMN name TYPE text USING name::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_parts' + AND a.attname = 'part_type' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_parts + ALTER COLUMN part_type TYPE text USING part_type::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_parts' + AND a.attname = 'summary' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_parts + ALTER COLUMN summary TYPE text USING summary::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_parts' + AND a.attname = 'tags' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text[]']) THEN + ALTER TABLE public.agent_parts + ALTER COLUMN tags TYPE text[] USING tags::text[]; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_parts' + AND a.attname = 'updated_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.agent_parts + ALTER COLUMN updated_at TYPE timestamptz USING updated_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_persona_parts' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.agent_persona_parts + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_persona_parts' + AND a.attname = 'part_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.agent_persona_parts + ALTER COLUMN part_id TYPE bigint USING part_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_persona_parts' + AND a.attname = 'part_order' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['integer']) THEN + ALTER TABLE public.agent_persona_parts + ALTER COLUMN part_order TYPE integer USING part_order::integer; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_persona_parts' + AND a.attname = 'persona_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.agent_persona_parts + ALTER COLUMN persona_id TYPE bigint USING persona_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_persona_parts' + AND a.attname = 'priority' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['integer']) THEN + ALTER TABLE public.agent_persona_parts + ALTER COLUMN priority TYPE integer USING priority::integer; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_persona_skills' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.agent_persona_skills + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_persona_skills' + AND a.attname = 'persona_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.agent_persona_skills + ALTER COLUMN persona_id TYPE bigint USING persona_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_persona_skills' + AND a.attname = 'skill_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.agent_persona_skills + ALTER COLUMN skill_id TYPE bigint USING skill_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_persona_guardrails' + AND a.attname = 'guardrail_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.agent_persona_guardrails + ALTER COLUMN guardrail_id TYPE bigint USING guardrail_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_persona_guardrails' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.agent_persona_guardrails + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_persona_guardrails' + AND a.attname = 'persona_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.agent_persona_guardrails + ALTER COLUMN persona_id TYPE bigint USING persona_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_traits' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.agent_traits + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_traits' + AND a.attname = 'description' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_traits + ALTER COLUMN description TYPE text USING description::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_traits' + AND a.attname = 'guid' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['uuid']) THEN + ALTER TABLE public.agent_traits + ALTER COLUMN guid TYPE uuid USING guid::uuid; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_traits' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.agent_traits + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_traits' + AND a.attname = 'instruction' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_traits + ALTER COLUMN instruction TYPE text USING instruction::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_traits' + AND a.attname = 'name' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_traits + ALTER COLUMN name TYPE text USING name::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_traits' + AND a.attname = 'tags' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text[]']) THEN + ALTER TABLE public.agent_traits + ALTER COLUMN tags TYPE text[] USING tags::text[]; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_traits' + AND a.attname = 'trait_type' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_traits + ALTER COLUMN trait_type TYPE text USING trait_type::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_traits' + AND a.attname = 'updated_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.agent_traits + ALTER COLUMN updated_at TYPE timestamptz USING updated_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_persona_traits' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.agent_persona_traits + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_persona_traits' + AND a.attname = 'persona_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.agent_persona_traits + ALTER COLUMN persona_id TYPE bigint USING persona_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_persona_traits' + AND a.attname = 'trait_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.agent_persona_traits + ALTER COLUMN trait_id TYPE bigint USING trait_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'character_arcs' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.character_arcs + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'character_arcs' + AND a.attname = 'description' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.character_arcs + ALTER COLUMN description TYPE text USING description::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'character_arcs' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.character_arcs + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'character_arcs' + AND a.attname = 'name' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.character_arcs + ALTER COLUMN name TYPE text USING name::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'character_arcs' + AND a.attname = 'summary' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.character_arcs + ALTER COLUMN summary TYPE text USING summary::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'character_arcs' + AND a.attname = 'updated_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.character_arcs + ALTER COLUMN updated_at TYPE timestamptz USING updated_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'arc_stages' + AND a.attname = 'arc_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.arc_stages + ALTER COLUMN arc_id TYPE bigint USING arc_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'arc_stages' + AND a.attname = 'condition' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.arc_stages + ALTER COLUMN condition TYPE text USING condition::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'arc_stages' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.arc_stages + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'arc_stages' + AND a.attname = 'description' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.arc_stages + ALTER COLUMN description TYPE text USING description::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'arc_stages' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.arc_stages + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'arc_stages' + AND a.attname = 'name' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.arc_stages + ALTER COLUMN name TYPE text USING name::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'arc_stages' + AND a.attname = 'stage_order' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['integer']) THEN + ALTER TABLE public.arc_stages + ALTER COLUMN stage_order TYPE integer USING stage_order::integer; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'arc_stage_parts' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.arc_stage_parts + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'arc_stage_parts' + AND a.attname = 'part_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.arc_stage_parts + ALTER COLUMN part_id TYPE bigint USING part_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'arc_stage_parts' + AND a.attname = 'stage_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.arc_stage_parts + ALTER COLUMN stage_id TYPE bigint USING stage_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'persona_arc' + AND a.attname = 'arc_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.persona_arc + ALTER COLUMN arc_id TYPE bigint USING arc_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'persona_arc' + AND a.attname = 'current_stage_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.persona_arc + ALTER COLUMN current_stage_id TYPE bigint USING current_stage_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'persona_arc' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.persona_arc + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'persona_arc' + AND a.attname = 'persona_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.persona_arc + ALTER COLUMN persona_id TYPE bigint USING persona_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'persona_arc' + AND a.attname = 'updated_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.persona_arc + ALTER COLUMN updated_at TYPE timestamptz USING updated_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'thoughts' + AND a.attname = 'archived_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.thoughts + ALTER COLUMN archived_at TYPE timestamptz USING archived_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'thoughts' + AND a.attname = 'content' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.thoughts + ALTER COLUMN content TYPE text USING content::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'thoughts' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.thoughts + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'thoughts' + AND a.attname = 'guid' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['uuid']) THEN + ALTER TABLE public.thoughts + ALTER COLUMN guid TYPE uuid USING guid::uuid; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'thoughts' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.thoughts + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'thoughts' + AND a.attname = 'metadata' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['jsonb']) THEN + ALTER TABLE public.thoughts + ALTER COLUMN metadata TYPE jsonb USING metadata::jsonb; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'thoughts' + AND a.attname = 'project_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.thoughts + ALTER COLUMN project_id TYPE bigint USING project_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'thoughts' + AND a.attname = 'updated_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.thoughts + ALTER COLUMN updated_at TYPE timestamptz USING updated_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'projects' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.projects + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'projects' + AND a.attname = 'description' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.projects + ALTER COLUMN description TYPE text USING description::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'projects' + AND a.attname = 'guid' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['uuid']) THEN + ALTER TABLE public.projects + ALTER COLUMN guid TYPE uuid USING guid::uuid; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'projects' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.projects + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'projects' + AND a.attname = 'last_active_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.projects + ALTER COLUMN last_active_at TYPE timestamptz USING last_active_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'projects' + AND a.attname = 'name' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.projects + ALTER COLUMN name TYPE text USING name::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'thought_links' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.thought_links + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'thought_links' + AND a.attname = 'from_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.thought_links + ALTER COLUMN from_id TYPE bigint USING from_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'thought_links' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.thought_links + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'thought_links' + AND a.attname = 'relation' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.thought_links + ALTER COLUMN relation TYPE text USING relation::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'thought_links' + AND a.attname = 'to_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.thought_links + ALTER COLUMN to_id TYPE bigint USING to_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'embeddings' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.embeddings + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'embeddings' + AND a.attname = 'dim' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['integer']) THEN + ALTER TABLE public.embeddings + ALTER COLUMN dim TYPE integer USING dim::integer; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'embeddings' + AND a.attname = 'embedding' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['vector']) THEN + ALTER TABLE public.embeddings + ALTER COLUMN embedding TYPE vector USING embedding::vector; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'embeddings' + AND a.attname = 'guid' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['uuid']) THEN + ALTER TABLE public.embeddings + ALTER COLUMN guid TYPE uuid USING guid::uuid; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'embeddings' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.embeddings + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'embeddings' + AND a.attname = 'model' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.embeddings + ALTER COLUMN model TYPE text USING model::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'embeddings' + AND a.attname = 'thought_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.embeddings + ALTER COLUMN thought_id TYPE bigint USING thought_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'embeddings' + AND a.attname = 'updated_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.embeddings + ALTER COLUMN updated_at TYPE timestamptz USING updated_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'stored_files' + AND a.attname = 'content' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bytea']) THEN + ALTER TABLE public.stored_files + ALTER COLUMN content TYPE bytea USING content::bytea; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'stored_files' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.stored_files + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'stored_files' + AND a.attname = 'encoding' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.stored_files + ALTER COLUMN encoding TYPE text USING encoding::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'stored_files' + AND a.attname = 'guid' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['uuid']) THEN + ALTER TABLE public.stored_files + ALTER COLUMN guid TYPE uuid USING guid::uuid; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'stored_files' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.stored_files + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'stored_files' + AND a.attname = 'kind' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.stored_files + ALTER COLUMN kind TYPE text USING kind::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'stored_files' + AND a.attname = 'media_type' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.stored_files + ALTER COLUMN media_type TYPE text USING media_type::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'stored_files' + AND a.attname = 'name' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.stored_files + ALTER COLUMN name TYPE text USING name::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'stored_files' + AND a.attname = 'project_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.stored_files + ALTER COLUMN project_id TYPE bigint USING project_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'stored_files' + AND a.attname = 'sha256' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.stored_files + ALTER COLUMN sha256 TYPE text USING sha256::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'stored_files' + AND a.attname = 'size_bytes' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.stored_files + ALTER COLUMN size_bytes TYPE bigint USING size_bytes::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'stored_files' + AND a.attname = 'thought_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.stored_files + ALTER COLUMN thought_id TYPE bigint USING thought_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'stored_files' + AND a.attname = 'updated_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.stored_files + ALTER COLUMN updated_at TYPE timestamptz USING updated_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'chat_histories' + AND a.attname = 'agent_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.chat_histories + ALTER COLUMN agent_id TYPE text USING agent_id::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'chat_histories' + AND a.attname = 'channel' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.chat_histories + ALTER COLUMN channel TYPE text USING channel::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'chat_histories' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.chat_histories + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'chat_histories' + AND a.attname = 'guid' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['uuid']) THEN + ALTER TABLE public.chat_histories + ALTER COLUMN guid TYPE uuid USING guid::uuid; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'chat_histories' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.chat_histories + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'chat_histories' + AND a.attname = 'messages' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['jsonb']) THEN + ALTER TABLE public.chat_histories + ALTER COLUMN messages TYPE jsonb USING messages::jsonb; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'chat_histories' + AND a.attname = 'metadata' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['jsonb']) THEN + ALTER TABLE public.chat_histories + ALTER COLUMN metadata TYPE jsonb USING metadata::jsonb; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'chat_histories' + AND a.attname = 'project_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.chat_histories + ALTER COLUMN project_id TYPE bigint USING project_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'chat_histories' + AND a.attname = 'session_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.chat_histories + ALTER COLUMN session_id TYPE text USING session_id::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'chat_histories' + AND a.attname = 'summary' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.chat_histories + ALTER COLUMN summary TYPE text USING summary::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'chat_histories' + AND a.attname = 'title' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.chat_histories + ALTER COLUMN title TYPE text USING title::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'chat_histories' + AND a.attname = 'updated_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.chat_histories + ALTER COLUMN updated_at TYPE timestamptz USING updated_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'tool_annotations' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.tool_annotations + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'tool_annotations' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.tool_annotations + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'tool_annotations' + AND a.attname = 'notes' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.tool_annotations + ALTER COLUMN notes TYPE text USING notes::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'tool_annotations' + AND a.attname = 'tool_name' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.tool_annotations + ALTER COLUMN tool_name TYPE text USING tool_name::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'tool_annotations' + AND a.attname = 'updated_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.tool_annotations + ALTER COLUMN updated_at TYPE timestamptz USING updated_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'action_required' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['boolean']) THEN + ALTER TABLE public.learnings + ALTER COLUMN action_required TYPE boolean USING action_required::boolean; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'area' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.learnings + ALTER COLUMN area TYPE text USING area::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'category' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.learnings + ALTER COLUMN category TYPE text USING category::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'confidence' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.learnings + ALTER COLUMN confidence TYPE text USING confidence::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.learnings + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'details' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.learnings + ALTER COLUMN details TYPE text USING details::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'duplicate_of_learning_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.learnings + ALTER COLUMN duplicate_of_learning_id TYPE bigint USING duplicate_of_learning_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'guid' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['uuid']) THEN + ALTER TABLE public.learnings + ALTER COLUMN guid TYPE uuid USING guid::uuid; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.learnings + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'priority' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.learnings + ALTER COLUMN priority TYPE text USING priority::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'project_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.learnings + ALTER COLUMN project_id TYPE bigint USING project_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'related_skill_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.learnings + ALTER COLUMN related_skill_id TYPE bigint USING related_skill_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'related_thought_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.learnings + ALTER COLUMN related_thought_id TYPE bigint USING related_thought_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'reviewed_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.learnings + ALTER COLUMN reviewed_at TYPE timestamptz USING reviewed_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'reviewed_by' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.learnings + ALTER COLUMN reviewed_by TYPE text USING reviewed_by::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'source_ref' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.learnings + ALTER COLUMN source_ref TYPE text USING source_ref::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'source_type' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.learnings + ALTER COLUMN source_type TYPE text USING source_type::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'status' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.learnings + ALTER COLUMN status TYPE text USING status::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'summary' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.learnings + ALTER COLUMN summary TYPE text USING summary::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'supersedes_learning_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.learnings + ALTER COLUMN supersedes_learning_id TYPE bigint USING supersedes_learning_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'tags' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text[]']) THEN + ALTER TABLE public.learnings + ALTER COLUMN tags TYPE text[] USING tags::text[]; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'learnings' + AND a.attname = 'updated_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.learnings + ALTER COLUMN updated_at TYPE timestamptz USING updated_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'oauth_clients' + AND a.attname = 'client_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.oauth_clients + ALTER COLUMN client_id TYPE text USING client_id::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'oauth_clients' + AND a.attname = 'client_name' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.oauth_clients + ALTER COLUMN client_name TYPE text USING client_name::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'oauth_clients' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.oauth_clients + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'oauth_clients' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.oauth_clients + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'oauth_clients' + AND a.attname = 'redirect_uris' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text[]']) THEN + ALTER TABLE public.oauth_clients + ALTER COLUMN redirect_uris TYPE text[] USING redirect_uris::text[]; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plans' + AND a.attname = 'completed_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.plans + ALTER COLUMN completed_at TYPE timestamptz USING completed_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plans' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.plans + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plans' + AND a.attname = 'description' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.plans + ALTER COLUMN description TYPE text USING description::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plans' + AND a.attname = 'due_date' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.plans + ALTER COLUMN due_date TYPE timestamptz USING due_date::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plans' + AND a.attname = 'guid' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['uuid']) THEN + ALTER TABLE public.plans + ALTER COLUMN guid TYPE uuid USING guid::uuid; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plans' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.plans + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plans' + AND a.attname = 'last_reviewed_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.plans + ALTER COLUMN last_reviewed_at TYPE timestamptz USING last_reviewed_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plans' + AND a.attname = 'owner' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.plans + ALTER COLUMN owner TYPE text USING owner::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plans' + AND a.attname = 'priority' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.plans + ALTER COLUMN priority TYPE text USING priority::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plans' + AND a.attname = 'project_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.plans + ALTER COLUMN project_id TYPE bigint USING project_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plans' + AND a.attname = 'reviewed_by' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.plans + ALTER COLUMN reviewed_by TYPE text USING reviewed_by::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plans' + AND a.attname = 'status' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.plans + ALTER COLUMN status TYPE text USING status::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plans' + AND a.attname = 'supersedes_plan_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.plans + ALTER COLUMN supersedes_plan_id TYPE bigint USING supersedes_plan_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plans' + AND a.attname = 'tags' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text[]']) THEN + ALTER TABLE public.plans + ALTER COLUMN tags TYPE text[] USING tags::text[]; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plans' + AND a.attname = 'title' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.plans + ALTER COLUMN title TYPE text USING title::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plans' + AND a.attname = 'updated_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.plans + ALTER COLUMN updated_at TYPE timestamptz USING updated_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plan_dependencies' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.plan_dependencies + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plan_dependencies' + AND a.attname = 'depends_on_plan_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.plan_dependencies + ALTER COLUMN depends_on_plan_id TYPE bigint USING depends_on_plan_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plan_dependencies' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.plan_dependencies + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plan_dependencies' + AND a.attname = 'plan_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.plan_dependencies + ALTER COLUMN plan_id TYPE bigint USING plan_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plan_related_plans' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.plan_related_plans + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plan_related_plans' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.plan_related_plans + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plan_related_plans' + AND a.attname = 'plan_a_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.plan_related_plans + ALTER COLUMN plan_a_id TYPE bigint USING plan_a_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plan_related_plans' + AND a.attname = 'plan_b_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.plan_related_plans + ALTER COLUMN plan_b_id TYPE bigint USING plan_b_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plan_skills' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.plan_skills + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plan_skills' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.plan_skills + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plan_skills' + AND a.attname = 'plan_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.plan_skills + ALTER COLUMN plan_id TYPE bigint USING plan_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plan_skills' + AND a.attname = 'skill_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.plan_skills + ALTER COLUMN skill_id TYPE bigint USING skill_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plan_guardrails' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.plan_guardrails + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plan_guardrails' + AND a.attname = 'guardrail_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.plan_guardrails + ALTER COLUMN guardrail_id TYPE bigint USING guardrail_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plan_guardrails' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.plan_guardrails + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'plan_guardrails' + AND a.attname = 'plan_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.plan_guardrails + ALTER COLUMN plan_id TYPE bigint USING plan_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_skills' + AND a.attname = 'content' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_skills + ALTER COLUMN content TYPE text USING content::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_skills' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.agent_skills + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_skills' + AND a.attname = 'description' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_skills + ALTER COLUMN description TYPE text USING description::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_skills' + AND a.attname = 'domain_tags' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text[]']) THEN + ALTER TABLE public.agent_skills + ALTER COLUMN domain_tags TYPE text[] USING domain_tags::text[]; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_skills' + AND a.attname = 'framework_tags' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text[]']) THEN + ALTER TABLE public.agent_skills + ALTER COLUMN framework_tags TYPE text[] USING framework_tags::text[]; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_skills' + AND a.attname = 'guid' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['uuid']) THEN + ALTER TABLE public.agent_skills + ALTER COLUMN guid TYPE uuid USING guid::uuid; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_skills' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.agent_skills + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_skills' + AND a.attname = 'language_tags' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text[]']) THEN + ALTER TABLE public.agent_skills + ALTER COLUMN language_tags TYPE text[] USING language_tags::text[]; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_skills' + AND a.attname = 'library_tags' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text[]']) THEN + ALTER TABLE public.agent_skills + ALTER COLUMN library_tags TYPE text[] USING library_tags::text[]; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_skills' + AND a.attname = 'name' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_skills + ALTER COLUMN name TYPE text USING name::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_skills' + AND a.attname = 'tags' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text[]']) THEN + ALTER TABLE public.agent_skills + ALTER COLUMN tags TYPE text[] USING tags::text[]; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_skills' + AND a.attname = 'updated_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.agent_skills + ALTER COLUMN updated_at TYPE timestamptz USING updated_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_guardrails' + AND a.attname = 'content' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_guardrails + ALTER COLUMN content TYPE text USING content::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_guardrails' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.agent_guardrails + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_guardrails' + AND a.attname = 'description' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_guardrails + ALTER COLUMN description TYPE text USING description::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_guardrails' + AND a.attname = 'guid' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['uuid']) THEN + ALTER TABLE public.agent_guardrails + ALTER COLUMN guid TYPE uuid USING guid::uuid; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_guardrails' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.agent_guardrails + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_guardrails' + AND a.attname = 'name' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_guardrails + ALTER COLUMN name TYPE text USING name::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_guardrails' + AND a.attname = 'severity' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text']) THEN + ALTER TABLE public.agent_guardrails + ALTER COLUMN severity TYPE text USING severity::text; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_guardrails' + AND a.attname = 'tags' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['text[]']) THEN + ALTER TABLE public.agent_guardrails + ALTER COLUMN tags TYPE text[] USING tags::text[]; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'agent_guardrails' + AND a.attname = 'updated_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.agent_guardrails + ALTER COLUMN updated_at TYPE timestamptz USING updated_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'project_skills' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.project_skills + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'project_skills' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.project_skills + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'project_skills' + AND a.attname = 'project_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.project_skills + ALTER COLUMN project_id TYPE bigint USING project_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'project_skills' + AND a.attname = 'skill_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.project_skills + ALTER COLUMN skill_id TYPE bigint USING skill_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'project_guardrails' + AND a.attname = 'created_at' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['timestamptz', 'timestamp with time zone']) THEN + ALTER TABLE public.project_guardrails + ALTER COLUMN created_at TYPE timestamptz USING created_at::timestamptz; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'project_guardrails' + AND a.attname = 'guardrail_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.project_guardrails + ALTER COLUMN guardrail_id TYPE bigint USING guardrail_id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'project_guardrails' + AND a.attname = 'id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.project_guardrails + ALTER COLUMN id TYPE bigint USING id::bigint; + END IF; +END; +$$; + +DO $$ +DECLARE + current_type text; +BEGIN + SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) + INTO current_type + FROM pg_attribute a + JOIN pg_class t ON t.oid = a.attrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname = 'public' + AND t.relname = 'project_guardrails' + AND a.attname = 'project_id' + AND a.attnum > 0 + AND NOT a.attisdropped; + + IF current_type IS NOT NULL + AND current_type <> ALL(ARRAY['bigint']) THEN + ALTER TABLE public.project_guardrails + ALTER COLUMN project_id TYPE bigint USING project_id::bigint; + END IF; +END; +$$; + -- Primary keys for schema: public DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'agent_personas' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('agent_personas_pkey', 'public_agent_personas_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'agent_personas' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'agent_personas' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.agent_personas DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('agent_personas_pkey', 'public_agent_personas_pkey') THEN + EXECUTE 'ALTER TABLE public.agent_personas DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'agent_personas' - AND constraint_name = 'pk_public_agent_personas' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('agent_personas_pkey', 'public_agent_personas_pkey')) THEN ALTER TABLE public.agent_personas ADD CONSTRAINT pk_public_agent_personas PRIMARY KEY (id); END IF; END; @@ -3280,27 +8152,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'agent_parts' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('agent_parts_pkey', 'public_agent_parts_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'agent_parts' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'agent_parts' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.agent_parts DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('agent_parts_pkey', 'public_agent_parts_pkey') THEN + EXECUTE 'ALTER TABLE public.agent_parts DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'agent_parts' - AND constraint_name = 'pk_public_agent_parts' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('agent_parts_pkey', 'public_agent_parts_pkey')) THEN ALTER TABLE public.agent_parts ADD CONSTRAINT pk_public_agent_parts PRIMARY KEY (id); END IF; END; @@ -3308,27 +8196,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'agent_persona_parts' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('agent_persona_parts_pkey', 'public_agent_persona_parts_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'agent_persona_parts' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'agent_persona_parts' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.agent_persona_parts DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('agent_persona_parts_pkey', 'public_agent_persona_parts_pkey') THEN + EXECUTE 'ALTER TABLE public.agent_persona_parts DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'agent_persona_parts' - AND constraint_name = 'pk_public_agent_persona_parts' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('agent_persona_parts_pkey', 'public_agent_persona_parts_pkey')) THEN ALTER TABLE public.agent_persona_parts ADD CONSTRAINT pk_public_agent_persona_parts PRIMARY KEY (id); END IF; END; @@ -3336,27 +8240,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'agent_persona_skills' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('agent_persona_skills_pkey', 'public_agent_persona_skills_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'agent_persona_skills' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'agent_persona_skills' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.agent_persona_skills DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('agent_persona_skills_pkey', 'public_agent_persona_skills_pkey') THEN + EXECUTE 'ALTER TABLE public.agent_persona_skills DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'agent_persona_skills' - AND constraint_name = 'pk_public_agent_persona_skills' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('agent_persona_skills_pkey', 'public_agent_persona_skills_pkey')) THEN ALTER TABLE public.agent_persona_skills ADD CONSTRAINT pk_public_agent_persona_skills PRIMARY KEY (id); END IF; END; @@ -3364,27 +8284,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'agent_persona_guardrails' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('agent_persona_guardrails_pkey', 'public_agent_persona_guardrails_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'agent_persona_guardrails' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'agent_persona_guardrails' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.agent_persona_guardrails DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('agent_persona_guardrails_pkey', 'public_agent_persona_guardrails_pkey') THEN + EXECUTE 'ALTER TABLE public.agent_persona_guardrails DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'agent_persona_guardrails' - AND constraint_name = 'pk_public_agent_persona_guardrails' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('agent_persona_guardrails_pkey', 'public_agent_persona_guardrails_pkey')) THEN ALTER TABLE public.agent_persona_guardrails ADD CONSTRAINT pk_public_agent_persona_guardrails PRIMARY KEY (id); END IF; END; @@ -3392,27 +8328,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'agent_traits' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('agent_traits_pkey', 'public_agent_traits_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'agent_traits' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'agent_traits' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.agent_traits DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('agent_traits_pkey', 'public_agent_traits_pkey') THEN + EXECUTE 'ALTER TABLE public.agent_traits DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'agent_traits' - AND constraint_name = 'pk_public_agent_traits' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('agent_traits_pkey', 'public_agent_traits_pkey')) THEN ALTER TABLE public.agent_traits ADD CONSTRAINT pk_public_agent_traits PRIMARY KEY (id); END IF; END; @@ -3420,27 +8372,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'agent_persona_traits' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('agent_persona_traits_pkey', 'public_agent_persona_traits_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'agent_persona_traits' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'agent_persona_traits' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.agent_persona_traits DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('agent_persona_traits_pkey', 'public_agent_persona_traits_pkey') THEN + EXECUTE 'ALTER TABLE public.agent_persona_traits DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'agent_persona_traits' - AND constraint_name = 'pk_public_agent_persona_traits' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('agent_persona_traits_pkey', 'public_agent_persona_traits_pkey')) THEN ALTER TABLE public.agent_persona_traits ADD CONSTRAINT pk_public_agent_persona_traits PRIMARY KEY (id); END IF; END; @@ -3448,27 +8416,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'character_arcs' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('character_arcs_pkey', 'public_character_arcs_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'character_arcs' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'character_arcs' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.character_arcs DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('character_arcs_pkey', 'public_character_arcs_pkey') THEN + EXECUTE 'ALTER TABLE public.character_arcs DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'character_arcs' - AND constraint_name = 'pk_public_character_arcs' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('character_arcs_pkey', 'public_character_arcs_pkey')) THEN ALTER TABLE public.character_arcs ADD CONSTRAINT pk_public_character_arcs PRIMARY KEY (id); END IF; END; @@ -3476,27 +8460,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'arc_stages' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('arc_stages_pkey', 'public_arc_stages_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'arc_stages' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'arc_stages' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.arc_stages DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('arc_stages_pkey', 'public_arc_stages_pkey') THEN + EXECUTE 'ALTER TABLE public.arc_stages DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'arc_stages' - AND constraint_name = 'pk_public_arc_stages' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('arc_stages_pkey', 'public_arc_stages_pkey')) THEN ALTER TABLE public.arc_stages ADD CONSTRAINT pk_public_arc_stages PRIMARY KEY (id); END IF; END; @@ -3504,27 +8504,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'arc_stage_parts' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('arc_stage_parts_pkey', 'public_arc_stage_parts_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'arc_stage_parts' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'arc_stage_parts' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.arc_stage_parts DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('arc_stage_parts_pkey', 'public_arc_stage_parts_pkey') THEN + EXECUTE 'ALTER TABLE public.arc_stage_parts DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'arc_stage_parts' - AND constraint_name = 'pk_public_arc_stage_parts' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('arc_stage_parts_pkey', 'public_arc_stage_parts_pkey')) THEN ALTER TABLE public.arc_stage_parts ADD CONSTRAINT pk_public_arc_stage_parts PRIMARY KEY (id); END IF; END; @@ -3532,27 +8548,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'persona_arc' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('persona_arc_pkey', 'public_persona_arc_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'persona_arc' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id', 'persona_id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'persona_arc' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.persona_arc DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('persona_arc_pkey', 'public_persona_arc_pkey') THEN + EXECUTE 'ALTER TABLE public.persona_arc DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'persona_arc' - AND constraint_name = 'pk_public_persona_arc' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('persona_arc_pkey', 'public_persona_arc_pkey')) THEN ALTER TABLE public.persona_arc ADD CONSTRAINT pk_public_persona_arc PRIMARY KEY (id, persona_id); END IF; END; @@ -3560,27 +8592,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'thoughts' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('thoughts_pkey', 'public_thoughts_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'thoughts' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'thoughts' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.thoughts DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('thoughts_pkey', 'public_thoughts_pkey') THEN + EXECUTE 'ALTER TABLE public.thoughts DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'thoughts' - AND constraint_name = 'pk_public_thoughts' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('thoughts_pkey', 'public_thoughts_pkey')) THEN ALTER TABLE public.thoughts ADD CONSTRAINT pk_public_thoughts PRIMARY KEY (id); END IF; END; @@ -3588,27 +8636,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'projects' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('projects_pkey', 'public_projects_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'projects' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'projects' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.projects DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('projects_pkey', 'public_projects_pkey') THEN + EXECUTE 'ALTER TABLE public.projects DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'projects' - AND constraint_name = 'pk_public_projects' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('projects_pkey', 'public_projects_pkey')) THEN ALTER TABLE public.projects ADD CONSTRAINT pk_public_projects PRIMARY KEY (id); END IF; END; @@ -3616,27 +8680,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'thought_links' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('thought_links_pkey', 'public_thought_links_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'thought_links' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'thought_links' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.thought_links DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('thought_links_pkey', 'public_thought_links_pkey') THEN + EXECUTE 'ALTER TABLE public.thought_links DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'thought_links' - AND constraint_name = 'pk_public_thought_links' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('thought_links_pkey', 'public_thought_links_pkey')) THEN ALTER TABLE public.thought_links ADD CONSTRAINT pk_public_thought_links PRIMARY KEY (id); END IF; END; @@ -3644,27 +8724,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'embeddings' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('embeddings_pkey', 'public_embeddings_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'embeddings' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'embeddings' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.embeddings DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('embeddings_pkey', 'public_embeddings_pkey') THEN + EXECUTE 'ALTER TABLE public.embeddings DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'embeddings' - AND constraint_name = 'pk_public_embeddings' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('embeddings_pkey', 'public_embeddings_pkey')) THEN ALTER TABLE public.embeddings ADD CONSTRAINT pk_public_embeddings PRIMARY KEY (id); END IF; END; @@ -3672,27 +8768,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'stored_files' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('stored_files_pkey', 'public_stored_files_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'stored_files' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'stored_files' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.stored_files DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('stored_files_pkey', 'public_stored_files_pkey') THEN + EXECUTE 'ALTER TABLE public.stored_files DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'stored_files' - AND constraint_name = 'pk_public_stored_files' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('stored_files_pkey', 'public_stored_files_pkey')) THEN ALTER TABLE public.stored_files ADD CONSTRAINT pk_public_stored_files PRIMARY KEY (id); END IF; END; @@ -3700,27 +8812,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'chat_histories' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('chat_histories_pkey', 'public_chat_histories_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'chat_histories' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'chat_histories' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.chat_histories DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('chat_histories_pkey', 'public_chat_histories_pkey') THEN + EXECUTE 'ALTER TABLE public.chat_histories DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'chat_histories' - AND constraint_name = 'pk_public_chat_histories' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('chat_histories_pkey', 'public_chat_histories_pkey')) THEN ALTER TABLE public.chat_histories ADD CONSTRAINT pk_public_chat_histories PRIMARY KEY (id); END IF; END; @@ -3728,27 +8856,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'tool_annotations' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('tool_annotations_pkey', 'public_tool_annotations_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'tool_annotations' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'tool_annotations' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.tool_annotations DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('tool_annotations_pkey', 'public_tool_annotations_pkey') THEN + EXECUTE 'ALTER TABLE public.tool_annotations DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'tool_annotations' - AND constraint_name = 'pk_public_tool_annotations' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('tool_annotations_pkey', 'public_tool_annotations_pkey')) THEN ALTER TABLE public.tool_annotations ADD CONSTRAINT pk_public_tool_annotations PRIMARY KEY (id); END IF; END; @@ -3756,27 +8900,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'learnings' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('learnings_pkey', 'public_learnings_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'learnings' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'learnings' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.learnings DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('learnings_pkey', 'public_learnings_pkey') THEN + EXECUTE 'ALTER TABLE public.learnings DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'learnings' - AND constraint_name = 'pk_public_learnings' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('learnings_pkey', 'public_learnings_pkey')) THEN ALTER TABLE public.learnings ADD CONSTRAINT pk_public_learnings PRIMARY KEY (id); END IF; END; @@ -3784,27 +8944,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'oauth_clients' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('oauth_clients_pkey', 'public_oauth_clients_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'oauth_clients' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'oauth_clients' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.oauth_clients DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('oauth_clients_pkey', 'public_oauth_clients_pkey') THEN + EXECUTE 'ALTER TABLE public.oauth_clients DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'oauth_clients' - AND constraint_name = 'pk_public_oauth_clients' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('oauth_clients_pkey', 'public_oauth_clients_pkey')) THEN ALTER TABLE public.oauth_clients ADD CONSTRAINT pk_public_oauth_clients PRIMARY KEY (id); END IF; END; @@ -3812,27 +8988,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'plans' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('plans_pkey', 'public_plans_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'plans' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'plans' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.plans DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('plans_pkey', 'public_plans_pkey') THEN + EXECUTE 'ALTER TABLE public.plans DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'plans' - AND constraint_name = 'pk_public_plans' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('plans_pkey', 'public_plans_pkey')) THEN ALTER TABLE public.plans ADD CONSTRAINT pk_public_plans PRIMARY KEY (id); END IF; END; @@ -3840,27 +9032,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'plan_dependencies' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('plan_dependencies_pkey', 'public_plan_dependencies_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'plan_dependencies' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'plan_dependencies' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.plan_dependencies DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('plan_dependencies_pkey', 'public_plan_dependencies_pkey') THEN + EXECUTE 'ALTER TABLE public.plan_dependencies DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'plan_dependencies' - AND constraint_name = 'pk_public_plan_dependencies' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('plan_dependencies_pkey', 'public_plan_dependencies_pkey')) THEN ALTER TABLE public.plan_dependencies ADD CONSTRAINT pk_public_plan_dependencies PRIMARY KEY (id); END IF; END; @@ -3868,27 +9076,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'plan_related_plans' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('plan_related_plans_pkey', 'public_plan_related_plans_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'plan_related_plans' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'plan_related_plans' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.plan_related_plans DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('plan_related_plans_pkey', 'public_plan_related_plans_pkey') THEN + EXECUTE 'ALTER TABLE public.plan_related_plans DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'plan_related_plans' - AND constraint_name = 'pk_public_plan_related_plans' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('plan_related_plans_pkey', 'public_plan_related_plans_pkey')) THEN ALTER TABLE public.plan_related_plans ADD CONSTRAINT pk_public_plan_related_plans PRIMARY KEY (id); END IF; END; @@ -3896,27 +9120,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'plan_skills' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('plan_skills_pkey', 'public_plan_skills_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'plan_skills' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'plan_skills' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.plan_skills DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('plan_skills_pkey', 'public_plan_skills_pkey') THEN + EXECUTE 'ALTER TABLE public.plan_skills DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'plan_skills' - AND constraint_name = 'pk_public_plan_skills' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('plan_skills_pkey', 'public_plan_skills_pkey')) THEN ALTER TABLE public.plan_skills ADD CONSTRAINT pk_public_plan_skills PRIMARY KEY (id); END IF; END; @@ -3924,27 +9164,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'plan_guardrails' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('plan_guardrails_pkey', 'public_plan_guardrails_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'plan_guardrails' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'plan_guardrails' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.plan_guardrails DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('plan_guardrails_pkey', 'public_plan_guardrails_pkey') THEN + EXECUTE 'ALTER TABLE public.plan_guardrails DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'plan_guardrails' - AND constraint_name = 'pk_public_plan_guardrails' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('plan_guardrails_pkey', 'public_plan_guardrails_pkey')) THEN ALTER TABLE public.plan_guardrails ADD CONSTRAINT pk_public_plan_guardrails PRIMARY KEY (id); END IF; END; @@ -3952,27 +9208,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'agent_skills' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('agent_skills_pkey', 'public_agent_skills_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'agent_skills' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'agent_skills' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.agent_skills DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('agent_skills_pkey', 'public_agent_skills_pkey') THEN + EXECUTE 'ALTER TABLE public.agent_skills DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'agent_skills' - AND constraint_name = 'pk_public_agent_skills' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('agent_skills_pkey', 'public_agent_skills_pkey')) THEN ALTER TABLE public.agent_skills ADD CONSTRAINT pk_public_agent_skills PRIMARY KEY (id); END IF; END; @@ -3980,27 +9252,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'agent_guardrails' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('agent_guardrails_pkey', 'public_agent_guardrails_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'agent_guardrails' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'agent_guardrails' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.agent_guardrails DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('agent_guardrails_pkey', 'public_agent_guardrails_pkey') THEN + EXECUTE 'ALTER TABLE public.agent_guardrails DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'agent_guardrails' - AND constraint_name = 'pk_public_agent_guardrails' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('agent_guardrails_pkey', 'public_agent_guardrails_pkey')) THEN ALTER TABLE public.agent_guardrails ADD CONSTRAINT pk_public_agent_guardrails PRIMARY KEY (id); END IF; END; @@ -4008,27 +9296,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'project_skills' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('project_skills_pkey', 'public_project_skills_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'project_skills' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'project_skills' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.project_skills DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('project_skills_pkey', 'public_project_skills_pkey') THEN + EXECUTE 'ALTER TABLE public.project_skills DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'project_skills' - AND constraint_name = 'pk_public_project_skills' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('project_skills_pkey', 'public_project_skills_pkey')) THEN ALTER TABLE public.project_skills ADD CONSTRAINT pk_public_project_skills PRIMARY KEY (id); END IF; END; @@ -4036,27 +9340,43 @@ $$; DO $$ DECLARE - auto_pk_name text; + current_pk_name text; + current_pk_matches boolean := false; BEGIN - -- Drop auto-generated primary key if it exists - SELECT constraint_name INTO auto_pk_name - FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'project_guardrails' - AND constraint_type = 'PRIMARY KEY' - AND constraint_name IN ('project_guardrails_pkey', 'public_project_guardrails_pkey'); + SELECT tc.constraint_name, + COALESCE( + ARRAY( + SELECT a.attname::text + FROM pg_constraint c + JOIN pg_class t ON t.oid = c.conrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + JOIN unnest(c.conkey) WITH ORDINALITY AS cols(attnum, ord) + ON TRUE + JOIN pg_attribute a + ON a.attrelid = t.oid + AND a.attnum = cols.attnum + WHERE c.contype = 'p' + AND n.nspname = 'public' + AND t.relname = 'project_guardrails' + ORDER BY cols.ord + ), + ARRAY[]::text[] + ) = ARRAY['id'] + INTO current_pk_name, current_pk_matches + FROM information_schema.table_constraints tc + WHERE tc.table_schema = 'public' + AND tc.table_name = 'project_guardrails' + AND tc.constraint_type = 'PRIMARY KEY'; - IF auto_pk_name IS NOT NULL THEN - EXECUTE 'ALTER TABLE public.project_guardrails DROP CONSTRAINT ' || quote_ident(auto_pk_name); + IF current_pk_name IS NOT NULL + AND NOT current_pk_matches + AND current_pk_name IN ('project_guardrails_pkey', 'public_project_guardrails_pkey') THEN + EXECUTE 'ALTER TABLE public.project_guardrails DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE'; END IF; - -- Add named primary key if it doesn't exist - IF NOT EXISTS ( - SELECT 1 FROM information_schema.table_constraints - WHERE table_schema = 'public' - AND table_name = 'project_guardrails' - AND constraint_name = 'pk_public_project_guardrails' - ) THEN + -- Add the desired primary key only when no matching primary key already exists. + IF current_pk_name IS NULL + OR (NOT current_pk_matches AND current_pk_name IN ('project_guardrails_pkey', 'public_project_guardrails_pkey')) THEN ALTER TABLE public.project_guardrails ADD CONSTRAINT pk_public_project_guardrails PRIMARY KEY (id); END IF; END; @@ -4085,10 +9405,10 @@ CREATE INDEX IF NOT EXISTS idx_learnings_summary ON public.learnings USING gin (summary gin_trgm_ops); CREATE INDEX IF NOT EXISTS idx_learnings_tags - ON public.learnings USING gin (tags gin_trgm_ops); + ON public.learnings USING gin (tags array_ops); CREATE INDEX IF NOT EXISTS idx_plans_tags - ON public.plans USING gin (tags gin_trgm_ops); + ON public.plans USING gin (tags array_ops); CREATE INDEX IF NOT EXISTS idx_plans_title ON public.plans USING gin (title gin_trgm_ops); diff --git a/migrations/100_rls_and_grants.sql b/migrations/100_rls_and_grants.sql index 20dfbdb..1ee3e83 100644 --- a/migrations/100_rls_and_grants.sql +++ b/migrations/100_rls_and_grants.sql @@ -6,35 +6,35 @@ GRANT ALL ON TABLE public.thought_links TO amcs; GRANT ALL ON TABLE public.embeddings TO amcs; -- Household Knowledge (011) -GRANT ALL ON TABLE public.household_items TO amcs; -GRANT ALL ON TABLE public.household_vendors TO amcs; +-- GRANT ALL ON TABLE public.household_items TO amcs; +-- GRANT ALL ON TABLE public.household_vendors TO amcs; -- Home Maintenance (012) -GRANT ALL ON TABLE public.maintenance_tasks TO amcs; -GRANT ALL ON TABLE public.maintenance_logs TO amcs; +-- GRANT ALL ON TABLE public.maintenance_tasks TO amcs; +-- GRANT ALL ON TABLE public.maintenance_logs TO amcs; -- Family Calendar (013) -GRANT ALL ON TABLE public.family_members TO amcs; -GRANT ALL ON TABLE public.activities TO amcs; -GRANT ALL ON TABLE public.important_dates TO amcs; +-- GRANT ALL ON TABLE public.family_members TO amcs; +-- GRANT ALL ON TABLE public.activities TO amcs; +-- GRANT ALL ON TABLE public.important_dates TO amcs; --- Meal Planning (014) -GRANT ALL ON TABLE public.recipes TO amcs; -GRANT ALL ON TABLE public.meal_plans TO amcs; -GRANT ALL ON TABLE public.shopping_lists TO amcs; +-- -- Meal Planning (014) +-- GRANT ALL ON TABLE public.recipes TO amcs; +-- GRANT ALL ON TABLE public.meal_plans TO amcs; +-- GRANT ALL ON TABLE public.shopping_lists TO amcs; --- Professional CRM (015) -GRANT ALL ON TABLE public.professional_contacts TO amcs; -GRANT ALL ON TABLE public.contact_interactions TO amcs; -GRANT ALL ON TABLE public.opportunities TO amcs; +-- -- Professional CRM (015) +-- GRANT ALL ON TABLE public.professional_contacts TO amcs; +-- GRANT ALL ON TABLE public.contact_interactions TO amcs; +-- GRANT ALL ON TABLE public.opportunities TO amcs; -GRANT ALL ON TABLE public.stored_files TO amcs; -GRANT ALL ON TABLE public.agent_guardrails TO amcs; -GRANT ALL ON TABLE public.agent_skills TO amcs; +-- GRANT ALL ON TABLE public.stored_files TO amcs; +-- GRANT ALL ON TABLE public.agent_guardrails TO amcs; +-- GRANT ALL ON TABLE public.agent_skills TO amcs; -GRANT ALL ON TABLE public.project_skills TO amcs; +-- GRANT ALL ON TABLE public.project_skills TO amcs; -GRANT ALL ON TABLE public.project_guardrails TO amcs; +-- GRANT ALL ON TABLE public.project_guardrails TO amcs; -- Chat Histories (018) GRANT ALL ON TABLE public.chat_histories TO amcs;