Ejemplo n.º 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))
	}
}
Ejemplo n.º 2
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
}
Ejemplo n.º 3
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)
	}
}