当前位置: 首页>>代码示例>>Golang>>正文


Golang DB.HasTable方法代码示例

本文整理汇总了Golang中github.com/jinzhu/gorm.DB.HasTable方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.HasTable方法的具体用法?Golang DB.HasTable怎么用?Golang DB.HasTable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/jinzhu/gorm.DB的用法示例。


在下文中一共展示了DB.HasTable方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: createTables

func createTables(db *gorm.DB, values ...interface{}) error {
	for _, value := range values {
		if !db.HasTable(value) {
			err := db.CreateTable(value).Error
			if err != nil {
				errClose := db.Close()
				if errClose != nil {
					fmt.Printf("%s", errClose)
				}
				return err
			}
		}
	}
	return nil
}
开发者ID:ReanGD,项目名称:go-web-search,代码行数:15,代码来源:content.go

示例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)
}
开发者ID:8legd,项目名称:hugocms,代码行数:96,代码来源:server.go

示例3: IsOkDB

// IsOkDB checks if database is ok
func IsOkDB(DB gorm.DB) bool {
	// Check if all tables exists
	// user
	if !DB.HasTable(&User{}) {
		return false
	}
	if !DB.HasTable(&Alias{}) {
		return false
	}
	if !DB.HasTable(&RcptHost{}) {
		return false
	}
	if !DB.HasTable(&Mailbox{}) {
		return false
	}
	if !DB.HasTable(&RelayIpOk{}) {
		return false
	}
	if !DB.HasTable(&QMessage{}) {
		return false
	}
	if !DB.HasTable(&Route{}) {
		return false
	}
	if !DB.HasTable(&DkimConfig{}) {
		return false
	}
	return true
}
开发者ID:teefax,项目名称:tmail,代码行数:30,代码来源:database.go

示例4: InitDB

// InitDB create tables if needed and initialize them
// TODO: SKIP in CLI
// TODO:  check regularly structure & indexes
func InitDB(DB gorm.DB) error {
	var err error
	//users table
	if !DB.HasTable(&User{}) {
		if err = DB.CreateTable(&User{}).Error; err != nil {
			return errors.New("Unable to create table user - " + err.Error())
		}
	}

	// Alias
	if !DB.HasTable(&Alias{}) {
		if err = DB.CreateTable(&Alias{}).Error; err != nil {
			return errors.New("Unable to create table Alias - " + err.Error())
		}
	}

	//rcpthosts table
	if !DB.HasTable(&RcptHost{}) {
		if err = DB.CreateTable(&RcptHost{}).Error; err != nil {
			return errors.New("Unable to create RcptHost - " + err.Error())
		}
		// Index
		if err = DB.Model(&RcptHost{}).AddIndex("idx_rcpthots_hostname", "hostname").Error; err != nil {
			return errors.New("Unable to add index idx_rcpthots_domain on table RcptHost - " + err.Error())
		}
	}

	// mailbox
	if !DB.HasTable(&Mailbox{}) {
		if err = DB.CreateTable(&Mailbox{}).Error; err != nil {
			return errors.New("Unable to create Mailbox - " + err.Error())
		}
		// Index
	}

	//relay_ip_oks table
	if !DB.HasTable(&RelayIpOk{}) {
		if err = DB.CreateTable(&RelayIpOk{}).Error; err != nil {
			return errors.New("Unable to create relay_ok_ips - " + err.Error())
		}
		// Index
		if err = DB.Model(&RelayIpOk{}).AddIndex("idx_relay_ok_ips_ip", "ip").Error; err != nil {
			return errors.New("Unable to add index idx_rcpthots_domain on table relay_ok_ips - " + err.Error())
		}
	}

	//queued_messages table
	if !DB.HasTable(&QMessage{}) {
		if err = DB.CreateTable(&QMessage{}).Error; err != nil {
			return errors.New("Unable to create table queued_messages - " + err.Error())
		}
	}
	// deliverd.route
	if !DB.HasTable(&Route{}) {
		if err = DB.CreateTable(&Route{}).Error; err != nil {
			return errors.New("Unable to create table route - " + err.Error())
		}
		// Index
		if err = DB.Model(&Route{}).AddIndex("idx_route_host", "host").Error; err != nil {
			return errors.New("Unable to add index idx_route_host on table route - " + err.Error())
		}
	}

	if !DB.HasTable(&DkimConfig{}) {
		if err = DB.CreateTable(&DkimConfig{}).Error; err != nil {
			return errors.New("Unable to create table dkim_config - " + err.Error())
		}
		// Index
		if err = DB.Model(&DkimConfig{}).AddIndex("idx_domain", "domain").Error; err != nil {
			return errors.New("Unable to add index idx_domain on table dkim_config - " + err.Error())
		}
	}

	return nil
}
开发者ID:teefax,项目名称:tmail,代码行数:78,代码来源:database.go


注:本文中的github.com/jinzhu/gorm.DB.HasTable方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。