-
Notifications
You must be signed in to change notification settings - Fork 467
Expand file tree
/
Copy pathdiff_test.go
More file actions
363 lines (346 loc) · 13.3 KB
/
diff_test.go
File metadata and controls
363 lines (346 loc) · 13.3 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package diff
import (
"context"
"errors"
"io"
"net/http"
"os"
"path/filepath"
"testing"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/h2non/gock"
"github.com/jackc/pgconn"
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v4"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/supabase/cli/internal/testing/apitest"
"github.com/supabase/cli/internal/testing/fstest"
"github.com/supabase/cli/internal/testing/helper"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/migration"
"github.com/supabase/cli/pkg/pgtest"
)
var dbConfig = pgconn.Config{
Host: "db.supabase.co",
Port: 5432,
User: "admin",
Password: "password",
Database: "postgres",
}
func TestRun(t *testing.T) {
t.Run("runs migra diff", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, flags.LoadConfig(fsys))
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
apitest.MockDockerStart(utils.Docker, utils.GetRegistryImageUrl(utils.Config.Db.Image), "test-shadow-db")
gock.New(utils.Docker.DaemonHost()).
Delete("/v" + utils.Docker.ClientVersion() + "/containers/test-shadow-db").
Reply(http.StatusOK)
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/test-shadow-db/json").
Reply(http.StatusOK).
JSON(container.InspectResponse{ContainerJSONBase: &container.ContainerJSONBase{
State: &container.State{
Running: true,
Health: &container.Health{Status: types.Healthy},
},
}})
apitest.MockDockerStart(utils.Docker, utils.GetRegistryImageUrl(utils.Config.Realtime.Image), "test-shadow-realtime")
require.NoError(t, apitest.MockDockerLogs(utils.Docker, "test-shadow-realtime", ""))
apitest.MockDockerStart(utils.Docker, utils.GetRegistryImageUrl(utils.Config.Storage.Image), "test-shadow-storage")
require.NoError(t, apitest.MockDockerLogs(utils.Docker, "test-shadow-storage", ""))
apitest.MockDockerStart(utils.Docker, utils.GetRegistryImageUrl(utils.Config.Auth.Image), "test-shadow-auth")
require.NoError(t, apitest.MockDockerLogs(utils.Docker, "test-shadow-auth", ""))
apitest.MockDockerStart(utils.Docker, utils.GetRegistryImageUrl(utils.Config.EdgeRuntime.Image), "test-migra")
diff := "create table test();"
require.NoError(t, apitest.MockDockerLogs(utils.Docker, "test-migra", diff))
// Setup mock postgres
conn := pgtest.NewConn()
conn.Query(CREATE_TEMPLATE).
Reply("CREATE DATABASE")
defer conn.Close(t)
// Run test
err := Run(context.Background(), []string{"public"}, "file", dbConfig, DiffSchemaMigra, fsys, func(cc *pgx.ConnConfig) {
if cc.Host == dbConfig.Host {
// Fake a SSL error when connecting to target database
cc.LookupFunc = func(ctx context.Context, host string) (addrs []string, err error) {
return nil, errors.New("server refused TLS connection")
}
} else {
// Hijack connection to shadow database
conn.Intercept(cc)
}
})
// Check error
assert.NoError(t, err)
assert.Empty(t, apitest.ListUnmatchedRequests())
// Check diff file
files, err := afero.ReadDir(fsys, utils.Paths.MigrationsDir)
assert.NoError(t, err)
assert.Equal(t, 1, len(files))
diffPath := filepath.Join(utils.Paths.MigrationsDir, files[0].Name())
contents, err := afero.ReadFile(fsys, diffPath)
assert.NoError(t, err)
assert.Equal(t, []byte(diff), contents)
})
t.Run("throws error on failure to diff target", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/images/" + utils.GetRegistryImageUrl(utils.Config.Db.Image) + "/json").
ReplyError(errors.New("network error"))
// Run test
err := Run(context.Background(), []string{"public"}, "file", dbConfig, DiffSchemaMigra, fsys)
// Check error
assert.ErrorContains(t, err, "network error")
assert.Empty(t, apitest.ListUnmatchedRequests())
})
}
func TestMigrateShadow(t *testing.T) {
utils.Config.Db.MajorVersion = 14
t.Run("migrates shadow database", func(t *testing.T) {
utils.Config.Db.ShadowPort = 54320
utils.GlobalsSql = "create schema public"
utils.InitialSchemaPg14Sql = "create schema private"
// Setup in-memory fs
fsys := afero.NewMemMapFs()
path := filepath.Join(utils.Paths.MigrationsDir, "0_test.sql")
sql := "create schema test"
require.NoError(t, afero.WriteFile(fsys, path, []byte(sql), 0644))
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
conn.Query(utils.GlobalsSql).
Reply("CREATE SCHEMA").
Query(utils.InitialSchemaPg14Sql).
Reply("CREATE SCHEMA").
Query(CREATE_TEMPLATE).
Reply("CREATE DATABASE")
helper.MockMigrationHistory(conn).
Query("RESET ALL").
Reply("RESET").
Query(sql).
Reply("CREATE SCHEMA").
Query(migration.INSERT_MIGRATION_VERSION, "0", "test", []string{sql}).
Reply("INSERT 0 1")
// Run test
err := MigrateShadowDatabase(context.Background(), "test-shadow-db", fsys, conn.Intercept)
// Check error
assert.NoError(t, err)
})
t.Run("throws error on timeout", func(t *testing.T) {
utils.Config.Db.ShadowPort = 54320
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Setup cancelled context
ctx, cancel := context.WithCancel(context.Background())
cancel()
// Run test
err := MigrateShadowDatabase(ctx, "", fsys)
// Check error
assert.ErrorIs(t, err, context.Canceled)
})
t.Run("throws error on permission denied", func(t *testing.T) {
// Setup in-memory fs
fsys := &fstest.OpenErrorFs{DenyPath: utils.Paths.MigrationsDir}
// Run test
err := MigrateShadowDatabase(context.Background(), "", fsys)
// Check error
assert.ErrorIs(t, err, os.ErrPermission)
})
t.Run("throws error on globals schema", func(t *testing.T) {
utils.Config.Db.ShadowPort = 54320
utils.GlobalsSql = "create schema public"
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
conn.Query(utils.GlobalsSql).
ReplyError(pgerrcode.DuplicateSchema, `schema "public" already exists`)
// Run test
err := MigrateShadowDatabase(context.Background(), "test-shadow-db", fsys, conn.Intercept)
// Check error
assert.ErrorContains(t, err, `ERROR: schema "public" already exists (SQLSTATE 42P06)`)
})
}
func TestDiffDatabase(t *testing.T) {
utils.Config.Db.MajorVersion = 14
utils.Config.Db.ShadowPort = 54320
utils.GlobalsSql = "create schema public"
utils.InitialSchemaPg14Sql = "create schema private"
t.Run("throws error on failure to create shadow", func(t *testing.T) {
errNetwork := errors.New("network error")
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/images/" + utils.GetRegistryImageUrl(utils.Config.Db.Image) + "/json").
ReplyError(errNetwork)
// Run test
diff, err := DiffDatabase(context.Background(), []string{"public"}, dbConfig, io.Discard, fsys, DiffSchemaMigra)
// Check error
assert.Empty(t, diff)
assert.ErrorIs(t, err, errNetwork)
assert.Empty(t, apitest.ListUnmatchedRequests())
})
t.Run("throws error on health check failure", func(t *testing.T) {
utils.Config.Db.HealthTimeout = time.Millisecond
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
apitest.MockDockerStart(utils.Docker, utils.GetRegistryImageUrl(utils.Config.Db.Image), "test-shadow-db")
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/test-shadow-db/json").
Reply(http.StatusOK).
JSON(container.InspectResponse{ContainerJSONBase: &container.ContainerJSONBase{
State: &container.State{
Running: false,
Status: "exited",
},
}})
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/test-shadow-db/logs").
Reply(http.StatusServiceUnavailable)
gock.New(utils.Docker.DaemonHost()).
Delete("/v" + utils.Docker.ClientVersion() + "/containers/test-shadow-db").
Reply(http.StatusOK)
// Run test
diff, err := DiffDatabase(context.Background(), []string{"public"}, dbConfig, io.Discard, fsys, DiffSchemaMigra)
// Check error
assert.Empty(t, diff)
assert.ErrorContains(t, err, "test-shadow-db container is not running: exited")
assert.Empty(t, apitest.ListUnmatchedRequests())
})
t.Run("throws error on failure to migrate shadow", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
apitest.MockDockerStart(utils.Docker, utils.GetRegistryImageUrl(utils.Config.Db.Image), "test-shadow-db")
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/test-shadow-db/json").
Reply(http.StatusOK).
JSON(container.InspectResponse{ContainerJSONBase: &container.ContainerJSONBase{
State: &container.State{
Running: true,
Health: &container.Health{Status: types.Healthy},
},
}})
gock.New(utils.Docker.DaemonHost()).
Delete("/v" + utils.Docker.ClientVersion() + "/containers/test-shadow-db").
Reply(http.StatusOK)
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
conn.Query(utils.GlobalsSql).
ReplyError(pgerrcode.DuplicateSchema, `schema "public" already exists`)
// Run test
diff, err := DiffDatabase(context.Background(), []string{"public"}, dbConfig, io.Discard, fsys, DiffSchemaMigra, conn.Intercept)
// Check error
assert.Empty(t, diff)
assert.ErrorContains(t, err, `ERROR: schema "public" already exists (SQLSTATE 42P06)
At statement: 0
create schema public`)
assert.Empty(t, apitest.ListUnmatchedRequests())
})
t.Run("throws error on failure to diff target", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
path := filepath.Join(utils.Paths.MigrationsDir, "0_test.sql")
sql := "create schema test"
require.NoError(t, afero.WriteFile(fsys, path, []byte(sql), 0644))
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
apitest.MockDockerStart(utils.Docker, utils.GetRegistryImageUrl(utils.Config.Db.Image), "test-shadow-db")
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/test-shadow-db/json").
Reply(http.StatusOK).
JSON(container.InspectResponse{ContainerJSONBase: &container.ContainerJSONBase{
State: &container.State{
Running: true,
Health: &container.Health{Status: types.Healthy},
},
}})
gock.New(utils.Docker.DaemonHost()).
Delete("/v" + utils.Docker.ClientVersion() + "/containers/test-shadow-db").
Reply(http.StatusOK)
apitest.MockDockerStart(utils.Docker, utils.GetRegistryImageUrl(utils.Config.EdgeRuntime.Image), "test-migra")
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/test-migra/logs").
ReplyError(errors.New("network error"))
gock.New(utils.Docker.DaemonHost()).
Delete("/v" + utils.Docker.ClientVersion() + "/containers/test-migra").
Reply(http.StatusOK)
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
conn.Query(utils.GlobalsSql).
Reply("CREATE SCHEMA").
Query(utils.InitialSchemaPg14Sql).
Reply("CREATE SCHEMA").
Query(CREATE_TEMPLATE).
Reply("CREATE DATABASE")
helper.MockMigrationHistory(conn).
Query("RESET ALL").
Reply("RESET").
Query(sql).
Reply("CREATE SCHEMA").
Query(migration.INSERT_MIGRATION_VERSION, "0", "test", []string{sql}).
Reply("INSERT 0 1")
// Run test
diff, err := DiffDatabase(context.Background(), []string{"public"}, dbConfig, io.Discard, fsys, DiffSchemaMigra, func(cc *pgx.ConnConfig) {
if cc.Host == dbConfig.Host {
// Fake a SSL error when connecting to target database
cc.LookupFunc = func(ctx context.Context, host string) (addrs []string, err error) {
return nil, errors.New("server refused TLS connection")
}
} else {
// Hijack connection to shadow database
conn.Intercept(cc)
}
})
// Check error
assert.Empty(t, diff)
assert.ErrorContains(t, err, "error diffing schema")
assert.Empty(t, apitest.ListUnmatchedRequests())
})
}
func TestDropStatements(t *testing.T) {
drops := findDropStatements("create table t(); drop table t; alter table t drop column c")
assert.Equal(t, []string{"drop table t", "alter table t drop column c"}, drops)
}
func TestLoadSchemas(t *testing.T) {
expected := []string{
filepath.Join(utils.Paths.SchemasDir, "comment", "model.sql"),
filepath.Join(utils.Paths.SchemasDir, "model.sql"),
filepath.Join(utils.Paths.SchemasDir, "reaction", "dislike", "model.sql"),
filepath.Join(utils.Paths.SchemasDir, "reaction", "like", "model.sql"),
}
fsys := afero.NewMemMapFs()
for _, fp := range expected {
require.NoError(t, afero.WriteFile(fsys, fp, nil, 0644))
}
// Run test
schemas, err := loadDeclaredSchemas(fsys)
// Check error
assert.NoError(t, err)
assert.ElementsMatch(t, expected, schemas)
}