-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration.go
165 lines (130 loc) · 3.64 KB
/
migration.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
package migration
import (
"context"
"crypto/sha256"
"database/sql"
"encoding/hex"
"errors"
"fmt"
"time"
"github.com/lukasdietrich/groundwork/noorm"
)
var (
// ErrHashMismatch is returned when a changeset was already applied, but the queries changed.
ErrHashMismatch = errors.New("migration: hash does not match already applied version")
// ErrNameTooLong is returned when a changeset name exceeds the limit.
ErrNameTooLong = errors.New("migration: changeset name too long (max. 256)")
)
type Options struct {
// ChangelogTablename is the name of the table used to track already applied changesets.
ChangelogTablename string
}
func fillDefaults(opts *Options) *Options {
if opts == nil {
opts = &Options{}
}
if opts.ChangelogTablename == "" {
opts.ChangelogTablename = "database_changelog"
}
return opts
}
// Changeset represents a series of related queries to advance the database schema.
type Changeset interface {
// Name returns the name of the changeset.
// It is used to keep track of already applied changesets.
Name() string
// Queries returns the content of this changeset as raw sql.
Queries() (string, error)
}
// Up applies all changesets in order, if they have not already been applied.
// All changesets are applied within a transaction.
// When an error occurs, the process will stop, but previously applied changesets won't be rolled
// back.
// When a changeset was already applied, but does not match in content, an errors is returned.
// Up expects a groundwork/noorm.Querier to be present in the context (see WithDatabase).
func Up(ctx context.Context, changesets []Changeset, opts *Options) ([]Changeset, error) {
opts = fillDefaults(opts)
dao, err := newChangelogDao(ctx, opts.ChangelogTablename)
if err != nil {
return nil, err
}
if err := dao.setupTable(ctx); err != nil {
return nil, err
}
return (&migrator{dao}).up(ctx, changesets)
}
type migrator struct {
dao *changelogDao
}
func (m *migrator) up(ctx context.Context, changesets []Changeset) ([]Changeset, error) {
var applied []Changeset
for _, changeset := range changesets {
wasApplied, err := m.apply(ctx, changeset)
if err != nil {
return applied, fmt.Errorf("%w on changeset %q", err, changeset.Name())
}
if wasApplied {
applied = append(applied, changeset)
}
}
return applied, nil
}
func (m *migrator) apply(ctx context.Context, changeset Changeset) (bool, error) {
queries, err := changeset.Queries()
if err != nil {
return false, err
}
name := changeset.Name()
if len(name) > nameSize {
return false, ErrNameTooLong
}
hash := calculateHash(queries)
ctx, tx, err := noorm.Begin(ctx, nil)
if err != nil {
return false, err
}
defer tx.Rollback()
shouldApply, err := m.check(ctx, changeset, hash)
if err != nil {
return false, err
}
if shouldApply {
if _, err := noorm.Exec(ctx, noorm.SQL{Query: queries}); err != nil {
return false, err
}
entry := changelogEntry{
Name: changeset.Name(),
Hash: hash,
Time: time.Now().Format(timeFormat),
}
if err := m.dao.insert(ctx, &entry); err != nil {
return false, err
}
}
if err := tx.Commit(); err != nil {
return false, err
}
return shouldApply, nil
}
func (m *migrator) check(ctx context.Context, changeset Changeset, hash string) (bool, error) {
entry, err := m.dao.lookup(ctx, changeset.Name())
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return true, nil
}
return false, err
}
if entry == nil {
return true, nil
}
if hash != entry.Hash {
return false, ErrHashMismatch
}
return false, nil
}
func calculateHash(s string) string {
m := sha256.New()
m.Write([]byte(s))
h := m.Sum(nil)
return hex.EncodeToString(h)
}