예제 #1
0
파일: db.go 프로젝트: hobeone/gonab
// NewMemoryDBHandle creates a new in memory database.  Only used for testing.
// The name of the database is a random string so multiple tests can run in
// parallel with their own database.  This will setup the database with the
// all the tables as well.
func NewMemoryDBHandle(verbose bool, loadFixtures bool) *Handle {
	// The DSN is super important here. https://www.sqlite.org/inmemorydb.html
	// We want a named in memory db with a shared cache so that multiple
	// connections from the database layer share the cache but each call to this
	// function will return a different named database so unit tests don't stomp
	// on each other.
	gormdb := openDB("sqlite3", fmt.Sprintf("file:%s?mode=memory&cache=shared", randString()), verbose)

	migrator, err := gomigrate.NewMigrator(gormdb.DB(), gomigrate.Sqlite3{}, "../db/migrations/sqlite3")
	if err != nil {
		panic(err)
	}
	err = migrator.Migrate()
	if err != nil {
		panic(err)
	}

	if loadFixtures {
		// load Fixtures
		migrator, err = gomigrate.NewMigrator(gormdb.DB(), gomigrate.Sqlite3{}, "../db/testdata/fixtures")
		if err != nil {
			panic(err)
		}
		err = migrator.Migrate()
		if err != nil {
			panic(err)
		}
	}

	return &Handle{DB: gormdb}
}
예제 #2
0
파일: dbutil.go 프로젝트: karampok/gocf
func RestoreData() {
	migPath := fmt.Sprintf("%s/migrations_data", os.Getenv("TMPDIR"))
	files := mig.AssetNames()
	for _, f := range files {
		err := mig.RestoreAsset(os.Getenv("TMPDIR"), f)
		if err != nil {
			log.Fatal("Migration were not found")
		}
	}
	cStr := getConnectionStr()
	log.Printf("Connect str %s ", cStr)
	db, err := sql.Open("mysql", cStr)
	if err != nil {
		log.Fatal("Can not open DB:", err)
	}
	defer db.Close()

	var version string
	db.QueryRow("SELECT VERSION()").Scan(&version)
	log.Printf("Connected to :%s\n", version)

	if err := db.Ping(); err != nil {
		log.Fatal("DB Error Ping", err.Error())
	}

	if _, err := os.Stat(migPath); os.IsNotExist(err) {
		fmt.Printf("no such file or directory: %s", migPath)
		return
	}
	migrator, err := gomigrate.NewMigrator(db, gomigrate.Mariadb{}, migPath)
	if err := migrator.Migrate(); err != nil {
		log.Fatal("Can not execute migrations:", err)
	}
	dbstuff(db)
}
예제 #3
0
파일: db.go 프로젝트: gshilin/shidur-go
func SetupMigrator() (*gomigrate.Migrator, error) {
	db, dbError := DBConnect()
	if dbError != nil {
		return nil, errors.New("DB connection failed")
	}
	migrator, migError := gomigrate.NewMigrator(db, gomigrate.Postgres{}, "./migrations")
	return migrator, migError
}
예제 #4
0
func RunMigrations(db *sqlx.DB) error {
	migrator, err := gomigrate.NewMigrator(db.DB, gomigrate.Mysql{}, "./migrations")
	if err != nil {
		return err
	}
	if err := migrator.Migrate(); err != nil {
		return err
	}
	return nil
}
예제 #5
0
파일: createdb.go 프로젝트: hobeone/gonab
func createdb(c *kingpin.ParseContext) error {
	_, dbh := commonInit()

	d := dbh.DB.DB()
	migrator, err := gomigrate.NewMigrator(d, gomigrate.Mysql{}, "db/migrations/mysql")
	if err != nil {
		logrus.Fatalf("Error starting migration: %v", err)
	}
	err = migrator.Migrate()
	return err
}
예제 #6
0
func (a *App) buildMigrator() gomigrate.Migrator {
	db := (*a.Database).Driver()

	m, err := gomigrate.NewMigrator(
		db.(*sql.DB),
		gomigrate.Postgres{},
		"./migrations",
	)

	if err != nil {
		log.Fatalln("migrations failed", err.Error())
	}

	return *m
}
예제 #7
0
func init() {

	// create db connection
	db, err := sql.Open("mysql", config.DbUser+":"+config.DbPassword+"@"+config.DbServer+"/"+config.DbName+"?parseTime=true")
	if err != nil {
		log.Fatal("Database Error: ", err.Error())
	}
	database = db

	// create migrator
	m, _ := gomigrate.NewMigrator(database, gomigrate.Mysql{}, "database/migrations/")
	tableExists, _ := m.MigrationTableExists()
	if !tableExists {
		err = m.CreateMigrationsTable()
		if err != nil {
			log.Fatal("Migrator Error: ", err.Error())
		}
	}
	migrator = m

}
예제 #8
0
파일: mysql.go 프로젝트: byrnedo/apibase
func Migrate(migrations string) error {
	migrator, _ := gomigrate.NewMigrator(DB.DB, gomigrate.Mysql{}, migrations)
	return migrator.Migrate()
}
예제 #9
0
func getMigrator(c *DatabaseConfig) (m *gomigrate.Migrator, err error) {
	var db *sql.DB
	db, err = GetDB(c)
	m, err = gomigrate.NewMigrator(db, gomigrate.Postgres{}, "database/migrations")
	return
}