I am using gorm
to query and manage mysql. The function named SyncDB
in the following snippet migrates the latest user schema found in the user.go
file under schema
directory.
package db
import (
"my-server/db/schema"
"github.com/jinzhu/gorm"
)
func SyncDB(db *gorm.DB) {
db.AutoMigrate(&schema.User{})
}
I have multiple files under the schema
directory. I tried reading all the file names under the directory schema
and have the filenames as an array which looks like:
filenames := []string{
"user.go",
"password.go",
"profile.go",
}
Is there a way to use filenames
array and dynamically call:
db.AutoMigrate(&schema.User{})
For example, &schema.User{}
gets replaced by &schema.Password{}
in the next call. How could I make this thing dynamic?