-- PostgreSQL Database Schema
-- 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
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_agent_parts_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_agent_persona_parts_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_agent_persona_skills_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_project_personas_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_agent_persona_guardrails_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_agent_traits_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_agent_persona_traits_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_character_arcs_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_arc_stages_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_arc_stage_parts_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_persona_arc_persona_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_thoughts_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_projects_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_thought_links_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_thought_learning_links_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_embeddings_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_stored_files_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_chat_histories_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_tool_annotations_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_learnings_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_oauth_clients_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_plans_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_plan_dependencies_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_plan_related_plans_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_plan_skills_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_plan_guardrails_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_agent_skills_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_agent_guardrails_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_project_skills_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE SEQUENCE IF NOT EXISTS public.identity_project_guardrails_id
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

-- Tables for schema: public
CREATE TABLE IF NOT EXISTS public.agent_personas (
  compiled_at timestamptz,
  compiled_detail text NOT NULL DEFAULT '',
  compiled_summary text NOT NULL DEFAULT '',
  created_at timestamptz NOT NULL DEFAULT now(),
  description text NOT NULL DEFAULT '',
  detail text NOT NULL DEFAULT '',
  guid uuid NOT NULL DEFAULT gen_random_uuid(),
  id bigserial NOT NULL,
  name text NOT NULL,
  summary text NOT NULL,
  tags text[] NOT NULL DEFAULT '{}',
  updated_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS public.agent_parts (
  content text NOT NULL DEFAULT '',
  created_at timestamptz NOT NULL DEFAULT now(),
  description text NOT NULL DEFAULT '',
  guid uuid NOT NULL DEFAULT gen_random_uuid(),
  id bigserial NOT NULL,
  name text NOT NULL,
  part_type text NOT NULL,
  summary text NOT NULL,
  tags text[] NOT NULL DEFAULT '{}',
  updated_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS public.agent_persona_parts (
  id bigserial NOT NULL,
  part_id bigint NOT NULL,
  part_order integer NOT NULL DEFAULT 0,
  persona_id bigint NOT NULL,
  priority integer NOT NULL DEFAULT 0
);

CREATE TABLE IF NOT EXISTS public.agent_persona_skills (
  id bigserial NOT NULL,
  override boolean NOT NULL DEFAULT false,
  persona_id bigint NOT NULL,
  skill_id bigint NOT NULL
);

CREATE TABLE IF NOT EXISTS public.project_personas (
  created_at timestamptz NOT NULL DEFAULT now(),
  id bigserial NOT NULL,
  is_default boolean NOT NULL DEFAULT false,
  persona_id bigint NOT NULL,
  project_id bigint NOT NULL
);

CREATE TABLE IF NOT EXISTS public.agent_persona_guardrails (
  guardrail_id bigint NOT NULL,
  id bigserial NOT NULL,
  persona_id bigint NOT NULL
);

CREATE TABLE IF NOT EXISTS public.agent_traits (
  created_at timestamptz NOT NULL DEFAULT now(),
  description text NOT NULL DEFAULT '',
  guid uuid NOT NULL DEFAULT gen_random_uuid(),
  id bigserial NOT NULL,
  instruction text NOT NULL DEFAULT '',
  name text NOT NULL,
  tags text[] NOT NULL DEFAULT '{}',
  trait_type text NOT NULL,
  updated_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS public.agent_persona_traits (
  id bigserial NOT NULL,
  persona_id bigint NOT NULL,
  trait_id bigint NOT NULL
);

CREATE TABLE IF NOT EXISTS public.character_arcs (
  created_at timestamptz NOT NULL DEFAULT now(),
  description text NOT NULL DEFAULT '',
  id bigserial NOT NULL,
  name text NOT NULL,
  summary text NOT NULL DEFAULT '',
  updated_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS public.arc_stages (
  arc_id bigint NOT NULL,
  condition text NOT NULL DEFAULT '',
  created_at timestamptz NOT NULL DEFAULT now(),
  description text NOT NULL DEFAULT '',
  id bigserial NOT NULL,
  name text NOT NULL,
  stage_order integer NOT NULL DEFAULT 0
);

CREATE TABLE IF NOT EXISTS public.arc_stage_parts (
  id bigserial NOT NULL,
  part_id bigint NOT NULL,
  stage_id bigint NOT NULL
);

CREATE TABLE IF NOT EXISTS public.persona_arc (
  arc_id bigint NOT NULL,
  current_stage_id bigint NOT NULL,
  id bigserial NOT NULL,
  persona_id bigint NOT NULL,
  updated_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS public.thoughts (
  archived_at timestamptz,
  content text NOT NULL,
  created_at timestamptz DEFAULT now(),
  guid uuid NOT NULL DEFAULT gen_random_uuid(),
  id bigserial NOT NULL,
  metadata jsonb DEFAULT '{}'::jsonb,
  project_id bigint,
  tenant_key text,
  updated_at timestamptz DEFAULT now()
);

CREATE TABLE IF NOT EXISTS public.projects (
  created_at timestamptz DEFAULT now(),
  description text,
  guid uuid NOT NULL DEFAULT gen_random_uuid(),
  id bigserial NOT NULL,
  last_active_at timestamptz DEFAULT now(),
  name text NOT NULL,
  tenant_key text
);

CREATE TABLE IF NOT EXISTS public.thought_links (
  created_at timestamptz DEFAULT now(),
  from_id bigint NOT NULL,
  id bigserial NOT NULL,
  relation text NOT NULL,
  to_id bigint NOT NULL
);

CREATE TABLE IF NOT EXISTS public.thought_learning_links (
  created_at timestamptz NOT NULL DEFAULT now(),
  id bigserial NOT NULL,
  learning_id bigint NOT NULL,
  relation text NOT NULL DEFAULT 'source',
  thought_id bigint NOT NULL
);

CREATE TABLE IF NOT EXISTS public.embeddings (
  created_at timestamptz DEFAULT now(),
  dim integer NOT NULL,
  embedding vector NOT NULL,
  guid uuid NOT NULL DEFAULT gen_random_uuid(),
  id bigserial NOT NULL,
  model text NOT NULL,
  thought_id bigint NOT NULL,
  updated_at timestamptz DEFAULT now()
);

CREATE TABLE IF NOT EXISTS public.stored_files (
  content bytea NOT NULL,
  created_at timestamptz NOT NULL DEFAULT now(),
  encoding text NOT NULL DEFAULT 'base64',
  guid uuid NOT NULL DEFAULT gen_random_uuid(),
  id bigserial NOT NULL,
  kind text NOT NULL DEFAULT 'file',
  media_type text NOT NULL,
  name text NOT NULL,
  project_id bigint,
  sha256 text NOT NULL,
  size_bytes bigint NOT NULL,
  tenant_key text,
  thought_id bigint,
  updated_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS public.chat_histories (
  agent_id text,
  channel text,
  created_at timestamptz NOT NULL DEFAULT now(),
  guid uuid NOT NULL DEFAULT gen_random_uuid(),
  id bigserial NOT NULL,
  messages jsonb NOT NULL DEFAULT '[]',
  metadata jsonb NOT NULL DEFAULT '{}',
  project_id bigint,
  session_id text NOT NULL,
  summary text,
  tenant_key text,
  title text,
  updated_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS public.tool_annotations (
  created_at timestamptz NOT NULL DEFAULT now(),
  id bigserial NOT NULL,
  notes text NOT NULL DEFAULT '',
  tool_name text NOT NULL,
  updated_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS public.learnings (
  action_required boolean NOT NULL DEFAULT false,
  area text NOT NULL DEFAULT 'other',
  category text NOT NULL DEFAULT 'insight',
  confidence text NOT NULL DEFAULT 'hypothesis',
  created_at timestamptz NOT NULL DEFAULT now(),
  details text NOT NULL DEFAULT '',
  duplicate_of_learning_id bigint,
  guid uuid NOT NULL DEFAULT gen_random_uuid(),
  id bigserial NOT NULL,
  priority text NOT NULL DEFAULT 'medium',
  project_id bigint,
  related_skill_id bigint,
  related_thought_id bigint,
  reviewed_at timestamptz,
  reviewed_by text,
  source_ref text,
  source_type text,
  status text NOT NULL DEFAULT 'pending',
  summary text NOT NULL,
  supersedes_learning_id bigint,
  tags text[] NOT NULL DEFAULT '{}',
  tenant_key text,
  updated_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS public.oauth_clients (
  client_id text NOT NULL,
  client_name text NOT NULL DEFAULT '',
  created_at timestamptz NOT NULL DEFAULT now(),
  id bigserial NOT NULL,
  redirect_uris text[] NOT NULL DEFAULT '{}'
);

CREATE TABLE IF NOT EXISTS public.plans (
  completed_at timestamptz,
  created_at timestamptz NOT NULL DEFAULT now(),
  description text NOT NULL DEFAULT '',
  due_date timestamptz,
  guid uuid NOT NULL DEFAULT gen_random_uuid(),
  id bigserial NOT NULL,
  last_reviewed_at timestamptz,
  owner text,
  priority text NOT NULL DEFAULT 'medium',
  project_id bigint,
  reviewed_by text,
  status text NOT NULL DEFAULT 'draft',
  supersedes_plan_id bigint,
  tags text[] NOT NULL DEFAULT '{}',
  tenant_key text,
  title text NOT NULL,
  updated_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS public.plan_dependencies (
  created_at timestamptz NOT NULL DEFAULT now(),
  depends_on_plan_id bigint NOT NULL,
  id bigserial NOT NULL,
  plan_id bigint NOT NULL
);

CREATE TABLE IF NOT EXISTS public.plan_related_plans (
  created_at timestamptz NOT NULL DEFAULT now(),
  id bigserial NOT NULL,
  plan_a_id bigint NOT NULL,
  plan_b_id bigint NOT NULL
);

CREATE TABLE IF NOT EXISTS public.plan_skills (
  created_at timestamptz NOT NULL DEFAULT now(),
  id bigserial NOT NULL,
  plan_id bigint NOT NULL,
  skill_id bigint NOT NULL
);

CREATE TABLE IF NOT EXISTS public.plan_guardrails (
  created_at timestamptz NOT NULL DEFAULT now(),
  guardrail_id bigint NOT NULL,
  id bigserial NOT NULL,
  plan_id bigint NOT NULL
);

CREATE TABLE IF NOT EXISTS public.agent_skills (
  content text NOT NULL,
  created_at timestamptz NOT NULL DEFAULT now(),
  description text NOT NULL DEFAULT '',
  domain_tags text[] NOT NULL DEFAULT '{}',
  framework_tags text[] NOT NULL DEFAULT '{}',
  guid uuid NOT NULL DEFAULT gen_random_uuid(),
  id bigserial NOT NULL,
  language_tags text[] NOT NULL DEFAULT '{}',
  library_tags text[] NOT NULL DEFAULT '{}',
  name text NOT NULL,
  tags text[] NOT NULL DEFAULT '{}',
  updated_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS public.agent_guardrails (
  content text NOT NULL,
  created_at timestamptz NOT NULL DEFAULT now(),
  description text NOT NULL DEFAULT '',
  guid uuid NOT NULL DEFAULT gen_random_uuid(),
  id bigserial NOT NULL,
  name text NOT NULL,
  severity text NOT NULL DEFAULT 'medium',
  tags text[] NOT NULL DEFAULT '{}',
  updated_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS public.project_skills (
  created_at timestamptz NOT NULL DEFAULT now(),
  id bigserial NOT NULL,
  override boolean NOT NULL DEFAULT false,
  project_id bigint NOT NULL,
  skill_id bigint NOT NULL
);

CREATE TABLE IF NOT EXISTS public.project_guardrails (
  created_at timestamptz NOT NULL DEFAULT now(),
  guardrail_id bigint NOT NULL,
  id bigserial NOT NULL,
  project_id bigint NOT NULL
);

-- Add missing columns for schema: public
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_personas'
      AND column_name = 'compiled_at'
  ) THEN
    ALTER TABLE public.agent_personas ADD COLUMN compiled_at timestamptz;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_personas'
      AND column_name = 'compiled_detail'
  ) THEN
    ALTER TABLE public.agent_personas ADD COLUMN compiled_detail text NOT NULL DEFAULT '';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_personas'
      AND column_name = 'compiled_summary'
  ) THEN
    ALTER TABLE public.agent_personas ADD COLUMN compiled_summary text NOT NULL DEFAULT '';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_personas'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.agent_personas ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_personas'
      AND column_name = 'description'
  ) THEN
    ALTER TABLE public.agent_personas ADD COLUMN description text NOT NULL DEFAULT '';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_personas'
      AND column_name = 'detail'
  ) THEN
    ALTER TABLE public.agent_personas ADD COLUMN detail text NOT NULL DEFAULT '';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_personas'
      AND column_name = 'guid'
  ) THEN
    ALTER TABLE public.agent_personas ADD COLUMN guid uuid NOT NULL DEFAULT gen_random_uuid();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_personas'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.agent_personas ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_personas'
      AND column_name = 'name'
  ) THEN
    ALTER TABLE public.agent_personas ADD COLUMN name text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_personas'
      AND column_name = 'summary'
  ) THEN
    ALTER TABLE public.agent_personas ADD COLUMN summary text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_personas'
      AND column_name = 'tags'
  ) THEN
    ALTER TABLE public.agent_personas ADD COLUMN tags text[] NOT NULL DEFAULT '{}';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_personas'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.agent_personas ADD COLUMN updated_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_parts'
      AND column_name = 'content'
  ) THEN
    ALTER TABLE public.agent_parts ADD COLUMN content text NOT NULL DEFAULT '';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_parts'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.agent_parts ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_parts'
      AND column_name = 'description'
  ) THEN
    ALTER TABLE public.agent_parts ADD COLUMN description text NOT NULL DEFAULT '';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_parts'
      AND column_name = 'guid'
  ) THEN
    ALTER TABLE public.agent_parts ADD COLUMN guid uuid NOT NULL DEFAULT gen_random_uuid();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_parts'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.agent_parts ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_parts'
      AND column_name = 'name'
  ) THEN
    ALTER TABLE public.agent_parts ADD COLUMN name text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_parts'
      AND column_name = 'part_type'
  ) THEN
    ALTER TABLE public.agent_parts ADD COLUMN part_type text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_parts'
      AND column_name = 'summary'
  ) THEN
    ALTER TABLE public.agent_parts ADD COLUMN summary text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_parts'
      AND column_name = 'tags'
  ) THEN
    ALTER TABLE public.agent_parts ADD COLUMN tags text[] NOT NULL DEFAULT '{}';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_parts'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.agent_parts ADD COLUMN updated_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_parts'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.agent_persona_parts ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_parts'
      AND column_name = 'part_id'
  ) THEN
    ALTER TABLE public.agent_persona_parts ADD COLUMN part_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_parts'
      AND column_name = 'part_order'
  ) THEN
    ALTER TABLE public.agent_persona_parts ADD COLUMN part_order integer NOT NULL DEFAULT 0;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_parts'
      AND column_name = 'persona_id'
  ) THEN
    ALTER TABLE public.agent_persona_parts ADD COLUMN persona_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_parts'
      AND column_name = 'priority'
  ) THEN
    ALTER TABLE public.agent_persona_parts ADD COLUMN priority integer NOT NULL DEFAULT 0;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_skills'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.agent_persona_skills ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_skills'
      AND column_name = 'override'
  ) THEN
    ALTER TABLE public.agent_persona_skills ADD COLUMN override boolean NOT NULL DEFAULT false;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_skills'
      AND column_name = 'persona_id'
  ) THEN
    ALTER TABLE public.agent_persona_skills ADD COLUMN persona_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_skills'
      AND column_name = 'skill_id'
  ) THEN
    ALTER TABLE public.agent_persona_skills ADD COLUMN skill_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'project_personas'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.project_personas ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'project_personas'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.project_personas ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'project_personas'
      AND column_name = 'is_default'
  ) THEN
    ALTER TABLE public.project_personas ADD COLUMN is_default boolean NOT NULL DEFAULT false;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'project_personas'
      AND column_name = 'persona_id'
  ) THEN
    ALTER TABLE public.project_personas ADD COLUMN persona_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'project_personas'
      AND column_name = 'project_id'
  ) THEN
    ALTER TABLE public.project_personas ADD COLUMN project_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_guardrails'
      AND column_name = 'guardrail_id'
  ) THEN
    ALTER TABLE public.agent_persona_guardrails ADD COLUMN guardrail_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_guardrails'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.agent_persona_guardrails ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_guardrails'
      AND column_name = 'persona_id'
  ) THEN
    ALTER TABLE public.agent_persona_guardrails ADD COLUMN persona_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_traits'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.agent_traits ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_traits'
      AND column_name = 'description'
  ) THEN
    ALTER TABLE public.agent_traits ADD COLUMN description text NOT NULL DEFAULT '';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_traits'
      AND column_name = 'guid'
  ) THEN
    ALTER TABLE public.agent_traits ADD COLUMN guid uuid NOT NULL DEFAULT gen_random_uuid();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_traits'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.agent_traits ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_traits'
      AND column_name = 'instruction'
  ) THEN
    ALTER TABLE public.agent_traits ADD COLUMN instruction text NOT NULL DEFAULT '';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_traits'
      AND column_name = 'name'
  ) THEN
    ALTER TABLE public.agent_traits ADD COLUMN name text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_traits'
      AND column_name = 'tags'
  ) THEN
    ALTER TABLE public.agent_traits ADD COLUMN tags text[] NOT NULL DEFAULT '{}';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_traits'
      AND column_name = 'trait_type'
  ) THEN
    ALTER TABLE public.agent_traits ADD COLUMN trait_type text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_traits'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.agent_traits ADD COLUMN updated_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_traits'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.agent_persona_traits ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_traits'
      AND column_name = 'persona_id'
  ) THEN
    ALTER TABLE public.agent_persona_traits ADD COLUMN persona_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_traits'
      AND column_name = 'trait_id'
  ) THEN
    ALTER TABLE public.agent_persona_traits ADD COLUMN trait_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'character_arcs'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.character_arcs ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'character_arcs'
      AND column_name = 'description'
  ) THEN
    ALTER TABLE public.character_arcs ADD COLUMN description text NOT NULL DEFAULT '';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'character_arcs'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.character_arcs ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'character_arcs'
      AND column_name = 'name'
  ) THEN
    ALTER TABLE public.character_arcs ADD COLUMN name text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'character_arcs'
      AND column_name = 'summary'
  ) THEN
    ALTER TABLE public.character_arcs ADD COLUMN summary text NOT NULL DEFAULT '';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'character_arcs'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.character_arcs ADD COLUMN updated_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'arc_stages'
      AND column_name = 'arc_id'
  ) THEN
    ALTER TABLE public.arc_stages ADD COLUMN arc_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'arc_stages'
      AND column_name = 'condition'
  ) THEN
    ALTER TABLE public.arc_stages ADD COLUMN condition text NOT NULL DEFAULT '';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'arc_stages'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.arc_stages ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'arc_stages'
      AND column_name = 'description'
  ) THEN
    ALTER TABLE public.arc_stages ADD COLUMN description text NOT NULL DEFAULT '';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'arc_stages'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.arc_stages ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'arc_stages'
      AND column_name = 'name'
  ) THEN
    ALTER TABLE public.arc_stages ADD COLUMN name text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'arc_stages'
      AND column_name = 'stage_order'
  ) THEN
    ALTER TABLE public.arc_stages ADD COLUMN stage_order integer NOT NULL DEFAULT 0;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'arc_stage_parts'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.arc_stage_parts ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'arc_stage_parts'
      AND column_name = 'part_id'
  ) THEN
    ALTER TABLE public.arc_stage_parts ADD COLUMN part_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'arc_stage_parts'
      AND column_name = 'stage_id'
  ) THEN
    ALTER TABLE public.arc_stage_parts ADD COLUMN stage_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'persona_arc'
      AND column_name = 'arc_id'
  ) THEN
    ALTER TABLE public.persona_arc ADD COLUMN arc_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'persona_arc'
      AND column_name = 'current_stage_id'
  ) THEN
    ALTER TABLE public.persona_arc ADD COLUMN current_stage_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'persona_arc'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.persona_arc ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'persona_arc'
      AND column_name = 'persona_id'
  ) THEN
    ALTER TABLE public.persona_arc ADD COLUMN persona_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'persona_arc'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.persona_arc ADD COLUMN updated_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'thoughts'
      AND column_name = 'archived_at'
  ) THEN
    ALTER TABLE public.thoughts ADD COLUMN archived_at timestamptz;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'thoughts'
      AND column_name = 'content'
  ) THEN
    ALTER TABLE public.thoughts ADD COLUMN content text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'thoughts'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.thoughts ADD COLUMN created_at timestamptz DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'thoughts'
      AND column_name = 'guid'
  ) THEN
    ALTER TABLE public.thoughts ADD COLUMN guid uuid NOT NULL DEFAULT gen_random_uuid();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'thoughts'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.thoughts ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'thoughts'
      AND column_name = 'metadata'
  ) THEN
    ALTER TABLE public.thoughts ADD COLUMN metadata jsonb DEFAULT '{}'::jsonb;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'thoughts'
      AND column_name = 'project_id'
  ) THEN
    ALTER TABLE public.thoughts ADD COLUMN project_id bigint;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'thoughts'
      AND column_name = 'tenant_key'
  ) THEN
    ALTER TABLE public.thoughts ADD COLUMN tenant_key text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'thoughts'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.thoughts ADD COLUMN updated_at timestamptz DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'projects'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.projects ADD COLUMN created_at timestamptz DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'projects'
      AND column_name = 'description'
  ) THEN
    ALTER TABLE public.projects ADD COLUMN description text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'projects'
      AND column_name = 'guid'
  ) THEN
    ALTER TABLE public.projects ADD COLUMN guid uuid NOT NULL DEFAULT gen_random_uuid();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'projects'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.projects ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'projects'
      AND column_name = 'last_active_at'
  ) THEN
    ALTER TABLE public.projects ADD COLUMN last_active_at timestamptz DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'projects'
      AND column_name = 'name'
  ) THEN
    ALTER TABLE public.projects ADD COLUMN name text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'projects'
      AND column_name = 'tenant_key'
  ) THEN
    ALTER TABLE public.projects ADD COLUMN tenant_key text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'thought_links'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.thought_links ADD COLUMN created_at timestamptz DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'thought_links'
      AND column_name = 'from_id'
  ) THEN
    ALTER TABLE public.thought_links ADD COLUMN from_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'thought_links'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.thought_links ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'thought_links'
      AND column_name = 'relation'
  ) THEN
    ALTER TABLE public.thought_links ADD COLUMN relation text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'thought_links'
      AND column_name = 'to_id'
  ) THEN
    ALTER TABLE public.thought_links ADD COLUMN to_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'thought_learning_links'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.thought_learning_links ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'thought_learning_links'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.thought_learning_links ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'thought_learning_links'
      AND column_name = 'learning_id'
  ) THEN
    ALTER TABLE public.thought_learning_links ADD COLUMN learning_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'thought_learning_links'
      AND column_name = 'relation'
  ) THEN
    ALTER TABLE public.thought_learning_links ADD COLUMN relation text NOT NULL DEFAULT 'source';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'thought_learning_links'
      AND column_name = 'thought_id'
  ) THEN
    ALTER TABLE public.thought_learning_links ADD COLUMN thought_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'embeddings'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.embeddings ADD COLUMN created_at timestamptz DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'embeddings'
      AND column_name = 'dim'
  ) THEN
    ALTER TABLE public.embeddings ADD COLUMN dim integer NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'embeddings'
      AND column_name = 'embedding'
  ) THEN
    ALTER TABLE public.embeddings ADD COLUMN embedding vector NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'embeddings'
      AND column_name = 'guid'
  ) THEN
    ALTER TABLE public.embeddings ADD COLUMN guid uuid NOT NULL DEFAULT gen_random_uuid();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'embeddings'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.embeddings ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'embeddings'
      AND column_name = 'model'
  ) THEN
    ALTER TABLE public.embeddings ADD COLUMN model text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'embeddings'
      AND column_name = 'thought_id'
  ) THEN
    ALTER TABLE public.embeddings ADD COLUMN thought_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'embeddings'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.embeddings ADD COLUMN updated_at timestamptz DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'stored_files'
      AND column_name = 'content'
  ) THEN
    ALTER TABLE public.stored_files ADD COLUMN content bytea NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'stored_files'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.stored_files ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'stored_files'
      AND column_name = 'encoding'
  ) THEN
    ALTER TABLE public.stored_files ADD COLUMN encoding text NOT NULL DEFAULT 'base64';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'stored_files'
      AND column_name = 'guid'
  ) THEN
    ALTER TABLE public.stored_files ADD COLUMN guid uuid NOT NULL DEFAULT gen_random_uuid();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'stored_files'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.stored_files ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'stored_files'
      AND column_name = 'kind'
  ) THEN
    ALTER TABLE public.stored_files ADD COLUMN kind text NOT NULL DEFAULT 'file';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'stored_files'
      AND column_name = 'media_type'
  ) THEN
    ALTER TABLE public.stored_files ADD COLUMN media_type text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'stored_files'
      AND column_name = 'name'
  ) THEN
    ALTER TABLE public.stored_files ADD COLUMN name text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'stored_files'
      AND column_name = 'project_id'
  ) THEN
    ALTER TABLE public.stored_files ADD COLUMN project_id bigint;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'stored_files'
      AND column_name = 'sha256'
  ) THEN
    ALTER TABLE public.stored_files ADD COLUMN sha256 text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'stored_files'
      AND column_name = 'size_bytes'
  ) THEN
    ALTER TABLE public.stored_files ADD COLUMN size_bytes bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'stored_files'
      AND column_name = 'tenant_key'
  ) THEN
    ALTER TABLE public.stored_files ADD COLUMN tenant_key text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'stored_files'
      AND column_name = 'thought_id'
  ) THEN
    ALTER TABLE public.stored_files ADD COLUMN thought_id bigint;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'stored_files'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.stored_files ADD COLUMN updated_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'chat_histories'
      AND column_name = 'agent_id'
  ) THEN
    ALTER TABLE public.chat_histories ADD COLUMN agent_id text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'chat_histories'
      AND column_name = 'channel'
  ) THEN
    ALTER TABLE public.chat_histories ADD COLUMN channel text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'chat_histories'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.chat_histories ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF 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 ADD COLUMN guid uuid NOT NULL DEFAULT gen_random_uuid();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'chat_histories'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.chat_histories ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'chat_histories'
      AND column_name = 'messages'
  ) THEN
    ALTER TABLE public.chat_histories ADD COLUMN messages jsonb NOT NULL DEFAULT '[]';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'chat_histories'
      AND column_name = 'metadata'
  ) THEN
    ALTER TABLE public.chat_histories ADD COLUMN metadata jsonb NOT NULL DEFAULT '{}';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'chat_histories'
      AND column_name = 'project_id'
  ) THEN
    ALTER TABLE public.chat_histories ADD COLUMN project_id bigint;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'chat_histories'
      AND column_name = 'session_id'
  ) THEN
    ALTER TABLE public.chat_histories ADD COLUMN session_id text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'chat_histories'
      AND column_name = 'summary'
  ) THEN
    ALTER TABLE public.chat_histories ADD COLUMN summary text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'chat_histories'
      AND column_name = 'tenant_key'
  ) THEN
    ALTER TABLE public.chat_histories ADD COLUMN tenant_key text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'chat_histories'
      AND column_name = 'title'
  ) THEN
    ALTER TABLE public.chat_histories ADD COLUMN title text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'chat_histories'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.chat_histories ADD COLUMN updated_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'tool_annotations'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.tool_annotations ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'tool_annotations'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.tool_annotations ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'tool_annotations'
      AND column_name = 'notes'
  ) THEN
    ALTER TABLE public.tool_annotations ADD COLUMN notes text NOT NULL DEFAULT '';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'tool_annotations'
      AND column_name = 'tool_name'
  ) THEN
    ALTER TABLE public.tool_annotations ADD COLUMN tool_name text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'tool_annotations'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.tool_annotations ADD COLUMN updated_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'action_required'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN action_required boolean NOT NULL DEFAULT false;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'area'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN area text NOT NULL DEFAULT 'other';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'category'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN category text NOT NULL DEFAULT 'insight';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'confidence'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN confidence text NOT NULL DEFAULT 'hypothesis';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'details'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN details text NOT NULL DEFAULT '';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'duplicate_of_learning_id'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN duplicate_of_learning_id bigint;
  END IF;
