-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathops_test.go
More file actions
378 lines (363 loc) · 13.1 KB
/
Copy pathops_test.go
File metadata and controls
378 lines (363 loc) · 13.1 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package statement_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/block/pg-sprite/pkg/statement"
)
func parseOneOp(t *testing.T, sql string) statement.Op {
t.Helper()
ops, err := statement.ParseOps(sql)
require.NoError(t, err)
require.Len(t, ops, 1)
return ops[0]
}
func TestParseOpsShapes(t *testing.T) {
cases := []struct {
name string
sql string
want statement.Op
}{
{
name: "add column plain",
sql: "ALTER TABLE t ADD COLUMN age int",
want: statement.Op{Kind: statement.OpAddColumn, Column: "age", NewType: "int4"},
},
{
name: "add column constant default",
sql: "ALTER TABLE t ADD COLUMN age int DEFAULT 0",
want: statement.Op{Kind: statement.OpAddColumn, Column: "age", NewType: "int4", Default: statement.DefaultConstant},
},
{
name: "add column cast constant default",
sql: "ALTER TABLE t ADD COLUMN created timestamptz DEFAULT '2020-01-01'::timestamptz",
want: statement.Op{Kind: statement.OpAddColumn, Column: "created", NewType: "timestamptz", Default: statement.DefaultConstant},
},
{
name: "add column function default",
sql: "ALTER TABLE t ADD COLUMN id uuid DEFAULT uuid_generate_v4()",
want: statement.Op{Kind: statement.OpAddColumn, Column: "id", NewType: "uuid", Default: statement.DefaultExpression},
},
{
name: "add column value function default",
sql: "ALTER TABLE t ADD COLUMN created timestamptz DEFAULT CURRENT_TIMESTAMP",
want: statement.Op{Kind: statement.OpAddColumn, Column: "created", NewType: "timestamptz", Default: statement.DefaultExpression},
},
{
name: "add column serial",
sql: "ALTER TABLE t ADD COLUMN n serial",
want: statement.Op{Kind: statement.OpAddColumn, Column: "n", NewType: "serial", Default: statement.DefaultExpression},
},
{
name: "add column identity",
sql: "ALTER TABLE t ADD COLUMN n bigint GENERATED ALWAYS AS IDENTITY",
want: statement.Op{Kind: statement.OpAddColumn, Column: "n", NewType: "int8", Default: statement.DefaultExpression},
},
{
name: "add column generated stored",
sql: "ALTER TABLE t ADD COLUMN total numeric GENERATED ALWAYS AS (price * qty) STORED",
want: statement.Op{Kind: statement.OpAddColumn, Column: "total", NewType: "numeric", GeneratedStored: true},
},
{
name: "add column inline unique",
sql: "ALTER TABLE t ADD COLUMN c int UNIQUE",
want: statement.Op{Kind: statement.OpAddColumn, Column: "c", NewType: "int4",
InlineConstraints: []statement.ConstraintKind{statement.ConstraintUnique}},
},
{
name: "add column inline primary key",
sql: "ALTER TABLE t ADD COLUMN c int PRIMARY KEY",
want: statement.Op{Kind: statement.OpAddColumn, Column: "c", NewType: "int4",
InlineConstraints: []statement.ConstraintKind{statement.ConstraintPrimaryKey}},
},
{
name: "add column inline foreign key",
sql: "ALTER TABLE t ADD COLUMN c int REFERENCES parent (id)",
want: statement.Op{Kind: statement.OpAddColumn, Column: "c", NewType: "int4",
InlineConstraints: []statement.ConstraintKind{statement.ConstraintForeignKey}},
},
{
name: "add column inline check",
sql: "ALTER TABLE t ADD COLUMN c int CHECK (c > 0)",
want: statement.Op{Kind: statement.OpAddColumn, Column: "c", NewType: "int4",
InlineConstraints: []statement.ConstraintKind{statement.ConstraintCheck}},
},
{
name: "add column inline check with constant default",
sql: "ALTER TABLE t ADD COLUMN c int DEFAULT 0 CHECK (c > 0)",
want: statement.Op{Kind: statement.OpAddColumn, Column: "c", NewType: "int4",
Default: statement.DefaultConstant,
InlineConstraints: []statement.ConstraintKind{statement.ConstraintCheck}},
},
{
name: "add column not null carries no inline constraint",
sql: "ALTER TABLE t ADD COLUMN c int NOT NULL DEFAULT 0",
want: statement.Op{Kind: statement.OpAddColumn, Column: "c", NewType: "int4",
Default: statement.DefaultConstant},
},
{
name: "drop column",
sql: "ALTER TABLE t DROP COLUMN age",
want: statement.Op{Kind: statement.OpDropColumn, Column: "age"},
},
{
name: "alter type with mods",
sql: "ALTER TABLE t ALTER COLUMN name TYPE varchar(100)",
want: statement.Op{Kind: statement.OpAlterColumnType, Column: "name", NewType: "varchar", NewTypeMods: []int32{100}},
},
{
name: "alter type with using",
sql: "ALTER TABLE t ALTER COLUMN doc TYPE jsonb USING doc::jsonb",
want: statement.Op{Kind: statement.OpAlterColumnType, Column: "doc", NewType: "jsonb", HasUsing: true},
},
{
name: "set default",
sql: "ALTER TABLE t ALTER COLUMN age SET DEFAULT 1",
want: statement.Op{Kind: statement.OpSetDefault, Column: "age"},
},
{
name: "drop default",
sql: "ALTER TABLE t ALTER COLUMN age DROP DEFAULT",
want: statement.Op{Kind: statement.OpDropDefault, Column: "age"},
},
{
name: "set not null",
sql: "ALTER TABLE t ALTER COLUMN age SET NOT NULL",
want: statement.Op{Kind: statement.OpSetNotNull, Column: "age"},
},
{
name: "drop not null",
sql: "ALTER TABLE t ALTER COLUMN age DROP NOT NULL",
want: statement.Op{Kind: statement.OpDropNotNull, Column: "age"},
},
{
name: "set statistics",
sql: "ALTER TABLE t ALTER COLUMN age SET STATISTICS 500",
want: statement.Op{Kind: statement.OpSetColumnOptions, Column: "age"},
},
{
name: "rename column",
sql: "ALTER TABLE t RENAME COLUMN a TO b",
want: statement.Op{Kind: statement.OpRenameColumn, Column: "a", Name: "b"},
},
{
name: "rename table",
sql: "ALTER TABLE t RENAME TO t2",
want: statement.Op{Kind: statement.OpRenameTable, Name: "t2"},
},
{
name: "rename index",
sql: "ALTER INDEX i RENAME TO i2",
want: statement.Op{Kind: statement.OpRenameIndex, Name: "i2"},
},
{
name: "set schema",
sql: "ALTER TABLE t SET SCHEMA s2",
want: statement.Op{Kind: statement.OpSetSchema, Name: "s2"},
},
{
name: "set tablespace",
sql: "ALTER TABLE t SET TABLESPACE fast",
want: statement.Op{Kind: statement.OpSetTablespace, Name: "fast"},
},
{
name: "set rel options",
sql: "ALTER TABLE t SET (fillfactor = 70)",
want: statement.Op{Kind: statement.OpSetRelOptions},
},
{
name: "add primary key",
sql: "ALTER TABLE t ADD CONSTRAINT t_pkey PRIMARY KEY (id)",
want: statement.Op{Kind: statement.OpAddConstraint, Name: "t_pkey", Constraint: statement.ConstraintPrimaryKey, Columns: []string{"id"}},
},
{
name: "add unique using index",
sql: "ALTER TABLE t ADD CONSTRAINT u UNIQUE USING INDEX u_idx",
want: statement.Op{Kind: statement.OpAddConstraint, Name: "u", Constraint: statement.ConstraintUnique, UsingIndex: true},
},
{
name: "add check not valid",
sql: "ALTER TABLE t ADD CONSTRAINT c CHECK (age > 0) NOT VALID",
want: statement.Op{Kind: statement.OpAddConstraint, Name: "c", Constraint: statement.ConstraintCheck, NotValid: true},
},
{
name: "add foreign key",
sql: "ALTER TABLE t ADD CONSTRAINT fk FOREIGN KEY (pid) REFERENCES p (id)",
want: statement.Op{Kind: statement.OpAddConstraint, Name: "fk", Constraint: statement.ConstraintForeignKey},
},
{
name: "validate constraint",
sql: "ALTER TABLE t VALIDATE CONSTRAINT c",
want: statement.Op{Kind: statement.OpValidateConstraint, Name: "c"},
},
{
name: "drop constraint",
sql: "ALTER TABLE t DROP CONSTRAINT c",
want: statement.Op{Kind: statement.OpDropConstraint, Name: "c"},
},
{
name: "attach partition",
sql: "ALTER TABLE t ATTACH PARTITION p FOR VALUES FROM (1) TO (10)",
want: statement.Op{Kind: statement.OpAttachPartition},
},
{
name: "detach partition",
sql: "ALTER TABLE t DETACH PARTITION p",
want: statement.Op{Kind: statement.OpDetachPartition},
},
{
name: "detach partition concurrently",
sql: "ALTER TABLE t DETACH PARTITION p CONCURRENTLY",
want: statement.Op{Kind: statement.OpDetachPartition, Concurrent: true},
},
{
name: "create index",
sql: "CREATE INDEX i ON t (a)",
want: statement.Op{Kind: statement.OpCreateIndex, Name: "i"},
},
{
name: "create unique index concurrently",
sql: "CREATE UNIQUE INDEX CONCURRENTLY i ON t (a)",
want: statement.Op{Kind: statement.OpCreateIndex, Name: "i", Concurrent: true, Unique: true},
},
{
name: "create index if not exists",
sql: "CREATE INDEX IF NOT EXISTS i ON t (a)",
want: statement.Op{Kind: statement.OpCreateIndex, Name: "i", IfNotExists: true},
},
{
name: "drop index",
sql: "DROP INDEX i",
want: statement.Op{Kind: statement.OpDropIndex, Name: "i"},
},
{
name: "drop index concurrently",
sql: "DROP INDEX CONCURRENTLY i",
want: statement.Op{Kind: statement.OpDropIndex, Name: "i", Concurrent: true},
},
{
name: "reindex",
sql: "REINDEX INDEX i",
want: statement.Op{Kind: statement.OpReindex, Name: "i"},
},
{
name: "reindex concurrently",
sql: "REINDEX INDEX CONCURRENTLY i",
want: statement.Op{Kind: statement.OpReindex, Name: "i", Concurrent: true},
},
{
name: "create table",
sql: "CREATE TABLE t (id int PRIMARY KEY)",
want: statement.Op{Kind: statement.OpCreateTable},
},
{
name: "unrecognized statement",
sql: "VACUUM FULL t",
want: statement.Op{Kind: statement.OpUnrecognized},
},
{
name: "unrecognized subcommand",
sql: "ALTER TABLE t ENABLE ROW LEVEL SECURITY",
want: statement.Op{Kind: statement.OpUnrecognized},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.want, parseOneOp(t, tc.sql))
})
}
}
// Describe must render the type modifier: the modifier is what
// distinguishes a widen (varchar(100), routes native) from a narrow
// (varchar(30), routes copy-and-swap), and a label that drops it would
// render changes that route in opposite directions identically.
func TestDescribeRendersTypeModifiers(t *testing.T) {
cases := []struct {
sql string
want string
}{
{"ALTER TABLE t ALTER COLUMN v TYPE varchar(100)", "ALTER COLUMN v TYPE varchar(100)"},
{"ALTER TABLE t ALTER COLUMN p TYPE numeric(12,2)", "ALTER COLUMN p TYPE numeric(12,2)"},
{"ALTER TABLE t ALTER COLUMN v TYPE text", "ALTER COLUMN v TYPE text"},
}
for _, tc := range cases {
assert.Equal(t, tc.want, parseOneOp(t, tc.sql).Describe(), tc.sql)
}
}
func TestParseOpsMultipleSubcommands(t *testing.T) {
ops, err := statement.ParseOps("ALTER TABLE t ADD COLUMN a int, DROP COLUMN b")
require.NoError(t, err)
require.Len(t, ops, 2)
assert.Equal(t, statement.OpAddColumn, ops[0].Kind)
assert.Equal(t, statement.OpDropColumn, ops[1].Kind)
}
func TestParseOpsRejectsMultipleStatements(t *testing.T) {
_, err := statement.ParseOps("SELECT 1; SELECT 2")
assert.ErrorIs(t, err, statement.ErrNotOneStatement)
}
func TestConcurrentlyRewrites(t *testing.T) {
// The rewrite contract is syntactic: the result must parse back with
// the concurrency flag set. Exact deparser wording is not a contract.
for _, sql := range []string{
"CREATE INDEX i ON t (a)",
"DROP INDEX i",
"REINDEX INDEX i",
"ALTER TABLE t DETACH PARTITION p",
} {
t.Run(sql, func(t *testing.T) {
safer, err := statement.Concurrently(sql)
require.NoError(t, err)
op := parseOneOp(t, safer)
assert.True(t, op.Concurrent, "rewritten statement must be concurrent: %s", safer)
})
}
}
func TestConcurrentlyRefusesOtherStatements(t *testing.T) {
_, err := statement.Concurrently("ALTER TABLE t ADD COLUMN a int")
assert.ErrorIs(t, err, statement.ErrNotRewritable)
}
func TestAddNotValidRewritesNamedCheck(t *testing.T) {
safer, name, err := statement.AddNotValid("ALTER TABLE t ADD CONSTRAINT c CHECK (age > 0)")
require.NoError(t, err)
assert.Equal(t, "c", name)
op := parseOneOp(t, safer)
assert.True(t, op.NotValid, "rewritten constraint must be NOT VALID: %s", safer)
}
func TestAddNotValidRefusals(t *testing.T) {
cases := []struct {
name string
sql string
}{
{"unnamed constraint", "ALTER TABLE t ADD CHECK (age > 0)"},
{"primary key", "ALTER TABLE t ADD CONSTRAINT p PRIMARY KEY (id)"},
{"not an add constraint", "ALTER TABLE t DROP COLUMN a"},
{"multiple subcommands", "ALTER TABLE t ADD CONSTRAINT c CHECK (a > 0), DROP COLUMN b"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, _, err := statement.AddNotValid(tc.sql)
assert.ErrorIs(t, err, statement.ErrNotValidNotApplicable)
})
}
}
// A drop-index operation names what it discards: the label carries the
// (qualified) index name so a destructive decision is never anonymous.
func TestParseOpsDropIndexCapturesName(t *testing.T) {
ops, err := statement.ParseOps("DROP INDEX app.orders_legacy_idx")
require.NoError(t, err)
require.Len(t, ops, 1)
assert.Equal(t, statement.OpDropIndex, ops[0].Kind)
assert.Equal(t, "app.orders_legacy_idx", ops[0].Name)
assert.Equal(t, "DROP INDEX app.orders_legacy_idx", ops[0].Describe())
multi, err := statement.ParseOps("DROP INDEX a_idx, b_idx")
require.NoError(t, err)
require.Len(t, multi, 1)
assert.Equal(t, "a_idx, b_idx", multi[0].Name)
}
// An unnamed CREATE INDEX labels cleanly, without a dangling space.
func TestDescribeUnnamedCreateIndexHasNoTrailingSpace(t *testing.T) {
ops, err := statement.ParseOps("CREATE INDEX ON t (c)")
require.NoError(t, err)
require.Len(t, ops, 1)
assert.Equal(t, "CREATE INDEX", ops[0].Describe())
}