- models/agentdecision.go: AgentDecision GORM model (context/decision as jsonb, reasoning as text) - migrations/migrate.go: AutoMigrate AgentDecision then ALTER TABLE to add vector(1536) column and CREATE ivfflat index via raw SQL - controllers/agentDecisionController.go: CreateAgentDecision, FindSimilarDecisions (cosine distance), UpdateDecisionOutcome - routes/routes.go: three routes under /api/v1/internal (InternalKeyAuth applied at group level) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
67 lines
1.8 KiB
Go
67 lines
1.8 KiB
Go
package migrations
|
|
|
|
import (
|
|
"doormile/models"
|
|
"doormile/utils"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func Migrate(db *gorm.DB) error {
|
|
utils.Info("Starting database auto-migrations for 21 logistics tables...")
|
|
|
|
err := db.AutoMigrate(
|
|
&models.PartnerInfo{},
|
|
&models.Hub{},
|
|
&models.Vehicle{},
|
|
&models.AppUser{},
|
|
&models.MilerProfile{},
|
|
&models.AppCustomer{},
|
|
&models.AppCustomerLocation{},
|
|
&models.Consignment{},
|
|
&models.PickupBooking{},
|
|
&models.BookingParcel{},
|
|
&models.BookingServiceOption{},
|
|
&models.BookingPayment{},
|
|
&models.BookingAssignment{},
|
|
&models.BookingVehicleRequirement{},
|
|
&models.Tripsheet{},
|
|
&models.TripsheetItem{},
|
|
&models.DeliveryProof{},
|
|
&models.Pricing{},
|
|
&models.ConsignmentHistory{},
|
|
&models.ConsignmentException{},
|
|
&models.TenantLocation{},
|
|
&models.AppLocation{},
|
|
&models.Tenant{},
|
|
&models.Customer{},
|
|
&models.CustomerLocation{},
|
|
&models.DoormileClient{},
|
|
&models.DoormileAuth{},
|
|
&models.CompetitorBranch{},
|
|
&models.CarrierPricing{},
|
|
&models.DoormilePricing{},
|
|
&models.AgentDecision{},
|
|
)
|
|
|
|
if err != nil {
|
|
utils.Error("❌ Database migration failed", "error", err)
|
|
return err
|
|
}
|
|
|
|
utils.Info("✅ Database migration completed successfully!")
|
|
|
|
if res := db.Exec(`ALTER TABLE agent_decisions ADD COLUMN IF NOT EXISTS context_embedding vector(1536)`); res.Error != nil {
|
|
utils.Error("❌ Failed to add context_embedding column", "error", res.Error)
|
|
} else {
|
|
utils.Info("✅ context_embedding vector column ready")
|
|
}
|
|
|
|
if res := db.Exec(`CREATE INDEX IF NOT EXISTS idx_agent_decisions_embedding ON agent_decisions USING ivfflat (context_embedding vector_cosine_ops) WITH (lists = 100)`); res.Error != nil {
|
|
utils.Error("❌ Failed to create ivfflat index on context_embedding", "error", res.Error)
|
|
} else {
|
|
utils.Info("✅ ivfflat index on context_embedding ready")
|
|
}
|
|
|
|
return nil
|
|
}
|