END;
$$;

DO $$
BEGIN
  IF 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 ADD COLUMN guid uuid NOT NULL DEFAULT gen_random_uuid();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'priority'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN priority text NOT NULL DEFAULT 'medium';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'project_id'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN project_id bigint;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'related_skill_id'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN related_skill_id bigint;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'related_thought_id'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN related_thought_id bigint;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'reviewed_at'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN reviewed_at timestamptz;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'reviewed_by'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN reviewed_by text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'source_ref'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN source_ref text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'source_type'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN source_type text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'status'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN status text NOT NULL DEFAULT 'pending';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'summary'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN summary text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'supersedes_learning_id'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN supersedes_learning_id bigint;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'tags'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN tags text[] NOT NULL DEFAULT '{}';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'tenant_key'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN tenant_key text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.learnings ADD COLUMN updated_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'oauth_clients'
      AND column_name = 'client_id'
  ) THEN
    ALTER TABLE public.oauth_clients ADD COLUMN client_id text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'oauth_clients'
      AND column_name = 'client_name'
  ) THEN
    ALTER TABLE public.oauth_clients ADD COLUMN client_name text NOT NULL DEFAULT '';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'oauth_clients'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.oauth_clients ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'oauth_clients'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.oauth_clients ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'oauth_clients'
      AND column_name = 'redirect_uris'
  ) THEN
    ALTER TABLE public.oauth_clients ADD COLUMN redirect_uris text[] NOT NULL DEFAULT '{}';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plans'
      AND column_name = 'completed_at'
  ) THEN
    ALTER TABLE public.plans ADD COLUMN completed_at timestamptz;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plans'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.plans ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plans'
      AND column_name = 'description'
  ) THEN
    ALTER TABLE public.plans ADD COLUMN description text NOT NULL DEFAULT '';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plans'
      AND column_name = 'due_date'
  ) THEN
    ALTER TABLE public.plans ADD COLUMN due_date timestamptz;
  END IF;
