forked from custompro98/rambler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
183 lines (148 loc) · 4.17 KB
/
service.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/bradfitz/slice"
"github.com/wizehire/rambler/driver"
_ "github.com/wizehire/rambler/driver/mysql"
_ "github.com/wizehire/rambler/driver/postgresql"
_ "github.com/wizehire/rambler/driver/sqlite"
)
var (
// ErrNilMigration is returned when the service is given a nil migration
ErrNilMigration = errors.New("nil migration")
)
// Service is the struct that gather operations to manipulate the
// database and migrations on disk
type Service struct {
conn driver.Conn
env Environment
dryRun bool
}
// NewService initialize a new service with the given environment
func NewService(env Environment, dryRun bool) (*Service, error) {
fi, err := os.Stat(env.Directory)
if err != nil {
return nil, fmt.Errorf("directory %s unavailable: %s", env.Directory, err.Error())
}
if !fi.Mode().IsDir() {
return nil, fmt.Errorf("%s isn't a directory", env.Directory)
}
driver, err := driver.Get(env.Driver)
if err != nil {
return nil, fmt.Errorf("unable to initialize driver: %s", err.Error())
}
conn, err := driver.New(env.Config)
if err != nil {
return nil, fmt.Errorf("unable to initialize connection: %s", err.Error())
}
return &Service{
conn: conn,
env: env,
dryRun: dryRun,
}, nil
}
// Initialized check if the migration table exists in the
// database
func (s Service) Initialized() (bool, error) {
return s.conn.HasTable()
}
// Initialize create the migration table in the database
func (s Service) Initialize() error {
return s.conn.CreateTable()
}
// Available return the migrations in the environment's directory sorted in
// ascending lexicographic order.
func (s Service) Available() ([]*Migration, error) {
files, _ := filepath.Glob(filepath.Join(s.env.Directory, "*.sql")) // The only possible error here is a pattern error
var migrations []*Migration
for _, file := range files {
migration, err := NewMigration(file)
if err != nil {
return nil, err
}
migrations = append(migrations, migration)
}
slice.Sort(migrations, func(i, j int) bool {
return migrations[i].Name < migrations[j].Name
})
return migrations, nil
}
// Applied return the migrations in the environment's directory that are marked
// as applied in the database sorted in ascending lexicographic order.
func (s Service) Applied() ([]*Migration, error) {
files, err := s.conn.GetApplied()
if err != nil {
return nil, err
}
var migrations []*Migration
for _, file := range files {
migration, err := NewMigration(filepath.Join(s.env.Directory, file))
if err != nil {
return nil, err
}
migrations = append(migrations, migration)
}
slice.Sort(migrations, func(i, j int) bool {
return migrations[i].Name < migrations[j].Name
})
return migrations, nil
}
// Apply execute the up statements of the given migration to the
// database then mark the migration as applied
func (s Service) Apply(migration *Migration, save bool) error {
if migration == nil {
return ErrNilMigration
}
for _, statement := range migration.Up() {
if s.dryRun {
logger.Info("statement: %s", statement)
continue
}
err := s.conn.Execute(statement)
if err != nil {
return fmt.Errorf("unable to apply migration %s: %s\n%s", migration.Name, err, statement)
}
}
if s.dryRun {
return nil
}
if !save {
return nil
}
err := s.conn.AddApplied(migration.Name)
if err != nil {
return fmt.Errorf("unable to mark migration %s as applied: %s", migration.Name, err)
}
return nil
}
// Reverse execute the down statements of the given migration to the
// database then mark the migration as not applied
func (s Service) Reverse(migration *Migration, save bool) error {
if migration == nil {
return ErrNilMigration
}
for _, statement := range migration.Down() {
if s.dryRun {
logger.Info("statement: %s", statement)
continue
}
err := s.conn.Execute(statement)
if err != nil {
return fmt.Errorf("unable to reverse migration %s: %s\n%s", migration.Name, err, statement)
}
}
if s.dryRun {
return nil
}
if !save {
return nil
}
err := s.conn.RemoveApplied(migration.Name)
if err != nil {
return fmt.Errorf("unable to mark migration %s as not applied: %s", migration.Name, err)
}
return nil
}