feat: add IF NOT EXISTS config for migrator

This commit is contained in:
zhtiyissr 2023-10-08 23:33:38 +08:00
parent e57e5d8884
commit d34abe9128

View File

@ -35,6 +35,7 @@ type Migrator struct {
// Config schema config // Config schema config
type Config struct { type Config struct {
CreateIndexAfterCreateTable bool CreateIndexAfterCreateTable bool
CreateTableUseIfNotExists bool
DB *gorm.DB DB *gorm.DB
gorm.Dialector gorm.Dialector
} }
@ -213,6 +214,10 @@ func (m Migrator) CreateTable(values ...interface{}) error {
hasPrimaryKeyInDataType bool hasPrimaryKeyInDataType bool
) )
if m.CreateTableUseIfNotExists {
createTableSQL = "CREATE TABLE IF NOT EXISTS ? ("
}
for _, dbName := range stmt.Schema.DBNames { for _, dbName := range stmt.Schema.DBNames {
field := stmt.Schema.FieldsByDBName[dbName] field := stmt.Schema.FieldsByDBName[dbName]
if !field.IgnoreMigration { if !field.IgnoreMigration {