Skip to content

Commit

Permalink
feat(gorm): add migration table config
Browse files Browse the repository at this point in the history
  • Loading branch information
Alice52 committed Jun 13, 2024
1 parent f3b319d commit ed80292
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
1 change: 1 addition & 0 deletions gormx/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ mysql:
log-zap: true
migration: true
migration-path: file://./source/migrations
migration-table: schema_migrations_xxx

31 changes: 16 additions & 15 deletions gormx/gconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ type DsnProvider interface {

// GeneralDB 也被 Pgsql 和 Mysql 原样使用
type GeneralDB struct {
Prefix string `mapstructure:"prefix" json:"prefix" yaml:"prefix"`
Port string `mapstructure:"port" json:"port" yaml:"port"`
Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置
Dbname string `mapstructure:"db-name" json:"db-name" yaml:"db-name"` // 数据库名
Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库密码
Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码
Path string `mapstructure:"path" json:"path" yaml:"path"`
Engine string `mapstructure:"engine" json:"engine" yaml:"engine" default:"InnoDB"` //数据库引擎,默认InnoDB
LogMode string `mapstructure:"log-mode" json:"log-mode" yaml:"log-mode"` // 是否开启Gorm全局日志
MaxIdleConns int `mapstructure:"max-idle-conns" json:"max-idle-conns" yaml:"max-idle-conns"` // 空闲中的最大连接数
MaxOpenConns int `mapstructure:"max-open-conns" json:"max-open-conns" yaml:"max-open-conns"` // 打开到数据库的最大连接数
Singular bool `mapstructure:"singular" json:"singular" yaml:"singular"` //是否开启全局禁用复数,true表示开启
LogType string `mapstructure:"log-type" json:"log-type" yaml:"log-type"` // default:console, optional[console, zap, go-zero]
Migration bool `mapstructure:"migration" json:"migration" yaml:"migration"`
MigrationPath string `mapstructure:"migration-path" json:"migration-path" yaml:"migration-path"` // migration-path
Prefix string `mapstructure:"prefix" json:"prefix" yaml:"prefix"`
Port string `mapstructure:"port" json:"port" yaml:"port"`
Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置
Dbname string `mapstructure:"db-name" json:"db-name" yaml:"db-name"` // 数据库名
Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库密码
Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码
Path string `mapstructure:"path" json:"path" yaml:"path"`
Engine string `mapstructure:"engine" json:"engine" yaml:"engine" default:"InnoDB"` //数据库引擎,默认InnoDB
LogMode string `mapstructure:"log-mode" json:"log-mode" yaml:"log-mode"` // 是否开启Gorm全局日志
MaxIdleConns int `mapstructure:"max-idle-conns" json:"max-idle-conns" yaml:"max-idle-conns"` // 空闲中的最大连接数
MaxOpenConns int `mapstructure:"max-open-conns" json:"max-open-conns" yaml:"max-open-conns"` // 打开到数据库的最大连接数
Singular bool `mapstructure:"singular" json:"singular" yaml:"singular"` //是否开启全局禁用复数,true表示开启
LogType string `mapstructure:"log-type" json:"log-type" yaml:"log-type"` // default:console, optional[console, zap, go-zero]
Migration bool `mapstructure:"migration" json:"migration" yaml:"migration"`
MigrationPath string `mapstructure:"migration-path" json:"migration-path" yaml:"migration-path"` // migration-path
MigrationTable string `mapstructure:"migration-table" json:"migration-table" yaml:"migration-table"`
}
10 changes: 7 additions & 3 deletions gormx/migration/mysql_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,26 @@ import (
// InitializeMysql 初始化函数, 在项目启动时调用
func InitializeMysql(db *gorm.DB) {
mp := kg.C.Mysql.MigrationPath
mt := kg.C.Mysql.MigrationTable
if len(mt) == 0 {
mt = "schema_migrations"
}

if len(mp) == 0 {
return
}

if s, err := db.DB(); err != nil {
panic(err)
} else if err := MigrateMysql(s, mp); err != nil { // 执行数据库迁移
} else if err := MigrateMysql(s, mp, mt); err != nil { // 执行数据库迁移
panic(err)
}
}

// MigrateMysql 执行数据库迁移
func MigrateMysql(db *sql.DB, mp string) error {
func MigrateMysql(db *sql.DB, mp, mt string) error {
// Create migration instance
driver, err := mysql.WithInstance(db, &mysql.Config{})
driver, err := mysql.WithInstance(db, &mysql.Config{MigrationsTable: mt})
if err != nil {
return err
}
Expand Down
11 changes: 7 additions & 4 deletions gormx/migration/pgsql_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,25 @@ import (
// InitializePgsql 初始化函数, 在项目启动时调用
func InitializePgsql(db *gorm.DB) {
mp := kg.C.Pgsql.MigrationPath

mt := kg.C.Pgsql.MigrationTable
if len(mt) == 0 {
mt = "schema_migrations"
}
if len(mp) == 0 {
return
}

if s, err := db.DB(); err != nil {
panic(err)
} else if err := MigratePgsql(s, mp); err != nil { // 执行数据库迁移
} else if err := MigratePgsql(s, mp, mt); err != nil { // 执行数据库迁移
panic(err)
}
}

// MigratePgsql 执行数据库迁移
func MigratePgsql(db *sql.DB, mp string) error {
func MigratePgsql(db *sql.DB, mp, mt string) error {
// 创建迁移实例
driver, err := postgres.WithInstance(db, &postgres.Config{})
driver, err := postgres.WithInstance(db, &postgres.Config{MigrationsTable: mt})
if err != nil {
return err
}
Expand Down

0 comments on commit ed80292

Please sign in to comment.