create view
This commit is contained in:
parent
04cbd956eb
commit
2f228b606c
@ -559,12 +559,28 @@ func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error) {
|
||||
|
||||
// CreateView create view
|
||||
func (m Migrator) CreateView(name string, option gorm.ViewOption) error {
|
||||
return gorm.ErrNotImplemented
|
||||
sql := new(strings.Builder)
|
||||
sql.WriteString("CREATE ")
|
||||
if option.Replace {
|
||||
sql.WriteString("OR REPLACE ")
|
||||
}
|
||||
sql.WriteString("VIEW ")
|
||||
m.QuoteTo(sql, name)
|
||||
sql.WriteString(" AS ")
|
||||
|
||||
m.DB.Statement.AddVar(sql, option.Query)
|
||||
|
||||
if option.CheckOption != "" {
|
||||
sql.WriteString(" WITH ")
|
||||
sql.WriteString(option.CheckOption)
|
||||
sql.WriteString(" CHECK OPTION")
|
||||
}
|
||||
return m.DB.Exec(sql.String()).Error
|
||||
}
|
||||
|
||||
// DropView drop view
|
||||
func (m Migrator) DropView(name string) error {
|
||||
return gorm.ErrNotImplemented
|
||||
return m.DB.Exec("DROP VIEW IF EXISTS ?", clause.Table{Name: name}).Error
|
||||
}
|
||||
|
||||
func buildConstraint(constraint *schema.Constraint) (sql string, results []interface{}) {
|
||||
|
@ -644,6 +644,27 @@ func TestMigrateColumns(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateView(t *testing.T) {
|
||||
DB.Save(GetUser("joins-args-db", Config{Pets: 2}))
|
||||
|
||||
query := DB.Model(&User{}).
|
||||
Select("users.id as users_id, users.name as users_name, pets.id as pets_id, pets.name as pets_name").
|
||||
Joins("inner join pets on pets.user_id = users.id")
|
||||
|
||||
if err := DB.Migrator().CreateView("users_pets", gorm.ViewOption{Query: query}); err != nil {
|
||||
t.Fatalf("Failed to crate view, got %v", err)
|
||||
}
|
||||
|
||||
var count int64
|
||||
if err := DB.Table("users_pets").Count(&count).Error; err != nil {
|
||||
t.Fatalf("should found created view")
|
||||
}
|
||||
|
||||
if err := DB.Migrator().DropView("users_pets"); err != nil {
|
||||
t.Fatalf("Failed to drop view, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateConstraint(t *testing.T) {
|
||||
names := []string{"Account", "fk_users_account", "Pets", "fk_users_pets", "Company", "fk_users_company", "Team", "fk_users_team", "Languages", "fk_users_languages"}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user