Пример #1
1
// SetUp creates the database.
func SetUp(seed string) {
	conf := newConf()

	// Retrieve the original DSN value
	originalDSN := conf.Driver.OpenStr

	// Remove the database name
	conf.Driver.OpenStr = strings.Replace(conf.Driver.OpenStr, "DBNAME", "", -1)
	db, err := sql.Open(conf.Driver.Name, conf.Driver.OpenStr)
	if err != nil {
		log.WithError(fail.Trace(err)).Fatal("Could not open db connection")
	}

	// Create a database for this test.
	dbName := "test_" + seed
	_, err = db.Query("CREATE DATABASE IF NOT EXISTS " + dbName)
	if err != nil {
		log.WithError(fail.Trace(err)).WithField("name", dbName).Fatal("Could not create database")
	}

	// Replace the dsn for Goose's sake with the new database name
	conf.Driver.OpenStr = strings.Replace(originalDSN, "DBNAME", dbName, -1)
	os.Setenv("DATABASE_URL", conf.Driver.OpenStr)

	// Get the migrations to run then run them
	target, err := goose.GetMostRecentDBVersion(conf.MigrationsDir)
	if err != nil {
		log.WithError(fail.Trace(err)).Fatal("Could not get most recent db version")
	}

	if err := goose.RunMigrations(conf, conf.MigrationsDir, target); err != nil {
		log.WithError(fail.Trace(err)).Fatal("Could not run setup migrations")
	}
}
Пример #2
0
func upRun(cmd *Command, args ...string) {

	conf, err := dbConfFromFlags()
	if err != nil {
		log.Fatal(err)
	}

	target, err := goose.GetMostRecentDBVersion(conf.MigrationsDir)
	if err != nil {
		log.Fatal(err)
	}

	if err := goose.RunMigrations(conf, conf.MigrationsDir, target); err != nil {
		log.Fatal(err)
	}
}