forked from xakep666/mongo-migrate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
migrate.go
261 lines (220 loc) · 6.58 KB
/
migrate.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Package migrate allows to perform versioned migrations in your MongoDB.
package migrate
import (
"context"
"errors"
"fmt"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type collectionSpecification struct {
Name string `bson:"name"`
Type string `bson:"type"`
}
type versionRecord struct {
Version uint64 `bson:"version"`
Description string `bson:"description,omitempty"`
Timestamp time.Time `bson:"timestamp"`
}
const defaultMigrationsCollection = "migrations"
// AllAvailable used in "Up" or "Down" methods to run all available migrations.
const AllAvailable = -1
// Migrate is type for performing migrations in provided database.
// Database versioned using dedicated collection.
// Each migration applying ("up" and "down") adds new document to collection.
// This document consists migration version, migration description and timestamp.
// Current database version determined as version in latest added document (biggest "_id") from collection mentioned above.
type Migrate struct {
db *mongo.Database
migrations []Migration
migrationsCollection string
log Logger
}
func NewMigrate(db *mongo.Database, migrations ...Migration) *Migrate {
internalMigrations := make([]Migration, len(migrations))
copy(internalMigrations, migrations)
return &Migrate{
db: db,
migrations: internalMigrations,
migrationsCollection: defaultMigrationsCollection,
}
}
// SetMigrationsCollection replaces name of collection for storing migration information.
// By default, it is "migrations".
func (m *Migrate) SetMigrationsCollection(name string) {
m.migrationsCollection = name
}
func (m *Migrate) isCollectionExist(ctx context.Context, name string) (isExist bool, err error) {
collections, err := m.getCollections(ctx)
if err != nil {
return false, err
}
for _, c := range collections {
if name == c.Name {
return true, nil
}
}
return false, nil
}
func (m *Migrate) createCollectionIfNotExist(ctx context.Context, name string) error {
exist, err := m.isCollectionExist(ctx, name)
if err != nil {
return err
}
if exist {
return nil
}
command := bson.D{bson.E{Key: "create", Value: name}}
if err = m.db.RunCommand(ctx, command).Err(); err != nil {
return err
}
return nil
}
func (m *Migrate) getCollections(ctx context.Context) (collections []collectionSpecification, err error) {
cursor, err := m.db.ListCollections(ctx, bson.D{})
if err != nil {
return nil, err
}
if cursor != nil {
defer func(cursor *mongo.Cursor) {
curErr := cursor.Close(ctx)
if curErr != nil {
if err != nil {
err = fmt.Errorf("migrate: get collection failed: %w", err)
} else {
err = curErr
}
}
}(cursor)
}
for cursor.Next(ctx) {
var collection collectionSpecification
err := cursor.Decode(&collection)
if err != nil {
return nil, err
}
if len(collection.Type) == 0 || collection.Type == "collection" {
collections = append(collections, collection)
}
}
if err := cursor.Err(); err != nil {
return nil, err
}
return
}
// Version returns current database version and comment.
func (m *Migrate) Version(ctx context.Context) (uint64, string, error) {
if err := m.createCollectionIfNotExist(ctx, m.migrationsCollection); err != nil {
return 0, "", err
}
filter := bson.D{{}}
sort := bson.D{bson.E{Key: "_id", Value: -1}}
opts := options.FindOne().SetSort(sort)
// find record with the greatest id (assuming it`s latest also)
result := m.db.Collection(m.migrationsCollection).FindOne(ctx, filter, opts)
err := result.Err()
switch {
case errors.Is(err, mongo.ErrNoDocuments):
return 0, "", nil
case err != nil:
return 0, "", err
}
var rec versionRecord
if err := result.Decode(&rec); err != nil {
return 0, "", err
}
return rec.Version, rec.Description, nil
}
// SetVersion forcibly changes database version to provided one.
func (m *Migrate) SetVersion(ctx context.Context, version uint64, description string) error {
rec := versionRecord{
Version: version,
Timestamp: time.Now().UTC(),
Description: description,
}
_, err := m.db.Collection(m.migrationsCollection).InsertOne(ctx, rec)
if err != nil {
return err
}
return nil
}
// Up performs "up" migrations to latest available version.
// If n<=0 all "up" migrations with newer versions will be performed.
// If n>0 only n migrations with newer version will be performed.
func (m *Migrate) Up(ctx context.Context, n int) error {
currentVersion, _, err := m.Version(ctx)
if err != nil {
return err
}
if n <= 0 || n > len(m.migrations) {
n = len(m.migrations)
}
migrationSort(m.migrations)
for i, p := 0, 0; i < len(m.migrations) && p < n; i++ {
migration := m.migrations[i]
if migration.Version <= currentVersion || migration.Up == nil {
continue
}
p++
if err := migration.Up(ctx, m.db); err != nil {
return err
}
if err := m.SetVersion(ctx, migration.Version, migration.Description); err != nil {
return err
}
m.printUp(migration.Version, migration.Description)
}
return nil
}
// Down performs "down" migration to the oldest available version.
// If n<=0 all "down" migrations with older version will be performed.
// If n>0 only n migrations with older version will be performed.
func (m *Migrate) Down(ctx context.Context, n int) error {
currentVersion, _, err := m.Version(ctx)
if err != nil {
return err
}
if n <= 0 || n > len(m.migrations) {
n = len(m.migrations)
}
migrationSort(m.migrations)
for i, p := len(m.migrations)-1, 0; i >= 0 && p < n; i-- {
migration := m.migrations[i]
if migration.Version > currentVersion || migration.Down == nil {
continue
}
p++
if err := migration.Down(ctx, m.db); err != nil {
return err
}
var prevMigration Migration
if i == 0 {
prevMigration = Migration{Version: 0}
} else {
prevMigration = m.migrations[i-1]
}
if err := m.SetVersion(ctx, prevMigration.Version, prevMigration.Description); err != nil {
return err
}
m.printDown(migration.Version, migration.Description)
}
return nil
}
// SetLogger sets a logger to print the migration process
func (m *Migrate) SetLogger(log Logger) {
m.log = log
}
func (m *Migrate) printUp(migrationVersion uint64, migrationDescription string) {
m.printf("Migrated UP: %d %s", migrationVersion, migrationDescription)
}
func (m *Migrate) printDown(migrationVersion uint64, migrationDescription string) {
m.printf("Migrated DOWN: %d %s", migrationVersion, migrationDescription)
}
func (m *Migrate) printf(msg string, args ...any) {
if m.log == nil {
return
}
m.log.Printf(msg, args...)
}