go-gorm / sharding

High performance table sharding plugin for Gorm.
MIT License
276 stars 60 forks source link

panic: interface conversion: sharding.ShardingMigrator is not migrator.BuildIndexOptionsInterface: missing method BuildIndexOptions #97

Closed zishiguo closed 1 year ago

zishiguo commented 1 year ago

GORM Playground Link

569

Description

detailed error as follows

2023/02/19 21:14:12 testing mysql...
panic: interface conversion: sharding.ShardingMigrator is not migrator.BuildIndexOptionsInterface: missing method BuildIndexOptions

goroutine 1 [running]:
gorm.io/gorm/migrator.Migrator.CreateTable.func1(0x14000484380)
    /Users/zhengjb/go/src/github.com/zishiguo/playground/gorm/migrator/migrator.go:247 +0x568
gorm.io/gorm/migrator.Migrator.RunWithValue({{0x0?, 0x14000271500?, {0x105414100?, 0x1400023c000?}}}, {0x105330f80?, 0x1400023c230}, 0x140000e3540)
    /Users/zhengjb/go/src/github.com/zishiguo/playground/gorm/migrator/migrator.go:62 +0x13c
gorm.io/gorm/migrator.Migrator.CreateTable({{0x70?, 0x14000271500?, {0x105414100?, 0x1400023c000?}}}, {0x140004803c0?, 0x140004803c0?, 0x0?})
    /Users/zhengjb/go/src/github.com/zishiguo/playground/gorm/migrator/migrator.go:198 +0x114
gorm.io/gorm/migrator.Migrator.AutoMigrate({{0x0?, 0x14000270720?, {0x105414100?, 0x1400023c000?}}}, {0x1400021bad0?, 0x105311aa0?, 0x1400023c001?})
    /Users/zhengjb/go/src/github.com/zishiguo/playground/gorm/migrator/migrator.go:112 +0x244
gorm.io/sharding.ShardingMigrator.AutoMigrate({{0x105417ca8, 0x14000270690}, 0x140002368f0, {0x105413e40, 0x14000010bd0}}, {0x1400021b770?, 0x0?, 0x0?})
    /Users/zhengjb/go/pkg/mod/gorm.io/sharding@v0.5.2/dialector.go:45 +0x308
gorm.io/gorm.(*DB).AutoMigrate(0x140000017a0?, {0x1400021b770, 0x1, 0x1})
    /Users/zhengjb/go/src/github.com/zishiguo/playground/gorm/migrator.go:28 +0x44
gorm.io/playground.RunMigrations()
    /Users/zhengjb/go/src/github.com/zishiguo/playground/db.go:113 +0x194
gorm.io/playground.init.0()
    /Users/zhengjb/go/src/github.com/zishiguo/playground/db.go:37 +0x108

Process finished with the exit code 1
IvanWhisper commented 1 year ago

ShardingMigrator need impl BuildIndexOptions(opts []schema.IndexOption, stmt *gorm.Statement) (results []interface{})

So I copy migrator.Migrator.BuildIndexOptions[v1.25.1] to ShardingMigrator for impl

var  _ = migrator.BuildIndexOptionsInterface(sharding.ShardingMigrator{}) // check impl

// BuildIndexOptions build index options
func (m ShardingMigrator) BuildIndexOptions(opts []schema.IndexOption, stmt *gorm.Statement) (results []interface{}) {
    for _, opt := range opts {
        str := stmt.Quote(opt.DBName)
        if opt.Expression != "" {
            str = opt.Expression
        } else if opt.Length > 0 {
            str += fmt.Sprintf("(%d)", opt.Length)
        }

        if opt.Collate != "" {
            str += " COLLATE " + opt.Collate
        }

        if opt.Sort != "" {
            str += " " + opt.Sort
        }
        results = append(results, clause.Expr{SQL: str})
    }
    return
}

@jinzhu suggest fix this bug, thanks