You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
paopao-ce/internal/migration/migration_embed.go

90 lines
2.7 KiB

//go:build migration
// +build migration
package migration
import (
"database/sql"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
"github.com/golang-migrate/migrate/v4/database/mysql"
"github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/database/sqlite3"
"github.com/golang-migrate/migrate/v4/source"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/rocboss/paopao-ce/internal/conf"
"github.com/rocboss/paopao-ce/scripts/migration"
"github.com/sirupsen/logrus"
)
func Run() {
if !conf.CfgIf("Migration") {
logrus.Infoln("skip migrate because not add Migration feature in config.yaml")
return
}
var (
db *sql.DB
dbName string
dbDriver database.Driver
srcDriver source.Driver
err, err2 error
)
if conf.CfgIf("MySQL") {
dbName = conf.MysqlSetting.DBName
db, err = sql.Open("mysql", conf.MysqlSetting.Dsn()+"&multiStatements=true")
} else if conf.CfgIf("PostgreSQL") || conf.CfgIf("Postgres") {
dbName = (*conf.PostgresSetting)["DBName"]
db, err = sql.Open("postgres", conf.PostgresSetting.Dsn())
} else if conf.CfgIf("Sqlite3") {
db, err = conf.OpenSqlite3()
} else {
dbName = conf.MysqlSetting.DBName
db, err = sql.Open("mysql", conf.MysqlSetting.Dsn())
}
if err != nil {
logrus.Errorf("initial db for migration failed: %s", err)
return
}
migrationsTable := conf.DatabaseSetting.TablePrefix + "schema_migrations"
if conf.CfgIf("MySQL") {
srcDriver, err = iofs.New(migration.Files, "mysql")
dbDriver, err2 = mysql.WithInstance(db, &mysql.Config{MigrationsTable: migrationsTable})
} else if conf.CfgIf("PostgreSQL") || conf.CfgIf("Postgres") {
srcDriver, err = iofs.New(migration.Files, "postgres")
dbDriver, err2 = postgres.WithInstance(db, &postgres.Config{MigrationsTable: migrationsTable})
} else if conf.CfgIf("Sqlite3") {
srcDriver, err = iofs.New(migration.Files, "sqlite3")
dbDriver, err2 = sqlite3.WithInstance(db, &sqlite3.Config{MigrationsTable: migrationsTable})
} else {
srcDriver, err = iofs.New(migration.Files, "mysql")
dbDriver, err2 = mysql.WithInstance(db, &mysql.Config{MigrationsTable: migrationsTable})
}
if err2 != nil {
logrus.Errorf("new database driver failed: %s", err)
return
} else {
defer dbDriver.Close()
}
if err != nil {
logrus.Errorf("new source driver failed: %s", err)
return
}
m, err := migrate.NewWithInstance("iofs", srcDriver, dbName, dbDriver)
if err != nil {
logrus.Errorf("new migrate instance failed: %s", err)
return
}
if err = m.Up(); err != nil && err != migrate.ErrNoChange {
logrus.Errorf("migrate up failed: %s", err)
return
}
logrus.Infoln("migrate up success")
}