END;
$$;

DO $$
BEGIN
  IF 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 ADD COLUMN guid uuid NOT NULL DEFAULT gen_random_uuid();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plans'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.plans ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plans'
      AND column_name = 'last_reviewed_at'
  ) THEN
    ALTER TABLE public.plans ADD COLUMN last_reviewed_at timestamptz;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plans'
      AND column_name = 'owner'
  ) THEN
    ALTER TABLE public.plans ADD COLUMN owner text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plans'
      AND column_name = 'priority'
  ) THEN
    ALTER TABLE public.plans ADD COLUMN priority text NOT NULL DEFAULT 'medium';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plans'
      AND column_name = 'project_id'
  ) THEN
    ALTER TABLE public.plans ADD COLUMN project_id bigint;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plans'
      AND column_name = 'reviewed_by'
  ) THEN
    ALTER TABLE public.plans ADD COLUMN reviewed_by text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plans'
      AND column_name = 'status'
  ) THEN
    ALTER TABLE public.plans ADD COLUMN status text NOT NULL DEFAULT 'draft';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plans'
      AND column_name = 'supersedes_plan_id'
  ) THEN
    ALTER TABLE public.plans ADD COLUMN supersedes_plan_id bigint;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plans'
      AND column_name = 'tags'
  ) THEN
    ALTER TABLE public.plans ADD COLUMN tags text[] NOT NULL DEFAULT '{}';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plans'
      AND column_name = 'tenant_key'
  ) THEN
    ALTER TABLE public.plans ADD COLUMN tenant_key text;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plans'
      AND column_name = 'title'
  ) THEN
    ALTER TABLE public.plans ADD COLUMN title text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plans'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.plans ADD COLUMN updated_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plan_dependencies'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.plan_dependencies ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plan_dependencies'
      AND column_name = 'depends_on_plan_id'
  ) THEN
    ALTER TABLE public.plan_dependencies ADD COLUMN depends_on_plan_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plan_dependencies'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.plan_dependencies ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plan_dependencies'
      AND column_name = 'plan_id'
  ) THEN
    ALTER TABLE public.plan_dependencies ADD COLUMN plan_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plan_related_plans'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.plan_related_plans ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plan_related_plans'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.plan_related_plans ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plan_related_plans'
      AND column_name = 'plan_a_id'
  ) THEN
    ALTER TABLE public.plan_related_plans ADD COLUMN plan_a_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plan_related_plans'
      AND column_name = 'plan_b_id'
  ) THEN
    ALTER TABLE public.plan_related_plans ADD COLUMN plan_b_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plan_skills'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.plan_skills ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plan_skills'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.plan_skills ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plan_skills'
      AND column_name = 'plan_id'
  ) THEN
    ALTER TABLE public.plan_skills ADD COLUMN plan_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plan_skills'
      AND column_name = 'skill_id'
  ) THEN
    ALTER TABLE public.plan_skills ADD COLUMN skill_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plan_guardrails'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.plan_guardrails ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plan_guardrails'
      AND column_name = 'guardrail_id'
  ) THEN
    ALTER TABLE public.plan_guardrails ADD COLUMN guardrail_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plan_guardrails'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.plan_guardrails ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'plan_guardrails'
      AND column_name = 'plan_id'
  ) THEN
    ALTER TABLE public.plan_guardrails ADD COLUMN plan_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_skills'
      AND column_name = 'content'
  ) THEN
    ALTER TABLE public.agent_skills ADD COLUMN content text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_skills'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.agent_skills ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_skills'
      AND column_name = 'description'
  ) THEN
    ALTER TABLE public.agent_skills ADD COLUMN description text NOT NULL DEFAULT '';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_skills'
      AND column_name = 'domain_tags'
  ) THEN
    ALTER TABLE public.agent_skills ADD COLUMN domain_tags text[] NOT NULL DEFAULT '{}';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_skills'
      AND column_name = 'framework_tags'
  ) THEN
    ALTER TABLE public.agent_skills ADD COLUMN framework_tags text[] NOT NULL DEFAULT '{}';
  END IF;
END;
$$;

DO $$
BEGIN
  IF 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 ADD COLUMN guid uuid NOT NULL DEFAULT gen_random_uuid();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_skills'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.agent_skills ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_skills'
      AND column_name = 'language_tags'
  ) THEN
    ALTER TABLE public.agent_skills ADD COLUMN language_tags text[] NOT NULL DEFAULT '{}';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_skills'
      AND column_name = 'library_tags'
  ) THEN
    ALTER TABLE public.agent_skills ADD COLUMN library_tags text[] NOT NULL DEFAULT '{}';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_skills'
      AND column_name = 'name'
  ) THEN
    ALTER TABLE public.agent_skills ADD COLUMN name text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_skills'
      AND column_name = 'tags'
  ) THEN
    ALTER TABLE public.agent_skills ADD COLUMN tags text[] NOT NULL DEFAULT '{}';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_skills'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.agent_skills ADD COLUMN updated_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_guardrails'
      AND column_name = 'content'
  ) THEN
    ALTER TABLE public.agent_guardrails ADD COLUMN content text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_guardrails'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.agent_guardrails ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_guardrails'
      AND column_name = 'description'
  ) THEN
    ALTER TABLE public.agent_guardrails ADD COLUMN description text NOT NULL DEFAULT '';
  END IF;
