@@ -90,23 +90,35 @@ func TestConvertAlterTableStatements(t *testing.T) {
90
90
},
91
91
{
92
92
sql : "ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d);" ,
93
- expectedOp : expect .AddForeignKeyOp1WithOnDelete ( migrations .ForeignKeyActionNOACTION ),
93
+ expectedOp : expect .AddForeignKeyOp1WithParams ( migrations . ForeignKeyMatchTypeSIMPLE , migrations . ForeignKeyActionNOACTION , migrations .ForeignKeyActionNOACTION ),
94
94
},
95
95
{
96
96
sql : "ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON DELETE NO ACTION;" ,
97
- expectedOp : expect .AddForeignKeyOp1WithOnDelete ( migrations .ForeignKeyActionNOACTION ),
97
+ expectedOp : expect .AddForeignKeyOp1WithParams ( migrations . ForeignKeyMatchTypeSIMPLE , migrations . ForeignKeyActionNOACTION , migrations .ForeignKeyActionNOACTION ),
98
98
},
99
99
{
100
100
sql : "ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON DELETE RESTRICT;" ,
101
- expectedOp : expect .AddForeignKeyOp1WithOnDelete (migrations .ForeignKeyActionRESTRICT ),
101
+ expectedOp : expect .AddForeignKeyOp1WithParams (migrations .ForeignKeyMatchTypeSIMPLE , migrations . ForeignKeyActionRESTRICT , migrations . ForeignKeyActionNOACTION ),
102
102
},
103
103
{
104
104
sql : "ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON DELETE SET DEFAULT ;" ,
105
- expectedOp : expect .AddForeignKeyOp1WithOnDelete (migrations .ForeignKeyActionSETDEFAULT ),
105
+ expectedOp : expect .AddForeignKeyOp1WithParams (migrations .ForeignKeyMatchTypeSIMPLE , migrations . ForeignKeyActionSETDEFAULT , migrations . ForeignKeyActionNOACTION ),
106
106
},
107
107
{
108
108
sql : "ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON DELETE SET NULL;" ,
109
- expectedOp : expect .AddForeignKeyOp1WithOnDelete (migrations .ForeignKeyActionSETNULL ),
109
+ expectedOp : expect .AddForeignKeyOp1WithParams (migrations .ForeignKeyMatchTypeSIMPLE , migrations .ForeignKeyActionSETNULL , migrations .ForeignKeyActionNOACTION ),
110
+ },
111
+ {
112
+ sql : "ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON DELETE RESTRICT ON UPDATE SET DEFAULT;" ,
113
+ expectedOp : expect .AddForeignKeyOp1WithParams (migrations .ForeignKeyMatchTypeSIMPLE , migrations .ForeignKeyActionRESTRICT , migrations .ForeignKeyActionSETDEFAULT ),
114
+ },
115
+ {
116
+ sql : "ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON DELETE SET DEFAULT ON UPDATE RESTRICT;" ,
117
+ expectedOp : expect .AddForeignKeyOp1WithParams (migrations .ForeignKeyMatchTypeSIMPLE , migrations .ForeignKeyActionSETDEFAULT , migrations .ForeignKeyActionRESTRICT ),
118
+ },
119
+ {
120
+ sql : "ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) MATCH FULL ON UPDATE SET NULL;" ,
121
+ expectedOp : expect .AddForeignKeyOp1WithParams (migrations .ForeignKeyMatchTypeFULL , migrations .ForeignKeyActionNOACTION , migrations .ForeignKeyActionSETNULL ),
110
122
},
111
123
{
112
124
sql : "ALTER TABLE foo ADD CONSTRAINT fk_bar_c FOREIGN KEY (a) REFERENCES bar (c);" ,
@@ -195,32 +207,68 @@ func TestConvertAlterTableStatements(t *testing.T) {
195
207
expectedOp : expect .AddColumnOp7 ,
196
208
},
197
209
{
198
- sql : "ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar)" ,
199
- expectedOp : expect .AddColumnOp8WithOnDeleteAction (migrations .ForeignKeyActionNOACTION ),
210
+ sql : "ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) MATCH FULL " ,
211
+ expectedOp : expect .AddColumnOp8WithOnDeleteAction (migrations .ForeignKeyActionNOACTION , migrations . ForeignKeyActionNOACTION , migrations . ForeignKeyMatchTypeFULL ),
200
212
},
201
213
{
202
214
sql : "ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) ON UPDATE NO ACTION" ,
203
- expectedOp : expect .AddColumnOp8WithOnDeleteAction (migrations .ForeignKeyActionNOACTION ),
215
+ expectedOp : expect .AddColumnOp8WithOnDeleteAction (migrations .ForeignKeyActionNOACTION , migrations . ForeignKeyActionNOACTION , migrations . ForeignKeyMatchTypeSIMPLE ),
204
216
},
205
217
{
206
218
sql : "ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) ON DELETE NO ACTION" ,
207
- expectedOp : expect .AddColumnOp8WithOnDeleteAction (migrations .ForeignKeyActionNOACTION ),
219
+ expectedOp : expect .AddColumnOp8WithOnDeleteAction (migrations .ForeignKeyActionNOACTION , migrations . ForeignKeyActionNOACTION , migrations . ForeignKeyMatchTypeSIMPLE ),
208
220
},
209
221
{
210
222
sql : "ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) ON DELETE RESTRICT" ,
211
- expectedOp : expect .AddColumnOp8WithOnDeleteAction (migrations .ForeignKeyActionRESTRICT ),
223
+ expectedOp : expect .AddColumnOp8WithOnDeleteAction (migrations .ForeignKeyActionRESTRICT , migrations .ForeignKeyActionNOACTION , migrations .ForeignKeyMatchTypeSIMPLE ),
224
+ },
225
+ {
226
+ sql : "ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) MATCH SIMPLE ON DELETE SET NULL " ,
227
+ expectedOp : expect .AddColumnOp8WithOnDeleteAction (migrations .ForeignKeyActionSETNULL , migrations .ForeignKeyActionNOACTION , migrations .ForeignKeyMatchTypeSIMPLE ),
228
+ },
229
+ {
230
+ sql : "ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) ON DELETE SET DEFAULT ON UPDATE NO ACTION" ,
231
+ expectedOp : expect .AddColumnOp8WithOnDeleteAction (migrations .ForeignKeyActionSETDEFAULT , migrations .ForeignKeyActionNOACTION , migrations .ForeignKeyMatchTypeSIMPLE ),
232
+ },
233
+ {
234
+ sql : "ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE" ,
235
+ expectedOp : expect .AddColumnOp8WithOnDeleteAction (migrations .ForeignKeyActionCASCADE , migrations .ForeignKeyActionCASCADE , migrations .ForeignKeyMatchTypeFULL ),
236
+ },
237
+ {
238
+ sql : "ALTER TABLE foo ADD COLUMN bar int REFERENCES baz (bar) ON UPDATE RESTRICT" ,
239
+ expectedOp : expect .AddColumnOp9WithOnDeleteActionUnnamed ("foo_bar_fkey" , migrations .ForeignKeyActionNOACTION , migrations .ForeignKeyActionRESTRICT , migrations .ForeignKeyMatchTypeSIMPLE ),
240
+ },
241
+ {
242
+ sql : "ALTER TABLE foo ADD COLUMN bar int REFERENCES baz (bar) ON UPDATE CASCADE" ,
243
+ expectedOp : expect .AddColumnOp9WithOnDeleteActionUnnamed ("foo_bar_fkey" , migrations .ForeignKeyActionNOACTION , migrations .ForeignKeyActionCASCADE , migrations .ForeignKeyMatchTypeSIMPLE ),
244
+ },
245
+ {
246
+ sql : "ALTER TABLE foo ADD COLUMN bar int REFERENCES baz (bar) ON UPDATE SET NULL" ,
247
+ expectedOp : expect .AddColumnOp9WithOnDeleteActionUnnamed ("foo_bar_fkey" , migrations .ForeignKeyActionNOACTION , migrations .ForeignKeyActionSETNULL , migrations .ForeignKeyMatchTypeSIMPLE ),
248
+ },
249
+ {
250
+ sql : "ALTER TABLE foo ADD COLUMN bar int REFERENCES baz (bar) ON UPDATE SET DEFAULT" ,
251
+ expectedOp : expect .AddColumnOp9WithOnDeleteActionUnnamed ("foo_bar_fkey" , migrations .ForeignKeyActionNOACTION , migrations .ForeignKeyActionSETDEFAULT , migrations .ForeignKeyMatchTypeSIMPLE ),
252
+ },
253
+ {
254
+ sql : "ALTER TABLE foo ADD CONSTRAINT fk_baz FOREIGN KEY (a, b) REFERENCES bar (c, d) ON UPDATE RESTRICT" ,
255
+ expectedOp : expect .AddConstraintOp10ForeignKey (migrations .ForeignKeyActionNOACTION , migrations .ForeignKeyActionRESTRICT , migrations .ForeignKeyMatchTypeSIMPLE ),
256
+ },
257
+ {
258
+ sql : "ALTER TABLE foo ADD CONSTRAINT fk_baz FOREIGN KEY (a, b) REFERENCES bar (c, d) ON UPDATE CASCADE" ,
259
+ expectedOp : expect .AddConstraintOp10ForeignKey (migrations .ForeignKeyActionNOACTION , migrations .ForeignKeyActionCASCADE , migrations .ForeignKeyMatchTypeSIMPLE ),
212
260
},
213
261
{
214
- sql : "ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar ) ON DELETE SET NULL " ,
215
- expectedOp : expect .AddColumnOp8WithOnDeleteAction (migrations .ForeignKeyActionSETNULL ),
262
+ sql : "ALTER TABLE foo ADD CONSTRAINT fk_baz FOREIGN KEY (a, b) REFERENCES bar (c, d ) ON UPDATE SET NULL" ,
263
+ expectedOp : expect .AddConstraintOp10ForeignKey (migrations .ForeignKeyActionNOACTION , migrations . ForeignKeyActionSETNULL , migrations . ForeignKeyMatchTypeSIMPLE ),
216
264
},
217
265
{
218
- sql : "ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar ) ON DELETE SET DEFAULT" ,
219
- expectedOp : expect .AddColumnOp8WithOnDeleteAction (migrations .ForeignKeyActionSETDEFAULT ),
266
+ sql : "ALTER TABLE foo ADD CONSTRAINT fk_baz FOREIGN KEY (a, b) REFERENCES bar (c, d ) ON UPDATE SET DEFAULT" ,
267
+ expectedOp : expect .AddConstraintOp10ForeignKey (migrations .ForeignKeyActionNOACTION , migrations . ForeignKeyActionSETDEFAULT , migrations . ForeignKeyMatchTypeSIMPLE ),
220
268
},
221
269
{
222
- sql : "ALTER TABLE foo ADD COLUMN bar int CONSTRAINT fk_baz REFERENCES baz (bar) ON DELETE CASCADE " ,
223
- expectedOp : expect .AddColumnOp8WithOnDeleteAction (migrations .ForeignKeyActionCASCADE ),
270
+ sql : "ALTER TABLE foo ADD CONSTRAINT fk_baz FOREIGN KEY (a, b) REFERENCES bar (c, d) MATCH FULL " ,
271
+ expectedOp : expect .AddConstraintOp10ForeignKey (migrations .ForeignKeyActionNOACTION , migrations . ForeignKeyActionNOACTION , migrations . ForeignKeyMatchTypeFULL ),
224
272
},
225
273
{
226
274
sql : "ALTER TABLE foo ADD COLUMN bar int GENERATED BY DEFAULT AS IDENTITY " ,
@@ -265,11 +313,6 @@ func TestUnconvertableAlterTableStatements(t *testing.T) {
265
313
"ALTER TABLE foo DROP COLUMN IF EXISTS bar" ,
266
314
267
315
// Unsupported foreign key statements
268
- "ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON UPDATE RESTRICT;" ,
269
- "ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON UPDATE CASCADE;" ,
270
- "ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON UPDATE SET NULL;" ,
271
- "ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON UPDATE SET DEFAULT;" ,
272
- "ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) MATCH FULL;" ,
273
316
"ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) NOT VALID" ,
274
317
// MATCH PARTIAL is not implemented in the actual parser yet
275
318
//"ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) MATCH PARTIAL;",
@@ -283,10 +326,6 @@ func TestUnconvertableAlterTableStatements(t *testing.T) {
283
326
"ALTER TABLE foo ADD CONSTRAINT bar CHECK (age > 0) NOT VALID" ,
284
327
285
328
// ADD COLUMN cases not yet covered
286
- "ALTER TABLE foo ADD COLUMN bar int REFERENCES bar (c) ON UPDATE RESTRICT" ,
287
- "ALTER TABLE foo ADD COLUMN bar int REFERENCES bar (c) ON UPDATE CASCADE" ,
288
- "ALTER TABLE foo ADD COLUMN bar int REFERENCES bar (c) ON UPDATE SET NULL" ,
289
- "ALTER TABLE foo ADD COLUMN bar int REFERENCES bar (c) ON UPDATE SET DEFAULT" ,
290
329
"ALTER TABLE foo ADD COLUMN IF NOT EXISTS bar int" ,
291
330
"ALTER TABLE foo ADD COLUMN bar int UNIQUE DEFERRABLE" ,
292
331
"ALTER TABLE foo ADD COLUMN bar int UNIQUE INITIALLY DEFERRED" ,
0 commit comments