fix: validate explicit migrations and preserve large business ids
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package business
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMySQLPromoteCapacityPreservesIDsAndNextValue(t *testing.T) {
|
||||
db := openRehearsalMySQL(t)
|
||||
if err := db.Exec("CREATE TABLE tab_promote(id INT NOT NULL AUTO_INCREMENT COMMENT '渠道主键',source_db VARCHAR(10) NOT NULL,PRIMARY KEY(id,source_db)) AUTO_INCREMENT=2147483647").Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Exec("INSERT INTO tab_promote(id,source_db) VALUES(29701,'lx'),(29701,'hw')").Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
if err := migratePromoteIDCapacity(db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
var next uint64
|
||||
if err := db.Raw("SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_schema=DATABASE() AND table_name='tab_promote'").Scan(&next).Error; err != nil || next != 2147483647 {
|
||||
t.Fatalf("high-water mark changed: %d, %v", next, err)
|
||||
}
|
||||
if err := db.Exec("INSERT INTO tab_promote(source_db) VALUES('lx'),('lx')").Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ids []uint64
|
||||
if err := db.Table("tab_promote").Order("id").Pluck("id", &ids).Error; err != nil || len(ids) != 4 || ids[0] != 29701 || ids[1] != 29701 || ids[2] != 2147483647 || ids[3] != 2147483648 {
|
||||
t.Fatalf("IDs changed or overflowed: %v, %v", ids, err)
|
||||
}
|
||||
var comment string
|
||||
if err := db.Raw("SELECT column_comment FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='tab_promote' AND column_name='id'").Scan(&comment).Error; err != nil || comment != "渠道主键" {
|
||||
t.Fatalf("comment changed: %q, %v", comment, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBindingStatusIndexRepairsAnExistingLegacyTable(t *testing.T) {
|
||||
db := openRehearsalMySQL(t)
|
||||
const index = "idx_sys_user_bind_platform_status"
|
||||
if err := db.Migrator().DropIndex(&bindingPO{}, index); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
if err := migrateBindingStatusIndex(db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if !db.Migrator().HasIndex(&bindingPO{}, index) {
|
||||
t.Fatal("legacy status index was not created")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user