// clearDB initializes the database, upgrading it if needed, and removes all // data to ensure that the test begins with a clean slate. Returns a MySQLTestDatabase // which must be closed after the test finishes. func clearDB(t *testing.T) *testutil.MySQLTestDatabase { failMsg := "Database initialization failed. Do you have the test database set up properly? Details: %v" // Set up the database. testDb := testutil.SetupMySQLTestDatabase(t, alerting.MigrationSteps()) conf := testutil.LocalTestDatabaseConfig(alerting.MigrationSteps()) DB, err := sqlx.Open("mysql", conf.MySQLString()) assert.Nil(t, err, failMsg) alerting.DB = DB return testDb }
func main() { defer common.LogPanic() // Set up flags. dbConf := database.ConfigFromFlags(alerting.PROD_DB_HOST, alerting.PROD_DB_PORT, database.USER_ROOT, alerting.PROD_DB_NAME, alerting.MigrationSteps()) // Global init to initialize glog and parse arguments. common.Init() if err := dbConf.PromptForPassword(); err != nil { glog.Fatal(err) } vdb, err := dbConf.NewVersionedDB() if err != nil { glog.Fatal(err) } // Get the current database version maxDBVersion := vdb.MaxDBVersion() glog.Infof("Latest database version: %d", maxDBVersion) dbVersion, err := vdb.DBVersion() if err != nil { glog.Fatalf("Unable to retrieve database version. Error: %s", err) } glog.Infof("Current database version: %d", dbVersion) if dbVersion < maxDBVersion { glog.Infof("Migrating to version: %d", maxDBVersion) err = vdb.Migrate(maxDBVersion) if err != nil { glog.Fatalf("Unable to retrieve database version. Error: %s", err) } } glog.Infoln("Database migration finished.") }