Beispiel #1
1
func init() {
	var err error

	if os.Getenv("DB") == "mysql" {
		if DB, err = gorm.Open("mysql", "qor:qor@/qor_bookstore?parseTime=True&loc=Local"); err != nil {
			panic(err)
		}
	} else {
		if DB, err = gorm.Open("postgres", "user=qor password=qor dbname=qor_bookstore sslmode=disable"); err != nil {
			panic(err)
		}
	}

	DB.AutoMigrate(&models.Author{}, &models.Book{}, &models.User{})
	DB.LogMode(true)

	Pub = publish.New(&DB)
	Pub.AutoMigrate(&models.Author{}, &models.Book{})

	StagingDB = Pub.DraftDB()         // Draft resources are saved here
	ProductionDB = Pub.ProductionDB() // Published resources are saved here

	l10n.Global = "en-US"
	l10n.RegisterCallbacks(&DB)
}
Beispiel #2
0
func init() {
	var err error
	var db gorm.DB

	dbConfig := config.Config.DB
	if config.Config.DB.Adapter == "mysql" {
		db, err = gorm.Open("mysql", fmt.Sprintf("%v:%v@/%v?parseTime=True&loc=Local", dbConfig.User, dbConfig.Password, dbConfig.Name))
	} else if config.Config.DB.Adapter == "postgres" {
		db, err = gorm.Open("postgres", fmt.Sprintf("user=%v password=%v dbname=%v sslmode=disable", dbConfig.User, dbConfig.Password, dbConfig.Name))
	} else {
		panic(errors.New("not supported database adapter"))
	}

	if err == nil {
		DB = &db
		Publish = publish.New(DB)
		config.Config.I18n = i18n.New(database.New(DB))

		l10n.RegisterCallbacks(DB)
		sorting.RegisterCallbacks(DB)
		validations.RegisterCallbacks(DB)
	} else {
		panic(err)
	}
}
Beispiel #3
0
func PrepareDB() {
	var err error

	// Be able to start a server for develop test
	dbuser, dbpwd = "qor", "qor"
	if devMode {
		dbname = "qor_integration"
	} else {
		dbname = "qor_integration_test"
	}

	if os.Getenv("TEST_ENV") == "CI" {
		dbuser, dbpwd = os.Getenv("DB_USER"), os.Getenv("DB_PWD")
	}

	DB, err = gorm.Open("mysql", fmt.Sprintf("%s:%s@/%s?charset=utf8&parseTime=True&loc=Local", dbuser, dbpwd, dbname))
	if err != nil {
		panic(err)
	}

	Publish = publish.New(&DB)

	SetupDb(true)

	draftDB = Publish.DraftDB()
	l10n.RegisterCallbacks(&DB)
}
Beispiel #4
0
func init() {
	db = utils.TestDB()
	sorting.RegisterCallbacks(db)
	l10n.RegisterCallbacks(db)

	pb = publish.New(db)
	pb.ProductionDB().DropTable(&User{}, &Product{}, &Brand{})
	pb.DraftDB().DropTable(&Product{})
	db.AutoMigrate(&User{}, &Product{}, &Brand{})
	pb.AutoMigrate(&Product{})
}
Beispiel #5
0
func init() {
	db = utils.TestDB()
	pb = publish.New(db)
	pbdraft = pb.DraftDB()
	pbprod = pb.ProductionDB()

	for _, table := range []string{"products", "products_draft", "colors", "categories", "languages", "product_categories", "product_categories_draft", "languages", "product_languages_draft"} {
		pbprod.Exec(fmt.Sprintf("drop table %v", table))
	}
	pbprod.AutoMigrate(&Product{}, &Color{}, &Category{}, &Language{})
	pb.AutoMigrate(&Product{}, &Category{})
}
Beispiel #6
0
func initAdmin() {
	Admin = admin.New(&qor.Config{DB: models.DB})
	Publish = publish.New(models.DB)
	sorting.RegisterCallbacks(models.DB)
	validations.RegisterCallbacks(models.DB)

	nodeSelectMeta := &admin.Meta{Name: "NodeId", Type: "select_one", Collection: nodeCollection}
	bodyMeta := &admin.Meta{Name: "Body", Type: "text"}

	Admin.AddResource(&admin.AssetManager{}, &admin.Config{Invisible: true})

	topic := Admin.AddResource(&models.Topic{})
	topic.SearchAttrs("Title")
	topic.NewAttrs("Title", "NodeId", "Body", "UserId")
	topic.EditAttrs("Title", "NodeId", "Body", "UserId")
	topic.IndexAttrs("Id", "UserId", "Title", "NodeId", "RepliesCount", "CreatedAt", "UpdatedAt")
	topic.Meta(bodyMeta)
	topic.Meta(nodeSelectMeta)

	reply := Admin.AddResource(&models.Reply{})
	reply.NewAttrs("TopicId", "UserId", "Body")
	reply.EditAttrs("Body")
	reply.IndexAttrs("Id", "Topic", "User", "Body", "CreatedAt", "UpdatedAt")
	reply.Meta(bodyMeta)

	user := Admin.AddResource(&models.User{})
	user.SearchAttrs("Login", "Email")
	user.EditAttrs("Login", "Email", "Location", "GitHub", "Twitter", "HomePage", "Tagline", "Description")
	user.IndexAttrs("Id", "Login", "Email", "Location", "CreatedAt", "UpdatedAt")

	node := Admin.AddResource(&models.Node{})
	node.IndexAttrs("Id", "NodeGroupId", "Name", "Summary", "Sort")
	node.NewAttrs("NodeGroupId", "Name", "Summary", "Sort")
	node.EditAttrs("NodeGroupId", "Name", "Summary", "Sort")
	node.Meta(&admin.Meta{Name: "NodeGroupId", Type: "select_one", Collection: nodeGroupCollection})

	nodeGroup := Admin.AddResource(&models.NodeGroup{})
	nodeGroup.IndexAttrs("Id", "Name", "Sort")
	nodeGroup.NewAttrs("Name", "Sort")
	nodeGroup.EditAttrs("Name", "Sort")

	notification := Admin.AddResource(&models.Notification{})
	notification.EditAttrs("Id")
	notification.IndexAttrs("Id", "UserId", "ActorId", "NotifyType", "Read", "NotifyableType", "NotifyableId", "CreatedAt", "UpdatedAt")

	mux = http.NewServeMux()
	mux.Handle("/system/", http.FileServer(http.Dir("public")))
	Admin.MountTo("/admin", mux)
}
Beispiel #7
0
func init() {
	db = utils.TestDB()
	l10n.RegisterCallbacks(db)

	pb = publish.New(db)
	pbdraft = pb.DraftDB()
	pbprod = pb.ProductionDB()

	for _, table := range []string{"product_categories", "product_categories_draft", "product_languages", "product_languages_draft", "author_books", "author_books_draft"} {
		pbprod.Exec(fmt.Sprintf("drop table %v", table))
	}

	for _, value := range []interface{}{&Product{}, &Color{}, &Category{}, &Language{}, &Book{}, &Publisher{}, &Comment{}, &Author{}} {
		pbprod.DropTable(value)
		pbdraft.DropTable(value)

		pbprod.AutoMigrate(value)
		pb.AutoMigrate(value)
	}
}
Beispiel #8
0
func init() {
	var err error
	// CREATE USER 'qor'@'localhost' IDENTIFIED BY 'qor';
	// CREATE DATABASE qor_example;
	// GRANT ALL PRIVILEGES ON qor_example.* TO 'qor'@'localhost';
	dbuser, dbpwd := "qor", "qor"
	if os.Getenv("WEB_ENV") == "online" {
		dbuser, dbpwd = os.Getenv("DB_USER"), os.Getenv("DB_PWD")
	}
	DB, err = gorm.Open("mysql", fmt.Sprintf("%s:%s@/qor_example?charset=utf8&parseTime=True&loc=Local", dbuser, dbpwd))
	if err != nil {
		panic(err)
	}

	DB.AutoMigrate(&User{}, &CreditCard{}, &Address{}, &Role{}, &Language{}, &Product{}, &Collection{}, &Order{}, &OrderItem{}, &admin.AssetManager{})

	Publish = publish.New(&DB)
	Publish.AutoMigrate(&Product{})

	l10n.RegisterCallbacks(&DB)

	var AdminRole Role
	DB.FirstOrCreate(&AdminRole, Role{Name: "admin"})
	DB.FirstOrCreate(&Role{}, Role{Name: "dev"})
	DB.FirstOrCreate(&Role{}, Role{Name: "customer_support"})

	DB.FirstOrCreate(&User{}, User{Name: "admin", RoleID: AdminRole.ID})
	DB.FirstOrCreate(&User{}, User{Name: "global_admin", RoleID: AdminRole.ID})

	DB.FirstOrCreate(&Language{}, Language{Name: "CN"})
	DB.FirstOrCreate(&Language{}, Language{Name: "JP"})
	DB.FirstOrCreate(&Language{}, Language{Name: "EN"})
	DB.FirstOrCreate(&Language{}, Language{Name: "DE"})

	DB.LogMode(true)
}