Пример #1
0
func ResetTestDatabase(dbmap *gorp.DbMap) {
	conf := &goose.DBConf{
		MigrationsDir: filepath.Join("..", "db", "migrations"),
		Env:           "test",
		Driver: goose.DBDriver{
			Name:    "sqlite3",
			OpenStr: ":memory:", // this is actually never used as we just migrate dbmap.Db
			Import:  "github.com/mattn/go-sqlite3",
			Dialect: &goose.Sqlite3Dialect{},
		},
		PgSchema: "",
	}

	// Not merged yet but for future:
	os.Setenv("GOOSE_SILENT_MIGRATION", "1")

	target, err := goose.GetMostRecentDBVersion(conf.MigrationsDir)
	if err != nil {
		panic(fmt.Sprintf("cannot get recent db version with goose: %v\n", err))
	}

	err = goose.RunMigrationsOnDb(conf, conf.MigrationsDir, target, dbmap.Db)
	if err != nil {
		panic(fmt.Sprintf("cannot run migration with goose: %v\n", err))
	}
}
Пример #2
0
// migrate runs all available migrations on a pgSQL database.
func migrate(source string) error {
	log.Info("running database migrations")

	_, filename, _, _ := runtime.Caller(1)
	migrationDir := filepath.Join(filepath.Dir(filename), "/migrations/")
	conf := &goose.DBConf{
		MigrationsDir: migrationDir,
		Driver: goose.DBDriver{
			Name:    "postgres",
			OpenStr: source,
			Import:  "github.com/lib/pq",
			Dialect: &goose.PostgresDialect{},
		},
	}

	// Determine the most recent revision available from the migrations folder.
	target, err := goose.GetMostRecentDBVersion(conf.MigrationsDir)
	if err != nil {
		return fmt.Errorf("pgsql: could not get most recent migration: %v", err)
	}

	// Run migrations.
	err = goose.RunMigrations(conf, conf.MigrationsDir, target)
	if err != nil {
		return fmt.Errorf("pgsql: an error occured while running migrations: %v", err)
	}

	log.Info("database migration ran successfully")
	return nil
}
Пример #3
0
func GetMigrationInfo() (latest_db_version int64, migrations []*goose.Migration, err error) {
	goose_conf := generateGooseDbConf()
	db := models.GetDbSession()

	latest_db_version, _ = goose.GetMostRecentDBVersion(goose_conf.MigrationsDir)
	current_db_version, _ := goose.EnsureDBVersion(goose_conf, db.Db)
	migrations, _ = goose.CollectMigrations(goose_conf.MigrationsDir, current_db_version, latest_db_version)

	return latest_db_version, migrations, err
}
Пример #4
0
// Setup initializes the Conn object
// It also populates the Gophish Config object
func Setup() error {
	create_db := false
	if _, err = os.Stat(config.Conf.DBPath); err != nil || config.Conf.DBPath == ":memory:" {
		create_db = true
	}
	// Setup the goose configuration
	migrateConf := &goose.DBConf{
		MigrationsDir: config.Conf.MigrationsPath,
		Env:           "production",
		Driver: goose.DBDriver{
			Name:    "sqlite3",
			OpenStr: config.Conf.DBPath,
			Import:  "github.com/mattn/go-sqlite3",
			Dialect: &goose.Sqlite3Dialect{},
		},
	}
	// Get the latest possible migration
	latest, err := goose.GetMostRecentDBVersion(migrateConf.MigrationsDir)
	if err != nil {
		Logger.Println(err)
		return err
	}
	// Open our database connection
	db, err = gorm.Open("sqlite3", config.Conf.DBPath)
	db.LogMode(false)
	db.SetLogger(Logger)
	db.DB().SetMaxOpenConns(1)
	if err != nil {
		Logger.Println(err)
		return err
	}
	// Migrate up to the latest version
	err = goose.RunMigrationsOnDb(migrateConf, migrateConf.MigrationsDir, latest, db.DB())
	if err != nil {
		Logger.Println(err)
		return err
	}
	//If the database didn't exist, we need to create the admin user
	if create_db {
		//Create the default user
		initUser := User{
			Username: "******",
			Hash:     "$2a$10$IYkPp0.QsM81lYYPrQx6W.U6oQGw7wMpozrKhKAHUBVL4mkm/EvAS", //gophish
		}
		initUser.ApiKey = generateSecureKey()
		err = db.Save(&initUser).Error
		if err != nil {
			Logger.Println(err)
			return err
		}
	}
	return nil
}
Пример #5
0
func CreateOrUpdate() {
	conf, err := config()
	if err != nil {
		log.Panic(err)
	}
	dir := conf.MigrationsDir
	target, err := goose.GetMostRecentDBVersion(dir)
	if err != nil {
		log.Panic(err)
	}
	if err := goose.RunMigrations(conf, dir, target); err != nil {
		log.Panic(err)
	}
}
Пример #6
0
func Up() {

	var dirpath string = viper.GetString("GOOSE_DIR")
	cfg := newGooseConf()
	fmt.Printf("Open str: %s", cfg.Driver.OpenStr)
	version, err := goose.GetMostRecentDBVersion(dirpath)
	if err != nil {
		log.Fatal(err)
	}

	err = goose.RunMigrations(cfg, dirpath, version)
	if err != nil {
		log.Fatal(err)
	}
}
Пример #7
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)
	}
}
Пример #8
0
func Down() {
	var dirpath string = viper.GetString("GOOSE_DIR")
	cfg := newGooseConf()
	version, err := goose.GetMostRecentDBVersion(dirpath)
	if err != nil {
		log.Fatal(err)
	}

	previous, err := goose.GetPreviousDBVersion(dirpath, version)
	if err != nil {
		log.Fatal(err)
	}
	err = goose.RunMigrations(cfg, dirpath, previous)
	if err != nil {
		log.Fatal(err)
	}
}
Пример #9
0
func upRun(cmd *Command, args ...string) {

	confs, err := loadConfig()
	if err != nil {
		log.Fatal(err)
	}

	for _, conf := range confs {
		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)
		}
	}
}
Пример #10
0
func newTestDb(t *testing.T) *sql.DB {
	testConf, err := goose.NewDBConf(dbConfigDirectory, "test", "")
	checkErr(t, err)

	db, err := goose.OpenDBFromDBConf(testConf)
	checkErr(t, err)

	currentVersion, err := goose.EnsureDBVersion(testConf, db)
	checkErr(t, err)

	newestVersion, err := goose.GetMostRecentDBVersion(migrationsDirectory)
	checkErr(t, err)

	if currentVersion != newestVersion {
		t.Errorf("test DB not fully migrated. current version: %d, possible version: %d", currentVersion, newestVersion)
	}

	return db
}
Пример #11
0
func (db DB) Migrate(migrationsDir string) {
	dbConf := &goose.DBConf{
		MigrationsDir: migrationsDir,
		Env:           "notifications",
		Driver: goose.DBDriver{
			Dialect: &goose.MySqlDialect{},
		},
	}

	target, err := goose.GetMostRecentDBVersion(dbConf.MigrationsDir)
	if err != nil {
		panic(err)
	}

	err = goose.RunMigrationsOnDb(dbConf, dbConf.MigrationsDir, target, db.Connection.Db)
	if err != nil {
		panic(err)
	}
}
Пример #12
0
func isDBMigrated(db *sql.DB) (bool, error) {
	dbconf, err := goose.NewDBConf(*dbConfDir, *env, "")
	if err != nil {
		return false, err
	}

	currentVersion, err := goose.EnsureDBVersion(dbconf, db)
	if err != nil {
		return false, err
	}

	newestVersion, err := goose.GetMostRecentDBVersion(*migrationDir)
	if err != nil {
		return false, err
	}

	if currentVersion != newestVersion {
		return false, nil
	}

	return true, nil
}