fix(#149): add purchase match work item migration
This commit is contained in:
+27
@@ -0,0 +1,27 @@
|
||||
package version_local
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
|
||||
goautomigrations "go-admin/app/goauto/migrations"
|
||||
"go-admin/cmd/migrate/migration"
|
||||
common "go-admin/common/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// #149 completes the additive #148 schema change for databases whose earlier
|
||||
// migration versions have already been recorded.
|
||||
func init() {
|
||||
_, fileName, _, _ := runtime.Caller(0)
|
||||
migration.Migrate.SetVersion(migration.GetFilename(fileName), migratePurchaseSpecMatchWorkItem)
|
||||
}
|
||||
|
||||
func migratePurchaseSpecMatchWorkItem(db *gorm.DB, version string) error {
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := goautomigrations.Migrate(tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Create(&common.Migration{Version: version}).Error
|
||||
})
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package version_local
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
goautomigrations "go-admin/app/goauto/migrations"
|
||||
"go-admin/app/goauto/models"
|
||||
common "go-admin/common/models"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestMigratePurchaseSpecMatchWorkItemUpgradesExistingSchema(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open("file:purchase-match-work-item-upgrade?mode=memory&cache=shared"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
oldModels := make([]any, 0, len(goautomigrations.MigratedModels())-1)
|
||||
for _, model := range goautomigrations.MigratedModels() {
|
||||
if _, isNewModel := model.(*models.PurchaseSpecMatchWorkItem); !isNewModel {
|
||||
oldModels = append(oldModels, model)
|
||||
}
|
||||
}
|
||||
if err = db.AutoMigrate(oldModels...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = db.AutoMigrate(&common.Migration{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = db.Create(&common.Migration{Version: "1787885600000"}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if db.Migrator().HasTable(&models.PurchaseSpecMatchWorkItem{}) {
|
||||
t.Fatal("upgrade precondition failed: purchase match work item table already exists")
|
||||
}
|
||||
|
||||
const version = "1787983400000"
|
||||
if err = migratePurchaseSpecMatchWorkItem(db, version); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !db.Migrator().HasTable(&models.PurchaseSpecMatchWorkItem{}) {
|
||||
t.Fatal("purchase match work item table was not created")
|
||||
}
|
||||
for _, index := range []string{
|
||||
"ux_purchase_match_task",
|
||||
"idx_purchase_spec_match_work_item_status",
|
||||
"idx_purchase_spec_match_work_item_next_attempt_at",
|
||||
"idx_purchase_spec_match_work_item_lease_expires_at",
|
||||
} {
|
||||
if !db.Migrator().HasIndex(&models.PurchaseSpecMatchWorkItem{}, index) {
|
||||
t.Fatalf("expected index %s was not created", index)
|
||||
}
|
||||
}
|
||||
var count int64
|
||||
if err = db.Model(&common.Migration{}).Where("version = ?", version).Count(&count).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatalf("migration version record count = %d, want 1", count)
|
||||
}
|
||||
if err = goautomigrations.VerifyTables(db); err != nil {
|
||||
t.Fatalf("schema verification failed after upgrade: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user