Пример #1
0
// createDatabaseConn creates a new GORM database with the specified database
// configuration.
func (s *Server) createDatabaseConn() (*gorm.DB, error) {
	var (
		db               gorm.DB
		err              error
		dbCfg            = s.cfg.Db
		connectionString = fmt.Sprintf(
			"host=%v port=%v user=%v password=%v dbname=%v sslmode=disable",
			dbCfg.Host,
			dbCfg.Port,
			dbCfg.User,
			dbCfg.Password,
			dbCfg.Name,
		)
	)

	db, err = gorm.Open("postgres", connectionString)
	if err != nil {
		return nil, err
	}

	db.DB().SetMaxIdleConns(10)
	db.LogMode(s.cfg.DbLogMode)

	return &db, nil
}
Пример #2
0
// DBinit is a generic helper function that will try to connect to a database with the config in the input.
// Supported DB types:
// * postgres
// * sqlite3
func DBInit(dbConfig *DBConfig) (*gorm.DB, error) {
	var DB gorm.DB
	var err error
	switch dbConfig.DbType {
	case "postgres":
		conn := "dbname=%s user=%s password=%s host=%s sslmode=%s port=%d"
		conn = fmt.Sprintf(conn,
			dbConfig.DbName,
			dbConfig.Username,
			dbConfig.Password,
			dbConfig.Url,
			dbConfig.Sslmode,
			dbConfig.Port)
		DB, err = gorm.Open("postgres", conn)
	case "sqlite3":
		DB, err = gorm.Open("sqlite3", dbConfig.DbName)
	default:
		errorString := "Cannot connect. Unsupported DB type: (" + dbConfig.DbType + ")"
		log.Println(errorString)
		return nil, errors.New(errorString)
	}
	if err != nil {
		log.Println("Error!")
		return nil, err
	}

	if err = DB.DB().Ping(); err != nil {
		log.Println("Unable to verify connection to database")
		return nil, err
	}
	return &DB, nil
}
Пример #3
0
func InitGORM(dialect, args string) (*gorm.DB, error) {
	var db gorm.DB
	var err error

	var i int
retry:
	for {
		db, err = gorm.Open(dialect, args)
		switch {
		case err == nil:
			break retry
		case i >= RETRY:
			return nil, err
		default:
			logs.Error(err)
			i++
		}
		time.Sleep(TIMEOUT)
	}

	if db.DB().Ping() != nil {
		return &db, err
	}

	return &db, nil
}
Пример #4
0
func prepareDatabase(driver string, db *gorm.DB) error {
	if driver == "mysql" {
		db.DB().SetMaxIdleConns(0)
	}

	validations.RegisterCallbacks(
		db,
	)

	return nil
}
Пример #5
0
func loadData(paths []string, cnf *config.Config, db *gorm.DB) {
	for _, path := range paths {
		data, err := ioutil.ReadFile(path)
		if err != nil {
			log.Fatal(err)
		}

		if err := fixtures.Load(data, db.DB(), cnf.Database.Type); err != nil {
			log.Fatal(err)
		}
	}
}
Пример #6
0
func pingDatabase(driver string, db *gorm.DB) error {
	for i := 0; i < 30; i++ {
		err := db.DB().Ping()

		if err == nil {
			return nil
		}

		logrus.Infof("Database ping failed, retry in 1s")
		time.Sleep(time.Second)
	}

	return fmt.Errorf("Database ping attempts failed")
}
Пример #7
0
// LoadData loads fixtures
func LoadData(paths []string, cnf *config.Config, db *gorm.DB) error {
	for _, path := range paths {
		data, err := ioutil.ReadFile(path)
		if err != nil {
			return err
		}

		if err := fixtures.Load(data, db.DB(), cnf.Database.Type); err != nil {
			return err
		}
	}

	return nil
}
Пример #8
0
// 建立数据库连接
func opendb(dbname, dbuser, dbpass string) (*gorm.DB, error) {
	var (
		dbtype, dsn string
		db          gorm.DB
		err         error
	)

	if dbuser == "" {
		dbuser = config.GetStringDefault("dbuser", "root")
	}
	if dbpass == "" {
		dbpass = config.GetStringDefault("dbpass", "root")
	}

	dbtype = strings.ToLower(config.GetStringDefault("dbtype", "mysql"))
	if dbtype == "mysql" {
		dsn = fmt.Sprintf("%s:%s@%s(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local",
			dbuser,
			dbpass,
			config.GetStringDefault("dbproto", "tcp"),
			config.GetStringDefault("dbhost", "localhost"),
			config.GetIntDefault("dbport", 3306),
			dbname,
		)
	} else if dbtype == "pg" || dbtype == "postgres" || dbtype == "postgresql" {
		dbtype = "postgres"
		dsn = fmt.Sprintf("user=%s password=%s host=%s port=%d dbname=%s sslmode=disable",
			dbuser,
			dbpass,
			config.GetStringDefault("dbhost", "127.0.0.1"),
			config.GetIntDefault("dbport", 5432),
			dbname)
	}

	db, err = gorm.Open(dbtype, dsn)
	if err != nil {
		log.Println(err.Error())
		return &db, err
	}

	err = db.DB().Ping()
	if err != nil {
		log.Println(err.Error())
		return &db, err
	}

	return &db, nil
}
Пример #9
0
//Connect init gorm ORM.
func (orm *AppOrm) Connect(conf interfaces.Config) error {
	var db gorm.DB
	dbUri, err := conf.Get("DatabaseUri")
	if dbUri, ok := dbUri.(string); err == nil && ok {
		log.Println(dbUri)
		db, err = gorm.Open("mysql", dbUri)
	}
	if err != nil {
		log.Fatal(err.Error())
		return err
	}
	db.DB()

	db.DB().Ping()
	db.DB().SetMaxIdleConns(10)
	db.DB().SetMaxOpenConns(100)

	// Disable table name's pluralization
	db.SingularTable(true)
	if env, err := conf.Get("env"); err == nil && env == "dev" {
		db.LogMode(true)
	}
	orm.driver = &db
	return nil
}
Пример #10
0
func initDb(db gorm.DB) {
	defer fmt.Printf("Init DB DONE! \n")

	db.DB().Ping()
	db.DB().SetMaxIdleConns(10)
	db.DB().SetMaxOpenConns(100)

	db.CreateTable(&Erp{})
	db.CreateTable(&Entry{})

	db.CreateTable(&SyncField{})
	db.Model(&Entry{}).Related(&SyncField{}, "EntryId")

	db.CreateTable(&Decorator{})
	db.Model(&SyncField{}).Related(&Decorator{}, "DecoratorId")

	db.CreateTable(&SyncEvent{})

}
Пример #11
0
//ExtractDBSQL ---
func ExtractDBSQL() (gormdb chaospeddler.GormDB) {
	var err error
	var db *gorm.DB
	appEnv, _ := cfenv.Current()
	service, _ := appEnv.Services.WithName("sql-info")
	host := fmt.Sprintf("%v", service.Credentials["hostname"])
	port := fmt.Sprintf("%v", service.Credentials["port"])
	dbname := fmt.Sprintf("%v", service.Credentials["name"])
	user := fmt.Sprintf("%v", service.Credentials["username"])
	pass := fmt.Sprintf("%v", service.Credentials["password"])
	connectionString := user + ":" + pass + "@tcp(" + host + ":" + port + ")" + "/" + dbname + "?charset=utf8&parseTime=True&loc=Local"
	lo.G.Error("connection string: ", connectionString)

	if db, err = gorm.Open("mysql", connectionString); err == nil {
		db.DB()
		db.DB().Ping()
		db.DB().SetMaxIdleConns(10)
		db.DB().SetMaxOpenConns(100)
		db.SingularTable(true)
		db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(
			new(chaospeddler.ServiceInstance),
			new(chaospeddler.ServiceBinding),
		)
		gormdb = db

	} else {
		lo.G.Error("there was an error connecting to mysql: ", err)
		panic(err)
	}
	return
}
Пример #12
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://*****:*****@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
}
Пример #13
0
func NewDb(Cnf DbConfig) *Db {
	var err error
	var db *gorm.DB
	switch Cnf.DriverName {
	case "sqlite3":
		db, err = gorm.Open("sqlite3", Cnf.DataSourceName)
		logger.CheckFatal(err, "Got error when connect database")
	//this.DbMap = &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
	case "mysql":
		//db, err := sql.Open("mysql", "user:password@tcp(localhost:5555)/dbname?charset=utf8&parseTime=True&loc=Local")
		db, err = gorm.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=%s&parseTime=True&loc=Local",
			Cnf.UserName,
			Cnf.Password,
			Cnf.Host,
			Cnf.DataSourceName,
			Cnf.Encoding))
		logger.CheckFatal(err, "Got error when connect database")

	//this.DbMap = &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
	default:
		logger.Fatal("The types of database does not support:"+Cnf.DriverName, err)
	}

	db.DB()
	db.DB().Ping()
	db.DB().SetMaxIdleConns(10)
	db.DB().SetMaxOpenConns(100)

	// Disable table name's pluralization
	db.SingularTable(true)

	// construct a gorp DbMap

	//

	return &Db{Cnf: Cnf, db: db}
}
Пример #14
0
			&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{},
					Import:  "github.com/lib/pq",
				},
			}
		*/

		var target int64
		target, err = goose.GetMostRecentDBVersion(path)
		if err != nil {
			l.Fatal(err)
		}
		if err = goose.RunMigrations(db.DB(), path, target); err != nil {
			l.Fatal(err)
		}
	},
}
Пример #15
0
func main() {
	// --------------------------------------------------------------------
	// Parse flags
	// --------------------------------------------------------------------
	var configFilePath string
	var printConfig bool
	var migrateDB bool
	var scheduler *remoteworkitem.Scheduler
	flag.StringVar(&configFilePath, "config", "", "Path to the config file to read")
	flag.BoolVar(&printConfig, "printConfig", false, "Prints the config (including merged environment variables) and exits")
	flag.BoolVar(&migrateDB, "migrateDatabase", false, "Migrates the database to the newest version and exits.")
	flag.Parse()

	// Override default -config switch with environment variable only if -config switch was
	// not explicitly given via the command line.
	configSwitchIsSet := false
	flag.Visit(func(f *flag.Flag) {
		if f.Name == "config" {
			configSwitchIsSet = true
		}
	})
	if !configSwitchIsSet {
		if envConfigPath, ok := os.LookupEnv("ALMIGHTY_CONFIG_FILE_PATH"); ok {
			configFilePath = envConfigPath
		}
	}

	var err error
	if err = configuration.Setup(configFilePath); err != nil {
		logrus.Panic(nil, map[string]interface{}{
			"configFilePath": configFilePath,
			"err":            err,
		}, "failed to setup the configuration")
	}

	if printConfig {
		os.Exit(0)
	}

	// Initialized developer mode flag for the logger
	log.InitializeLogger(configuration.IsPostgresDeveloperModeEnabled())

	printUserInfo()

	var db *gorm.DB
	for {
		db, err = gorm.Open("postgres", configuration.GetPostgresConfigString())
		if err != nil {
			db.Close()
			log.Logger().Errorf("ERROR: Unable to open connection to database %v\n", err)
			log.Logger().Infof("Retrying to connect in %v...\n", configuration.GetPostgresConnectionRetrySleep())
			time.Sleep(configuration.GetPostgresConnectionRetrySleep())
		} else {
			defer db.Close()
			break
		}
	}

	if configuration.IsPostgresDeveloperModeEnabled() {
		db = db.Debug()
	}

	// Migrate the schema
	err = migration.Migrate(db.DB())
	if err != nil {
		log.Panic(nil, map[string]interface{}{
			"err": fmt.Sprintf("%+v", err),
		}, "failed migration")
	}

	// Nothing to here except exit, since the migration is already performed.
	if migrateDB {
		os.Exit(0)
	}

	// Make sure the database is populated with the correct types (e.g. bug etc.)
	if configuration.GetPopulateCommonTypes() {
		// set a random request ID for the context
		ctx, req_id := client.ContextWithRequestID(context.Background())
		log.Debug(ctx, nil, "Initializing the population of the database... Request ID: %v", req_id)

		if err := models.Transactional(db, func(tx *gorm.DB) error {
			return migration.PopulateCommonTypes(ctx, tx, workitem.NewWorkItemTypeRepository(tx))
		}); err != nil {
			log.Panic(ctx, map[string]interface{}{
				"err": fmt.Sprintf("%+v", err),
			}, "failed to populate common types")
		}
		if err := models.Transactional(db, func(tx *gorm.DB) error {
			return migration.BootstrapWorkItemLinking(ctx, link.NewWorkItemLinkCategoryRepository(tx), link.NewWorkItemLinkTypeRepository(tx))
		}); err != nil {
			log.Panic(ctx, map[string]interface{}{
				"err": fmt.Sprintf("%+v", err),
			}, "failed to bootstap work item linking")
		}
	}

	// Scheduler to fetch and import remote tracker items
	scheduler = remoteworkitem.NewScheduler(db)
	defer scheduler.Stop()
	scheduler.ScheduleAllQueries()

	// Create service
	service := goa.New("alm")

	// Mount middleware
	service.Use(middleware.RequestID())
	service.Use(middleware.LogRequest(configuration.IsPostgresDeveloperModeEnabled()))
	service.Use(gzip.Middleware(9))
	service.Use(jsonapi.ErrorHandler(service, true))
	service.Use(middleware.Recover())

	service.WithLogger(goalogrus.New(log.Logger()))

	publicKey, err := token.ParsePublicKey(configuration.GetTokenPublicKey())
	if err != nil {
		log.Panic(nil, map[string]interface{}{
			"err": fmt.Sprintf("%+v", err),
		}, "failed to parse public token")
	}

	// Setup Account/Login/Security
	identityRepository := account.NewIdentityRepository(db)
	userRepository := account.NewUserRepository(db)

	tokenManager := token.NewManager(publicKey)
	app.UseJWTMiddleware(service, jwt.New(publicKey, nil, app.NewJWTSecurity()))
	service.Use(login.InjectTokenManager(tokenManager))

	// Mount "login" controller
	oauth := &oauth2.Config{
		ClientID:     configuration.GetKeycloakClientID(),
		ClientSecret: configuration.GetKeycloakSecret(),
		Scopes:       []string{"user:email"},
		Endpoint: oauth2.Endpoint{
			AuthURL:  configuration.GetKeycloakEndpointAuth(),
			TokenURL: configuration.GetKeycloakEndpointToken(),
		},
	}

	appDB := gormapplication.NewGormDB(db)

	loginService := login.NewKeycloakOAuthProvider(oauth, identityRepository, userRepository, tokenManager, appDB)
	loginCtrl := NewLoginController(service, loginService, tokenManager)
	app.MountLoginController(service, loginCtrl)

	// Mount "status" controller
	statusCtrl := NewStatusController(service, db)
	app.MountStatusController(service, statusCtrl)

	// Mount "workitem" controller
	workitemCtrl := NewWorkitemController(service, appDB)
	app.MountWorkitemController(service, workitemCtrl)

	// Mount "workitemtype" controller
	workitemtypeCtrl := NewWorkitemtypeController(service, appDB)
	app.MountWorkitemtypeController(service, workitemtypeCtrl)

	// Mount "work item link category" controller
	workItemLinkCategoryCtrl := NewWorkItemLinkCategoryController(service, appDB)
	app.MountWorkItemLinkCategoryController(service, workItemLinkCategoryCtrl)

	// Mount "work item link type" controller
	workItemLinkTypeCtrl := NewWorkItemLinkTypeController(service, appDB)
	app.MountWorkItemLinkTypeController(service, workItemLinkTypeCtrl)

	// Mount "work item link" controller
	workItemLinkCtrl := NewWorkItemLinkController(service, appDB)
	app.MountWorkItemLinkController(service, workItemLinkCtrl)

	// Mount "work item comments" controller
	workItemCommentsCtrl := NewWorkItemCommentsController(service, appDB)
	app.MountWorkItemCommentsController(service, workItemCommentsCtrl)

	// Mount "work item relationships links" controller
	workItemRelationshipsLinksCtrl := NewWorkItemRelationshipsLinksController(service, appDB)
	app.MountWorkItemRelationshipsLinksController(service, workItemRelationshipsLinksCtrl)

	// Mount "comments" controller
	commentsCtrl := NewCommentsController(service, appDB)
	app.MountCommentsController(service, commentsCtrl)

	// Mount "tracker" controller
	c5 := NewTrackerController(service, appDB, scheduler)
	app.MountTrackerController(service, c5)

	// Mount "trackerquery" controller
	c6 := NewTrackerqueryController(service, appDB, scheduler)
	app.MountTrackerqueryController(service, c6)

	// Mount "space" controller
	spaceCtrl := NewSpaceController(service, appDB)
	app.MountSpaceController(service, spaceCtrl)

	// Mount "user" controller
	userCtrl := NewUserController(service, appDB, tokenManager)
	app.MountUserController(service, userCtrl)

	// Mount "search" controller
	searchCtrl := NewSearchController(service, appDB)
	app.MountSearchController(service, searchCtrl)

	// Mount "indentity" controller
	identityCtrl := NewIdentityController(service, appDB)
	app.MountIdentityController(service, identityCtrl)

	// Mount "users" controller
	usersCtrl := NewUsersController(service, appDB)
	app.MountUsersController(service, usersCtrl)

	// Mount "iterations" controller
	iterationCtrl := NewIterationController(service, appDB)
	app.MountIterationController(service, iterationCtrl)

	// Mount "spaceiterations" controller
	spaceIterationCtrl := NewSpaceIterationsController(service, appDB)
	app.MountSpaceIterationsController(service, spaceIterationCtrl)

	// Mount "userspace" controller
	userspaceCtrl := NewUserspaceController(service, db)
	app.MountUserspaceController(service, userspaceCtrl)

	// Mount "render" controller
	renderCtrl := NewRenderController(service)
	app.MountRenderController(service, renderCtrl)

	// Mount "areas" controller
	areaCtrl := NewAreaController(service, appDB)
	app.MountAreaController(service, areaCtrl)

	spaceAreaCtrl := NewSpaceAreasController(service, appDB)
	app.MountSpaceAreasController(service, spaceAreaCtrl)

	log.Logger().Infoln("Git Commit SHA: ", Commit)
	log.Logger().Infoln("UTC Build Time: ", BuildTime)
	log.Logger().Infoln("UTC Start Time: ", StartTime)
	log.Logger().Infoln("Dev mode:       ", configuration.IsPostgresDeveloperModeEnabled())

	http.Handle("/api/", service.Mux)
	http.Handle("/", http.FileServer(assetFS()))
	http.Handle("/favicon.ico", http.NotFoundHandler())

	// Start http
	if err := http.ListenAndServe(configuration.GetHTTPAddress(), nil); err != nil {
		log.Error(nil, map[string]interface{}{
			"addr": configuration.GetHTTPAddress(),
			"err":  err,
		}, "unable to connect to server")
		service.LogError("startup", "err", err)
	}

}