-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres.go
67 lines (53 loc) · 1.72 KB
/
postgres.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package rdbms
import (
"database/sql"
"errors"
"fmt"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/lib/pq"
)
type postgresWrapper struct {
*rdbms
}
func NewPostgres(cfg *Config) (RDBMS, error) {
connString := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
cfg.Host, cfg.Port, cfg.Username, cfg.Password, cfg.Database,
)
db, err := sql.Open("postgres", connString)
if err != nil {
return nil, fmt.Errorf("Error openning connection:\n%v", err)
}
if err = db.Ping(); err != nil {
return nil, fmt.Errorf("Error ping database:\n%v", err)
}
return &postgresWrapper{&rdbms{db: db}}, nil
}
func (db *postgresWrapper) Migrate(source string, direction MigrateDirection) error {
var migrator func(m *migrate.Migrate) error
switch direction {
case MigrateUp:
migrator = func(m *migrate.Migrate) error { return m.Up() }
case MigrateDown:
migrator = func(m *migrate.Migrate) error { return m.Down() }
default:
return errors.New("invalid migration direction has been given")
}
return db.migrate(source, migrator)
}
func (db *postgresWrapper) migrate(source string, migrator func(*migrate.Migrate) error) error {
instance, err := postgres.WithInstance(db.db, &postgres.Config{})
if err != nil {
return fmt.Errorf("Error creating migrate instance\n%v", err)
}
migration, err := migrate.NewWithDatabaseInstance(source, "postgres", instance)
if err != nil {
return fmt.Errorf("Error loading migration files\n%v", err)
}
if err := migrator(migration); err != nil && err != migrate.ErrNoChange {
return fmt.Errorf("Error doing migrations\n%v", err)
}
return nil
}