Beispiel #1
0
func (self *DBRcs) Init(config *util.MzConfig) (err error) {

	dsn := fmt.Sprintf("user=%s password=%s host=%s dbname=%s sslmode=%s",
		config.Get("db.user", "user"),
		config.Get("db.password", "password"),
		config.Get("db.host", "localhost"),
		config.Get("db.db", "postgres"),
		config.Get("db.sslmode", "disable"))

	if self.Db, err = sql.Open("postgres", dsn); err != nil {
		return err
	}

	if _, err = self.Db.Exec("set search_path to public"); err != nil {
		return err
	}

	self.Db.SetMaxIdleConns(100)
	if err = self.Db.Ping(); err != nil {
		return err
	}

	if err = self.InitVersioning(); err != nil {
		return err
	}

	return err
}
Beispiel #2
0
func applyPostgresUpdates(config *util.MzConfig) error {
	if config.Get("ddl.create", "") != "" ||
		config.GetFlag("ddl.upgrade") ||
		config.Get("ddl.downgrade", "") != "" ||
		config.GetFlag("ddl.log") {
		if config.Get("ddl.create", "") != "" && config.GetFlag("ddl.upgrade") {
			log.Fatalf("Invalid DDL options.  You can only specify one DDL command at a time.")
			return errors.New("Invalid option")
		}

		rcs := new(DBRcs)
		rcs.Init(config)
		if create := config.Get("ddl.create", ""); create != "" {
			if _, _, err := rcs.CreateNextRev("sql/patches", create); err != nil {
				log.Fatalf("Could not create a new revision: %s", err.Error())
			}
			return errors.New("Invalid revision")
		}

		if config.GetFlag("ddl.upgrade") {
			err := rcs.Upgrade("sql/patches", true)
			if err != nil {
				log.Fatalf("Could not upgrade database: %s", err.Error())
			}
			return errors.New("Invalid database upgrade")
		}

		if down := config.Get("ddl.downgrade", ""); down != "" {
			err := rcs.Downgrade("sql/patches", down)
			if err != nil {
				log.Fatalf("Could not downgrade database: %s", err.Error())
			}
			return err
		}

		if config.GetFlag("ddl.log") {
			err := rcs.Changelog("sql/patches")
			if err != nil {
				log.Fatalf("Could not get changelog: %s", err.Error())
			}
			return err
		}
	}
	return nil
}
Beispiel #3
0
// Open the database.
func OpenPostgres(config *util.MzConfig, logger util.Logger, metrics util.Metrics) (storage Storage, err error) {

	dsn := fmt.Sprintf("user=%s password=%s host=%s dbname=%s sslmode=%s",
		config.Get("db.user", "user"),
		config.Get("db.password", "password"),
		config.Get("db.host", "localhost"),
		config.Get("db.db", "wmf"),
		config.Get("db.sslmode", "disable"))
	logCat := "storage"
	// default expry is 5 days
	defExpry, err := strconv.ParseInt(config.Get("db.default_expry", "432000"), 0, 64)
	if err != nil {
		defExpry = 432000
	}

	if err = applyPostgresUpdates(config); err != nil {
		return nil, err
	}

	db, err := sql.Open("postgres", dsn)
	if err != nil {
		panic("Storage is unavailable: " + err.Error() + "\n")
	}
	db.SetMaxIdleConns(100)
	if err = db.Ping(); err != nil {
		return nil, err
	}
	maxDev, err := strconv.ParseInt(config.Get("db.max_devices_per_user", "1"), 0, 64)
	if err != nil {
		maxDev = 1
	}

	return &pgStore{
		config:   config,
		logger:   logger,
		logCat:   logCat,
		defExpry: defExpry,
		maxDev:   maxDev,
		metrics:  metrics,
		dsn:      dsn,
		db:       db}, nil
}