Example #1
0
// AutoMigrateDB will keep tables reflecting structs
func AutoMigrateDB(DB gorm.DB) error {
	// if tables exists check if they reflects struts
	if err := DB.AutoMigrate(&User{}, &Alias{}, &RcptHost{}, &RelayIpOk{}, &QMessage{}, &Route{}, &DkimConfig{}).Error; err != nil {
		return errors.New("Unable autoMigrateDB - " + err.Error())
	}
	return nil
}
Example #2
0
func New(db *gorm.DB) i18n.Backend {
	db.AutoMigrate(&Translation{})
	if err := db.Model(&Translation{}).AddUniqueIndex("idx_translations_key_with_locale", "locale", "key").Error; err != nil {
		fmt.Printf("Failed to create unique index for translations key & locale, got: %v\n", err.Error())
	}
	return &Backend{DB: db}
}
Example #3
0
func MakeWebhooksContext(db gorm.DB) (*WebhookContext, error) {
	db.AutoMigrate(&Webhook{})

	return &WebhookContext{
		Db: db,
	}, nil
}
Example #4
0
func AddTables(db *gorm.DB) error {
	err := db.AutoMigrate(UserFileInfo{}).Error
	if err != nil {
		return err
	}
	return db.AutoMigrate(UserFile{}).Error
}
Example #5
0
func MakeMiniLogger(db gorm.DB) (*MiniLogger, error) {
	db.AutoMigrate(&MiniLog{})

	return &MiniLogger{
		Db: db,
	}, nil
}
Example #6
0
//Migrate db:migrate
func (p *Engine) Migrate(db *gorm.DB) {
	db.AutoMigrate(
		&Article{}, &Tag{}, &Comment{}, &Attachment{},
	)

	db.Model(&Tag{}).AddUniqueIndex("idx_cms_tag_name_lang", "name", "lang")

}
Example #7
0
func MakeDailyTasksContext(db gorm.DB, point Crontab) (*DailyTasksContext, error) {
	db.AutoMigrate(&DailyTask{})

	return &DailyTasksContext{
		Db:    db,
		Point: point,
	}, nil
}
Example #8
0
func New(db *gorm.DB) *Publish {
	tableHandler := gorm.DefaultTableNameHandler
	gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
		tableName := tableHandler(db, defaultTableName)

		if db != nil {
			if IsPublishableModel(db.Value) {
				// Set join table handler
				typ := utils.ModelType(db.Value)
				if !injectedJoinTableHandler[typ] {
					injectedJoinTableHandler[typ] = true
					scope := db.NewScope(db.Value)
					for _, field := range scope.GetModelStruct().StructFields {
						if many2many := utils.ParseTagOption(field.Tag.Get("gorm"))["MANY2MANY"]; many2many != "" {
							db.SetJoinTableHandler(db.Value, field.Name, &publishJoinTableHandler{})
							db.AutoMigrate(db.Value)
						}
					}
				}

				var forceDraftTable bool
				if forceDraftTable, ok := db.Get("publish:force_draft_table"); ok {
					if forceMode, ok := forceDraftTable.(bool); ok && forceMode {
						forceDraftTable = true
					}
				}

				if IsDraftMode(db) || forceDraftTable {
					return DraftTableName(tableName)
				}
			}
		}
		return tableName
	}

	db.AutoMigrate(&PublishEvent{})

	db.Callback().Create().Before("gorm:begin_transaction").Register("publish:set_table_to_draft", setTableAndPublishStatus(true))
	db.Callback().Create().Before("gorm:commit_or_rollback_transaction").
		Register("publish:sync_to_production_after_create", syncCreateFromProductionToDraft)
	db.Callback().Create().Before("gorm:commit_or_rollback_transaction").Register("gorm:create_publish_event", createPublishEvent)

	db.Callback().Delete().Before("gorm:begin_transaction").Register("publish:set_table_to_draft", setTableAndPublishStatus(true))
	db.Callback().Delete().Replace("gorm:delete", deleteScope)
	db.Callback().Delete().Before("gorm:commit_or_rollback_transaction").
		Register("publish:sync_to_production_after_delete", syncDeleteFromProductionToDraft)
	db.Callback().Delete().Before("gorm:commit_or_rollback_transaction").Register("gorm:create_publish_event", createPublishEvent)

	db.Callback().Update().Before("gorm:begin_transaction").Register("publish:set_table_to_draft", setTableAndPublishStatus(true))
	db.Callback().Update().Before("gorm:commit_or_rollback_transaction").
		Register("publish:sync_to_production", syncUpdateFromProductionToDraft)
	db.Callback().Update().Before("gorm:commit_or_rollback_transaction").Register("gorm:create_publish_event", createPublishEvent)

	db.Callback().RowQuery().Register("publish:set_table_in_draft_mode", setTableAndPublishStatus(false))
	db.Callback().Query().Before("gorm:query").Register("publish:set_table_in_draft_mode", setTableAndPublishStatus(false))
	return &Publish{DB: db}
}
Example #9
0
func InitTable(DB *gorm.DB) *gorm.DB {

	DB.DropTable(&User{})
	DB.AutoMigrate(&User{})

	account.InitTable(DB)

	createTestUser(DB)
	return DB
}
Example #10
0
// NewSqlEventStore creates a new SqlEventStore.
func NewSqlEventStore(eventBus EventBus, db gorm.DB) (*SqlEventStore, error) {
	db.AutoMigrate(&sqlAggregateRecord{})
	db.AutoMigrate(&sqlEventRecord{})

	s := &SqlEventStore{
		eventBus:  eventBus,
		factories: make(map[string]func() Event),
		db:        db,
	}
	return s, nil
}
Example #11
0
//Migrate db:migrate
func (p *Engine) Migrate(db *gorm.DB) {
	i18n.Migrate(db)

	db.AutoMigrate(
		&Setting{}, &Notice{},
		&User{}, &Role{}, &Permission{}, &Log{},
	)

	db.Model(&User{}).AddUniqueIndex("idx_user_provider_type_id", "provider_type", "provider_id")
	db.Model(&Role{}).AddUniqueIndex("idx_roles_name_resource_type_id", "name", "resource_type", "resource_id")
	db.Model(&Permission{}).AddUniqueIndex("idx_permissions_user_role", "user_id", "role_id")
}
Example #12
0
// NewMongoReadRepository creates a new MongoReadRepository.
func NewSqlReadRepository(db gorm.DB, factory func() interface{}) (*SqlReadRepository, error) {
	r := &SqlReadRepository{
		db:      db,
		factory: factory,
	}

	model := factory()

	db.AutoMigrate(model)

	return r, nil
}
Example #13
0
func migrateDb(db gorm.DB) {
	// Create table
	db.CreateTable(&model.User{})
	db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&model.User{})

	// Automating Migration
	db.AutoMigrate(&model.User{})
	db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&model.User{})
	// Feel free to change your struct, AutoMigrate will keep your database up-to-date.
	// AutoMigrate will ONLY add *new columns* and *new indexes*,
	// WON'T update current column's type or delete unused columns, to protect your data.
	// If the table is not existing, AutoMigrate will create the table automatically.
}
Example #14
0
func main() {
	// Neo4J Database setup
	var db *neoism.Database
	if os.Getenv("NEO4J_HOST") != "" && os.Getenv("NEO4J_PORT") != "" {
		if connected, err := database.Connect(
			os.Getenv("NEO4J_USER"),
			os.Getenv("NEO4J_PASS"),
			os.Getenv("NEO4J_HOST"),
			os.Getenv("NEO4J_PORT"),
		); err != nil {
			log.Fatal(err)
		} else {
			db = connected
		}
	}

	// Postgres Database setup
	var pgdb *gorm.DB
	if connected, err := gorm.Open("postgres", "user=postgres dbname=newsapi sslmode=disable host=db"); err != nil {
		// if connected, err := gorm.Open("postgres", "user=newsapi dbname=newsapi sslmode=disable"); err != nil {
		log.Fatal(err)
	} else {
		pgdb = &connected
		pgdb.AutoMigrate(&models.User{})
	}

	// Setup analytics client
	var analytics *ga.Client
	if token := os.Getenv("ANALYTICS_TOKEN"); token == "" {
		fmt.Printf("Analytics token not provided.\n")
	} else if client, err := ga.NewClient(token); err != nil {
		log.Fatal(err)
	} else {
		fmt.Printf("Analytics activated.\n")
		analytics = client
	}

	// Setup environment
	if environment := os.Getenv("ENVIRONMENT"); environment == "PRODUCTION" {
		gin.SetMode(gin.ReleaseMode)
	} else {
		gin.SetMode(gin.DebugMode)
		if db != nil {
			db.Session.Log = true
		}
	}

	// Start
	Server(db, pgdb, analytics).Run(port)
}
func MigrationsUp(db *gorm.DB) {
	log.Println("Starting migrations")

	// Drop table
	db.DropTable(&User{}, &Phone{}, &Email{}, &Language{})

	// Create table
	//db.CreateTable(&User{})
	db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&User{}, &Phone{}, &Email{}, &Language{})

	// ModifyColumn
	//db.Model(&User{}).ModifyColumn("description", "text")

	// DropColumn
	//db.Model(&User{}).DropColumn("description")

	// Automating Migration
	db.AutoMigrate(&User{}, &Phone{}, &Email{}, &Language{})
}
Example #16
0
// InitDatabase initialize new database connection
// the first argument is a driver to use, the second one - connection parameters
// Examples:
// InitDatabase("sqlite3","/var/lib/gossha/gossha.db")
// InitDatabase("sqlite3",":memory:")
// InitDatabase("mysql", "user:password@/dbname?charset=utf8&parseTime=True&loc=Local")
// InitDatabase("postgres", "user=gorm dbname=gorm sslmode=disable")
// InitDatabase("postgres", "postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full")
func InitDatabase(driver, dbPath string, verboseLogging bool) error {
	var db *gorm.DB
	db, err := gorm.Open(driver, dbPath)
	db.LogMode(verboseLogging)
	err = db.DB().Ping()
	if err != nil {
		return err
	}
	db.DB().SetMaxIdleConns(5)
	db.DB().SetMaxOpenConns(5)
	// Disable table name's pluralization
	db.SingularTable(true)
	err = db.AutoMigrate(&User{}, &Message{}, &Key{}, &Session{}).Error
	if err != nil {
		return err
	}
	err = db.Model(&User{}).AddUniqueIndex("idx_contact_name", "name").Error
	if err != nil {
		return err
	}
	err = db.Model(&Message{}).AddIndex("idx_message", "user_id", "created_at").Error
	if err != nil {
		return err
	}
	err = db.Model(&Key{}).AddIndex("idx_key", "user_id", "created_at").Error
	if err != nil {
		return err
	}
	err = db.Model(&Key{}).AddUniqueIndex("idx_key_content", "content", "user_id", "created_at").Error
	if err != nil {
		return err
	}
	err = db.Model(&Session{}).AddIndex("idx_session", "user_id", "created_at").Error
	if err != nil {
		return err
	}
	DB = db
	return nil
}
Example #17
0
func AddTables(db *gorm.DB) error {
	var err error
	err = db.AutoMigrate(Visitor{}).Error
	if err != nil {
		return err
	}
	err = db.AutoMigrate(UserBasic{}).Error
	if err != nil {
		return err
	}
	err = db.AutoMigrate(UserAddress{}).Error
	if err != nil {
		return err
	}
	err = db.AutoMigrate(UserPhone{}).Error
	if err != nil {
		return err
	}
	err = db.AutoMigrate(UserProfileElement{}).Error
	if err != nil {
		return err
	}
	return nil
}
Example #18
0
func InitTable(DB *gorm.DB) *gorm.DB {
	DB.DropTable(&Account{})
	DB.AutoMigrate(&Account{})
	return DB
}
Example #19
0
func AddTables(db *gorm.DB) error {
	return db.AutoMigrate(MaillistUser{}).Error
}
Example #20
0
//Migrate db:migrate
func (p *Engine) Migrate(db *gorm.DB) {
	db.AutoMigrate(&Note{}, &Book{})
}
Example #21
0
func script(ctx *cli.Context) error {
	var err error
	var config settings.Config
	if ctx.String("config") != "" {
		config, err = settings.Parse(ctx.String("config"))
		if err != nil {
			logs.Error(err)
		}
	}

	logs.Level(logs.DebugLevel)

	dialect, args, err := config.SqlDB()
	if err != nil {
		logs.Critical(err)
		os.Exit(1)
	}
	logs.Debug("database type: %s", dialect)

	var db *gorm.DB
	if db, err = databases.InitGORM(dialect, args); err != nil {
		logs.Critical(err)
		os.Exit(1)
	}
	logs.Debug("connected to %s", args)

	if config.Migrate() {
		db.AutoMigrate(models.Models()...)
		logs.Debug("database migrated successfully")
	}

	db.LogMode(true)

	ElasticSettings, err := config.Elasticsearch()
	var client *elastic.Client
	client, err = dialElasticRetry(ElasticSettings.String())
	if err != nil {
		logs.Critical(err)
		os.Exit(1)
	}

	// Use the IndexExists service to check if a specified index exists.
	exists, err := client.IndexExists("contacts").Do()
	if err != nil {
		logs.Critical(err)
		os.Exit(1)
	}
	if !exists {
		logs.Critical("No contacts index")
		os.Exit(1)
	}

	ID := config.Int("id")
	if ID == -1 {
		logs.Debug("Looking for ID")
		ID, err = findID(client)
		if err != nil {
			logs.Debug(err)
			os.Exit(1)
		}
	}
	logs.Debug("Last ID is : %d", ID)

	contacts, err := findContacts(ID, db)

	doContacts := config.Bool("contacts")
	doFacts := config.Bool("facts")
	if doContacts == false && doFacts == false {
		logs.Debug("Nothing to be done, please activate some options")
		os.Exit(1)
	}

	for _, contact := range contacts {
		id := contact.ID
		if id != 0 {
			contact, err := addAddresses(contact, db)
			if err == nil {
				if contact != nil {
					if doContacts {
						logs.Debug("Indexing contact %d : %s %s", id, contact.Surname, contact.Firstname)
						err = index(contact, client)
						if err != nil {
							logs.Critical(err)
						}
					}
					if doFacts {
						logs.Debug("Creating and indexing fact for contact %d : %s %s", id, contact.Surname, contact.Firstname)
						err = createFact(contact, client, db)
						if err != nil {
							logs.Critical(err)
						}
					}
				} else {
					logs.Debug("Could not index contact %d", id)
				}
			}
		}
	}

	return nil
}
Example #22
0
// NewUserspaceController creates a userspace controller.
func NewUserspaceController(service *goa.Service, db *gorm.DB) *UserspaceController {
	db.AutoMigrate(&Data{}).AddUniqueIndex("idx_userspace_path", "path")

	return &UserspaceController{Controller: service.NewController("UserspaceController"), db: db}
}
Example #23
0
func AddTables(db *gorm.DB) error {
	return db.AutoMigrate(UserAuth{}).Error
}
Example #24
0
//Migrate db:migrate
func (p *Engine) Migrate(db *gorm.DB) {
	db.AutoMigrate(&Domain{}, &User{}, &Alias{})
}
Example #25
0
// Migrate all tables
func autoMigrateTables(db *gorm.DB) {
	db.AutoMigrate(&models.User{})
	db.AutoMigrate(&models.Item{})
	db.AutoMigrate(&models.UserFavoriteItem{})
	db.AutoMigrate(&models.ItemImpression{})
	db.AutoMigrate(&models.ItemTag{})
	db.AutoMigrate(&models.Tag{})
	db.AutoMigrate(&models.UserTag{})
}
Example #26
0
func script(ctx *cli.Context) error {
	var err error
	var config settings.Config
	if ctx.String("config") != "" {
		config, err = settings.Parse(ctx.String("config"))
		if err != nil {
			logs.Error(err)
		}
	}

	logs.Level(logs.DebugLevel)

	dialect, args, err := config.SqlDB()
	if err != nil {
		logs.Critical(err)
		os.Exit(1)
	}
	logs.Debug("database type: %s", dialect)

	var db *gorm.DB
	if db, err = databases.InitGORM(dialect, args); err != nil {
		logs.Critical(err)
		os.Exit(1)
	}
	logs.Debug("connected to %s", args)

	if config.Migrate() {
		db.AutoMigrate(models.Models()...)
		logs.Debug("database migrated successfully")
	}

	db.LogMode(true)

	ElasticSettings, err := config.Elasticsearch()
	var client *elastic.Client
	client, err = dialElasticRetry(ElasticSettings.String())
	if err != nil {
		logs.Critical(err)
		os.Exit(1)
	}

	// Use the IndexExists service to check if a specified index exists.
	exists, err := client.IndexExists("contacts").Do()
	if err != nil {
		logs.Critical(err)
		os.Exit(1)
	}
	if !exists {
		logs.Critical("No contacts index")
		os.Exit(1)
	}

	ID, err := findID(client)
	if err != nil {
		logs.Debug(err)
		os.Exit(1)
	}
	logs.Debug("Last ID is : %d", ID)

	contacts, err := findContacts(ID, db)

	for _, contact := range contacts {
		contact, err := addAddresses(contact, db)
		if err != nil {
			logs.Critical(err)
			os.Exit(1)
		}
		logs.Debug("Indexing contact %d : %s %s", contact.ID, contact.Surname, contact.Firstname)
		err = index(contact, client)
		if err != nil {
			logs.Critical(err)
			os.Exit(1)
		}
	}

	return nil
}
Example #27
0
	},
}

