diff --git a/migrations/migrate.go b/migrations/migrate.go index 9042010..e26f5b7 100644 --- a/migrations/migrate.go +++ b/migrations/migrate.go @@ -79,5 +79,20 @@ func Migrate(db *gorm.DB) error { utils.Info("✅ tripsheets_status_check constraint includes Ready") } + // The consignments.status CHECK constraint also predates this codebase (not + // derived from a gorm tag) and was missing two values the code actually + // writes: Collected_By_Miler — the hyperlocal two-step intermediate state, + // which a pickup-complete writes when MILER_COLLECTED_STATE_ENABLED is on — + // and Cancelled, which the admin console sets via PUT /consignments/:id/status. + // Without them the INSERT/UPDATE fails with a 23514 check violation. Widen it + // to the full set of constants.Consignment* statuses plus Cancelled. + db.Exec(`ALTER TABLE consignments DROP CONSTRAINT IF EXISTS consignments_status_check`) + if res := db.Exec(`ALTER TABLE consignments ADD CONSTRAINT consignments_status_check + CHECK (status::text = ANY (ARRAY['Created','Inwarded_at_Hub','Collected_By_Miler','Tripsheet_Loaded','In_Transit','Out_for_Delivery','Delivered','RTO_Initiated','Returned_to_Sender','Missing','Damaged','Cancelled']::text[]))`); res.Error != nil { + utils.Error("❌ Failed to widen consignments_status_check constraint", "error", res.Error) + } else { + utils.Info("✅ consignments_status_check constraint includes Collected_By_Miler") + } + return nil }