Skip to content

Commit 35ee3ae

Browse files
authored
Fix replace option on CreateView migration (#161)
Co-authored-by: Nico Kleinschmidt <[email protected]>
1 parent 0a864b1 commit 35ee3ae

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

migrator.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,29 @@ WHERE TABLE_CATALOG = ? AND TABLE_NAME = ?`)
437437
return columnTypes, execErr
438438
}
439439

440+
func (m Migrator) CreateView(name string, option gorm.ViewOption) error {
441+
if option.Query == nil {
442+
return gorm.ErrSubQueryRequired
443+
}
444+
445+
sql := new(strings.Builder)
446+
sql.WriteString("CREATE ")
447+
if option.Replace {
448+
sql.WriteString("OR ALTER ")
449+
}
450+
sql.WriteString("VIEW ")
451+
m.QuoteTo(sql, name)
452+
sql.WriteString(" AS ")
453+
454+
m.DB.Statement.AddVar(sql, option.Query)
455+
456+
if option.CheckOption != "" {
457+
sql.WriteString(" ")
458+
sql.WriteString(option.CheckOption)
459+
}
460+
return m.DB.Exec(m.Explain(sql.String(), m.DB.Statement.Vars...)).Error
461+
}
462+
440463
func (m Migrator) CreateIndex(value interface{}, name string) error {
441464
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
442465
var idx *schema.Index

0 commit comments

Comments
 (0)