Skip to content

Commit 49e35c4

Browse files
Add extra validation checks to OpRenameColumn (#615)
Validate that `from` and `to` fields are not empty during validation of `OpRenameColumn` operations
1 parent 1214e52 commit 49e35c4

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

pkg/migrations/op_rename_column.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ func (o *OpRenameColumn) Rollback(ctx context.Context, conn db.DB, tr SQLTransfo
5151
func (o *OpRenameColumn) Validate(ctx context.Context, s *schema.Schema) error {
5252
table := s.GetTable(o.Table)
5353

54+
// Ensure that the `from` field is not empty
55+
if o.From == "" {
56+
return FieldRequiredError{Name: "from"}
57+
}
58+
59+
// Ensure that the `to` field is not empty
60+
if o.To == "" {
61+
return FieldRequiredError{Name: "to"}
62+
}
63+
5464
// Ensure that the table exists.
5565
if table == nil {
5666
return TableDoesNotExistError{Name: o.Table}

pkg/migrations/op_rename_column_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,38 @@ func TestOpRenameColumnValidation(t *testing.T) {
342342
}
343343

344344
ExecuteTests(t, TestCases{
345+
{
346+
name: "from field must be specified",
347+
migrations: []migrations.Migration{
348+
createTableMigration,
349+
{
350+
Name: "02_rename_column",
351+
Operations: migrations.Operations{
352+
&migrations.OpRenameColumn{
353+
Table: "users",
354+
To: "name",
355+
},
356+
},
357+
},
358+
},
359+
wantStartErr: migrations.FieldRequiredError{Name: "from"},
360+
},
361+
{
362+
name: "to field must be specified",
363+
migrations: []migrations.Migration{
364+
createTableMigration,
365+
{
366+
Name: "02_rename_column",
367+
Operations: migrations.Operations{
368+
&migrations.OpRenameColumn{
369+
Table: "users",
370+
From: "username",
371+
},
372+
},
373+
},
374+
},
375+
wantStartErr: migrations.FieldRequiredError{Name: "to"},
376+
},
345377
{
346378
name: "table must exist",
347379
migrations: []migrations.Migration{

0 commit comments

Comments
 (0)