var dbMigrate = &cobra.Command{
	Use:   "db:migrate",
	Short: "Migrate the databse to the latest version",
	Run: func(cmd *cobra.Command, args []string) {
		if db == nil {
			LoadEnv(true)
		}

		var err error
		if err = db.AutoMigrate(
			&models.Line{},
			&models.ContentType{},
			&models.File{},
			&models.Pattern{},
			&models.PatternPart{},
			&models.Workflow{},
		).Error; err != nil {
			l.Fatal("Could not automigrate.", err)
			return
		}

		path := filepath.Join(os.Getenv("GOPATH"), "/src/github.com/Bnei-Baruch/mms-file-manager/migrations")
		/*
			dbConf := goose.DBConf{
				MigrationsDir: path,
				Driver: goose.DBDriver{
					Name:    "postgres",
					DSN:     os.Getenv("DATABASE_URL"),
					Dialect: goose.PostgresDialect{},
Example #28
0
func New(db *gorm.DB) i18n.Backend {
	db.AutoMigrate(&Translation{})
	return &Backend{DB: db}
}
Example #29
0
func AddTables(db *gorm.DB) error {
	var err error
	err = db.AutoMigrate(VisitorPageRequest{}).Error
	if err != nil {
		return err
	}
	err = db.AutoMigrate(VisitorMouseEvents{}).Error
	if err != nil {
		return err
	}
	err = db.AutoMigrate(VisitorKeyboardEvents{}).Error
	if err != nil {
		return err
	}
	err = db.AutoMigrate(UserPageRequest{}).Error
	if err != nil {
		return err
	}
	err = db.AutoMigrate(UserMouseEvents{}).Error
	if err != nil {
		return err
	}
	err = db.AutoMigrate(UserKeyboardEvents{}).Error
	if err != nil {
		return err
	}
	return nil
}
Example #30
0
func ListenAndServe(addr string, auth Auth, dbType DatabaseType) {
	var db *gorm.DB
	var err error

	if dbType == DB_MySQL {
		dbConn := fmt.Sprintf("%s:%s@tcp(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)
}