本文整理汇总了Golang中github.com/jinzhu/gorm.DB.DropTableIfExists方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.DropTableIfExists方法的具体用法?Golang DB.DropTableIfExists怎么用?Golang DB.DropTableIfExists使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/jinzhu/gorm.DB
的用法示例。
在下文中一共展示了DB.DropTableIfExists方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: deleteAllFromDB
func deleteAllFromDB(db gorm.DB, selectedType string) error {
switch selectedType {
case "jobs":
db.DropTableIfExists(&application.Job{})
db.CreateTable(&application.Job{})
case "freelancers":
db.DropTableIfExists(&application.Freelancer{})
db.CreateTable(&application.Freelancer{})
}
return nil
}
示例2: ListenAndServe
func ListenAndServe(addr string, auth Auth, dbType DatabaseType) {
var db *gorm.DB
var err error
if dbType == DB_MySQL {
dbConn := fmt.Sprintf("%s:%[email protected](127.0.0.1:3306)", auth.UserName, auth.Password)
db, err = gorm.Open("mysql", dbConn+"/hugocms_"+auth.UserName+"?charset=utf8&parseTime=True&loc=Local")
} else {
db, err = gorm.Open("sqlite3", "hugocms_"+auth.UserName+".db")
}
if err != nil {
handleError(err)
}
db.LogMode(true)
// to reset to an empty database drop the settings table
if !db.HasTable(&models.Settings{}) {
for _, table := range hugocms_qor.Tables {
if err := db.DropTableIfExists(table).Error; err != nil {
handleError(err)
}
}
}
for _, table := range hugocms_qor.Tables {
if err := db.AutoMigrate(table).Error; err != nil {
handleError(err)
}
}
// Because this is a singleton a single record must already exist in the database (there is no create through QOR admin just update)
db.FirstOrCreate(&models.Settings{})
siteName := fmt.Sprintf("%s - Hugo CMS", auth.UserName)
if err := setupConfig(addr, siteName, db, auth); err != nil {
handleError(err)
}
// Add session support - used by Auth
sessionLifetime := 3600 // session lifetime in seconds
SessionManager, err = session.NewManager("memory", fmt.Sprintf(`{"cookieName":"gosessionid","gclifetime":%d}`, sessionLifetime))
if err != nil {
handleError(err)
}
go SessionManager.GC()
// Create Hugo's content directory if it doesnt exist
// TODO read content dir from config
if _, err := os.Stat("./content"); os.IsNotExist(err) {
err = os.MkdirAll("./content", os.ModePerm)
}
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.Dir("public")))
adm := hugocms_qor.SetupAdmin()
adm.MountTo("/admin", mux)
adm.GetRouter().Post("/auth", func(ctx *admin.Context) {
// we will only hit this on succesful login - redirect to admin dashboard
w := ctx.Writer
r := ctx.Request
http.Redirect(w, r, "/admin", http.StatusFound)
})
adm.GetRouter().Get("/logout", func(ctx *admin.Context) {
w := ctx.Writer
r := ctx.Request
sess, err := SessionManager.SessionStart(w, r)
if err != nil {
handleError(err)
}
defer sess.SessionRelease(w)
sess.Delete("User")
http.Redirect(w, r, "/login", http.StatusFound)
})
// NOTE: `system` is where QOR admin will upload files e.g. images - we map this to Hugo's static dir along with our other static assets
// TODO read static dir from config
// TODO read static assets list from config
for _, path := range []string{"system", "css", "fonts", "images", "js", "login"} {
mux.Handle(fmt.Sprintf("/%s/", path), http.FileServer(http.Dir("static")))
}
if err := http.ListenAndServe(config.QOR.Addr, mux); err != nil {
handleError(err)
}
// to re-generate site delete `config.json`
if _, err := os.Stat("config.json"); os.IsNotExist(err) {
hugocms_qor.CallSave(adm)
}
fmt.Printf("Listening on: %s\n", config.QOR.Addr)
}
示例3: FlushDatabase
func FlushDatabase(db gorm.DB) {
db.DropTableIfExists(&Question{})
db.DropTableIfExists(&Answer{})
db.DropTableIfExists(&Client{})
}
示例4: DropOsInstallTables
func DropOsInstallTables(db *gorm.DB, conf *config.Config) error {
db.DropTableIfExists(osInstallTables...)
return nil
}