-- PostgreSQL Database Schema
-- Database: database
-- Generated by RelSpec

-- 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_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_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_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_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 (
  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 (
  persona_id bigint NOT NULL,
  skill_id bigint NOT NULL
);

CREATE TABLE IF NOT EXISTS public.agent_persona_guardrails (
  guardrail_id bigint 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 (
  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 (
  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,
  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,
  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
);

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.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,
  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,
  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 '{}',
  updated_at timestamptz NOT NULL DEFAULT now()
);

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 '{}',
  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,
  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 = '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 = '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 = '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 = '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 = '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 = '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 = '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 = '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 = '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 = '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 = '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 = '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 = '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 = '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 = '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 = '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;
$$;

-- Primary keys for schema: public
DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'agent_personas'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('agent_personas_pkey', 'public_agent_personas_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.agent_personas DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'agent_personas'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.agent_personas ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'agent_parts'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('agent_parts_pkey', 'public_agent_parts_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.agent_parts DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'agent_parts'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.agent_parts ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'agent_traits'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('agent_traits_pkey', 'public_agent_traits_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.agent_traits DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'agent_traits'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.agent_traits ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'character_arcs'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('character_arcs_pkey', 'public_character_arcs_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.character_arcs DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'character_arcs'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.character_arcs ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'arc_stages'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('arc_stages_pkey', 'public_arc_stages_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.arc_stages DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'arc_stages'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.arc_stages ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'persona_arc'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('persona_arc_pkey', 'public_persona_arc_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.persona_arc DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'persona_arc'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.persona_arc ADD CONSTRAINT [REDACTED]PRIMARY KEY (persona_id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'thoughts'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('thoughts_pkey', 'public_thoughts_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.thoughts DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'thoughts'
      AND constraint_name = 'pk_public_thoughts'
  ) THEN
    ALTER TABLE public.thoughts ADD CONSTRAINT pk_public_thoughts PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'projects'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('projects_pkey', 'public_projects_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.projects DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'projects'
      AND constraint_name = 'pk_public_projects'
  ) THEN
    ALTER TABLE public.projects ADD CONSTRAINT pk_public_projects PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'thought_links'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('thought_links_pkey', 'public_thought_links_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.thought_links DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'thought_links'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.thought_links ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'embeddings'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('embeddings_pkey', 'public_embeddings_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.embeddings DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'embeddings'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.embeddings ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'stored_files'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('stored_files_pkey', 'public_stored_files_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.stored_files DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'stored_files'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.stored_files ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'chat_histories'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('chat_histories_pkey', 'public_chat_histories_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.chat_histories DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'chat_histories'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.chat_histories ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'tool_annotations'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('tool_annotations_pkey', 'public_tool_annotations_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.tool_annotations DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'tool_annotations'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.tool_annotations ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'learnings'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('learnings_pkey', 'public_learnings_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.learnings DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'learnings'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.learnings ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'plans'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('plans_pkey', 'public_plans_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.plans DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'plans'
      AND constraint_name = 'pk_public_plans'
  ) THEN
    ALTER TABLE public.plans ADD CONSTRAINT pk_public_plans PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'plan_dependencies'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('plan_dependencies_pkey', 'public_plan_dependencies_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.plan_dependencies DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'plan_dependencies'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.plan_dependencies ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'plan_related_plans'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('plan_related_plans_pkey', 'public_plan_related_plans_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.plan_related_plans DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'plan_related_plans'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.plan_related_plans ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'plan_skills'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('plan_skills_pkey', 'public_plan_skills_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.plan_skills DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'plan_skills'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.plan_skills ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'plan_guardrails'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('plan_guardrails_pkey', 'public_plan_guardrails_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.plan_guardrails DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'plan_guardrails'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.plan_guardrails ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'agent_skills'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('agent_skills_pkey', 'public_agent_skills_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.agent_skills DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'agent_skills'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.agent_skills ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'agent_guardrails'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('agent_guardrails_pkey', 'public_agent_guardrails_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.agent_guardrails DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'agent_guardrails'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.agent_guardrails ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'project_skills'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('project_skills_pkey', 'public_project_skills_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.project_skills DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'project_skills'
      AND constraint_name = '[REDACTED]
  ) THEN
    ALTER TABLE public.project_skills ADD CONSTRAINT [REDACTED]PRIMARY KEY (id);
  END IF;
END;
$$;

DO $$
DECLARE
  auto_pk_name text;
BEGIN
  -- Drop auto-generated primary key if it exists
  SELECT constraint_name INTO auto_pk_name
  FROM information_schema.table_constraints
  WHERE table_schema = 'public'
    AND table_name = 'project_guardrails'
    AND constraint_type = 'PRIMARY KEY'
    AND constraint_name IN ('project_guardrails_pkey', 'public_project_guardrails_pkey');

  IF auto_pk_name IS NOT NULL THEN
    EXECUTE 'ALTER TABLE public.project_guardrails DROP CONSTRAINT ' || quote_ident(auto_pk_name);
  END IF;

  -- Add named primary key if it doesn't exist
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE table_schema = 'public'
      AND table_name = 'project_guardrails'
      AND constraint_name = '[REDACTED]
  ) 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_agent_persona_guardrails_persona_id_guardrail_id
  ON public.agent_persona_guardrails USING btree (persona_id, guardrail_id);

CREATE INDEX IF NOT EXISTS idx_agent_persona_traits_persona_id_trait_id
  ON public.agent_persona_traits USING btree (persona_id, trait_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_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_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);

CREATE INDEX IF NOT EXISTS idx_plans_tags
  ON public.plans USING gin (tags);

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 = 'projects'
      AND constraint_name = 'ukey_projects_name'
  ) THEN
    ALTER TABLE public.projects ADD CONSTRAINT ukey_projects_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 = '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 = '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 = '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 = '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_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_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_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_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_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




























