forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrator_test.go
274 lines (221 loc) · 7.52 KB
/
migrator_test.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
262
263
264
265
266
267
268
269
270
271
272
273
274
package sqlite
import (
"context"
"embed"
"fmt"
"io/fs"
"os"
"testing"
"github.com/influxdata/influxdb/v2/kit/errors"
"github.com/influxdata/influxdb/v2/kit/migration"
"github.com/influxdata/influxdb/v2/sqlite/test_migrations"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
)
type tableInfo struct {
Cid int `db:"cid"`
Name string `db:"name"`
Db_type string `db:"type"`
Notnull int `db:"notnull"`
Dflt_value interface{} `db:"dflt_value"`
Pk int `db:"pk"`
}
func TestUp(t *testing.T) {
t.Parallel()
store, clean := NewTestStore(t)
defer clean(t)
upsOnlyAll, err := test_migrations.AllUp.ReadDir(".")
require.NoError(t, err)
upsOnlyFirst, err := test_migrations.FirstUp.ReadDir(".")
require.NoError(t, err)
migrator := NewMigrator(store, zaptest.NewLogger(t))
// empty db contains no migrations
names, err := store.allMigrationNames()
require.NoError(t, err)
require.Equal(t, []string(nil), names)
// run the first migrations
migrateUpAndCheck(t, migrator, store, test_migrations.FirstUp, upsOnlyFirst)
// run the rest of the migrations
migrateUpAndCheck(t, migrator, store, test_migrations.AllUp, upsOnlyAll)
// test_table_1 had the "id" column renamed to "org_id"
var table1Info []*tableInfo
err = store.DB.Select(&table1Info, "PRAGMA table_info(test_table_1)")
require.NoError(t, err)
require.Len(t, table1Info, 3)
require.Equal(t, "org_id", table1Info[0].Name)
// test_table_2 was created correctly
var table2Info []*tableInfo
err = store.DB.Select(&table2Info, "PRAGMA table_info(test_table_2)")
require.NoError(t, err)
require.Len(t, table2Info, 3)
require.Equal(t, "user_id", table2Info[0].Name)
}
func TestUpErrors(t *testing.T) {
t.Parallel()
t.Run("only unknown migration exists", func(t *testing.T) {
store, clean := NewTestStore(t)
defer clean(t)
ctx := context.Background()
migrator := NewMigrator(store, zaptest.NewLogger(t))
require.NoError(t, migrator.Up(ctx, test_migrations.MigrationTable))
require.NoError(t, store.execTrans(ctx, `INSERT INTO migrations (name) VALUES ("0010_some_bad_migration")`))
require.Equal(t, migration.ErrInvalidMigration("0010_some_bad_migration"), migrator.Up(ctx, test_migrations.AllUp))
})
t.Run("known + unknown migrations exist", func(t *testing.T) {
store, clean := NewTestStore(t)
defer clean(t)
ctx := context.Background()
migrator := NewMigrator(store, zaptest.NewLogger(t))
require.NoError(t, migrator.Up(ctx, test_migrations.FirstUp))
require.NoError(t, store.execTrans(ctx, `INSERT INTO migrations (name) VALUES ("0010_some_bad_migration")`))
require.Equal(t, migration.ErrInvalidMigration("0010_some_bad_migration"), migrator.Up(ctx, test_migrations.AllUp))
})
}
func TestUpWithBackups(t *testing.T) {
t.Parallel()
store, clean := NewTestStore(t)
defer clean(t)
logger := zaptest.NewLogger(t)
migrator := NewMigrator(store, logger)
backupPath := fmt.Sprintf("%s.bak", store.path)
migrator.SetBackupPath(backupPath)
upsOnlyAll, err := test_migrations.AllUp.ReadDir(".")
require.NoError(t, err)
upsOnlyFirst, err := test_migrations.FirstUp.ReadDir(".")
require.NoError(t, err)
// Run the first migrations.
migrateUpAndCheck(t, migrator, store, test_migrations.FirstUp, upsOnlyFirst)
// Backup file shouldn't exist, because there was nothing to back up.
_, err = os.Stat(backupPath)
require.True(t, os.IsNotExist(err))
// Run the remaining migrations.
migrateUpAndCheck(t, migrator, store, test_migrations.AllUp, upsOnlyAll)
// Backup file should now exist.
_, err = os.Stat(backupPath)
require.NoError(t, err)
// Open a 2nd store using the backup file.
backupStore, err := NewSqlStore(backupPath, zap.NewNop())
require.NoError(t, err)
defer backupStore.Close()
// Backup store contains the first migrations records.
backupNames, err := backupStore.allMigrationNames()
require.NoError(t, err)
migrationNamesMatch(t, backupNames, upsOnlyFirst)
// Run the remaining migrations on the backup and verify that it now contains the rest of the migration records.
backupMigrator := NewMigrator(backupStore, logger)
migrateUpAndCheck(t, backupMigrator, store, test_migrations.AllUp, upsOnlyAll)
}
func TestDown(t *testing.T) {
t.Parallel()
store, clean := NewTestStore(t)
defer clean(t)
upsOnlyAll, err := test_migrations.AllUp.ReadDir(".")
require.NoError(t, err)
upsOnlyFirst, err := test_migrations.FirstUp.ReadDir(".")
require.NoError(t, err)
migrator := NewMigrator(store, zaptest.NewLogger(t))
// no up migrations, then some down migrations
migrateDownAndCheck(t, migrator, store, test_migrations.FirstDown, []fs.DirEntry{}, 0)
// all up migrations, then all down migrations
migrateUpAndCheck(t, migrator, store, test_migrations.AllUp, upsOnlyAll)
migrateDownAndCheck(t, migrator, store, test_migrations.AllDown, []fs.DirEntry{}, 0)
// first of the up migrations, then first of the down migrations
migrateUpAndCheck(t, migrator, store, test_migrations.FirstUp, upsOnlyFirst)
migrateDownAndCheck(t, migrator, store, test_migrations.FirstDown, []fs.DirEntry{}, 0)
// first of the up migrations, then all of the down migrations
migrateUpAndCheck(t, migrator, store, test_migrations.FirstUp, upsOnlyFirst)
migrateDownAndCheck(t, migrator, store, test_migrations.AllDown, []fs.DirEntry{}, 0)
// all up migrations, then some of the down migrations (using untilMigration)
migrateUpAndCheck(t, migrator, store, test_migrations.AllUp, upsOnlyAll)
migrateDownAndCheck(t, migrator, store, test_migrations.AllDown, upsOnlyFirst, 2)
}
func TestScriptVersion(t *testing.T) {
t.Parallel()
tests := []struct {
name string
filename string
want int
wantErr error
}{
{
"single digit number",
"0001_some_file_name.sql",
1,
nil,
},
{
"larger number",
"0921_another_file.sql",
921,
nil,
},
{
"bad name",
"not_numbered_correctly.sql",
0,
&errors.Error{},
},
}
for _, tt := range tests {
tt := tt // capture range variable
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := scriptVersion(tt.filename)
require.Equal(t, tt.want, got)
if tt.wantErr == nil {
require.NoError(t, err)
} else {
require.Error(t, err)
}
})
}
}
func TestDropExtension(t *testing.T) {
tests := []struct {
input string
want string
}{
{
input: "0001_some_migration",
want: "0001_some_migration",
},
{
input: "0001_some_migration.sql",
want: "0001_some_migration",
},
{
input: "0001_some_migration.down.sql",
want: "0001_some_migration",
},
{
input: "0001_some_migration.something.anything.else",
want: "0001_some_migration",
},
}
for _, tt := range tests {
got := dropExtension(tt.input)
require.Equal(t, tt.want, got)
}
}
func migrateUpAndCheck(t *testing.T, m *Migrator, s *SqlStore, source embed.FS, expected []fs.DirEntry) {
t.Helper()
require.NoError(t, m.Up(context.Background(), source))
names, err := s.allMigrationNames()
require.NoError(t, err)
migrationNamesMatch(t, names, expected)
}
func migrateDownAndCheck(t *testing.T, m *Migrator, s *SqlStore, source embed.FS, expected []fs.DirEntry, untilMigration int) {
t.Helper()
require.NoError(t, m.Down(context.Background(), untilMigration, source))
names, err := s.allMigrationNames()
require.NoError(t, err)
migrationNamesMatch(t, names, expected)
}
func migrationNamesMatch(t *testing.T, names []string, files []fs.DirEntry) {
t.Helper()
require.Equal(t, len(names), len(files))
for idx := range files {
require.Equal(t, dropExtension(files[idx].Name()), names[idx])
}
}