-
Notifications
You must be signed in to change notification settings - Fork 27
/
sqlmigrate.go
141 lines (113 loc) · 2.65 KB
/
sqlmigrate.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package sqlmigrate
import (
"database/sql"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"time"
"github.com/lib/pq"
"github.com/rubenv/sql-migrate"
)
type MigrationSet struct {
MigrationSet migrate.MigrationSet
Dir string
}
type ConnectionOptions struct {
DatabaseURL string
DatabaseSchema string
}
func NewMigrateSet(tableName string, dir string) MigrationSet {
return MigrationSet{
MigrationSet: migrate.MigrationSet{
TableName: tableName,
},
Dir: dir,
}
}
func (s MigrationSet) Create(name string) (fileName string, err error) {
const migrationTemplate = `-- +migrate Up
-- +migrate Down
`
fileName = fmt.Sprintf("%s-%s.sql", time.Now().Format("20060102150405"), name)
err = os.MkdirAll(s.Dir, os.ModePerm)
if err != nil {
return
}
err = ioutil.WriteFile(path.Join(s.Dir, fileName), []byte(migrationTemplate), os.ModePerm)
if err != nil {
return
}
log.Printf("new migration '%s' created.", fileName)
return
}
func (s MigrationSet) Up(opts ConnectionOptions, max int) (n int, err error) {
db, err := s.openDB(opts)
if err != nil {
return
}
source := s.makeSource(opts)
n, err = s.MigrationSet.ExecMax(db, "postgres", source, migrate.Up, max)
if err != nil {
return
}
log.Printf("applied %d migrations.", n)
return
}
func (s MigrationSet) Down(opts ConnectionOptions, max int) (n int, err error) {
db, err := s.openDB(opts)
if err != nil {
return
}
source := s.makeSource(opts)
n, err = s.MigrationSet.ExecMax(db, "postgres", source, migrate.Down, max)
if err != nil {
return
}
log.Printf("reverted %d migrations.", n)
return
}
func (s MigrationSet) Status(opts ConnectionOptions) (plans []*migrate.PlannedMigration, err error) {
db, err := s.openDB(opts)
if err != nil {
return
}
source := s.makeSource(opts)
plans, _, err = s.MigrationSet.PlanMigration(db, "postgres", source, migrate.Up, 0)
if err != nil {
return
}
if len(plans) == 0 {
log.Print("database schema is up-to-date")
} else {
log.Print("pending migrations:")
for _, plan := range plans {
log.Printf("%s\n", plan.Id)
}
}
return
}
func (s MigrationSet) openDB(opts ConnectionOptions) (db *sql.DB, err error) {
db, err = sql.Open("postgres", opts.DatabaseURL)
if err != nil {
return
}
if opts.DatabaseSchema != "" {
_, err = db.Exec(fmt.Sprintf("SET search_path TO %s", pq.QuoteIdentifier(opts.DatabaseSchema)))
if err != nil {
return
}
}
return
}
func (s MigrationSet) makeSource(opts ConnectionOptions) *TemplateMigrationSource {
return &TemplateMigrationSource{
OriginSource: &migrate.FileMigrationSource{
Dir: s.Dir,
},
Data: map[string]interface{}{
"SCHEMA": opts.DatabaseSchema,
},
}
}