END;
$$;

DO $$
BEGIN
  IF 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 ADD COLUMN guid uuid NOT NULL DEFAULT gen_random_uuid();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_guardrails'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.agent_guardrails ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_guardrails'
      AND column_name = 'name'
  ) THEN
    ALTER TABLE public.agent_guardrails ADD COLUMN name text NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_guardrails'
      AND column_name = 'severity'
  ) THEN
    ALTER TABLE public.agent_guardrails ADD COLUMN severity text NOT NULL DEFAULT 'medium';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_guardrails'
      AND column_name = 'tags'
  ) THEN
    ALTER TABLE public.agent_guardrails ADD COLUMN tags text[] NOT NULL DEFAULT '{}';
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'agent_guardrails'
      AND column_name = 'updated_at'
  ) THEN
    ALTER TABLE public.agent_guardrails ADD COLUMN updated_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'project_skills'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.project_skills ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'project_skills'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.project_skills ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'project_skills'
      AND column_name = 'override'
  ) THEN
    ALTER TABLE public.project_skills ADD COLUMN override boolean NOT NULL DEFAULT false;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'project_skills'
      AND column_name = 'project_id'
  ) THEN
    ALTER TABLE public.project_skills ADD COLUMN project_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'project_skills'
      AND column_name = 'skill_id'
  ) THEN
    ALTER TABLE public.project_skills ADD COLUMN skill_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'project_guardrails'
      AND column_name = 'created_at'
  ) THEN
    ALTER TABLE public.project_guardrails ADD COLUMN created_at timestamptz NOT NULL DEFAULT now();
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'project_guardrails'
      AND column_name = 'guardrail_id'
  ) THEN
    ALTER TABLE public.project_guardrails ADD COLUMN guardrail_id bigint NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'project_guardrails'
      AND column_name = 'id'
  ) THEN
    ALTER TABLE public.project_guardrails ADD COLUMN id bigserial NOT NULL;
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = 'public'
      AND table_name = 'project_guardrails'
      AND column_name = 'project_id'
  ) THEN
    ALTER TABLE public.project_guardrails ADD COLUMN project_id bigint NOT NULL;
  END IF;
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 = 'override'
    AND a.attnum > 0
    AND NOT a.attisdropped;

  IF current_type IS NOT NULL
     AND current_type <> ALL(ARRAY['boolean']) THEN
    ALTER TABLE public.agent_persona_skills
      ALTER COLUMN override TYPE boolean USING override::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 = '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 = 'project_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.project_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 = 'project_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.project_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 = 'project_personas'
    AND a.attname = 'is_default'
    AND a.attnum > 0
    AND NOT a.attisdropped;

  IF current_type IS NOT NULL
     AND current_type <> ALL(ARRAY['boolean']) THEN
    ALTER TABLE public.project_personas
      ALTER COLUMN is_default TYPE boolean USING is_default::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 = 'project_personas'
    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.project_personas
      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 = 'project_personas'
    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_personas
      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 = '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 = 'tenant_key'
    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 tenant_key TYPE text USING tenant_key::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 = '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 = 'projects'
    AND a.attname = 'tenant_key'
    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 tenant_key TYPE text USING tenant_key::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 = 'thought_learning_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_learning_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_learning_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_learning_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_learning_links'
    AND a.attname = '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.thought_learning_links
      ALTER COLUMN learning_id TYPE bigint USING 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 = 'thought_learning_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_learning_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_learning_links'
    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.thought_learning_links
      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 = '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 = 'tenant_key'
    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 tenant_key TYPE text USING tenant_key::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 = '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 = 'tenant_key'
    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 tenant_key TYPE text USING tenant_key::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 = 'tenant_key'
    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 tenant_key TYPE text USING tenant_key::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 = 'tenant_key'
    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 tenant_key TYPE text USING tenant_key::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 = 'override'
    AND a.attnum > 0
    AND NOT a.attisdropped;

  IF current_type IS NOT NULL
     AND current_type <> ALL(ARRAY['boolean']) THEN
    ALTER TABLE public.project_skills
      ALTER COLUMN override TYPE boolean USING override::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 = '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
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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_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 = 'project_personas'
    AND tc.constraint_type = 'PRIMARY KEY';

  IF current_pk_name IS NOT NULL
     AND NOT current_pk_matches
     AND current_pk_name IN ('project_personas_pkey', 'public_project_personas_pkey') THEN
    EXECUTE 'ALTER TABLE public.project_personas DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE';
  END IF;

  -- 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_personas_pkey', 'public_project_personas_pkey')) THEN
    ALTER TABLE public.project_personas ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id, persona_id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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_learning_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_learning_links'
    AND tc.constraint_type = 'PRIMARY KEY';

  IF current_pk_name IS NOT NULL
     AND NOT current_pk_matches
     AND current_pk_name IN ('thought_learning_links_pkey', 'public_thought_learning_links_pkey') THEN
    EXECUTE 'ALTER TABLE public.thought_learning_links DROP CONSTRAINT ' || quote_ident(current_pk_name) || ' CASCADE';
  END IF;

  -- 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_learning_links_pkey', 'public_thought_learning_links_pkey')) THEN
    ALTER TABLE public.thought_learning_links ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  current_pk_name text;
  current_pk_matches boolean := false;
