mirror of https://github.com/go-gorm/gorm.git
Add DropTable
This commit is contained in:
parent
600d8e7277
commit
49cfb0d4a0
|
@ -248,6 +248,9 @@ db.Where("name <> ?", "jinzhu").Where("age >= ? and role <> ?", 20, "admin").Fin
|
||||||
// Create Table with struct
|
// Create Table with struct
|
||||||
db.CreateTable(&User{})
|
db.CreateTable(&User{})
|
||||||
|
|
||||||
|
// Drop Table
|
||||||
|
db.DropTable(&User{})
|
||||||
|
|
||||||
// Specify Table Name
|
// Specify Table Name
|
||||||
db.Table("deleted_users").CreateTable(&User{})
|
db.Table("deleted_users").CreateTable(&User{})
|
||||||
db.Table("users").Pluck("age", &ages)
|
db.Table("users").Pluck("age", &ages)
|
||||||
|
|
5
chain.go
5
chain.go
|
@ -236,6 +236,11 @@ func (s *Chain) CreateTable(value interface{}) *Chain {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Chain) DropTable(value interface{}) *Chain {
|
||||||
|
s.do(value).dropTable().exec()
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Chain) Unscoped() *Chain {
|
func (s *Chain) Unscoped() *Chain {
|
||||||
s.unscoped = true
|
s.unscoped = true
|
||||||
return s
|
return s
|
||||||
|
|
8
do.go
8
do.go
|
@ -577,6 +577,14 @@ func (s *Do) createTable() *Do {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Do) dropTable() *Do {
|
||||||
|
s.sql = fmt.Sprintf(
|
||||||
|
"DROP TABLE \"%v\"",
|
||||||
|
s.tableName(),
|
||||||
|
)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Do) initializeWithSearchCondition() {
|
func (s *Do) initializeWithSearchCondition() {
|
||||||
m := Model{data: s.value, driver: s.driver}
|
m := Model{data: s.value, driver: s.driver}
|
||||||
|
|
||||||
|
|
15
gorm_test.go
15
gorm_test.go
|
@ -50,18 +50,21 @@ func init() {
|
||||||
db.SetPool(10)
|
db.SetPool(10)
|
||||||
// db.DebugMode = true
|
// db.DebugMode = true
|
||||||
|
|
||||||
err = db.Exec("drop table users;").Error
|
err = db.DropTable(&User{}).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Got error when try to delete table users, %+v\n", err)
|
fmt.Printf("Got error when try to delete table users, %+v\n", err)
|
||||||
}
|
}
|
||||||
db.Exec("drop table products;")
|
db.Exec("drop table products")
|
||||||
|
|
||||||
orm := db.CreateTable(&User{})
|
err = db.CreateTable(&User{}).Error
|
||||||
if orm.Error != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("No error should happen when create table, but got %+v", orm.Error))
|
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
db.CreateTable(&Product{})
|
err = db.CreateTable(&Product{}).Error
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
|
||||||
|
}
|
||||||
|
|
||||||
var shortForm = "2006-01-02 15:04:05"
|
var shortForm = "2006-01-02 15:04:05"
|
||||||
t1, _ = time.Parse(shortForm, "2000-10-27 12:02:40")
|
t1, _ = time.Parse(shortForm, "2000-10-27 12:02:40")
|
||||||
|
|
4
main.go
4
main.go
|
@ -101,3 +101,7 @@ func (s *DB) Debug() *Chain {
|
||||||
func (s *DB) CreateTable(value interface{}) *Chain {
|
func (s *DB) CreateTable(value interface{}) *Chain {
|
||||||
return s.buildChain().CreateTable(value)
|
return s.buildChain().CreateTable(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *DB) DropTable(value interface{}) *Chain {
|
||||||
|
return s.buildChain().DropTable(value)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue