// 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} }
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) }
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 }
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 }
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 }
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 }
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 }
func Migrate(migrations string) error { migrator, _ := gomigrate.NewMigrator(DB.DB, gomigrate.Mysql{}, migrations) return migrator.Migrate() }
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 }