-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmigrator_test.go
365 lines (340 loc) · 15.2 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
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
364
365
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gorm
import (
"context"
"fmt"
"strconv"
"testing"
"cloud.google.com/go/longrunning/autogen/longrunningpb"
"cloud.google.com/go/spanner"
"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
"cloud.google.com/go/spanner/apiv1/spannerpb"
"github.com/googleapis/go-sql-spanner/testutil"
"google.golang.org/api/option"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/structpb"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type singer struct {
gorm.Model
FirstName string
LastName string
FullName string
Active bool
}
type album struct {
gorm.Model
Title string
Rating float32
SingerID uint
Singer *singer
}
type test struct {
ID uint `gorm:"primarykey" gorm_sequence_name:"overrided_sequence_name"`
Test string
SingerID uint
Singer *singer
}
func TestMigrate(t *testing.T) {
t.Parallel()
db, server, teardown := setupTestGormConnection(t)
defer teardown()
anyProto, err := anypb.New(&emptypb.Empty{})
if err != nil {
t.Fatal(err)
}
server.TestDatabaseAdmin.SetResps([]proto.Message{
&longrunningpb.Operation{
Name: "test-operation",
Done: true,
Result: &longrunningpb.Operation_Response{Response: anyProto},
},
})
err = db.Migrator().AutoMigrate(&singer{}, &album{}, &test{})
if err != nil {
t.Fatal(err)
}
requests := server.TestDatabaseAdmin.Reqs()
if g, w := len(requests), 1; g != w {
t.Fatalf("request count mismatch\n Got: %v\nWant: %v", g, w)
}
request := requests[0].(*databasepb.UpdateDatabaseDdlRequest)
if g, w := len(request.GetStatements()), 8; g != w {
t.Fatalf("statement count mismatch\n Got: %v\nWant: %v", g, w)
}
if g, w := request.GetStatements()[0],
`CREATE SEQUENCE IF NOT EXISTS singers_seq OPTIONS (sequence_kind = "bit_reversed_positive")`; g != w {
t.Fatalf("create singers sequence statement text mismatch\n Got: %s\nWant: %s", g, w)
}
if g, w := request.GetStatements()[1],
"CREATE TABLE `singers` ("+
"`id` INT64 DEFAULT (GET_NEXT_SEQUENCE_VALUE(Sequence singers_seq)),`created_at` TIMESTAMP,`updated_at` TIMESTAMP,`deleted_at` TIMESTAMP,"+
"`first_name` STRING(MAX),`last_name` STRING(MAX),`full_name` STRING(MAX),`active` BOOL) "+
"PRIMARY KEY (`id`)"; g != w {
t.Fatalf("create singers statement text mismatch\n Got: %s\nWant: %s", g, w)
}
if g, w := request.GetStatements()[2],
"CREATE INDEX `idx_singers_deleted_at` ON `singers`(`deleted_at`)"; g != w {
t.Fatalf("create idx_singers_deleted_at statement text mismatch\n Got: %s\nWant: %s", g, w)
}
if g, w := request.GetStatements()[3],
`CREATE SEQUENCE IF NOT EXISTS albums_seq OPTIONS (sequence_kind = "bit_reversed_positive")`; g != w {
t.Fatalf("create albums sequence statement text mismatch\n Got: %s\nWant: %s", g, w)
}
if g, w := request.GetStatements()[4],
"CREATE TABLE `albums` (`id` INT64 DEFAULT (GET_NEXT_SEQUENCE_VALUE(Sequence albums_seq)),`created_at` TIMESTAMP,`updated_at` TIMESTAMP,`deleted_at` TIMESTAMP,"+
"`title` STRING(MAX),`rating` FLOAT32,`singer_id` INT64,"+
"CONSTRAINT `fk_albums_singer` FOREIGN KEY (`singer_id`) REFERENCES `singers`(`id`)) "+
"PRIMARY KEY (`id`)"; g != w {
t.Fatalf("create albums statement text mismatch\n Got: %s\nWant: %s", g, w)
}
if g, w := request.GetStatements()[5],
"CREATE INDEX `idx_albums_deleted_at` ON `albums`(`deleted_at`)"; g != w {
t.Fatalf("create idx_albums_deleted_at statement text mismatch\n Got: %s\nWant: %s", g, w)
}
if g, w := request.GetStatements()[6],
`CREATE SEQUENCE IF NOT EXISTS overrided_sequence_name OPTIONS (sequence_kind = "bit_reversed_positive")`; g != w {
t.Fatalf("create albums sequence statement text mismatch\n Got: %s\nWant: %s", g, w)
}
if g, w := request.GetStatements()[7],
"CREATE TABLE `tests` (`id` INT64 DEFAULT (GET_NEXT_SEQUENCE_VALUE(Sequence overrided_sequence_name)),"+
"`test` STRING(MAX),`singer_id` INT64,"+
"CONSTRAINT `fk_tests_singer` FOREIGN KEY (`singer_id`) REFERENCES `singers`(`id`)) "+
"PRIMARY KEY (`id`)"; g != w {
t.Fatalf("create albums statement text mismatch\n Got: %s\nWant: %s", g, w)
}
}
func TestMigrateMultipleTimes(t *testing.T) {
t.Parallel()
db, server, teardown := setupTestGormConnection(t)
defer teardown()
anyProto, err := anypb.New(&emptypb.Empty{})
if err != nil {
t.Fatal(err)
}
server.TestDatabaseAdmin.SetResps([]proto.Message{
&longrunningpb.Operation{
Name: "test-operation-1",
Done: true,
Result: &longrunningpb.Operation_Response{Response: anyProto},
},
&longrunningpb.Operation{
Name: "test-operation-2",
Done: true,
Result: &longrunningpb.Operation_Response{Response: anyProto},
},
})
hasTableSql := "SELECT count(*) FROM information_schema.tables WHERE table_schema = @p1 AND table_name = @p2 AND table_type = @p3"
hasColSql := "SELECT count(*) FROM INFORMATION_SCHEMA.columns WHERE table_schema = @p1 AND table_name = @p2 AND column_name = @p3"
selectSingerRow := "SELECT * FROM `singers` LIMIT 1"
getColDetailsSql := "SELECT COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE = 'YES',\n\t\t\t\t\t REGEXP_REPLACE(SPANNER_TYPE, '\\\\(.*\\\\)', '') AS DATA_TYPE,\n\t\t\t\t\t SAFE_CAST(REPLACE(REPLACE(REGEXP_EXTRACT(SPANNER_TYPE, '\\\\(.*\\\\)'), '(', ''), ')', '') AS INT64) AS COLUMN_LENGTH,\n\t\t\t\t\t (SELECT IF(I.INDEX_TYPE='PRIMARY_KEY', 'PRI', 'UNI')\n\t\t\t\t\t\tFROM INFORMATION_SCHEMA.INDEXES I\n\t\t\t\t\t\tINNER JOIN INFORMATION_SCHEMA.INDEX_COLUMNS IC USING (TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, INDEX_NAME)\n\t\t\t\t\t\tWHERE IC.TABLE_CATALOG = C.TABLE_CATALOG\n\t\t\t\t\t\t AND IC.TABLE_SCHEMA = C.TABLE_SCHEMA\n\t\t\t\t\t\t AND IC.TABLE_NAME = C.TABLE_NAME\n\t\t\t\t\t\t AND IC.COLUMN_NAME = C.COLUMN_NAME\n\t\t\t\t\t\t AND I.IS_UNIQUE\n\t\t\t\t\t\tORDER BY I.INDEX_TYPE\n\t\t\t\t\t\tLIMIT 1\n\t\t\t\t\t ) AS KEY,\n FROM INFORMATION_SCHEMA.COLUMNS C WHERE TABLE_SCHEMA = @p1 AND TABLE_NAME = @p2 ORDER BY ORDINAL_POSITION"
hasIndexSql := "SELECT count(*) FROM information_schema.indexes WHERE table_schema = @p1 AND table_name = @p2 AND index_name = @p3"
_ = putCountStatementResult(server, hasTableSql, 0)
err = db.Migrator().AutoMigrate(&singer{}, &album{}, &test{})
// Verify that the first migration worked and executed the expected number of requests.
if err != nil {
t.Fatal(err)
}
requests := server.TestDatabaseAdmin.Reqs()
if g, w := len(requests), 1; g != w {
t.Fatalf("request count mismatch\n Got: %v\nWant: %v", g, w)
}
request := requests[0].(*databasepb.UpdateDatabaseDdlRequest)
if g, w := len(request.GetStatements()), 8; g != w {
t.Fatalf("statement count mismatch\n Got: %v\nWant: %v", g, w)
}
// Then auto-migrate again with an unchanged data model.
// This should lead to zero changes.
_ = putCountStatementResult(server, hasTableSql, 1)
_ = putCountStatementResult(server, hasColSql, 1)
_ = putSelectSingerRowResult(server, selectSingerRow)
_ = putSingerColDetailsResult(server, getColDetailsSql)
_ = putCountStatementResult(server, hasIndexSql, 1)
err = db.Migrator().AutoMigrate(&singer{})
if err != nil {
t.Fatal(err)
}
// The number of requests should still be 1, as we have made no changes to the `singer` table and model.
requests = server.TestDatabaseAdmin.Reqs()
if g, w := len(requests), 1; g != w {
t.Fatalf("request count mismatch\n Got: %v\nWant: %v", g, w)
}
}
func putCountStatementResult(server *testutil.MockedSpannerInMemTestServer, sql string, count int) error {
return server.TestSpanner.PutStatementResult(sql, &testutil.StatementResult{
Type: testutil.StatementResultResultSet,
ResultSet: &spannerpb.ResultSet{
Metadata: &spannerpb.ResultSetMetadata{
RowType: &spannerpb.StructType{
Fields: []*spannerpb.StructType_Field{
{Type: &spannerpb.Type{Code: spannerpb.TypeCode_INT64}, Name: "count"},
},
},
},
Rows: []*structpb.ListValue{
{Values: []*structpb.Value{{Kind: &structpb.Value_StringValue{StringValue: strconv.Itoa(count)}}}},
},
},
})
}
func putSingerColDetailsResult(server *testutil.MockedSpannerInMemTestServer, sql string) error {
return server.TestSpanner.PutStatementResult(sql, &testutil.StatementResult{
Type: testutil.StatementResultResultSet,
ResultSet: &spannerpb.ResultSet{
Metadata: &spannerpb.ResultSetMetadata{
RowType: &spannerpb.StructType{
Fields: []*spannerpb.StructType_Field{
{Type: &spannerpb.Type{Code: spannerpb.TypeCode_STRING}, Name: "COLUMN_NAME"},
{Type: &spannerpb.Type{Code: spannerpb.TypeCode_STRING}, Name: "COLUMN_DEFAULT"},
{Type: &spannerpb.Type{Code: spannerpb.TypeCode_BOOL}, Name: "IS_NULLABLE"},
{Type: &spannerpb.Type{Code: spannerpb.TypeCode_STRING}, Name: "DATA_TYPE"},
{Type: &spannerpb.Type{Code: spannerpb.TypeCode_INT64}, Name: "COLUMN_LENGTH"},
{Type: &spannerpb.Type{Code: spannerpb.TypeCode_STRING}, Name: "KEY"},
},
},
},
Rows: []*structpb.ListValue{
{Values: []*structpb.Value{
{Kind: &structpb.Value_StringValue{StringValue: "id"}},
{Kind: &structpb.Value_StringValue{StringValue: "GET_NEXT_SEQUENCE_VALUE(Sequence singers_seq)"}},
{Kind: &structpb.Value_BoolValue{BoolValue: true}},
{Kind: &structpb.Value_StringValue{StringValue: "INT64"}},
{Kind: &structpb.Value_NullValue{}},
{Kind: &structpb.Value_NullValue{}},
}},
{Values: []*structpb.Value{
{Kind: &structpb.Value_StringValue{StringValue: "created_at"}},
{Kind: &structpb.Value_NullValue{}},
{Kind: &structpb.Value_BoolValue{BoolValue: true}},
{Kind: &structpb.Value_StringValue{StringValue: "TIMESTAMP"}},
{Kind: &structpb.Value_NullValue{}},
{Kind: &structpb.Value_NullValue{}},
}},
{Values: []*structpb.Value{
{Kind: &structpb.Value_StringValue{StringValue: "updated_at"}},
{Kind: &structpb.Value_NullValue{}},
{Kind: &structpb.Value_BoolValue{BoolValue: true}},
{Kind: &structpb.Value_StringValue{StringValue: "TIMESTAMP"}},
{Kind: &structpb.Value_NullValue{}},
{Kind: &structpb.Value_NullValue{}},
}},
{Values: []*structpb.Value{
{Kind: &structpb.Value_StringValue{StringValue: "deleted_at"}},
{Kind: &structpb.Value_NullValue{}},
{Kind: &structpb.Value_BoolValue{BoolValue: true}},
{Kind: &structpb.Value_StringValue{StringValue: "TIMESTAMP"}},
{Kind: &structpb.Value_NullValue{}},
{Kind: &structpb.Value_NullValue{}},
}},
{Values: []*structpb.Value{
{Kind: &structpb.Value_StringValue{StringValue: "first_name"}},
{Kind: &structpb.Value_NullValue{}},
{Kind: &structpb.Value_BoolValue{BoolValue: true}},
{Kind: &structpb.Value_StringValue{StringValue: "STRING"}},
{Kind: &structpb.Value_NullValue{}},
{Kind: &structpb.Value_NullValue{}},
}},
{Values: []*structpb.Value{
{Kind: &structpb.Value_StringValue{StringValue: "last_name"}},
{Kind: &structpb.Value_NullValue{}},
{Kind: &structpb.Value_BoolValue{BoolValue: true}},
{Kind: &structpb.Value_StringValue{StringValue: "STRING"}},
{Kind: &structpb.Value_NullValue{}},
{Kind: &structpb.Value_NullValue{}},
}},
{Values: []*structpb.Value{
{Kind: &structpb.Value_StringValue{StringValue: "full_name"}},
{Kind: &structpb.Value_NullValue{}},
{Kind: &structpb.Value_BoolValue{BoolValue: true}},
{Kind: &structpb.Value_StringValue{StringValue: "STRING"}},
{Kind: &structpb.Value_NullValue{}},
{Kind: &structpb.Value_NullValue{}},
}},
{Values: []*structpb.Value{
{Kind: &structpb.Value_StringValue{StringValue: "active"}},
{Kind: &structpb.Value_NullValue{}},
{Kind: &structpb.Value_BoolValue{BoolValue: true}},
{Kind: &structpb.Value_StringValue{StringValue: "BOOL"}},
{Kind: &structpb.Value_NullValue{}},
{Kind: &structpb.Value_NullValue{}},
}},
},
},
})
}
func putSelectSingerRowResult(server *testutil.MockedSpannerInMemTestServer, sql string) error {
return server.TestSpanner.PutStatementResult(sql, &testutil.StatementResult{
Type: testutil.StatementResultResultSet,
ResultSet: &spannerpb.ResultSet{
Metadata: &spannerpb.ResultSetMetadata{
RowType: &spannerpb.StructType{
Fields: []*spannerpb.StructType_Field{
{Type: &spannerpb.Type{Code: spannerpb.TypeCode_INT64}, Name: "id"},
{Type: &spannerpb.Type{Code: spannerpb.TypeCode_TIMESTAMP}, Name: "created_at"},
{Type: &spannerpb.Type{Code: spannerpb.TypeCode_TIMESTAMP}, Name: "updated_at"},
{Type: &spannerpb.Type{Code: spannerpb.TypeCode_TIMESTAMP}, Name: "deleted_at"},
{Type: &spannerpb.Type{Code: spannerpb.TypeCode_STRING}, Name: "first_name"},
{Type: &spannerpb.Type{Code: spannerpb.TypeCode_STRING}, Name: "last_name"},
{Type: &spannerpb.Type{Code: spannerpb.TypeCode_STRING}, Name: "full_name"},
{Type: &spannerpb.Type{Code: spannerpb.TypeCode_BOOL}, Name: "active"},
},
},
},
Rows: []*structpb.ListValue{},
},
})
}
func setupTestGormConnection(t *testing.T) (db *gorm.DB, server *testutil.MockedSpannerInMemTestServer, teardown func()) {
return setupTestGormConnectionWithParams(t, "")
}
func setupTestGormConnectionWithParams(t *testing.T, params string) (db *gorm.DB, server *testutil.MockedSpannerInMemTestServer, teardown func()) {
server, _, serverTeardown := setupMockedTestServer(t)
db, err := gorm.Open(New(Config{
DriverName: "spanner",
DSN: fmt.Sprintf("%s/projects/p/instances/i/databases/d?useplaintext=true;%s", server.Address, params),
}), &gorm.Config{PrepareStmt: true, Logger: logger.Default.LogMode(logger.Silent)})
if err != nil {
serverTeardown()
t.Fatal(err)
}
return db, server, func() {
// TODO: Close database?
_ = db
serverTeardown()
}
}
func setupMockedTestServer(t *testing.T) (server *testutil.MockedSpannerInMemTestServer, client *spanner.Client, teardown func()) {
return setupMockedTestServerWithConfig(t, spanner.ClientConfig{})
}
func setupMockedTestServerWithConfig(t *testing.T, config spanner.ClientConfig) (server *testutil.MockedSpannerInMemTestServer, client *spanner.Client, teardown func()) {
return setupMockedTestServerWithConfigAndClientOptions(t, config, []option.ClientOption{})
}
func setupMockedTestServerWithConfigAndClientOptions(t *testing.T, config spanner.ClientConfig, clientOptions []option.ClientOption) (server *testutil.MockedSpannerInMemTestServer, client *spanner.Client, teardown func()) {
server, opts, serverTeardown := testutil.NewMockedSpannerInMemTestServer(t)
opts = append(opts, clientOptions...)
ctx := context.Background()
formattedDatabase := fmt.Sprintf("projects/%s/instances/%s/databases/%s", "[PROJECT]", "[INSTANCE]", "[DATABASE]")
client, err := spanner.NewClientWithConfig(ctx, formattedDatabase, config, opts...)
if err != nil {
t.Fatal(err)
}
return server, client, func() {
client.Close()
serverTeardown()
}
}