BEGIN
  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 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 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 [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

-- Indexes for schema: public
CREATE INDEX IF NOT EXISTS idx_agent_persona_parts_persona_id_part_id
  ON public.agent_persona_parts USING btree (persona_id, part_id);

CREATE INDEX IF NOT EXISTS idx_agent_persona_skills_persona_id_skill_id
  ON public.agent_persona_skills USING btree (persona_id, skill_id);

CREATE INDEX IF NOT EXISTS idx_project_personas_project_id_persona_id
  ON public.project_personas USING btree (project_id, persona_id);

CREATE INDEX IF NOT EXISTS idx_arc_stage_parts_stage_id_part_id
  ON public.arc_stage_parts USING btree (stage_id, part_id);

CREATE INDEX IF NOT EXISTS idx_thoughts_tenant_key_project_id
  ON public.thoughts USING btree (tenant_key, project_id);

CREATE UNIQUE INDEX IF NOT EXISTS uidx_projects_tenant_key_name
  ON public.projects USING btree (tenant_key, name);

CREATE INDEX IF NOT EXISTS idx_thought_links_from_id_to_id_relation
  ON public.thought_links USING btree (from_id, to_id, relation);

CREATE UNIQUE INDEX IF NOT EXISTS uidx_thought_learning_links_thought_id_learning_id
  ON public.thought_learning_links USING btree (thought_id, learning_id);

CREATE UNIQUE INDEX IF NOT EXISTS uidx_embeddings_thought_id_model
  ON public.embeddings USING btree (thought_id, model);

CREATE INDEX IF NOT EXISTS idx_learnings_details
  ON public.learnings USING gin (details gin_trgm_ops);

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 array_ops);

CREATE INDEX IF NOT EXISTS idx_plans_tags
  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);

CREATE UNIQUE INDEX IF NOT EXISTS uidx_plan_dependencies_plan_id_depends_on_plan_id
  ON public.plan_dependencies USING btree (plan_id, depends_on_plan_id);

CREATE UNIQUE INDEX IF NOT EXISTS uidx_plan_related_plans_plan_a_id_plan_b_id
  ON public.plan_related_plans USING btree (plan_a_id, plan_b_id);

CREATE UNIQUE INDEX IF NOT EXISTS uidx_plan_skills_plan_id_skill_id
  ON public.plan_skills USING btree (plan_id, skill_id);

CREATE UNIQUE INDEX IF NOT EXISTS uidx_plan_guardrails_plan_id_guardrail_id
  ON public.plan_guardrails USING btree (plan_id, guardrail_id);

CREATE INDEX IF NOT EXISTS idx_project_skills_project_id_skill_id
  ON public.project_skills USING btree (project_id, skill_id);

CREATE INDEX IF NOT EXISTS idx_project_guardrails_project_id_guardrail_id
  ON public.project_guardrails USING btree (project_id, guardrail_id);

-- Unique constraints for schema: public
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'agent_personas'
      AND constraint_name = 'ukey_agent_personas_guid'
  ) THEN
    ALTER TABLE public.agent_personas ADD CONSTRAINT ukey_agent_personas_guid 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_personas'
      AND constraint_name = 'ukey_agent_personas_name'
  ) THEN
    ALTER TABLE public.agent_personas ADD CONSTRAINT ukey_agent_personas_name UNIQUE (name);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'agent_parts'
      AND constraint_name = 'ukey_agent_parts_guid'
  ) THEN
    ALTER TABLE public.agent_parts ADD CONSTRAINT ukey_agent_parts_guid 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_parts'
      AND constraint_name = 'ukey_agent_parts_name'
  ) THEN
    ALTER TABLE public.agent_parts ADD CONSTRAINT ukey_agent_parts_name UNIQUE (name);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'agent_traits'
      AND constraint_name = 'ukey_agent_traits_guid'
  ) THEN
    ALTER TABLE public.agent_traits ADD CONSTRAINT ukey_agent_traits_guid 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_traits'
      AND constraint_name = 'ukey_agent_traits_name'
  ) THEN
    ALTER TABLE public.agent_traits ADD CONSTRAINT ukey_agent_traits_name UNIQUE (name);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'character_arcs'
      AND constraint_name = 'ukey_character_arcs_name'
  ) THEN
    ALTER TABLE public.character_arcs ADD CONSTRAINT ukey_character_arcs_name UNIQUE (name);
  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 = 'ukey_thoughts_guid'
  ) THEN
    ALTER TABLE public.thoughts ADD CONSTRAINT ukey_thoughts_guid UNIQUE (guid);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'projects'
      AND constraint_name = 'ukey_projects_guid'
  ) THEN
    ALTER TABLE public.projects ADD CONSTRAINT ukey_projects_guid UNIQUE (guid);
  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 = 'ukey_embeddings_guid'
  ) THEN
    ALTER TABLE public.embeddings ADD CONSTRAINT ukey_embeddings_guid UNIQUE (guid);
  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 = 'ukey_stored_files_guid'
  ) THEN
    ALTER TABLE public.stored_files ADD CONSTRAINT ukey_stored_files_guid 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 = 'ukey_chat_histories_guid'
  ) THEN
    ALTER TABLE public.chat_histories ADD CONSTRAINT ukey_chat_histories_guid UNIQUE (guid);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'tool_annotations'
      AND constraint_name = 'ukey_tool_annotations_tool_name'
  ) THEN
    ALTER TABLE public.tool_annotations ADD CONSTRAINT ukey_tool_annotations_tool_name UNIQUE (tool_name);
  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 = 'ukey_learnings_guid'
  ) THEN
    ALTER TABLE public.learnings ADD CONSTRAINT ukey_learnings_guid UNIQUE (guid);
  END IF;
END;
$$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'oauth_clients'
      AND constraint_name = 'ukey_oauth_clients_client_id'
  ) THEN
    ALTER TABLE public.oauth_clients ADD CONSTRAINT ukey_oauth_clients_client_id UNIQUE (client_id);
  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 = 'ukey_plans_guid'
  ) THEN
    ALTER TABLE public.plans ADD CONSTRAINT ukey_plans_guid 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_skills'
      AND constraint_name = 'ukey_agent_skills_guid'
  ) THEN
    ALTER TABLE public.agent_skills ADD CONSTRAINT ukey_agent_skills_guid 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_skills'
      AND constraint_name = 'ukey_agent_skills_name'
  ) THEN
    ALTER TABLE public.agent_skills ADD CONSTRAINT ukey_agent_skills_name UNIQUE (name);
  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 = 'ukey_agent_guardrails_guid'
  ) THEN
    ALTER TABLE public.agent_guardrails ADD CONSTRAINT ukey_agent_guardrails_guid 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 = 'ukey_agent_guardrails_name'
  ) THEN
    ALTER TABLE public.agent_guardrails ADD CONSTRAINT ukey_agent_guardrails_name UNIQUE (name);
  END IF;
END;
$$;

