From bae63cb5cf18eeb2e18dc7cb2d8b8b12fcbd3f02 Mon Sep 17 00:00:00 2001 From: Hein Date: Sun, 3 May 2026 16:24:16 +0200 Subject: [PATCH] fix(db): correct default values for jsonb and array columns * Update default values for metadata and messages columns in thoughts and chat_histories tables * Change tags column type to text[] with default in learnings, plans, agent_skills, and agent_guardrails tables * Adjust index creation for tags columns in learnings and plans tables --- .../sql_public_agent_guardrails.go | 22 +- .../sql_public_agent_skills.go | 22 +- .../sql_public_chat_histories.go | 2 +- .../generatedmodels/sql_public_embeddings.go | 4 +- .../generatedmodels/sql_public_learnings.go | 54 +- internal/generatedmodels/sql_public_plans.go | 48 +- .../generatedmodels/sql_public_thoughts.go | 2 +- migrations/015_bigserial_guid_migration.sql | 472 +++++++++++------- migrations/020_generated_schema.sql | 32 +- 9 files changed, 393 insertions(+), 265 deletions(-) diff --git a/internal/generatedmodels/sql_public_agent_guardrails.go b/internal/generatedmodels/sql_public_agent_guardrails.go index ee3af06..2bb4679 100644 --- a/internal/generatedmodels/sql_public_agent_guardrails.go +++ b/internal/generatedmodels/sql_public_agent_guardrails.go @@ -9,17 +9,17 @@ import ( type ModelPublicAgentGuardrails struct { bun.BaseModel `bun:"table:public.agent_guardrails,alias:agent_guardrails"` - ID resolvespec_common.SqlInt64 `bun:"id,type:bigserial,pk,autoincrement," json:"id"` - Content resolvespec_common.SqlString `bun:"content,type:text,notnull," json:"content"` - CreatedAt resolvespec_common.SqlTimeStamp `bun:"created_at,type:timestamptz,default:now(),notnull," json:"created_at"` - Description resolvespec_common.SqlString `bun:"description,type:text,default:'',notnull," json:"description"` - GUID resolvespec_common.SqlUUID `bun:"guid,type:uuid,default:gen_random_uuid(),notnull," json:"guid"` - Name resolvespec_common.SqlString `bun:"name,type:text,notnull," json:"name"` - Severity resolvespec_common.SqlString `bun:"severity,type:text,default:'medium',notnull," json:"severity"` - Tags resolvespec_common.SqlString `bun:"tags,type:text,nullzero," json:"tags"` - UpdatedAt resolvespec_common.SqlTimeStamp `bun:"updated_at,type:timestamptz,default:now(),notnull," json:"updated_at"` - RelGuardrailIDPublicPlanGuardrails []*ModelPublicPlanGuardrails `bun:"rel:has-many,join:id=guardrail_id" json:"relguardrailidpublicplanguardrails,omitempty"` // Has many ModelPublicPlanGuardrails - RelGuardrailIDPublicProjectGuardrails []*ModelPublicProjectGuardrails `bun:"rel:has-many,join:id=guardrail_id" json:"relguardrailidpublicprojectguardrails,omitempty"` // Has many ModelPublicProjectGuardrails + ID resolvespec_common.SqlInt64 `bun:"id,type:bigserial,pk,autoincrement," json:"id"` + Content resolvespec_common.SqlString `bun:"content,type:text,notnull," json:"content"` + CreatedAt resolvespec_common.SqlTimeStamp `bun:"created_at,type:timestamptz,default:now(),notnull," json:"created_at"` + Description resolvespec_common.SqlString `bun:"description,type:text,default:'',notnull," json:"description"` + GUID resolvespec_common.SqlUUID `bun:"guid,type:uuid,default:gen_random_uuid(),notnull," json:"guid"` + Name resolvespec_common.SqlString `bun:"name,type:text,notnull," json:"name"` + Severity resolvespec_common.SqlString `bun:"severity,type:text,default:'medium',notnull," json:"severity"` + Tags resolvespec_common.SqlStringArray `bun:"tags,type:text[],array,default:'{}',notnull," json:"tags"` + UpdatedAt resolvespec_common.SqlTimeStamp `bun:"updated_at,type:timestamptz,default:now(),notnull," json:"updated_at"` + RelGuardrailIDPublicPlanGuardrails []*ModelPublicPlanGuardrails `bun:"rel:has-many,join:id=guardrail_id" json:"relguardrailidpublicplanguardrails,omitempty"` // Has many ModelPublicPlanGuardrails + RelGuardrailIDPublicProjectGuardrails []*ModelPublicProjectGuardrails `bun:"rel:has-many,join:id=guardrail_id" json:"relguardrailidpublicprojectguardrails,omitempty"` // Has many ModelPublicProjectGuardrails } // TableName returns the table name for ModelPublicAgentGuardrails diff --git a/internal/generatedmodels/sql_public_agent_skills.go b/internal/generatedmodels/sql_public_agent_skills.go index 2c91489..6370d3e 100644 --- a/internal/generatedmodels/sql_public_agent_skills.go +++ b/internal/generatedmodels/sql_public_agent_skills.go @@ -9,17 +9,17 @@ import ( type ModelPublicAgentSkills struct { bun.BaseModel `bun:"table:public.agent_skills,alias:agent_skills"` - ID resolvespec_common.SqlInt64 `bun:"id,type:bigserial,pk,autoincrement," json:"id"` - Content resolvespec_common.SqlString `bun:"content,type:text,notnull," json:"content"` - CreatedAt resolvespec_common.SqlTimeStamp `bun:"created_at,type:timestamptz,default:now(),notnull," json:"created_at"` - Description resolvespec_common.SqlString `bun:"description,type:text,default:'',notnull," json:"description"` - GUID resolvespec_common.SqlUUID `bun:"guid,type:uuid,default:gen_random_uuid(),notnull," json:"guid"` - Name resolvespec_common.SqlString `bun:"name,type:text,notnull," json:"name"` - Tags resolvespec_common.SqlString `bun:"tags,type:text,nullzero," json:"tags"` - UpdatedAt resolvespec_common.SqlTimeStamp `bun:"updated_at,type:timestamptz,default:now(),notnull," json:"updated_at"` - RelRelatedSkillIDPublicLearnings []*ModelPublicLearnings `bun:"rel:has-many,join:id=related_skill_id" json:"relrelatedskillidpubliclearnings,omitempty"` // Has many ModelPublicLearnings - RelSkillIDPublicPlanSkills []*ModelPublicPlanSkills `bun:"rel:has-many,join:id=skill_id" json:"relskillidpublicplanskills,omitempty"` // Has many ModelPublicPlanSkills - RelSkillIDPublicProjectSkills []*ModelPublicProjectSkills `bun:"rel:has-many,join:id=skill_id" json:"relskillidpublicprojectskills,omitempty"` // Has many ModelPublicProjectSkills + ID resolvespec_common.SqlInt64 `bun:"id,type:bigserial,pk,autoincrement," json:"id"` + Content resolvespec_common.SqlString `bun:"content,type:text,notnull," json:"content"` + CreatedAt resolvespec_common.SqlTimeStamp `bun:"created_at,type:timestamptz,default:now(),notnull," json:"created_at"` + Description resolvespec_common.SqlString `bun:"description,type:text,default:'',notnull," json:"description"` + GUID resolvespec_common.SqlUUID `bun:"guid,type:uuid,default:gen_random_uuid(),notnull," json:"guid"` + Name resolvespec_common.SqlString `bun:"name,type:text,notnull," json:"name"` + Tags resolvespec_common.SqlStringArray `bun:"tags,type:text[],array,default:'{}',notnull," json:"tags"` + UpdatedAt resolvespec_common.SqlTimeStamp `bun:"updated_at,type:timestamptz,default:now(),notnull," json:"updated_at"` + RelRelatedSkillIDPublicLearnings []*ModelPublicLearnings `bun:"rel:has-many,join:id=related_skill_id" json:"relrelatedskillidpubliclearnings,omitempty"` // Has many ModelPublicLearnings + RelSkillIDPublicPlanSkills []*ModelPublicPlanSkills `bun:"rel:has-many,join:id=skill_id" json:"relskillidpublicplanskills,omitempty"` // Has many ModelPublicPlanSkills + RelSkillIDPublicProjectSkills []*ModelPublicProjectSkills `bun:"rel:has-many,join:id=skill_id" json:"relskillidpublicprojectskills,omitempty"` // Has many ModelPublicProjectSkills } // TableName returns the table name for ModelPublicAgentSkills diff --git a/internal/generatedmodels/sql_public_chat_histories.go b/internal/generatedmodels/sql_public_chat_histories.go index ce87e96..22b823c 100644 --- a/internal/generatedmodels/sql_public_chat_histories.go +++ b/internal/generatedmodels/sql_public_chat_histories.go @@ -14,7 +14,7 @@ type ModelPublicChatHistories struct { Channel resolvespec_common.SqlString `bun:"channel,type:text,nullzero," json:"channel"` CreatedAt resolvespec_common.SqlTimeStamp `bun:"created_at,type:timestamptz,default:now(),notnull," json:"created_at"` GUID resolvespec_common.SqlUUID `bun:"guid,type:uuid,default:gen_random_uuid(),notnull," json:"guid"` - Messages resolvespec_common.SqlJSONB `bun:"messages,type:jsonb,default:'[',notnull," json:"messages"` + Messages resolvespec_common.SqlJSONB `bun:"messages,type:jsonb,default:'[]',notnull," json:"messages"` Metadata resolvespec_common.SqlJSONB `bun:"metadata,type:jsonb,default:'{}',notnull," json:"metadata"` ProjectID resolvespec_common.SqlInt64 `bun:"project_id,type:bigint,nullzero," json:"project_id"` SessionID resolvespec_common.SqlString `bun:"session_id,type:text,notnull," json:"session_id"` diff --git a/internal/generatedmodels/sql_public_embeddings.go b/internal/generatedmodels/sql_public_embeddings.go index da90976..92d34f5 100644 --- a/internal/generatedmodels/sql_public_embeddings.go +++ b/internal/generatedmodels/sql_public_embeddings.go @@ -11,8 +11,8 @@ type ModelPublicEmbeddings struct { bun.BaseModel `bun:"table:public.embeddings,alias:embeddings"` ID resolvespec_common.SqlInt64 `bun:"id,type:bigserial,pk,autoincrement," json:"id"` CreatedAt resolvespec_common.SqlTimeStamp `bun:"created_at,type:timestamptz,default:now(),nullzero," json:"created_at"` - Dim resolvespec_common.SqlInt32 `bun:"dim,type:int,notnull," json:"dim"` - Embedding resolvespec_common.SqlString `bun:"embedding,type:vector,notnull," json:"embedding"` + Dim int32 `bun:"dim,type:int,notnull," json:"dim"` + Embedding resolvespec_common.SqlVector `bun:"embedding,type:vector,notnull," json:"embedding"` GUID resolvespec_common.SqlUUID `bun:"guid,type:uuid,default:gen_random_uuid(),notnull," json:"guid"` Model resolvespec_common.SqlString `bun:"model,type:text,notnull,unique:uidx_embeddings_thought_id_model," json:"model"` ThoughtID int64 `bun:"thought_id,type:bigint,notnull,unique:uidx_embeddings_thought_id_model," json:"thought_id"` diff --git a/internal/generatedmodels/sql_public_learnings.go b/internal/generatedmodels/sql_public_learnings.go index dfb2eff..9986161 100644 --- a/internal/generatedmodels/sql_public_learnings.go +++ b/internal/generatedmodels/sql_public_learnings.go @@ -9,33 +9,33 @@ import ( type ModelPublicLearnings struct { bun.BaseModel `bun:"table:public.learnings,alias:learnings"` - ID resolvespec_common.SqlInt64 `bun:"id,type:bigserial,pk,autoincrement," json:"id"` - ActionRequired bool `bun:"action_required,type:boolean,default:false,notnull," json:"action_required"` - Area resolvespec_common.SqlString `bun:"area,type:text,default:'other',notnull," json:"area"` - Category resolvespec_common.SqlString `bun:"category,type:text,default:'insight',notnull," json:"category"` - Confidence resolvespec_common.SqlString `bun:"confidence,type:text,default:'hypothesis',notnull," json:"confidence"` - CreatedAt resolvespec_common.SqlTimeStamp `bun:"created_at,type:timestamptz,default:now(),notnull," json:"created_at"` - Details resolvespec_common.SqlString `bun:"details,type:text,default:'',notnull," json:"details"` - DuplicateOfLearningID resolvespec_common.SqlInt64 `bun:"duplicate_of_learning_id,type:bigint,nullzero," json:"duplicate_of_learning_id"` - GUID resolvespec_common.SqlUUID `bun:"guid,type:uuid,default:gen_random_uuid(),notnull," json:"guid"` - Priority resolvespec_common.SqlString `bun:"priority,type:text,default:'medium',notnull," json:"priority"` - ProjectID resolvespec_common.SqlInt64 `bun:"project_id,type:bigint,nullzero," json:"project_id"` - RelatedSkillID resolvespec_common.SqlInt64 `bun:"related_skill_id,type:bigint,nullzero," json:"related_skill_id"` - RelatedThoughtID resolvespec_common.SqlInt64 `bun:"related_thought_id,type:bigint,nullzero," json:"related_thought_id"` - ReviewedAt resolvespec_common.SqlTimeStamp `bun:"reviewed_at,type:timestamptz,nullzero," json:"reviewed_at"` - ReviewedBy resolvespec_common.SqlString `bun:"reviewed_by,type:text,nullzero," json:"reviewed_by"` - SourceRef resolvespec_common.SqlString `bun:"source_ref,type:text,nullzero," json:"source_ref"` - SourceType resolvespec_common.SqlString `bun:"source_type,type:text,nullzero," json:"source_type"` - Status resolvespec_common.SqlString `bun:"status,type:text,default:'pending',notnull," json:"status"` - Summary resolvespec_common.SqlString `bun:"summary,type:text,notnull," json:"summary"` - SupersedesLearningID resolvespec_common.SqlInt64 `bun:"supersedes_learning_id,type:bigint,nullzero," json:"supersedes_learning_id"` - Tags resolvespec_common.SqlString `bun:"tags,type:text,nullzero," json:"tags"` - UpdatedAt resolvespec_common.SqlTimeStamp `bun:"updated_at,type:timestamptz,default:now(),notnull," json:"updated_at"` - RelDuplicateOfLearningID *ModelPublicLearnings `bun:"rel:has-one,join:duplicate_of_learning_id=id" json:"relduplicateoflearningid,omitempty"` // Has one ModelPublicLearnings - RelProjectID *ModelPublicProjects `bun:"rel:has-one,join:project_id=id" json:"relprojectid,omitempty"` // Has one ModelPublicProjects - RelRelatedSkillID *ModelPublicAgentSkills `bun:"rel:has-one,join:related_skill_id=id" json:"relrelatedskillid,omitempty"` // Has one ModelPublicAgentSkills - RelRelatedThoughtID *ModelPublicThoughts `bun:"rel:has-one,join:related_thought_id=id" json:"relrelatedthoughtid,omitempty"` // Has one ModelPublicThoughts - RelSupersedesLearningID *ModelPublicLearnings `bun:"rel:has-one,join:supersedes_learning_id=id" json:"relsupersedeslearningid,omitempty"` // Has one ModelPublicLearnings + ID resolvespec_common.SqlInt64 `bun:"id,type:bigserial,pk,autoincrement," json:"id"` + ActionRequired bool `bun:"action_required,type:boolean,default:false,notnull," json:"action_required"` + Area resolvespec_common.SqlString `bun:"area,type:text,default:'other',notnull," json:"area"` + Category resolvespec_common.SqlString `bun:"category,type:text,default:'insight',notnull," json:"category"` + Confidence resolvespec_common.SqlString `bun:"confidence,type:text,default:'hypothesis',notnull," json:"confidence"` + CreatedAt resolvespec_common.SqlTimeStamp `bun:"created_at,type:timestamptz,default:now(),notnull," json:"created_at"` + Details resolvespec_common.SqlString `bun:"details,type:text,default:'',notnull," json:"details"` + DuplicateOfLearningID resolvespec_common.SqlInt64 `bun:"duplicate_of_learning_id,type:bigint,nullzero," json:"duplicate_of_learning_id"` + GUID resolvespec_common.SqlUUID `bun:"guid,type:uuid,default:gen_random_uuid(),notnull," json:"guid"` + Priority resolvespec_common.SqlString `bun:"priority,type:text,default:'medium',notnull," json:"priority"` + ProjectID resolvespec_common.SqlInt64 `bun:"project_id,type:bigint,nullzero," json:"project_id"` + RelatedSkillID resolvespec_common.SqlInt64 `bun:"related_skill_id,type:bigint,nullzero," json:"related_skill_id"` + RelatedThoughtID resolvespec_common.SqlInt64 `bun:"related_thought_id,type:bigint,nullzero," json:"related_thought_id"` + ReviewedAt resolvespec_common.SqlTimeStamp `bun:"reviewed_at,type:timestamptz,nullzero," json:"reviewed_at"` + ReviewedBy resolvespec_common.SqlString `bun:"reviewed_by,type:text,nullzero," json:"reviewed_by"` + SourceRef resolvespec_common.SqlString `bun:"source_ref,type:text,nullzero," json:"source_ref"` + SourceType resolvespec_common.SqlString `bun:"source_type,type:text,nullzero," json:"source_type"` + Status resolvespec_common.SqlString `bun:"status,type:text,default:'pending',notnull," json:"status"` + Summary resolvespec_common.SqlString `bun:"summary,type:text,notnull," json:"summary"` + SupersedesLearningID resolvespec_common.SqlInt64 `bun:"supersedes_learning_id,type:bigint,nullzero," json:"supersedes_learning_id"` + Tags resolvespec_common.SqlStringArray `bun:"tags,type:text[],array,default:'{}',notnull," json:"tags"` + UpdatedAt resolvespec_common.SqlTimeStamp `bun:"updated_at,type:timestamptz,default:now(),notnull," json:"updated_at"` + RelDuplicateOfLearningID *ModelPublicLearnings `bun:"rel:has-one,join:duplicate_of_learning_id=id" json:"relduplicateoflearningid,omitempty"` // Has one ModelPublicLearnings + RelProjectID *ModelPublicProjects `bun:"rel:has-one,join:project_id=id" json:"relprojectid,omitempty"` // Has one ModelPublicProjects + RelRelatedSkillID *ModelPublicAgentSkills `bun:"rel:has-one,join:related_skill_id=id" json:"relrelatedskillid,omitempty"` // Has one ModelPublicAgentSkills + RelRelatedThoughtID *ModelPublicThoughts `bun:"rel:has-one,join:related_thought_id=id" json:"relrelatedthoughtid,omitempty"` // Has one ModelPublicThoughts + RelSupersedesLearningID *ModelPublicLearnings `bun:"rel:has-one,join:supersedes_learning_id=id" json:"relsupersedeslearningid,omitempty"` // Has one ModelPublicLearnings } // TableName returns the table name for ModelPublicLearnings diff --git a/internal/generatedmodels/sql_public_plans.go b/internal/generatedmodels/sql_public_plans.go index 3cd52a1..1dfea31 100644 --- a/internal/generatedmodels/sql_public_plans.go +++ b/internal/generatedmodels/sql_public_plans.go @@ -9,30 +9,30 @@ import ( type ModelPublicPlans struct { bun.BaseModel `bun:"table:public.plans,alias:plans"` - ID resolvespec_common.SqlInt64 `bun:"id,type:bigserial,pk,autoincrement," json:"id"` - CompletedAt resolvespec_common.SqlTimeStamp `bun:"completed_at,type:timestamptz,nullzero," json:"completed_at"` - CreatedAt resolvespec_common.SqlTimeStamp `bun:"created_at,type:timestamptz,default:now(),notnull," json:"created_at"` - Description resolvespec_common.SqlString `bun:"description,type:text,default:'',notnull," json:"description"` - DueDate resolvespec_common.SqlTimeStamp `bun:"due_date,type:timestamptz,nullzero," json:"due_date"` - GUID resolvespec_common.SqlUUID `bun:"guid,type:uuid,default:gen_random_uuid(),notnull," json:"guid"` - LastReviewedAt resolvespec_common.SqlTimeStamp `bun:"last_reviewed_at,type:timestamptz,nullzero," json:"last_reviewed_at"` - Owner resolvespec_common.SqlString `bun:"owner,type:text,nullzero," json:"owner"` - Priority resolvespec_common.SqlString `bun:"priority,type:text,default:'medium',notnull," json:"priority"` // low, medium, high, critical - ProjectID resolvespec_common.SqlInt64 `bun:"project_id,type:bigint,nullzero," json:"project_id"` - ReviewedBy resolvespec_common.SqlString `bun:"reviewed_by,type:text,nullzero," json:"reviewed_by"` - Status resolvespec_common.SqlString `bun:"status,type:text,default:'draft',notnull," json:"status"` // draft, active, blocked, completed, cancelled, superseded - SupersedesPlanID resolvespec_common.SqlInt64 `bun:"supersedes_plan_id,type:bigint,nullzero," json:"supersedes_plan_id"` - Tags resolvespec_common.SqlString `bun:"tags,type:text,nullzero," json:"tags"` - Title resolvespec_common.SqlString `bun:"title,type:text,notnull," json:"title"` - UpdatedAt resolvespec_common.SqlTimeStamp `bun:"updated_at,type:timestamptz,default:now(),notnull," json:"updated_at"` - RelProjectID *ModelPublicProjects `bun:"rel:has-one,join:project_id=id" json:"relprojectid,omitempty"` // Has one ModelPublicProjects - RelSupersedesPlanID *ModelPublicPlans `bun:"rel:has-one,join:supersedes_plan_id=id" json:"relsupersedesplanid,omitempty"` // Has one ModelPublicPlans - RelDependsOnPlanIDPublicPlanDependencies []*ModelPublicPlanDependencies `bun:"rel:has-many,join:id=depends_on_plan_id" json:"reldependsonplanidpublicplandependencies,omitempty"` // Has many ModelPublicPlanDependencies - RelPlanIDPublicPlanDependencies []*ModelPublicPlanDependencies `bun:"rel:has-many,join:id=plan_id" json:"relplanidpublicplandependencies,omitempty"` // Has many ModelPublicPlanDependencies - RelPlanAIDPublicPlanRelatedPlans []*ModelPublicPlanRelatedPlans `bun:"rel:has-many,join:id=plan_a_id" json:"relplanaidpublicplanrelatedplans,omitempty"` // Has many ModelPublicPlanRelatedPlans - RelPlanBIDPublicPlanRelatedPlans []*ModelPublicPlanRelatedPlans `bun:"rel:has-many,join:id=plan_b_id" json:"relplanbidpublicplanrelatedplans,omitempty"` // Has many ModelPublicPlanRelatedPlans - RelPlanIDPublicPlanSkills []*ModelPublicPlanSkills `bun:"rel:has-many,join:id=plan_id" json:"relplanidpublicplanskills,omitempty"` // Has many ModelPublicPlanSkills - RelPlanIDPublicPlanGuardrails []*ModelPublicPlanGuardrails `bun:"rel:has-many,join:id=plan_id" json:"relplanidpublicplanguardrails,omitempty"` // Has many ModelPublicPlanGuardrails + ID resolvespec_common.SqlInt64 `bun:"id,type:bigserial,pk,autoincrement," json:"id"` + CompletedAt resolvespec_common.SqlTimeStamp `bun:"completed_at,type:timestamptz,nullzero," json:"completed_at"` + CreatedAt resolvespec_common.SqlTimeStamp `bun:"created_at,type:timestamptz,default:now(),notnull," json:"created_at"` + Description resolvespec_common.SqlString `bun:"description,type:text,default:'',notnull," json:"description"` + DueDate resolvespec_common.SqlTimeStamp `bun:"due_date,type:timestamptz,nullzero," json:"due_date"` + GUID resolvespec_common.SqlUUID `bun:"guid,type:uuid,default:gen_random_uuid(),notnull," json:"guid"` + LastReviewedAt resolvespec_common.SqlTimeStamp `bun:"last_reviewed_at,type:timestamptz,nullzero," json:"last_reviewed_at"` + Owner resolvespec_common.SqlString `bun:"owner,type:text,nullzero," json:"owner"` + Priority resolvespec_common.SqlString `bun:"priority,type:text,default:'medium',notnull," json:"priority"` // low, medium, high, critical + ProjectID resolvespec_common.SqlInt64 `bun:"project_id,type:bigint,nullzero," json:"project_id"` + ReviewedBy resolvespec_common.SqlString `bun:"reviewed_by,type:text,nullzero," json:"reviewed_by"` + Status resolvespec_common.SqlString `bun:"status,type:text,default:'draft',notnull," json:"status"` // draft, active, blocked, completed, cancelled, superseded + SupersedesPlanID resolvespec_common.SqlInt64 `bun:"supersedes_plan_id,type:bigint,nullzero," json:"supersedes_plan_id"` + Tags resolvespec_common.SqlStringArray `bun:"tags,type:text[],array,default:'{}',notnull," json:"tags"` + Title resolvespec_common.SqlString `bun:"title,type:text,notnull," json:"title"` + UpdatedAt resolvespec_common.SqlTimeStamp `bun:"updated_at,type:timestamptz,default:now(),notnull," json:"updated_at"` + RelProjectID *ModelPublicProjects `bun:"rel:has-one,join:project_id=id" json:"relprojectid,omitempty"` // Has one ModelPublicProjects + RelSupersedesPlanID *ModelPublicPlans `bun:"rel:has-one,join:supersedes_plan_id=id" json:"relsupersedesplanid,omitempty"` // Has one ModelPublicPlans + RelDependsOnPlanIDPublicPlanDependencies []*ModelPublicPlanDependencies `bun:"rel:has-many,join:id=depends_on_plan_id" json:"reldependsonplanidpublicplandependencies,omitempty"` // Has many ModelPublicPlanDependencies + RelPlanIDPublicPlanDependencies []*ModelPublicPlanDependencies `bun:"rel:has-many,join:id=plan_id" json:"relplanidpublicplandependencies,omitempty"` // Has many ModelPublicPlanDependencies + RelPlanAIDPublicPlanRelatedPlans []*ModelPublicPlanRelatedPlans `bun:"rel:has-many,join:id=plan_a_id" json:"relplanaidpublicplanrelatedplans,omitempty"` // Has many ModelPublicPlanRelatedPlans + RelPlanBIDPublicPlanRelatedPlans []*ModelPublicPlanRelatedPlans `bun:"rel:has-many,join:id=plan_b_id" json:"relplanbidpublicplanrelatedplans,omitempty"` // Has many ModelPublicPlanRelatedPlans + RelPlanIDPublicPlanSkills []*ModelPublicPlanSkills `bun:"rel:has-many,join:id=plan_id" json:"relplanidpublicplanskills,omitempty"` // Has many ModelPublicPlanSkills + RelPlanIDPublicPlanGuardrails []*ModelPublicPlanGuardrails `bun:"rel:has-many,join:id=plan_id" json:"relplanidpublicplanguardrails,omitempty"` // Has many ModelPublicPlanGuardrails } // TableName returns the table name for ModelPublicPlans diff --git a/internal/generatedmodels/sql_public_thoughts.go b/internal/generatedmodels/sql_public_thoughts.go index e3dc038..3d81f7c 100644 --- a/internal/generatedmodels/sql_public_thoughts.go +++ b/internal/generatedmodels/sql_public_thoughts.go @@ -14,7 +14,7 @@ type ModelPublicThoughts struct { Content resolvespec_common.SqlString `bun:"content,type:text,notnull," json:"content"` CreatedAt resolvespec_common.SqlTimeStamp `bun:"created_at,type:timestamptz,default:now(),nullzero," json:"created_at"` GUID resolvespec_common.SqlUUID `bun:"guid,type:uuid,default:gen_random_uuid(),notnull," json:"guid"` - Metadata resolvespec_common.SqlJSONB `bun:"metadata,type:jsonb,default:'{}::jsonb',nullzero," json:"metadata"` + Metadata resolvespec_common.SqlJSONB `bun:"metadata,type:jsonb,default:{}::jsonb,nullzero," json:"metadata"` ProjectID resolvespec_common.SqlInt64 `bun:"project_id,type:bigint,nullzero," json:"project_id"` UpdatedAt resolvespec_common.SqlTimeStamp `bun:"updated_at,type:timestamptz,default:now(),nullzero," json:"updated_at"` RelProjectID *ModelPublicProjects `bun:"rel:has-one,join:project_id=id" json:"relprojectid,omitempty"` // Has one ModelPublicProjects diff --git a/migrations/015_bigserial_guid_migration.sql b/migrations/015_bigserial_guid_migration.sql index 7404481..f627758 100644 --- a/migrations/015_bigserial_guid_migration.sql +++ b/migrations/015_bigserial_guid_migration.sql @@ -2,6 +2,7 @@ -- Tables with uuid PK → bigserial: agent_skills, agent_guardrails, chat_histories, learnings, plans -- Tables with serial → bigserial: thought_links, plan_dependencies, plan_related_plans, plan_skills, plan_guardrails, project_skills, project_guardrails -- FK columns: all uuid FK columns converted to bigint referencing .id +-- Idempotent: safe to re-run at any point. BEGIN; @@ -10,217 +11,373 @@ BEGIN; -- ============================================================ ALTER TABLE public.thoughts DROP CONSTRAINT IF EXISTS thoughts_project_id_fkey; +ALTER TABLE public.thoughts DROP CONSTRAINT IF EXISTS fk_thoughts_project_id; ALTER TABLE public.embeddings DROP CONSTRAINT IF EXISTS embeddings_thought_id_fkey; +ALTER TABLE public.embeddings DROP CONSTRAINT IF EXISTS fk_embeddings_thought_id; ALTER TABLE public.stored_files DROP CONSTRAINT IF EXISTS stored_files_thought_id_fkey; +ALTER TABLE public.stored_files DROP CONSTRAINT IF EXISTS fk_stored_files_thought_id; ALTER TABLE public.stored_files DROP CONSTRAINT IF EXISTS stored_files_project_id_fkey; +ALTER TABLE public.stored_files DROP CONSTRAINT IF EXISTS fk_stored_files_project_id; ALTER TABLE public.project_skills DROP CONSTRAINT IF EXISTS project_skills_project_id_fkey; +ALTER TABLE public.project_skills DROP CONSTRAINT IF EXISTS fk_project_skills_project_id; ALTER TABLE public.project_skills DROP CONSTRAINT IF EXISTS project_skills_skill_id_fkey; +ALTER TABLE public.project_skills DROP CONSTRAINT IF EXISTS fk_project_skills_skill_id; ALTER TABLE public.project_guardrails DROP CONSTRAINT IF EXISTS project_guardrails_project_id_fkey; +ALTER TABLE public.project_guardrails DROP CONSTRAINT IF EXISTS fk_project_guardrails_project_id; ALTER TABLE public.project_guardrails DROP CONSTRAINT IF EXISTS project_guardrails_guardrail_id_fkey; +ALTER TABLE public.project_guardrails DROP CONSTRAINT IF EXISTS fk_project_guardrails_guardrail_id; ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS chat_histories_project_id_fkey; +ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS fk_chat_histories_project_id; ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_project_id_fkey; +ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_project_id; ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_related_thought_id_fkey; +ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_related_thought_id; ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_related_skill_id_fkey; +ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_related_skill_id; ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_duplicate_of_learning_id_fkey; +ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_duplicate_of_learning_id; ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_supersedes_learning_id_fkey; +ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_supersedes_learning_id; ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS plans_project_id_fkey; +ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS fk_plans_project_id; ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS plans_supersedes_plan_id_fkey; +ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS fk_plans_supersedes_plan_id; ALTER TABLE public.plan_dependencies DROP CONSTRAINT IF EXISTS plan_dependencies_plan_id_fkey; +ALTER TABLE public.plan_dependencies DROP CONSTRAINT IF EXISTS fk_plan_dependencies_plan_id; ALTER TABLE public.plan_dependencies DROP CONSTRAINT IF EXISTS plan_dependencies_depends_on_plan_id_fkey; +ALTER TABLE public.plan_dependencies DROP CONSTRAINT IF EXISTS fk_plan_dependencies_depends_on_plan_id; ALTER TABLE public.plan_related_plans DROP CONSTRAINT IF EXISTS plan_related_plans_plan_a_id_fkey; +ALTER TABLE public.plan_related_plans DROP CONSTRAINT IF EXISTS fk_plan_related_plans_plan_a_id; ALTER TABLE public.plan_related_plans DROP CONSTRAINT IF EXISTS plan_related_plans_plan_b_id_fkey; +ALTER TABLE public.plan_related_plans DROP CONSTRAINT IF EXISTS fk_plan_related_plans_plan_b_id; ALTER TABLE public.plan_skills DROP CONSTRAINT IF EXISTS plan_skills_plan_id_fkey; +ALTER TABLE public.plan_skills DROP CONSTRAINT IF EXISTS fk_plan_skills_plan_id; ALTER TABLE public.plan_skills DROP CONSTRAINT IF EXISTS plan_skills_skill_id_fkey; +ALTER TABLE public.plan_skills DROP CONSTRAINT IF EXISTS fk_plan_skills_skill_id; ALTER TABLE public.plan_guardrails DROP CONSTRAINT IF EXISTS plan_guardrails_plan_id_fkey; +ALTER TABLE public.plan_guardrails DROP CONSTRAINT IF EXISTS fk_plan_guardrails_plan_id; ALTER TABLE public.plan_guardrails DROP CONSTRAINT IF EXISTS plan_guardrails_guardrail_id_fkey; +ALTER TABLE public.plan_guardrails DROP CONSTRAINT IF EXISTS fk_plan_guardrails_guardrail_id; -- ============================================================ -- STEP 2: Drop PK constraints on uuid-pk tables -- ============================================================ ALTER TABLE public.agent_skills DROP CONSTRAINT IF EXISTS agent_skills_pkey; +ALTER TABLE public.agent_skills DROP CONSTRAINT IF EXISTS pk_public_agent_skills; ALTER TABLE public.agent_guardrails DROP CONSTRAINT IF EXISTS agent_guardrails_pkey; +ALTER TABLE public.agent_guardrails DROP CONSTRAINT IF EXISTS pk_public_agent_guardrails; ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS chat_histories_pkey; +ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS pk_public_chat_histories; ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_pkey; +ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS pk_public_learnings; ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS plans_pkey; +ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS pk_public_plans; -- ============================================================ -- STEP 3: Rename uuid id → guid on formerly uuid-pk tables +-- Only renames if id exists and guid does not yet exist. -- ============================================================ -ALTER TABLE public.agent_skills RENAME COLUMN id TO guid; -ALTER TABLE public.agent_guardrails RENAME COLUMN id TO guid; -ALTER TABLE public.chat_histories RENAME COLUMN id TO guid; -ALTER TABLE public.learnings RENAME COLUMN id TO guid; -ALTER TABLE public.plans RENAME COLUMN id TO guid; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='agent_skills' AND column_name='id') + AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='agent_skills' AND column_name='guid') THEN + ALTER TABLE public.agent_skills RENAME COLUMN id TO guid; + END IF; +END $$; + +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='agent_guardrails' AND column_name='id') + AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='agent_guardrails' AND column_name='guid') THEN + ALTER TABLE public.agent_guardrails RENAME COLUMN id TO guid; + END IF; +END $$; + +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='chat_histories' AND column_name='id') + AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='chat_histories' AND column_name='guid') THEN + ALTER TABLE public.chat_histories RENAME COLUMN id TO guid; + END IF; +END $$; + +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='id') + AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='guid') THEN + ALTER TABLE public.learnings RENAME COLUMN id TO guid; + END IF; +END $$; + +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='id') + AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='guid') THEN + ALTER TABLE public.plans RENAME COLUMN id TO guid; + END IF; +END $$; -- ============================================================ -- STEP 4: Add bigserial id columns to formerly uuid-pk tables -- ============================================================ -ALTER TABLE public.agent_skills ADD COLUMN id bigserial; -ALTER TABLE public.agent_guardrails ADD COLUMN id bigserial; -ALTER TABLE public.chat_histories ADD COLUMN id bigserial; -ALTER TABLE public.learnings ADD COLUMN id bigserial; -ALTER TABLE public.plans ADD COLUMN id bigserial; +ALTER TABLE public.agent_skills ADD COLUMN IF NOT EXISTS id bigserial; +ALTER TABLE public.agent_guardrails ADD COLUMN IF NOT EXISTS id bigserial; +ALTER TABLE public.chat_histories ADD COLUMN IF NOT EXISTS id bigserial; +ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS id bigserial; +ALTER TABLE public.plans ADD COLUMN IF NOT EXISTS id bigserial; -- ============================================================ --- STEP 5: Add new bigint FK columns (prefixed _new) where uuid FKs exist +-- STEP 5: Add new bigint FK columns and migrate data from uuid columns. +-- Each update is guarded: only runs when the source column is still uuid type. -- ============================================================ -- thoughts.project_id (uuid → bigint via projects.guid) -ALTER TABLE public.thoughts ADD COLUMN project_id_new bigint; -UPDATE public.thoughts t SET project_id_new = p.id FROM public.projects p WHERE p.guid = t.project_id; +ALTER TABLE public.thoughts ADD COLUMN IF NOT EXISTS project_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='thoughts' AND column_name='project_id' AND data_type='uuid') THEN + UPDATE public.thoughts t SET project_id_new = p.id FROM public.projects p WHERE p.guid = t.project_id AND t.project_id_new IS NULL; + END IF; +END $$; -- embeddings.thought_id (uuid → bigint via thoughts.guid) -ALTER TABLE public.embeddings ADD COLUMN thought_id_new bigint; -UPDATE public.embeddings e SET thought_id_new = t.id FROM public.thoughts t WHERE t.guid = e.thought_id; +ALTER TABLE public.embeddings ADD COLUMN IF NOT EXISTS thought_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='embeddings' AND column_name='thought_id' AND data_type='uuid') THEN + UPDATE public.embeddings e SET thought_id_new = t.id FROM public.thoughts t WHERE t.guid = e.thought_id AND e.thought_id_new IS NULL; + END IF; +END $$; -- stored_files.thought_id (uuid → bigint via thoughts.guid) -ALTER TABLE public.stored_files ADD COLUMN thought_id_new bigint; -UPDATE public.stored_files f SET thought_id_new = t.id FROM public.thoughts t WHERE t.guid = f.thought_id; +ALTER TABLE public.stored_files ADD COLUMN IF NOT EXISTS thought_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='stored_files' AND column_name='thought_id' AND data_type='uuid') THEN + UPDATE public.stored_files f SET thought_id_new = t.id FROM public.thoughts t WHERE t.guid = f.thought_id AND f.thought_id_new IS NULL; + END IF; +END $$; -- stored_files.project_id (uuid → bigint via projects.guid) -ALTER TABLE public.stored_files ADD COLUMN project_id_new bigint; -UPDATE public.stored_files f SET project_id_new = p.id FROM public.projects p WHERE p.guid = f.project_id; +ALTER TABLE public.stored_files ADD COLUMN IF NOT EXISTS project_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='stored_files' AND column_name='project_id' AND data_type='uuid') THEN + UPDATE public.stored_files f SET project_id_new = p.id FROM public.projects p WHERE p.guid = f.project_id AND f.project_id_new IS NULL; + END IF; +END $$; -- project_skills.project_id (uuid → bigint via projects.guid) -ALTER TABLE public.project_skills ADD COLUMN project_id_new bigint; -UPDATE public.project_skills ps SET project_id_new = p.id FROM public.projects p WHERE p.guid = ps.project_id; +ALTER TABLE public.project_skills ADD COLUMN IF NOT EXISTS project_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_skills' AND column_name='project_id' AND data_type='uuid') THEN + UPDATE public.project_skills ps SET project_id_new = p.id FROM public.projects p WHERE p.guid = ps.project_id AND ps.project_id_new IS NULL; + END IF; +END $$; -- project_skills.skill_id (uuid → bigint via agent_skills.guid) -ALTER TABLE public.project_skills ADD COLUMN skill_id_new bigint; -UPDATE public.project_skills ps SET skill_id_new = s.id FROM public.agent_skills s WHERE s.guid = ps.skill_id; +ALTER TABLE public.project_skills ADD COLUMN IF NOT EXISTS skill_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_skills' AND column_name='skill_id' AND data_type='uuid') THEN + UPDATE public.project_skills ps SET skill_id_new = s.id FROM public.agent_skills s WHERE s.guid = ps.skill_id AND ps.skill_id_new IS NULL; + END IF; +END $$; -- project_guardrails.project_id (uuid → bigint via projects.guid) -ALTER TABLE public.project_guardrails ADD COLUMN project_id_new bigint; -UPDATE public.project_guardrails pg SET project_id_new = p.id FROM public.projects p WHERE p.guid = pg.project_id; +ALTER TABLE public.project_guardrails ADD COLUMN IF NOT EXISTS project_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_guardrails' AND column_name='project_id' AND data_type='uuid') THEN + UPDATE public.project_guardrails pg SET project_id_new = p.id FROM public.projects p WHERE p.guid = pg.project_id AND pg.project_id_new IS NULL; + END IF; +END $$; -- project_guardrails.guardrail_id (uuid → bigint via agent_guardrails.guid) -ALTER TABLE public.project_guardrails ADD COLUMN guardrail_id_new bigint; -UPDATE public.project_guardrails pg SET guardrail_id_new = g.id FROM public.agent_guardrails g WHERE g.guid = pg.guardrail_id; +ALTER TABLE public.project_guardrails ADD COLUMN IF NOT EXISTS guardrail_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_guardrails' AND column_name='guardrail_id' AND data_type='uuid') THEN + UPDATE public.project_guardrails pg SET guardrail_id_new = g.id FROM public.agent_guardrails g WHERE g.guid = pg.guardrail_id AND pg.guardrail_id_new IS NULL; + END IF; +END $$; -- chat_histories.project_id (uuid → bigint via projects.guid) -ALTER TABLE public.chat_histories ADD COLUMN project_id_new bigint; -UPDATE public.chat_histories ch SET project_id_new = p.id FROM public.projects p WHERE p.guid = ch.project_id; +ALTER TABLE public.chat_histories ADD COLUMN IF NOT EXISTS project_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='chat_histories' AND column_name='project_id' AND data_type='uuid') THEN + UPDATE public.chat_histories ch SET project_id_new = p.id FROM public.projects p WHERE p.guid = ch.project_id AND ch.project_id_new IS NULL; + END IF; +END $$; -- learnings.project_id (uuid → bigint via projects.guid) -ALTER TABLE public.learnings ADD COLUMN project_id_new bigint; -UPDATE public.learnings l SET project_id_new = p.id FROM public.projects p WHERE p.guid = l.project_id; +ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS project_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='project_id' AND data_type='uuid') THEN + UPDATE public.learnings l SET project_id_new = p.id FROM public.projects p WHERE p.guid = l.project_id AND l.project_id_new IS NULL; + END IF; +END $$; -- learnings.related_thought_id (uuid → bigint via thoughts.guid) -ALTER TABLE public.learnings ADD COLUMN related_thought_id_new bigint; -UPDATE public.learnings l SET related_thought_id_new = t.id FROM public.thoughts t WHERE t.guid = l.related_thought_id; +ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS related_thought_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='related_thought_id' AND data_type='uuid') THEN + UPDATE public.learnings l SET related_thought_id_new = t.id FROM public.thoughts t WHERE t.guid = l.related_thought_id AND l.related_thought_id_new IS NULL; + END IF; +END $$; -- learnings.related_skill_id (uuid → bigint via agent_skills.guid) -ALTER TABLE public.learnings ADD COLUMN related_skill_id_new bigint; -UPDATE public.learnings l SET related_skill_id_new = s.id FROM public.agent_skills s WHERE s.guid = l.related_skill_id; +ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS related_skill_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='related_skill_id' AND data_type='uuid') THEN + UPDATE public.learnings l SET related_skill_id_new = s.id FROM public.agent_skills s WHERE s.guid = l.related_skill_id AND l.related_skill_id_new IS NULL; + END IF; +END $$; -- learnings.duplicate_of_learning_id (uuid → bigint via learnings.guid) -ALTER TABLE public.learnings ADD COLUMN duplicate_of_learning_id_new bigint; -UPDATE public.learnings l SET duplicate_of_learning_id_new = l2.id FROM public.learnings l2 WHERE l2.guid = l.duplicate_of_learning_id; +ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS duplicate_of_learning_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='duplicate_of_learning_id' AND data_type='uuid') THEN + UPDATE public.learnings l SET duplicate_of_learning_id_new = l2.id FROM public.learnings l2 WHERE l2.guid = l.duplicate_of_learning_id AND l.duplicate_of_learning_id_new IS NULL; + END IF; +END $$; -- learnings.supersedes_learning_id (uuid → bigint via learnings.guid) -ALTER TABLE public.learnings ADD COLUMN supersedes_learning_id_new bigint; -UPDATE public.learnings l SET supersedes_learning_id_new = l2.id FROM public.learnings l2 WHERE l2.guid = l.supersedes_learning_id; +ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS supersedes_learning_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='supersedes_learning_id' AND data_type='uuid') THEN + UPDATE public.learnings l SET supersedes_learning_id_new = l2.id FROM public.learnings l2 WHERE l2.guid = l.supersedes_learning_id AND l.supersedes_learning_id_new IS NULL; + END IF; +END $$; -- plans.project_id (uuid → bigint via projects.guid) -ALTER TABLE public.plans ADD COLUMN project_id_new bigint; -UPDATE public.plans pl SET project_id_new = p.id FROM public.projects p WHERE p.guid = pl.project_id; +ALTER TABLE public.plans ADD COLUMN IF NOT EXISTS project_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='project_id' AND data_type='uuid') THEN + UPDATE public.plans pl SET project_id_new = p.id FROM public.projects p WHERE p.guid = pl.project_id AND pl.project_id_new IS NULL; + END IF; +END $$; -- plans.supersedes_plan_id (uuid → bigint via plans.guid) -ALTER TABLE public.plans ADD COLUMN supersedes_plan_id_new bigint; -UPDATE public.plans pl SET supersedes_plan_id_new = pl2.id FROM public.plans pl2 WHERE pl2.guid = pl.supersedes_plan_id; +ALTER TABLE public.plans ADD COLUMN IF NOT EXISTS supersedes_plan_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='supersedes_plan_id' AND data_type='uuid') THEN + UPDATE public.plans pl SET supersedes_plan_id_new = pl2.id FROM public.plans pl2 WHERE pl2.guid = pl.supersedes_plan_id AND pl.supersedes_plan_id_new IS NULL; + END IF; +END $$; -- plan_dependencies.plan_id (uuid → bigint via plans.guid) -ALTER TABLE public.plan_dependencies ADD COLUMN plan_id_new bigint; -UPDATE public.plan_dependencies pd SET plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = pd.plan_id; +ALTER TABLE public.plan_dependencies ADD COLUMN IF NOT EXISTS plan_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_dependencies' AND column_name='plan_id' AND data_type='uuid') THEN + UPDATE public.plan_dependencies pd SET plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = pd.plan_id AND pd.plan_id_new IS NULL; + END IF; +END $$; -- plan_dependencies.depends_on_plan_id (uuid → bigint via plans.guid) -ALTER TABLE public.plan_dependencies ADD COLUMN depends_on_plan_id_new bigint; -UPDATE public.plan_dependencies pd SET depends_on_plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = pd.depends_on_plan_id; +ALTER TABLE public.plan_dependencies ADD COLUMN IF NOT EXISTS depends_on_plan_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_dependencies' AND column_name='depends_on_plan_id' AND data_type='uuid') THEN + UPDATE public.plan_dependencies pd SET depends_on_plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = pd.depends_on_plan_id AND pd.depends_on_plan_id_new IS NULL; + END IF; +END $$; -- plan_related_plans.plan_a_id (uuid → bigint via plans.guid) -ALTER TABLE public.plan_related_plans ADD COLUMN plan_a_id_new bigint; -UPDATE public.plan_related_plans pr SET plan_a_id_new = pl.id FROM public.plans pl WHERE pl.guid = pr.plan_a_id; +ALTER TABLE public.plan_related_plans ADD COLUMN IF NOT EXISTS plan_a_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_related_plans' AND column_name='plan_a_id' AND data_type='uuid') THEN + UPDATE public.plan_related_plans pr SET plan_a_id_new = pl.id FROM public.plans pl WHERE pl.guid = pr.plan_a_id AND pr.plan_a_id_new IS NULL; + END IF; +END $$; -- plan_related_plans.plan_b_id (uuid → bigint via plans.guid) -ALTER TABLE public.plan_related_plans ADD COLUMN plan_b_id_new bigint; -UPDATE public.plan_related_plans pr SET plan_b_id_new = pl.id FROM public.plans pl WHERE pl.guid = pr.plan_b_id; +ALTER TABLE public.plan_related_plans ADD COLUMN IF NOT EXISTS plan_b_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_related_plans' AND column_name='plan_b_id' AND data_type='uuid') THEN + UPDATE public.plan_related_plans pr SET plan_b_id_new = pl.id FROM public.plans pl WHERE pl.guid = pr.plan_b_id AND pr.plan_b_id_new IS NULL; + END IF; +END $$; -- plan_skills.plan_id (uuid → bigint via plans.guid) -ALTER TABLE public.plan_skills ADD COLUMN plan_id_new bigint; -UPDATE public.plan_skills ps SET plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = ps.plan_id; +ALTER TABLE public.plan_skills ADD COLUMN IF NOT EXISTS plan_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_skills' AND column_name='plan_id' AND data_type='uuid') THEN + UPDATE public.plan_skills ps SET plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = ps.plan_id AND ps.plan_id_new IS NULL; + END IF; +END $$; -- plan_skills.skill_id (uuid → bigint via agent_skills.guid) -ALTER TABLE public.plan_skills ADD COLUMN skill_id_new bigint; -UPDATE public.plan_skills ps SET skill_id_new = s.id FROM public.agent_skills s WHERE s.guid = ps.skill_id; +ALTER TABLE public.plan_skills ADD COLUMN IF NOT EXISTS skill_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_skills' AND column_name='skill_id' AND data_type='uuid') THEN + UPDATE public.plan_skills ps SET skill_id_new = s.id FROM public.agent_skills s WHERE s.guid = ps.skill_id AND ps.skill_id_new IS NULL; + END IF; +END $$; -- plan_guardrails.plan_id (uuid → bigint via plans.guid) -ALTER TABLE public.plan_guardrails ADD COLUMN plan_id_new bigint; -UPDATE public.plan_guardrails pg SET plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = pg.plan_id; +ALTER TABLE public.plan_guardrails ADD COLUMN IF NOT EXISTS plan_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_guardrails' AND column_name='plan_id' AND data_type='uuid') THEN + UPDATE public.plan_guardrails pg SET plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = pg.plan_id AND pg.plan_id_new IS NULL; + END IF; +END $$; -- plan_guardrails.guardrail_id (uuid → bigint via agent_guardrails.guid) -ALTER TABLE public.plan_guardrails ADD COLUMN guardrail_id_new bigint; -UPDATE public.plan_guardrails pg SET guardrail_id_new = g.id FROM public.agent_guardrails g WHERE g.guid = pg.guardrail_id; +ALTER TABLE public.plan_guardrails ADD COLUMN IF NOT EXISTS guardrail_id_new bigint; +DO $$ BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_guardrails' AND column_name='guardrail_id' AND data_type='uuid') THEN + UPDATE public.plan_guardrails pg SET guardrail_id_new = g.id FROM public.agent_guardrails g WHERE g.guid = pg.guardrail_id AND pg.guardrail_id_new IS NULL; + END IF; +END $$; -- ============================================================ -- STEP 6: Drop old uuid FK columns -- ============================================================ -ALTER TABLE public.thoughts DROP COLUMN project_id; -ALTER TABLE public.embeddings DROP COLUMN thought_id; -ALTER TABLE public.stored_files DROP COLUMN thought_id; -ALTER TABLE public.stored_files DROP COLUMN project_id; -ALTER TABLE public.project_skills DROP COLUMN project_id; -ALTER TABLE public.project_skills DROP COLUMN skill_id; -ALTER TABLE public.project_guardrails DROP COLUMN project_id; -ALTER TABLE public.project_guardrails DROP COLUMN guardrail_id; -ALTER TABLE public.chat_histories DROP COLUMN project_id; -ALTER TABLE public.learnings DROP COLUMN project_id; -ALTER TABLE public.learnings DROP COLUMN related_thought_id; -ALTER TABLE public.learnings DROP COLUMN related_skill_id; -ALTER TABLE public.learnings DROP COLUMN duplicate_of_learning_id; -ALTER TABLE public.learnings DROP COLUMN supersedes_learning_id; -ALTER TABLE public.plans DROP COLUMN project_id; -ALTER TABLE public.plans DROP COLUMN supersedes_plan_id; -ALTER TABLE public.plan_dependencies DROP COLUMN plan_id; -ALTER TABLE public.plan_dependencies DROP COLUMN depends_on_plan_id; -ALTER TABLE public.plan_related_plans DROP COLUMN plan_a_id; -ALTER TABLE public.plan_related_plans DROP COLUMN plan_b_id; -ALTER TABLE public.plan_skills DROP COLUMN plan_id; -ALTER TABLE public.plan_skills DROP COLUMN skill_id; -ALTER TABLE public.plan_guardrails DROP COLUMN plan_id; -ALTER TABLE public.plan_guardrails DROP COLUMN guardrail_id; +ALTER TABLE public.thoughts DROP COLUMN IF EXISTS project_id; +ALTER TABLE public.embeddings DROP COLUMN IF EXISTS thought_id; +ALTER TABLE public.stored_files DROP COLUMN IF EXISTS thought_id; +ALTER TABLE public.stored_files DROP COLUMN IF EXISTS project_id; +ALTER TABLE public.project_skills DROP COLUMN IF EXISTS project_id; +ALTER TABLE public.project_skills DROP COLUMN IF EXISTS skill_id; +ALTER TABLE public.project_guardrails DROP COLUMN IF EXISTS project_id; +ALTER TABLE public.project_guardrails DROP COLUMN IF EXISTS guardrail_id; +ALTER TABLE public.chat_histories DROP COLUMN IF EXISTS project_id; +ALTER TABLE public.learnings DROP COLUMN IF EXISTS project_id; +ALTER TABLE public.learnings DROP COLUMN IF EXISTS related_thought_id; +ALTER TABLE public.learnings DROP COLUMN IF EXISTS related_skill_id; +ALTER TABLE public.learnings DROP COLUMN IF EXISTS duplicate_of_learning_id; +ALTER TABLE public.learnings DROP COLUMN IF EXISTS supersedes_learning_id; +ALTER TABLE public.plans DROP COLUMN IF EXISTS project_id; +ALTER TABLE public.plans DROP COLUMN IF EXISTS supersedes_plan_id; +ALTER TABLE public.plan_dependencies DROP COLUMN IF EXISTS plan_id; +ALTER TABLE public.plan_dependencies DROP COLUMN IF EXISTS depends_on_plan_id; +ALTER TABLE public.plan_related_plans DROP COLUMN IF EXISTS plan_a_id; +ALTER TABLE public.plan_related_plans DROP COLUMN IF EXISTS plan_b_id; +ALTER TABLE public.plan_skills DROP COLUMN IF EXISTS plan_id; +ALTER TABLE public.plan_skills DROP COLUMN IF EXISTS skill_id; +ALTER TABLE public.plan_guardrails DROP COLUMN IF EXISTS plan_id; +ALTER TABLE public.plan_guardrails DROP COLUMN IF EXISTS guardrail_id; -- ============================================================ --- STEP 7: Rename _new columns to final names +-- STEP 7: Rename _new columns to final names (idempotent) -- ============================================================ -ALTER TABLE public.thoughts RENAME COLUMN project_id_new TO project_id; -ALTER TABLE public.embeddings RENAME COLUMN thought_id_new TO thought_id; -ALTER TABLE public.stored_files RENAME COLUMN thought_id_new TO thought_id; -ALTER TABLE public.stored_files RENAME COLUMN project_id_new TO project_id; -ALTER TABLE public.project_skills RENAME COLUMN project_id_new TO project_id; -ALTER TABLE public.project_skills RENAME COLUMN skill_id_new TO skill_id; -ALTER TABLE public.project_guardrails RENAME COLUMN project_id_new TO project_id; -ALTER TABLE public.project_guardrails RENAME COLUMN guardrail_id_new TO guardrail_id; -ALTER TABLE public.chat_histories RENAME COLUMN project_id_new TO project_id; -ALTER TABLE public.learnings RENAME COLUMN project_id_new TO project_id; -ALTER TABLE public.learnings RENAME COLUMN related_thought_id_new TO related_thought_id; -ALTER TABLE public.learnings RENAME COLUMN related_skill_id_new TO related_skill_id; -ALTER TABLE public.learnings RENAME COLUMN duplicate_of_learning_id_new TO duplicate_of_learning_id; -ALTER TABLE public.learnings RENAME COLUMN supersedes_learning_id_new TO supersedes_learning_id; -ALTER TABLE public.plans RENAME COLUMN project_id_new TO project_id; -ALTER TABLE public.plans RENAME COLUMN supersedes_plan_id_new TO supersedes_plan_id; -ALTER TABLE public.plan_dependencies RENAME COLUMN plan_id_new TO plan_id; -ALTER TABLE public.plan_dependencies RENAME COLUMN depends_on_plan_id_new TO depends_on_plan_id; -ALTER TABLE public.plan_related_plans RENAME COLUMN plan_a_id_new TO plan_a_id; -ALTER TABLE public.plan_related_plans RENAME COLUMN plan_b_id_new TO plan_b_id; -ALTER TABLE public.plan_skills RENAME COLUMN plan_id_new TO plan_id; -ALTER TABLE public.plan_skills RENAME COLUMN skill_id_new TO skill_id; -ALTER TABLE public.plan_guardrails RENAME COLUMN plan_id_new TO plan_id; -ALTER TABLE public.plan_guardrails RENAME COLUMN guardrail_id_new TO guardrail_id; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='thoughts' AND column_name='project_id_new') THEN ALTER TABLE public.thoughts RENAME COLUMN project_id_new TO project_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='embeddings' AND column_name='thought_id_new') THEN ALTER TABLE public.embeddings RENAME COLUMN thought_id_new TO thought_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='stored_files' AND column_name='thought_id_new') THEN ALTER TABLE public.stored_files RENAME COLUMN thought_id_new TO thought_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='stored_files' AND column_name='project_id_new') THEN ALTER TABLE public.stored_files RENAME COLUMN project_id_new TO project_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_skills' AND column_name='project_id_new') THEN ALTER TABLE public.project_skills RENAME COLUMN project_id_new TO project_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_skills' AND column_name='skill_id_new') THEN ALTER TABLE public.project_skills RENAME COLUMN skill_id_new TO skill_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_guardrails' AND column_name='project_id_new') THEN ALTER TABLE public.project_guardrails RENAME COLUMN project_id_new TO project_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_guardrails' AND column_name='guardrail_id_new') THEN ALTER TABLE public.project_guardrails RENAME COLUMN guardrail_id_new TO guardrail_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='chat_histories' AND column_name='project_id_new') THEN ALTER TABLE public.chat_histories RENAME COLUMN project_id_new TO project_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='project_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN project_id_new TO project_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='related_thought_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN related_thought_id_new TO related_thought_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='related_skill_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN related_skill_id_new TO related_skill_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='duplicate_of_learning_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN duplicate_of_learning_id_new TO duplicate_of_learning_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='supersedes_learning_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN supersedes_learning_id_new TO supersedes_learning_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='project_id_new') THEN ALTER TABLE public.plans RENAME COLUMN project_id_new TO project_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='supersedes_plan_id_new') THEN ALTER TABLE public.plans RENAME COLUMN supersedes_plan_id_new TO supersedes_plan_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_dependencies' AND column_name='plan_id_new') THEN ALTER TABLE public.plan_dependencies RENAME COLUMN plan_id_new TO plan_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_dependencies' AND column_name='depends_on_plan_id_new') THEN ALTER TABLE public.plan_dependencies RENAME COLUMN depends_on_plan_id_new TO depends_on_plan_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_related_plans' AND column_name='plan_a_id_new') THEN ALTER TABLE public.plan_related_plans RENAME COLUMN plan_a_id_new TO plan_a_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_related_plans' AND column_name='plan_b_id_new') THEN ALTER TABLE public.plan_related_plans RENAME COLUMN plan_b_id_new TO plan_b_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_skills' AND column_name='plan_id_new') THEN ALTER TABLE public.plan_skills RENAME COLUMN plan_id_new TO plan_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_skills' AND column_name='skill_id_new') THEN ALTER TABLE public.plan_skills RENAME COLUMN skill_id_new TO skill_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_guardrails' AND column_name='plan_id_new') THEN ALTER TABLE public.plan_guardrails RENAME COLUMN plan_id_new TO plan_id; END IF; END $$; +DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_guardrails' AND column_name='guardrail_id_new') THEN ALTER TABLE public.plan_guardrails RENAME COLUMN guardrail_id_new TO guardrail_id; END IF; END $$; -- ============================================================ -- STEP 8: Upgrade serial → bigserial (alter sequence type) @@ -258,6 +415,12 @@ ALTER TABLE public.project_guardrails ALTER COLUMN id TYPE bigint; -- STEP 9: Add PK constraints to formerly uuid-pk tables -- ============================================================ +ALTER TABLE public.agent_skills DROP CONSTRAINT IF EXISTS agent_skills_pkey; +ALTER TABLE public.agent_guardrails DROP CONSTRAINT IF EXISTS agent_guardrails_pkey; +ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS chat_histories_pkey; +ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_pkey; +ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS plans_pkey; + ALTER TABLE public.agent_skills ADD CONSTRAINT agent_skills_pkey PRIMARY KEY (id); ALTER TABLE public.agent_guardrails ADD CONSTRAINT agent_guardrails_pkey PRIMARY KEY (id); ALTER TABLE public.chat_histories ADD CONSTRAINT chat_histories_pkey PRIMARY KEY (id); @@ -265,74 +428,39 @@ ALTER TABLE public.learnings ADD CONSTRAINT learnings_pkey PRIMARY KEY (id); ALTER TABLE public.plans ADD CONSTRAINT plans_pkey PRIMARY KEY (id); -- Add unique constraint on guid columns -ALTER TABLE public.agent_skills ADD CONSTRAINT agent_skills_guid_key UNIQUE (guid); -ALTER TABLE public.agent_guardrails ADD CONSTRAINT agent_guardrails_guid_key UNIQUE (guid); -ALTER TABLE public.chat_histories ADD CONSTRAINT chat_histories_guid_key UNIQUE (guid); -ALTER TABLE public.learnings ADD CONSTRAINT learnings_guid_key UNIQUE (guid); -ALTER TABLE public.plans ADD CONSTRAINT plans_guid_key UNIQUE (guid); +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='agent_skills' AND constraint_name='agent_skills_guid_key') THEN ALTER TABLE public.agent_skills ADD CONSTRAINT agent_skills_guid_key UNIQUE (guid); END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='agent_guardrails' AND constraint_name='agent_guardrails_guid_key') THEN ALTER TABLE public.agent_guardrails ADD CONSTRAINT agent_guardrails_guid_key UNIQUE (guid); END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='chat_histories' AND constraint_name='chat_histories_guid_key') THEN ALTER TABLE public.chat_histories ADD CONSTRAINT chat_histories_guid_key UNIQUE (guid); END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_guid_key') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_guid_key UNIQUE (guid); END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plans' AND constraint_name='plans_guid_key') THEN ALTER TABLE public.plans ADD CONSTRAINT plans_guid_key UNIQUE (guid); END IF; END $$; -- ============================================================ -- STEP 10: Re-add FK constraints -- ============================================================ -ALTER TABLE public.thoughts - ADD CONSTRAINT thoughts_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id); - -ALTER TABLE public.embeddings - ADD CONSTRAINT embeddings_thought_id_fkey FOREIGN KEY (thought_id) REFERENCES public.thoughts(id); - -ALTER TABLE public.stored_files - ADD CONSTRAINT stored_files_thought_id_fkey FOREIGN KEY (thought_id) REFERENCES public.thoughts(id); -ALTER TABLE public.stored_files - ADD CONSTRAINT stored_files_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id); - -ALTER TABLE public.project_skills - ADD CONSTRAINT project_skills_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE; -ALTER TABLE public.project_skills - ADD CONSTRAINT project_skills_skill_id_fkey FOREIGN KEY (skill_id) REFERENCES public.agent_skills(id) ON DELETE CASCADE; - -ALTER TABLE public.project_guardrails - ADD CONSTRAINT project_guardrails_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE; -ALTER TABLE public.project_guardrails - ADD CONSTRAINT project_guardrails_guardrail_id_fkey FOREIGN KEY (guardrail_id) REFERENCES public.agent_guardrails(id) ON DELETE CASCADE; - -ALTER TABLE public.chat_histories - ADD CONSTRAINT chat_histories_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE SET NULL; - -ALTER TABLE public.learnings - ADD CONSTRAINT learnings_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE SET NULL; -ALTER TABLE public.learnings - ADD CONSTRAINT learnings_related_thought_id_fkey FOREIGN KEY (related_thought_id) REFERENCES public.thoughts(id) ON DELETE SET NULL; -ALTER TABLE public.learnings - ADD CONSTRAINT learnings_related_skill_id_fkey FOREIGN KEY (related_skill_id) REFERENCES public.agent_skills(id) ON DELETE SET NULL; -ALTER TABLE public.learnings - ADD CONSTRAINT learnings_duplicate_of_learning_id_fkey FOREIGN KEY (duplicate_of_learning_id) REFERENCES public.learnings(id) ON DELETE SET NULL; -ALTER TABLE public.learnings - ADD CONSTRAINT learnings_supersedes_learning_id_fkey FOREIGN KEY (supersedes_learning_id) REFERENCES public.learnings(id) ON DELETE SET NULL; - -ALTER TABLE public.plans - ADD CONSTRAINT plans_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE SET NULL; -ALTER TABLE public.plans - ADD CONSTRAINT plans_supersedes_plan_id_fkey FOREIGN KEY (supersedes_plan_id) REFERENCES public.plans(id) ON DELETE SET NULL; - -ALTER TABLE public.plan_dependencies - ADD CONSTRAINT plan_dependencies_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; -ALTER TABLE public.plan_dependencies - ADD CONSTRAINT plan_dependencies_depends_on_plan_id_fkey FOREIGN KEY (depends_on_plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; - -ALTER TABLE public.plan_related_plans - ADD CONSTRAINT plan_related_plans_plan_a_id_fkey FOREIGN KEY (plan_a_id) REFERENCES public.plans(id) ON DELETE CASCADE; -ALTER TABLE public.plan_related_plans - ADD CONSTRAINT plan_related_plans_plan_b_id_fkey FOREIGN KEY (plan_b_id) REFERENCES public.plans(id) ON DELETE CASCADE; - -ALTER TABLE public.plan_skills - ADD CONSTRAINT plan_skills_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; -ALTER TABLE public.plan_skills - ADD CONSTRAINT plan_skills_skill_id_fkey FOREIGN KEY (skill_id) REFERENCES public.agent_skills(id) ON DELETE CASCADE; - -ALTER TABLE public.plan_guardrails - ADD CONSTRAINT plan_guardrails_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; -ALTER TABLE public.plan_guardrails - ADD CONSTRAINT plan_guardrails_guardrail_id_fkey FOREIGN KEY (guardrail_id) REFERENCES public.agent_guardrails(id) ON DELETE CASCADE; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='thoughts' AND constraint_name='thoughts_project_id_fkey') THEN ALTER TABLE public.thoughts ADD CONSTRAINT thoughts_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id); END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='embeddings' AND constraint_name='embeddings_thought_id_fkey') THEN ALTER TABLE public.embeddings ADD CONSTRAINT embeddings_thought_id_fkey FOREIGN KEY (thought_id) REFERENCES public.thoughts(id); END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='stored_files' AND constraint_name='stored_files_thought_id_fkey') THEN ALTER TABLE public.stored_files ADD CONSTRAINT stored_files_thought_id_fkey FOREIGN KEY (thought_id) REFERENCES public.thoughts(id); END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='stored_files' AND constraint_name='stored_files_project_id_fkey') THEN ALTER TABLE public.stored_files ADD CONSTRAINT stored_files_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id); END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='project_skills' AND constraint_name='project_skills_project_id_fkey') THEN ALTER TABLE public.project_skills ADD CONSTRAINT project_skills_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE; END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='project_skills' AND constraint_name='project_skills_skill_id_fkey') THEN ALTER TABLE public.project_skills ADD CONSTRAINT project_skills_skill_id_fkey FOREIGN KEY (skill_id) REFERENCES public.agent_skills(id) ON DELETE CASCADE; END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='project_guardrails' AND constraint_name='project_guardrails_project_id_fkey') THEN ALTER TABLE public.project_guardrails ADD CONSTRAINT project_guardrails_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE; END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='project_guardrails' AND constraint_name='project_guardrails_guardrail_id_fkey') THEN ALTER TABLE public.project_guardrails ADD CONSTRAINT project_guardrails_guardrail_id_fkey FOREIGN KEY (guardrail_id) REFERENCES public.agent_guardrails(id) ON DELETE CASCADE; END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='chat_histories' AND constraint_name='chat_histories_project_id_fkey') THEN ALTER TABLE public.chat_histories ADD CONSTRAINT chat_histories_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE SET NULL; END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_project_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE SET NULL; END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_related_thought_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_related_thought_id_fkey FOREIGN KEY (related_thought_id) REFERENCES public.thoughts(id) ON DELETE SET NULL; END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_related_skill_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_related_skill_id_fkey FOREIGN KEY (related_skill_id) REFERENCES public.agent_skills(id) ON DELETE SET NULL; END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_duplicate_of_learning_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_duplicate_of_learning_id_fkey FOREIGN KEY (duplicate_of_learning_id) REFERENCES public.learnings(id) ON DELETE SET NULL; END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_supersedes_learning_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_supersedes_learning_id_fkey FOREIGN KEY (supersedes_learning_id) REFERENCES public.learnings(id) ON DELETE SET NULL; END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plans' AND constraint_name='plans_project_id_fkey') THEN ALTER TABLE public.plans ADD CONSTRAINT plans_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE SET NULL; END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plans' AND constraint_name='plans_supersedes_plan_id_fkey') THEN ALTER TABLE public.plans ADD CONSTRAINT plans_supersedes_plan_id_fkey FOREIGN KEY (supersedes_plan_id) REFERENCES public.plans(id) ON DELETE SET NULL; END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_dependencies' AND constraint_name='plan_dependencies_plan_id_fkey') THEN ALTER TABLE public.plan_dependencies ADD CONSTRAINT plan_dependencies_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_dependencies' AND constraint_name='plan_dependencies_depends_on_plan_id_fkey') THEN ALTER TABLE public.plan_dependencies ADD CONSTRAINT plan_dependencies_depends_on_plan_id_fkey FOREIGN KEY (depends_on_plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_related_plans' AND constraint_name='plan_related_plans_plan_a_id_fkey') THEN ALTER TABLE public.plan_related_plans ADD CONSTRAINT plan_related_plans_plan_a_id_fkey FOREIGN KEY (plan_a_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_related_plans' AND constraint_name='plan_related_plans_plan_b_id_fkey') THEN ALTER TABLE public.plan_related_plans ADD CONSTRAINT plan_related_plans_plan_b_id_fkey FOREIGN KEY (plan_b_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_skills' AND constraint_name='plan_skills_plan_id_fkey') THEN ALTER TABLE public.plan_skills ADD CONSTRAINT plan_skills_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_skills' AND constraint_name='plan_skills_skill_id_fkey') THEN ALTER TABLE public.plan_skills ADD CONSTRAINT plan_skills_skill_id_fkey FOREIGN KEY (skill_id) REFERENCES public.agent_skills(id) ON DELETE CASCADE; END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_guardrails' AND constraint_name='plan_guardrails_plan_id_fkey') THEN ALTER TABLE public.plan_guardrails ADD CONSTRAINT plan_guardrails_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$; +DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_guardrails' AND constraint_name='plan_guardrails_guardrail_id_fkey') THEN ALTER TABLE public.plan_guardrails ADD CONSTRAINT plan_guardrails_guardrail_id_fkey FOREIGN KEY (guardrail_id) REFERENCES public.agent_guardrails(id) ON DELETE CASCADE; END IF; END $$; COMMIT; diff --git a/migrations/020_generated_schema.sql b/migrations/020_generated_schema.sql index 726fb38..0f868f4 100644 --- a/migrations/020_generated_schema.sql +++ b/migrations/020_generated_schema.sql @@ -129,7 +129,7 @@ CREATE TABLE IF NOT EXISTS public.thoughts ( created_at timestamptz DEFAULT now(), guid uuid NOT NULL DEFAULT gen_random_uuid(), id bigserial NOT NULL, - metadata jsonb DEFAULT '''{}''::jsonb', + metadata jsonb DEFAULT '{}'::jsonb, project_id bigint, updated_at timestamptz DEFAULT now() ); @@ -184,8 +184,8 @@ CREATE TABLE IF NOT EXISTS public.chat_histories ( 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 '''{}''', + messages jsonb NOT NULL DEFAULT '[]', + metadata jsonb NOT NULL DEFAULT '{}', project_id bigint, session_id text NOT NULL, summary text, @@ -222,7 +222,7 @@ CREATE TABLE IF NOT EXISTS public.learnings ( status text NOT NULL DEFAULT 'pending', summary text NOT NULL, supersedes_learning_id bigint, - tags text, + tags text[] NOT NULL DEFAULT '{}', updated_at timestamptz NOT NULL DEFAULT now() ); @@ -240,7 +240,7 @@ CREATE TABLE IF NOT EXISTS public.plans ( reviewed_by text, status text NOT NULL DEFAULT 'draft', supersedes_plan_id bigint, - tags text, + tags text[] NOT NULL DEFAULT '{}', title text NOT NULL, updated_at timestamptz NOT NULL DEFAULT now() ); @@ -280,7 +280,7 @@ CREATE TABLE IF NOT EXISTS public.agent_skills ( guid uuid NOT NULL DEFAULT gen_random_uuid(), id bigserial NOT NULL, name text NOT NULL, - tags text, + tags text[] NOT NULL DEFAULT '{}', updated_at timestamptz NOT NULL DEFAULT now() ); @@ -292,7 +292,7 @@ CREATE TABLE IF NOT EXISTS public.agent_guardrails ( id bigserial NOT NULL, name text NOT NULL, severity text NOT NULL DEFAULT 'medium', - tags text, + tags text[] NOT NULL DEFAULT '{}', updated_at timestamptz NOT NULL DEFAULT now() ); @@ -384,7 +384,7 @@ BEGIN AND table_name = 'thoughts' AND column_name = 'metadata' ) THEN - ALTER TABLE public.thoughts ADD COLUMN metadata jsonb DEFAULT '''{}''::jsonb'; + ALTER TABLE public.thoughts ADD COLUMN metadata jsonb DEFAULT '{}'::jsonb; END IF; END; $$; @@ -904,7 +904,7 @@ BEGIN AND table_name = 'chat_histories' AND column_name = 'messages' ) THEN - ALTER TABLE public.chat_histories ADD COLUMN messages jsonb NOT NULL DEFAULT '''['; + ALTER TABLE public.chat_histories ADD COLUMN messages jsonb NOT NULL DEFAULT '[]'; END IF; END; $$; @@ -917,7 +917,7 @@ BEGIN AND table_name = 'chat_histories' AND column_name = 'metadata' ) THEN - ALTER TABLE public.chat_histories ADD COLUMN metadata jsonb NOT NULL DEFAULT '''{}'''; + ALTER TABLE public.chat_histories ADD COLUMN metadata jsonb NOT NULL DEFAULT '{}'; END IF; END; $$; @@ -1320,7 +1320,7 @@ BEGIN AND table_name = 'learnings' AND column_name = 'tags' ) THEN - ALTER TABLE public.learnings ADD COLUMN tags text; + ALTER TABLE public.learnings ADD COLUMN tags text[] NOT NULL DEFAULT '{}'; END IF; END; $$; @@ -1515,7 +1515,7 @@ BEGIN AND table_name = 'plans' AND column_name = 'tags' ) THEN - ALTER TABLE public.plans ADD COLUMN tags text; + ALTER TABLE public.plans ADD COLUMN tags text[] NOT NULL DEFAULT '{}'; END IF; END; $$; @@ -1840,7 +1840,7 @@ BEGIN AND table_name = 'agent_skills' AND column_name = 'tags' ) THEN - ALTER TABLE public.agent_skills ADD COLUMN tags text; + ALTER TABLE public.agent_skills ADD COLUMN tags text[] NOT NULL DEFAULT '{}'; END IF; END; $$; @@ -1957,7 +1957,7 @@ BEGIN AND table_name = 'agent_guardrails' AND column_name = 'tags' ) THEN - ALTER TABLE public.agent_guardrails ADD COLUMN tags text; + ALTER TABLE public.agent_guardrails ADD COLUMN tags text[] NOT NULL DEFAULT '{}'; END IF; END; $$; @@ -2570,10 +2570,10 @@ CREATE INDEX IF NOT EXISTS idx_learnings_summary ON public.learnings USING gin (summary gin_trgm_ops); CREATE INDEX IF NOT EXISTS idx_learnings_tags - ON public.learnings USING gin (tags gin_trgm_ops); + ON public.learnings USING gin (tags); CREATE INDEX IF NOT EXISTS idx_plans_tags - ON public.plans USING gin (tags gin_trgm_ops); + ON public.plans USING gin (tags); CREATE INDEX IF NOT EXISTS idx_plans_title ON public.plans USING gin (title gin_trgm_ops);