-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres.go
More file actions
91 lines (75 loc) · 1.99 KB
/
postgres.go
File metadata and controls
91 lines (75 loc) · 1.99 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package yam
import (
"database/sql"
_ "github.com/lib/pq" // pg driver
)
type postgres struct {
db *sql.DB
closeDB bool
}
// assert postgres implements migrator
var _ Migrator = &postgres{}
// NewPostgres creates a new postgres migrator from a new connection which
// cleans itself up after migrations are complete.
func NewPostgres(url string) (*postgres, error) {
db, err := sql.Open("postgres", url)
if err != nil {
return nil, err
}
return &postgres{db: db, closeDB: true}, err
}
// NewPostgresFromDB creates a new postgres migrator from an existing connection
// which does not clean itself up after migrations are complete.
func NewPostgresFromDB(db *sql.DB) *postgres {
return &postgres{db: db, closeDB: false}
}
func (p *postgres) setup() error {
if err := p.db.Ping(); err != nil {
return err
}
var count int
query := `SELECT COUNT(1) FROM information_schema.tables WHERE table_name = $1 AND table_schema = (SELECT current_schema()) LIMIT 1`
if err := p.db.QueryRow(query, "data_migrations").Scan(&count); err != nil {
return err
}
if count == 1 {
return nil
}
query = `CREATE TABLE data_migrations (version bigint not null primary key)`
if _, err := p.db.Exec(query); err != nil {
return err
}
return nil
}
func (p *postgres) checkVersion(version int64) (bool, error) {
var throwAway int64
if err := p.db.QueryRow(`
SELECT * from data_migrations
WHERE version = $1
LIMIT 1;
`, version).Scan(&throwAway); err != nil {
if err == sql.ErrNoRows {
return true, nil
}
return false, err
}
return false, nil
}
func (p *postgres) writeVersion(version int64) error {
if _, err := p.db.Exec("INSERT INTO data_migrations VALUES ($1);", version); err != nil {
return err
}
return nil
}
func (p *postgres) deleteVersion(version int64) error {
if _, err := p.db.Exec("DELETE FROM data_migrations where version = $1;", version); err != nil {
return err
}
return nil
}
func (p *postgres) teardown() error {
if p.closeDB {
return p.db.Close()
}
return nil
}