-- Check constraints for schema: public
-- Foreign keys for schema: public
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_parts'
      AND constraint_name = 'fk_agent_persona_parts_part_id'
  ) THEN
    ALTER TABLE public.agent_persona_parts
      ADD CONSTRAINT fk_agent_persona_parts_part_id
      FOREIGN KEY (part_id)
      REFERENCES public.agent_parts (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_parts'
      AND constraint_name = 'fk_agent_persona_parts_persona_id'
  ) THEN
    ALTER TABLE public.agent_persona_parts
      ADD CONSTRAINT fk_agent_persona_parts_persona_id
      FOREIGN KEY (persona_id)
      REFERENCES public.agent_personas (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_skills'
      AND constraint_name = 'fk_agent_persona_skills_persona_id'
  ) THEN
    ALTER TABLE public.agent_persona_skills
      ADD CONSTRAINT fk_agent_persona_skills_persona_id
      FOREIGN KEY (persona_id)
      REFERENCES public.agent_personas (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_skills'
      AND constraint_name = 'fk_agent_persona_skills_skill_id'
  ) THEN
    ALTER TABLE public.agent_persona_skills
      ADD CONSTRAINT fk_agent_persona_skills_skill_id
      FOREIGN KEY (skill_id)
      REFERENCES public.agent_skills (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'project_personas'
      AND constraint_name = 'fk_project_personas_persona_id'
  ) THEN
    ALTER TABLE public.project_personas
      ADD CONSTRAINT fk_project_personas_persona_id
      FOREIGN KEY (persona_id)
      REFERENCES public.agent_personas (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'project_personas'
      AND constraint_name = 'fk_project_personas_project_id'
  ) THEN
    ALTER TABLE public.project_personas
      ADD CONSTRAINT fk_project_personas_project_id
      FOREIGN KEY (project_id)
      REFERENCES public.projects (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_guardrails'
      AND constraint_name = 'fk_agent_persona_guardrails_guardrail_id'
  ) THEN
    ALTER TABLE public.agent_persona_guardrails
      ADD CONSTRAINT fk_agent_persona_guardrails_guardrail_id
      FOREIGN KEY (guardrail_id)
      REFERENCES public.agent_guardrails (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_guardrails'
      AND constraint_name = 'fk_agent_persona_guardrails_persona_id'
  ) THEN
    ALTER TABLE public.agent_persona_guardrails
      ADD CONSTRAINT fk_agent_persona_guardrails_persona_id
      FOREIGN KEY (persona_id)
      REFERENCES public.agent_personas (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_traits'
      AND constraint_name = 'fk_agent_persona_traits_persona_id'
  ) THEN
    ALTER TABLE public.agent_persona_traits
      ADD CONSTRAINT fk_agent_persona_traits_persona_id
      FOREIGN KEY (persona_id)
      REFERENCES public.agent_personas (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'agent_persona_traits'
      AND constraint_name = 'fk_agent_persona_traits_trait_id'
  ) THEN
    ALTER TABLE public.agent_persona_traits
      ADD CONSTRAINT fk_agent_persona_traits_trait_id
      FOREIGN KEY (trait_id)
      REFERENCES public.agent_traits (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'arc_stages'
      AND constraint_name = 'fk_arc_stages_arc_id'
  ) THEN
    ALTER TABLE public.arc_stages
      ADD CONSTRAINT fk_arc_stages_arc_id
      FOREIGN KEY (arc_id)
      REFERENCES public.character_arcs (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'arc_stage_parts'
      AND constraint_name = 'fk_arc_stage_parts_part_id'
  ) THEN
    ALTER TABLE public.arc_stage_parts
      ADD CONSTRAINT fk_arc_stage_parts_part_id
      FOREIGN KEY (part_id)
      REFERENCES public.agent_parts (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'arc_stage_parts'
      AND constraint_name = 'fk_arc_stage_parts_stage_id'
  ) THEN
    ALTER TABLE public.arc_stage_parts
      ADD CONSTRAINT fk_arc_stage_parts_stage_id
      FOREIGN KEY (stage_id)
      REFERENCES public.arc_stages (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'persona_arc'
      AND constraint_name = 'fk_persona_arc_arc_id'
  ) THEN
    ALTER TABLE public.persona_arc
      ADD CONSTRAINT fk_persona_arc_arc_id
      FOREIGN KEY (arc_id)
      REFERENCES public.character_arcs (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'persona_arc'
      AND constraint_name = 'fk_persona_arc_current_stage_id'
  ) THEN
    ALTER TABLE public.persona_arc
      ADD CONSTRAINT fk_persona_arc_current_stage_id
      FOREIGN KEY (current_stage_id)
      REFERENCES public.arc_stages (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'persona_arc'
      AND constraint_name = 'fk_persona_arc_persona_id'
  ) THEN
    ALTER TABLE public.persona_arc
      ADD CONSTRAINT fk_persona_arc_persona_id
      FOREIGN KEY (persona_id)
      REFERENCES public.agent_personas (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_thoughts_project_id'
  ) THEN
    ALTER TABLE public.thoughts
      ADD CONSTRAINT fk_thoughts_project_id
      FOREIGN KEY (project_id)
      REFERENCES public.projects (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'thought_links'
      AND constraint_name = 'fk_thought_links_from_id'
  ) THEN
    ALTER TABLE public.thought_links
      ADD CONSTRAINT fk_thought_links_from_id
      FOREIGN KEY (from_id)
      REFERENCES public.thoughts (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'thought_links'
      AND constraint_name = 'fk_thought_links_to_id'
  ) THEN
    ALTER TABLE public.thought_links
      ADD CONSTRAINT fk_thought_links_to_id
      FOREIGN KEY (to_id)
      REFERENCES public.thoughts (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'thought_learning_links'
      AND constraint_name = 'fk_thought_learning_links_learning_id'
  ) THEN
    ALTER TABLE public.thought_learning_links
      ADD CONSTRAINT fk_thought_learning_links_learning_id
      FOREIGN KEY (learning_id)
      REFERENCES public.learnings (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'thought_learning_links'
      AND constraint_name = 'fk_thought_learning_links_thought_id'
  ) THEN
    ALTER TABLE public.thought_learning_links
      ADD CONSTRAINT fk_thought_learning_links_thought_id
      FOREIGN KEY (thought_id)
      REFERENCES public.thoughts (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_embeddings_thought_id'
  ) THEN
    ALTER TABLE public.embeddings
      ADD CONSTRAINT fk_embeddings_thought_id
      FOREIGN KEY (thought_id)
      REFERENCES public.thoughts (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_stored_files_project_id'
  ) THEN
    ALTER TABLE public.stored_files
      ADD CONSTRAINT fk_stored_files_project_id
      FOREIGN KEY (project_id)
      REFERENCES public.projects (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_stored_files_thought_id'
  ) THEN
    ALTER TABLE public.stored_files
      ADD CONSTRAINT fk_stored_files_thought_id
      FOREIGN KEY (thought_id)
      REFERENCES public.thoughts (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_chat_histories_project_id'
  ) THEN
    ALTER TABLE public.chat_histories
      ADD CONSTRAINT fk_chat_histories_project_id
      FOREIGN KEY (project_id)
      REFERENCES public.projects (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_learnings_duplicate_of_learning_id'
  ) THEN
    ALTER TABLE public.learnings
      ADD CONSTRAINT fk_learnings_duplicate_of_learning_id
      FOREIGN KEY (duplicate_of_learning_id)
      REFERENCES public.learnings (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_learnings_project_id'
  ) THEN
    ALTER TABLE public.learnings
      ADD CONSTRAINT fk_learnings_project_id
      FOREIGN KEY (project_id)
      REFERENCES public.projects (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_learnings_related_skill_id'
  ) THEN
    ALTER TABLE public.learnings
      ADD CONSTRAINT fk_learnings_related_skill_id
      FOREIGN KEY (related_skill_id)
      REFERENCES public.agent_skills (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_learnings_related_thought_id'
  ) THEN
    ALTER TABLE public.learnings
      ADD CONSTRAINT fk_learnings_related_thought_id
      FOREIGN KEY (related_thought_id)
      REFERENCES public.thoughts (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_learnings_supersedes_learning_id'
  ) THEN
    ALTER TABLE public.learnings
      ADD CONSTRAINT fk_learnings_supersedes_learning_id
      FOREIGN KEY (supersedes_learning_id)
      REFERENCES public.learnings (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_plans_project_id'
  ) THEN
    ALTER TABLE public.plans
      ADD CONSTRAINT fk_plans_project_id
      FOREIGN KEY (project_id)
      REFERENCES public.projects (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_plans_supersedes_plan_id'
  ) THEN
    ALTER TABLE public.plans
      ADD CONSTRAINT fk_plans_supersedes_plan_id
      FOREIGN KEY (supersedes_plan_id)
      REFERENCES public.plans (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_plan_dependencies_depends_on_plan_id'
  ) THEN
    ALTER TABLE public.plan_dependencies
      ADD CONSTRAINT fk_plan_dependencies_depends_on_plan_id
      FOREIGN KEY (depends_on_plan_id)
      REFERENCES public.plans (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_plan_dependencies_plan_id'
  ) THEN
    ALTER TABLE public.plan_dependencies
      ADD CONSTRAINT fk_plan_dependencies_plan_id
      FOREIGN KEY (plan_id)
      REFERENCES public.plans (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_plan_related_plans_plan_a_id'
  ) THEN
    ALTER TABLE public.plan_related_plans
      ADD CONSTRAINT fk_plan_related_plans_plan_a_id
      FOREIGN KEY (plan_a_id)
      REFERENCES public.plans (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_plan_related_plans_plan_b_id'
  ) THEN
    ALTER TABLE public.plan_related_plans
      ADD CONSTRAINT fk_plan_related_plans_plan_b_id
      FOREIGN KEY (plan_b_id)
      REFERENCES public.plans (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_plan_skills_plan_id'
  ) THEN
    ALTER TABLE public.plan_skills
      ADD CONSTRAINT fk_plan_skills_plan_id
      FOREIGN KEY (plan_id)
      REFERENCES public.plans (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_plan_skills_skill_id'
  ) THEN
    ALTER TABLE public.plan_skills
      ADD CONSTRAINT fk_plan_skills_skill_id
      FOREIGN KEY (skill_id)
      REFERENCES public.agent_skills (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_plan_guardrails_guardrail_id'
  ) THEN
    ALTER TABLE public.plan_guardrails
      ADD CONSTRAINT fk_plan_guardrails_guardrail_id
      FOREIGN KEY (guardrail_id)
      REFERENCES public.agent_guardrails (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_plan_guardrails_plan_id'
  ) THEN
    ALTER TABLE public.plan_guardrails
      ADD CONSTRAINT fk_plan_guardrails_plan_id
      FOREIGN KEY (plan_id)
      REFERENCES public.plans (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_project_skills_project_id'
  ) THEN
    ALTER TABLE public.project_skills
      ADD CONSTRAINT fk_project_skills_project_id
      FOREIGN KEY (project_id)
      REFERENCES public.projects (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_project_skills_skill_id'
  ) THEN
    ALTER TABLE public.project_skills
      ADD CONSTRAINT fk_project_skills_skill_id
      FOREIGN KEY (skill_id)
      REFERENCES public.agent_skills (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_project_guardrails_guardrail_id'
  ) THEN
    ALTER TABLE public.project_guardrails
      ADD CONSTRAINT fk_project_guardrails_guardrail_id
      FOREIGN KEY (guardrail_id)
      REFERENCES public.agent_guardrails (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  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 = 'fk_project_guardrails_project_id'
  ) THEN
    ALTER TABLE public.project_guardrails
      ADD CONSTRAINT fk_project_guardrails_project_id
      FOREIGN KEY (project_id)
      REFERENCES public.projects (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;
  END IF;
END;
$$;-- Set sequence values for schema: public
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_agent_personas_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.agent_personas
    INTO m_cnt;
    
    PERFORM setval('public.identity_agent_personas_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_agent_parts_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.agent_parts
    INTO m_cnt;
    
    PERFORM setval('public.identity_agent_parts_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_agent_persona_parts_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.agent_persona_parts
    INTO m_cnt;
    
    PERFORM setval('public.identity_agent_persona_parts_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_agent_persona_skills_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.agent_persona_skills
    INTO m_cnt;
    
    PERFORM setval('public.identity_agent_persona_skills_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_project_personas_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.project_personas
    INTO m_cnt;
    
    PERFORM setval('public.identity_project_personas_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_agent_persona_guardrails_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.agent_persona_guardrails
    INTO m_cnt;
    
    PERFORM setval('public.identity_agent_persona_guardrails_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_agent_traits_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.agent_traits
    INTO m_cnt;
    
    PERFORM setval('public.identity_agent_traits_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_agent_persona_traits_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.agent_persona_traits
    INTO m_cnt;
    
    PERFORM setval('public.identity_agent_persona_traits_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_character_arcs_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.character_arcs
    INTO m_cnt;
    
    PERFORM setval('public.identity_character_arcs_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_arc_stages_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.arc_stages
    INTO m_cnt;
    
    PERFORM setval('public.identity_arc_stages_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_arc_stage_parts_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.arc_stage_parts
    INTO m_cnt;
    
    PERFORM setval('public.identity_arc_stage_parts_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_persona_arc_persona_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(persona_id), 0) + 1
    FROM public.persona_arc
    INTO m_cnt;
    
    PERFORM setval('public.identity_persona_arc_persona_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_thoughts_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.thoughts
    INTO m_cnt;
    
    PERFORM setval('public.identity_thoughts_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_projects_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.projects
    INTO m_cnt;
    
    PERFORM setval('public.identity_projects_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_thought_links_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.thought_links
    INTO m_cnt;
    
    PERFORM setval('public.identity_thought_links_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_thought_learning_links_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.thought_learning_links
    INTO m_cnt;
    
    PERFORM setval('public.identity_thought_learning_links_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_embeddings_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.embeddings
    INTO m_cnt;
    
    PERFORM setval('public.identity_embeddings_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_stored_files_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.stored_files
    INTO m_cnt;
    
    PERFORM setval('public.identity_stored_files_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_chat_histories_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.chat_histories
    INTO m_cnt;
    
    PERFORM setval('public.identity_chat_histories_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_tool_annotations_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.tool_annotations
    INTO m_cnt;
    
    PERFORM setval('public.identity_tool_annotations_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_learnings_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.learnings
    INTO m_cnt;
    
    PERFORM setval('public.identity_learnings_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_oauth_clients_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.oauth_clients
    INTO m_cnt;
    
    PERFORM setval('public.identity_oauth_clients_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_plans_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.plans
    INTO m_cnt;
    
    PERFORM setval('public.identity_plans_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_plan_dependencies_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.plan_dependencies
    INTO m_cnt;
    
    PERFORM setval('public.identity_plan_dependencies_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_plan_related_plans_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.plan_related_plans
    INTO m_cnt;
    
    PERFORM setval('public.identity_plan_related_plans_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_plan_skills_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.plan_skills
    INTO m_cnt;
    
    PERFORM setval('public.identity_plan_skills_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_plan_guardrails_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.plan_guardrails
    INTO m_cnt;
    
    PERFORM setval('public.identity_plan_guardrails_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_agent_skills_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.agent_skills
    INTO m_cnt;
    
    PERFORM setval('public.identity_agent_skills_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_agent_guardrails_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.agent_guardrails
    INTO m_cnt;
    
    PERFORM setval('public.identity_agent_guardrails_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_project_skills_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.project_skills
    INTO m_cnt;
    
    PERFORM setval('public.identity_project_skills_id'::regclass, m_cnt);
  END IF;
END;
$$;
DO $$
DECLARE
  m_cnt bigint;
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relname = 'identity_project_guardrails_id'
      AND n.nspname = 'public'
      AND c.relkind = 'S'
  ) THEN
    SELECT COALESCE(MAX(id), 0) + 1
    FROM public.project_guardrails
    INTO m_cnt;
    
    PERFORM setval('public.identity_project_guardrails_id'::regclass, m_cnt);
  END IF;
END;
$$;
-- Comments for schema: public































