From cf66d1f5b73870ec5772ce8f268f68d5efaea856 Mon Sep 17 00:00:00 2001 From: Andrew Farries Date: Tue, 26 Mar 2024 09:52:33 +0000 Subject: [PATCH] Update operation types to set a default of `""` for `up` and `down` SQL (#325) Improve consistency between operation types by updating operation types to ensure that all operations that use `up` or `down` SQL default these fields to `""`. There is no distinction between an empty string and `nil` for these fields. --- pkg/migrations/op_add_column.go | 8 +-- pkg/migrations/op_add_column_test.go | 18 +++--- pkg/migrations/op_alter_column.go | 35 +++++------ pkg/migrations/op_alter_column_test.go | 4 +- pkg/migrations/op_change_type_test.go | 36 +++++------ pkg/migrations/op_drop_column.go | 4 +- pkg/migrations/op_drop_column_test.go | 6 +- pkg/migrations/op_drop_constraint_test.go | 28 ++++----- pkg/migrations/op_drop_not_null_test.go | 36 +++++------ pkg/migrations/op_set_check_test.go | 40 ++++++------ pkg/migrations/op_set_fk_test.go | 76 +++++++++++------------ pkg/migrations/op_set_notnull_test.go | 28 ++++----- pkg/migrations/op_set_unique_test.go | 26 ++++---- pkg/migrations/types.go | 8 +-- pkg/roll/execute_test.go | 4 +- schema.json | 5 ++ 16 files changed, 180 insertions(+), 182 deletions(-) diff --git a/pkg/migrations/op_add_column.go b/pkg/migrations/op_add_column.go index c08999f..1d72cc2 100644 --- a/pkg/migrations/op_add_column.go +++ b/pkg/migrations/op_add_column.go @@ -41,7 +41,7 @@ func (o *OpAddColumn) Start(ctx context.Context, conn *sql.DB, stateSchema strin } var tableToBackfill *schema.Table - if o.Up != nil { + if o.Up != "" { err := createTrigger(ctx, conn, triggerConfig{ Name: TriggerName(o.Table, o.Column.Name), Direction: TriggerDirectionUp, @@ -50,7 +50,7 @@ func (o *OpAddColumn) Start(ctx context.Context, conn *sql.DB, stateSchema strin TableName: o.Table, PhysicalColumn: TemporaryName(o.Column.Name), StateSchema: stateSchema, - SQL: *o.Up, + SQL: o.Up, }) if err != nil { return nil, fmt.Errorf("failed to create trigger: %w", err) @@ -164,14 +164,14 @@ func (o *OpAddColumn) Validate(ctx context.Context, s *schema.Schema) error { } // Ensure backfill is possible - if o.Up != nil { + if o.Up != "" { err := checkBackfill(table) if err != nil { return err } } - if !o.Column.IsNullable() && o.Column.Default == nil && o.Up == nil { + if !o.Column.IsNullable() && o.Column.Default == nil && o.Up == "" { return FieldRequiredError{Name: "up"} } diff --git a/pkg/migrations/op_add_column_test.go b/pkg/migrations/op_add_column_test.go index 1a15277..957161e 100644 --- a/pkg/migrations/op_add_column_test.go +++ b/pkg/migrations/op_add_column_test.go @@ -142,7 +142,7 @@ func TestAddColumn(t *testing.T) { Nullable: ptr(false), Unique: ptr(true), }, - Up: ptr("'this is a description'"), + Up: "'this is a description'", }, }, }, @@ -310,7 +310,7 @@ func TestAddForeignKeyColumn(t *testing.T) { }, Nullable: ptr(false), }, - Up: ptr("1"), + Up: "1", }, }, }, @@ -627,7 +627,7 @@ func TestAddColumnWithUpSql(t *testing.T) { Operations: migrations.Operations{ &migrations.OpAddColumn{ Table: "products", - Up: ptr("UPPER(name)"), + Up: "UPPER(name)", Column: migrations.Column{ Name: "description", Type: "varchar(255)", @@ -709,7 +709,7 @@ func TestAddColumnWithUpSql(t *testing.T) { Operations: migrations.Operations{ &migrations.OpAddColumn{ Table: "products", - Up: ptr("UPPER(name)"), + Up: "UPPER(name)", Column: migrations.Column{ Name: "description", Type: "varchar(255)", @@ -798,7 +798,7 @@ func TestAddColumnWithUpSql(t *testing.T) { Operations: migrations.Operations{ &migrations.OpAddColumn{ Table: "products", - Up: ptr("UPPER(name)"), + Up: "UPPER(name)", Column: migrations.Column{ Name: "description", Type: "varchar(255)", @@ -892,7 +892,7 @@ func TestAddNotNullColumnWithNoDefault(t *testing.T) { Operations: migrations.Operations{ &migrations.OpAddColumn{ Table: "products", - Up: ptr("UPPER(name)"), + Up: "UPPER(name)", Column: migrations.Column{ Name: "description", Type: "varchar(255)", @@ -1075,7 +1075,7 @@ func TestAddColumnValidation(t *testing.T) { Operations: migrations.Operations{ &migrations.OpAddColumn{ Table: "orders", - Up: ptr("UPPER(name)"), + Up: "UPPER(name)", Column: migrations.Column{ Name: "description", Type: "text", @@ -1123,7 +1123,7 @@ func TestAddColumnValidation(t *testing.T) { Operations: migrations.Operations{ &migrations.OpAddColumn{ Table: "users", - Up: ptr("UPPER(name)"), + Up: "UPPER(name)", Column: migrations.Column{ Default: ptr("'foo'"), Name: "description", @@ -1144,7 +1144,7 @@ func TestAddColumnValidation(t *testing.T) { Operations: migrations.Operations{ &migrations.OpAddColumn{ Table: "users", - Up: ptr("UPPER(name)"), + Up: "UPPER(name)", Column: migrations.Column{ Default: ptr("'foo'"), Name: "description", diff --git a/pkg/migrations/op_alter_column.go b/pkg/migrations/op_alter_column.go index 9ede6b8..dd57063 100644 --- a/pkg/migrations/op_alter_column.go +++ b/pkg/migrations/op_alter_column.go @@ -53,10 +53,10 @@ func (o *OpAlterColumn) Validate(ctx context.Context, s *schema.Schema) error { // Apply any special validation rules for the inner operation op := o.innerOperation() if _, ok := op.(*OpRenameColumn); ok { - if o.Up != nil { + if o.Up != "" { return NoUpSQLAllowedError{} } - if o.Down != nil { + if o.Down != "" { return NoDownSQLAllowedError{} } } @@ -79,8 +79,8 @@ func (o *OpAlterColumn) innerOperation() Operation { Table: o.Table, Column: o.Column, Type: *o.Type, - Up: ptrToStr(o.Up), - Down: ptrToStr(o.Down), + Up: o.Up, + Down: o.Down, } case o.Check != nil: @@ -88,8 +88,8 @@ func (o *OpAlterColumn) innerOperation() Operation { Table: o.Table, Column: o.Column, Check: *o.Check, - Up: ptrToStr(o.Up), - Down: ptrToStr(o.Down), + Up: o.Up, + Down: o.Down, } case o.References != nil: @@ -97,24 +97,24 @@ func (o *OpAlterColumn) innerOperation() Operation { Table: o.Table, Column: o.Column, References: *o.References, - Up: ptrToStr(o.Up), - Down: ptrToStr(o.Down), + Up: o.Up, + Down: o.Down, } case o.Nullable != nil && !*o.Nullable: return &OpSetNotNull{ Table: o.Table, Column: o.Column, - Up: ptrToStr(o.Up), - Down: ptrToStr(o.Down), + Up: o.Up, + Down: o.Down, } case o.Nullable != nil && *o.Nullable: return &OpDropNotNull{ Table: o.Table, Column: o.Column, - Up: ptrToStr(o.Up), - Down: ptrToStr(o.Down), + Up: o.Up, + Down: o.Down, } case o.Unique != nil: @@ -122,8 +122,8 @@ func (o *OpAlterColumn) innerOperation() Operation { Table: o.Table, Column: o.Column, Name: o.Unique.Name, - Up: ptrToStr(o.Up), - Down: ptrToStr(o.Down), + Up: o.Up, + Down: o.Down, } } return nil @@ -155,10 +155,3 @@ func (o *OpAlterColumn) numChanges() int { return fieldsSet } - -func ptrToStr(s *string) string { - if s == nil { - return "" - } - return *s -} diff --git a/pkg/migrations/op_alter_column_test.go b/pkg/migrations/op_alter_column_test.go index 71e0b60..c165fa7 100644 --- a/pkg/migrations/op_alter_column_test.go +++ b/pkg/migrations/op_alter_column_test.go @@ -95,7 +95,7 @@ func TestAlterColumnValidation(t *testing.T) { Table: "posts", Column: "title", Name: ptr("renamed_title"), - Up: ptr("some up sql"), + Up: "some up sql", }, }, }, @@ -113,7 +113,7 @@ func TestAlterColumnValidation(t *testing.T) { Table: "posts", Column: "title", Name: ptr("renamed_title"), - Down: ptr("some down sql"), + Down: "some down sql", }, }, }, diff --git a/pkg/migrations/op_change_type_test.go b/pkg/migrations/op_change_type_test.go index 8cef798..12a6ecd 100644 --- a/pkg/migrations/op_change_type_test.go +++ b/pkg/migrations/op_change_type_test.go @@ -54,8 +54,8 @@ func TestChangeColumnType(t *testing.T) { Table: "reviews", Column: "rating", Type: ptr("integer"), - Up: ptr("CAST (rating AS integer)"), - Down: ptr("CAST (rating AS text)"), + Up: "CAST (rating AS integer)", + Down: "CAST (rating AS text)", }, }, }, @@ -207,8 +207,8 @@ func TestChangeColumnType(t *testing.T) { Table: "employees", Column: "department_id", Type: ptr("bigint"), - Up: ptr("department_id"), - Down: ptr("department_id"), + Up: "department_id", + Down: "department_id", }, }, }, @@ -255,8 +255,8 @@ func TestChangeColumnType(t *testing.T) { Table: "users", Column: "username", Type: ptr("varchar(255)"), - Up: ptr("username"), - Down: ptr("username"), + Up: "username", + Down: "username", }, }, }, @@ -323,8 +323,8 @@ func TestChangeColumnType(t *testing.T) { Table: "users", Column: "username", Type: ptr("varchar(255)"), - Up: ptr("username"), - Down: ptr("username"), + Up: "username", + Down: "username", }, }, }, @@ -376,8 +376,8 @@ func TestChangeColumnType(t *testing.T) { Table: "users", Column: "username", Type: ptr("varchar(255)"), - Up: ptr("username"), - Down: ptr("username"), + Up: "username", + Down: "username", }, }, }, @@ -426,8 +426,8 @@ func TestChangeColumnType(t *testing.T) { Table: "users", Column: "username", Unique: &migrations.UniqueConstraint{Name: "unique_username"}, - Up: ptr("username"), - Down: ptr("username"), + Up: "username", + Down: "username", }, }, }, @@ -438,8 +438,8 @@ func TestChangeColumnType(t *testing.T) { Table: "users", Column: "username", Type: ptr("varchar(255)"), - Up: ptr("username"), - Down: ptr("username"), + Up: "username", + Down: "username", }, }, }, @@ -502,8 +502,8 @@ func TestChangeColumnType(t *testing.T) { Table: "users", Column: "username", Type: ptr("varchar(255)"), - Up: ptr("username"), - Down: ptr("username"), + Up: "username", + Down: "username", }, }, }, @@ -565,7 +565,7 @@ func TestChangeColumnTypeValidation(t *testing.T) { Table: "reviews", Column: "rating", Type: ptr("integer"), - Down: ptr("CAST (rating AS text)"), + Down: "CAST (rating AS text)", }, }, }, @@ -583,7 +583,7 @@ func TestChangeColumnTypeValidation(t *testing.T) { Table: "reviews", Column: "rating", Type: ptr("integer"), - Up: ptr("CAST (rating AS integer)"), + Up: "CAST (rating AS integer)", }, }, }, diff --git a/pkg/migrations/op_drop_column.go b/pkg/migrations/op_drop_column.go index 27a50e1..7160652 100644 --- a/pkg/migrations/op_drop_column.go +++ b/pkg/migrations/op_drop_column.go @@ -14,7 +14,7 @@ import ( var _ Operation = (*OpDropColumn)(nil) func (o *OpDropColumn) Start(ctx context.Context, conn *sql.DB, stateSchema string, s *schema.Schema, cbs ...CallbackFn) (*schema.Table, error) { - if o.Down != nil { + if o.Down != "" { err := createTrigger(ctx, conn, triggerConfig{ Name: TriggerName(o.Table, o.Column), Direction: TriggerDirectionDown, @@ -23,7 +23,7 @@ func (o *OpDropColumn) Start(ctx context.Context, conn *sql.DB, stateSchema stri TableName: o.Table, PhysicalColumn: o.Column, StateSchema: stateSchema, - SQL: *o.Down, + SQL: o.Down, }) if err != nil { return nil, err diff --git a/pkg/migrations/op_drop_column_test.go b/pkg/migrations/op_drop_column_test.go index 7677de3..e2ee7b7 100644 --- a/pkg/migrations/op_drop_column_test.go +++ b/pkg/migrations/op_drop_column_test.go @@ -49,7 +49,7 @@ func TestDropColumnWithDownSQL(t *testing.T) { &migrations.OpDropColumn{ Table: "users", Column: "name", - Down: ptr("UPPER(email)"), + Down: "UPPER(email)", }, }, }, @@ -133,7 +133,7 @@ func TestDropColumnWithDownSQL(t *testing.T) { &migrations.OpDropColumn{ Table: "users", Column: "array", - Down: ptr("UPPER(email)"), + Down: "UPPER(email)", }, }, }, @@ -175,7 +175,7 @@ func TestDropColumnWithDownSQL(t *testing.T) { &migrations.OpDropColumn{ Table: "array", Column: "name", - Down: ptr("UPPER(email)"), + Down: "UPPER(email)", }, }, }, diff --git a/pkg/migrations/op_drop_constraint_test.go b/pkg/migrations/op_drop_constraint_test.go index 6ef0bb7..da58caf 100644 --- a/pkg/migrations/op_drop_constraint_test.go +++ b/pkg/migrations/op_drop_constraint_test.go @@ -47,8 +47,8 @@ func TestDropConstraint(t *testing.T) { Name: "check_title_length", Constraint: "length(title) > 3", }, - Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"), - Down: ptr("title"), + Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)", + Down: "title", }, }, }, @@ -176,8 +176,8 @@ func TestDropConstraint(t *testing.T) { Name: "check_title_length", Constraint: "length(title) > 3", }, - Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"), - Down: ptr("title"), + Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)", + Down: "title", }, }, }, @@ -262,8 +262,8 @@ func TestDropConstraint(t *testing.T) { Table: "users", Column: "id", }, - Up: ptr("(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)"), - Down: ptr("user_id"), + Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)", + Down: "user_id", }, }, }, @@ -637,8 +637,8 @@ func TestDropConstraint(t *testing.T) { Name: "check_title_length", Constraint: "length(title) > 3", }, - Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"), - Down: ptr("title"), + Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)", + Down: "title", }, }, }, @@ -702,8 +702,8 @@ func TestDropConstraint(t *testing.T) { Table: "posts", Column: "title", Unique: &migrations.UniqueConstraint{Name: "unique_title"}, - Up: ptr("title"), - Down: ptr("title"), + Up: "title", + Down: "title", }, }, }, @@ -717,8 +717,8 @@ func TestDropConstraint(t *testing.T) { Name: "check_title_length", Constraint: "length(title) > 3", }, - Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"), - Down: ptr("title"), + Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)", + Down: "title", }, }, }, @@ -899,8 +899,8 @@ func TestDropConstraintValidation(t *testing.T) { Name: "check_title_length", Constraint: "length(title) > 3", }, - Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"), - Down: ptr("title"), + Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)", + Down: "title", }, }, } diff --git a/pkg/migrations/op_drop_not_null_test.go b/pkg/migrations/op_drop_not_null_test.go index 0b91aa4..446a7ad 100644 --- a/pkg/migrations/op_drop_not_null_test.go +++ b/pkg/migrations/op_drop_not_null_test.go @@ -55,7 +55,7 @@ func TestDropNotNull(t *testing.T) { Table: "reviews", Column: "review", Nullable: ptr(true), - Down: ptr("(SELECT CASE WHEN review IS NULL THEN product || ' is good' ELSE review END)"), + Down: "(SELECT CASE WHEN review IS NULL THEN product || ' is good' ELSE review END)", }, }, }, @@ -191,8 +191,8 @@ func TestDropNotNull(t *testing.T) { Table: "reviews", Column: "review", Nullable: ptr(true), - Down: ptr("(SELECT CASE WHEN review IS NULL THEN product || ' is good' ELSE review END)"), - Up: ptr("review || ' (from the old column)'"), + Down: "(SELECT CASE WHEN review IS NULL THEN product || ' is good' ELSE review END)", + Up: "review || ' (from the old column)'", }, }, }, @@ -277,8 +277,8 @@ func TestDropNotNull(t *testing.T) { Table: "employees", Column: "department_id", Nullable: ptr(true), - Down: ptr("(SELECT CASE WHEN department_id IS NULL THEN 1 ELSE department_id END)"), - Up: ptr("department_id"), + Down: "(SELECT CASE WHEN department_id IS NULL THEN 1 ELSE department_id END)", + Up: "department_id", }, }, }, @@ -325,8 +325,8 @@ func TestDropNotNull(t *testing.T) { Table: "users", Column: "name", Nullable: ptr(true), - Up: ptr("name"), - Down: ptr("(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)"), + Up: "name", + Down: "(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)", }, }, }, @@ -393,8 +393,8 @@ func TestDropNotNull(t *testing.T) { Table: "users", Column: "name", Nullable: ptr(true), - Up: ptr("name"), - Down: ptr("(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)"), + Up: "name", + Down: "(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)", }, }, }, @@ -446,8 +446,8 @@ func TestDropNotNull(t *testing.T) { Table: "users", Column: "name", Unique: &migrations.UniqueConstraint{Name: "unique_name"}, - Up: ptr("name"), - Down: ptr("name"), + Up: "name", + Down: "name", }, }, }, @@ -458,8 +458,8 @@ func TestDropNotNull(t *testing.T) { Table: "users", Column: "name", Nullable: ptr(true), - Up: ptr("name"), - Down: ptr("(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)"), + Up: "name", + Down: "(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)", }, }, }, @@ -523,8 +523,8 @@ func TestDropNotNull(t *testing.T) { Table: "users", Column: "name", Nullable: ptr(true), - Up: ptr("name"), - Down: ptr("(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)"), + Up: "name", + Down: "(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)", }, }, }, @@ -577,7 +577,7 @@ func TestDropNotNullValidation(t *testing.T) { Table: "users", Column: "name", Nullable: ptr(true), - Up: ptr("name"), + Up: "name", }, }, }, @@ -614,8 +614,8 @@ func TestDropNotNullValidation(t *testing.T) { Table: "users", Column: "name", Nullable: ptr(true), - Up: ptr("name"), - Down: ptr("(SELECT CASE WHEN name IS NULL THEN 'placeholder' ELSE name END)"), + Up: "name", + Down: "(SELECT CASE WHEN name IS NULL THEN 'placeholder' ELSE name END)", }, }, }, diff --git a/pkg/migrations/op_set_check_test.go b/pkg/migrations/op_set_check_test.go index cc397ab..602fc80 100644 --- a/pkg/migrations/op_set_check_test.go +++ b/pkg/migrations/op_set_check_test.go @@ -47,8 +47,8 @@ func TestSetCheckConstraint(t *testing.T) { Name: "check_title_length", Constraint: "length(title) > 3", }, - Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"), - Down: ptr("title"), + Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)", + Down: "title", }, }, }, @@ -180,8 +180,8 @@ func TestSetCheckConstraint(t *testing.T) { Name: "check_title_length", Constraint: "length(title) > 3", }, - Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"), - Down: ptr("title"), + Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)", + Down: "title", }, }, }, @@ -278,8 +278,8 @@ func TestSetCheckConstraint(t *testing.T) { Name: "check_valid_department_id", Constraint: "department_id > 1", }, - Up: ptr("(SELECT CASE WHEN department_id <= 1 THEN 2 ELSE department_id END)"), - Down: ptr("department_id"), + Up: "(SELECT CASE WHEN department_id <= 1 THEN 2 ELSE department_id END)", + Down: "department_id", }, }, }, @@ -335,8 +335,8 @@ func TestSetCheckConstraint(t *testing.T) { Name: "check_body_length", Constraint: "length(body) > 3", }, - Up: ptr("(SELECT CASE WHEN length(body) <= 3 THEN LPAD(body, 4, '-') ELSE body END)"), - Down: ptr("body"), + Up: "(SELECT CASE WHEN length(body) <= 3 THEN LPAD(body, 4, '-') ELSE body END)", + Down: "body", }, }, }, @@ -393,8 +393,8 @@ func TestSetCheckConstraint(t *testing.T) { Name: "check_title_length", Constraint: "length(title) > 3", }, - Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"), - Down: ptr("title"), + Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)", + Down: "title", }, }, }, @@ -443,8 +443,8 @@ func TestSetCheckConstraint(t *testing.T) { Table: "posts", Column: "title", Unique: &migrations.UniqueConstraint{Name: "unique_title"}, - Up: ptr("title"), - Down: ptr("title"), + Up: "title", + Down: "title", }, }, }, @@ -458,8 +458,8 @@ func TestSetCheckConstraint(t *testing.T) { Name: "check_title_length", Constraint: "length(title) > 3", }, - Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"), - Down: ptr("title"), + Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)", + Down: "title", }, }, }, @@ -525,8 +525,8 @@ func TestSetCheckConstraint(t *testing.T) { Name: "check_title_length", Constraint: "length(title) > 3", }, - Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"), - Down: ptr("title"), + Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)", + Down: "title", }, }, }, @@ -582,8 +582,8 @@ func TestSetCheckConstraintValidation(t *testing.T) { Check: &migrations.CheckConstraint{ Constraint: "length(title) > 3", }, - Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"), - Down: ptr("title"), + Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)", + Down: "title", }, }, }, @@ -604,7 +604,7 @@ func TestSetCheckConstraintValidation(t *testing.T) { Name: "check_title_length", Constraint: "length(title) > 3", }, - Down: ptr("title"), + Down: "title", }, }, }, @@ -625,7 +625,7 @@ func TestSetCheckConstraintValidation(t *testing.T) { Name: "check_title_length", Constraint: "length(title) > 3", }, - Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"), + Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)", }, }, }, diff --git a/pkg/migrations/op_set_fk_test.go b/pkg/migrations/op_set_fk_test.go index 52e8947..665048e 100644 --- a/pkg/migrations/op_set_fk_test.go +++ b/pkg/migrations/op_set_fk_test.go @@ -67,8 +67,8 @@ func TestSetForeignKey(t *testing.T) { Table: "users", Column: "id", }, - Up: ptr("(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)"), - Down: ptr("user_id"), + Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)", + Down: "user_id", }, }, }, @@ -232,8 +232,8 @@ func TestSetForeignKey(t *testing.T) { Table: "users", Column: "id", }, - Up: ptr("(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)"), - Down: ptr("user_id"), + Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)", + Down: "user_id", }, }, }, @@ -322,8 +322,8 @@ func TestSetForeignKey(t *testing.T) { Column: "id", OnDelete: migrations.ForeignKeyReferenceOnDeleteCASCADE, }, - Up: ptr("(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)"), - Down: ptr("user_id"), + Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)", + Down: "user_id", }, }, }, @@ -428,8 +428,8 @@ func TestSetForeignKey(t *testing.T) { Column: "id", OnDelete: migrations.ForeignKeyReferenceOnDeleteSETNULL, }, - Up: ptr("(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)"), - Down: ptr("user_id"), + Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)", + Down: "user_id", }, }, }, @@ -538,8 +538,8 @@ func TestSetForeignKey(t *testing.T) { Column: "id", OnDelete: migrations.ForeignKeyReferenceOnDeleteSETDEFAULT, }, - Up: ptr("(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)"), - Down: ptr("user_id"), + Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)", + Down: "user_id", }, }, }, @@ -649,8 +649,8 @@ func TestSetForeignKey(t *testing.T) { Table: "users", Column: "id", }, - Up: ptr("(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)"), - Down: ptr("user_id"), + Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)", + Down: "user_id", }, }, }, @@ -743,8 +743,8 @@ func TestSetForeignKey(t *testing.T) { Column: "id", OnDelete: migrations.ForeignKeyReferenceOnDeleteCASCADE, }, - Up: ptr("(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)"), - Down: ptr("user_id"), + Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)", + Down: "user_id", }, }, }, @@ -759,8 +759,8 @@ func TestSetForeignKey(t *testing.T) { Table: "users", Column: "id", }, - Up: ptr("(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)"), - Down: ptr("user_id"), + Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)", + Down: "user_id", }, }, }, @@ -831,8 +831,8 @@ func TestSetForeignKey(t *testing.T) { Table: "users", Column: "id", }, - Up: ptr("(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)"), - Down: ptr("user_id"), + Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)", + Down: "user_id", }, }, }, @@ -914,8 +914,8 @@ func TestSetForeignKey(t *testing.T) { Table: "users", Column: "id", }, - Up: ptr("(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)"), - Down: ptr("user_id"), + Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)", + Down: "user_id", }, }, }, @@ -984,8 +984,8 @@ func TestSetForeignKey(t *testing.T) { Table: "posts", Column: "user_id", Unique: &migrations.UniqueConstraint{Name: "unique_user_id"}, - Up: ptr("user_id"), - Down: ptr("user_id"), + Up: "user_id", + Down: "user_id", }, }, }, @@ -1000,8 +1000,8 @@ func TestSetForeignKey(t *testing.T) { Table: "users", Column: "id", }, - Up: ptr("(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)"), - Down: ptr("user_id"), + Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)", + Down: "user_id", }, }, }, @@ -1102,8 +1102,8 @@ func TestSetForeignKey(t *testing.T) { Table: "users", Column: "id", }, - Up: ptr("(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)"), - Down: ptr("user_id"), + Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)", + Down: "user_id", }, }, }, @@ -1178,8 +1178,8 @@ func TestSetForeignKeyValidation(t *testing.T) { Table: "users", Column: "id", }, - Up: ptr("(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)"), - Down: ptr("user_id"), + Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)", + Down: "user_id", }, }, }, @@ -1205,8 +1205,8 @@ func TestSetForeignKeyValidation(t *testing.T) { Table: "doesntexist", Column: "id", }, - Up: ptr("(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)"), - Down: ptr("user_id"), + Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)", + Down: "user_id", }, }, }, @@ -1232,8 +1232,8 @@ func TestSetForeignKeyValidation(t *testing.T) { Table: "users", Column: "doesntexist", }, - Up: ptr("(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)"), - Down: ptr("user_id"), + Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)", + Down: "user_id", }, }, }, @@ -1260,8 +1260,8 @@ func TestSetForeignKeyValidation(t *testing.T) { Column: "id", OnDelete: "invalid", }, - Up: ptr("(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)"), - Down: ptr("user_id"), + Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)", + Down: "user_id", }, }, }, @@ -1287,8 +1287,8 @@ func TestSetForeignKeyValidation(t *testing.T) { Column: "id", OnDelete: "no action", }, - Up: ptr("(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)"), - Down: ptr("user_id"), + Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)", + Down: "user_id", }, }, }, @@ -1311,8 +1311,8 @@ func TestSetForeignKeyValidation(t *testing.T) { Column: "id", OnDelete: "SET NULL", }, - Up: ptr("(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)"), - Down: ptr("user_id"), + Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)", + Down: "user_id", }, }, }, diff --git a/pkg/migrations/op_set_notnull_test.go b/pkg/migrations/op_set_notnull_test.go index 6c0535e..87cc339 100644 --- a/pkg/migrations/op_set_notnull_test.go +++ b/pkg/migrations/op_set_notnull_test.go @@ -55,7 +55,7 @@ func TestSetNotNull(t *testing.T) { Table: "reviews", Column: "review", Nullable: ptr(false), - Up: ptr("(SELECT CASE WHEN review IS NULL THEN product || ' is good' ELSE review END)"), + Up: "(SELECT CASE WHEN review IS NULL THEN product || ' is good' ELSE review END)", }, }, }, @@ -197,8 +197,8 @@ func TestSetNotNull(t *testing.T) { Table: "reviews", Column: "review", Nullable: ptr(false), - Up: ptr("(SELECT CASE WHEN review IS NULL THEN product || ' is good' ELSE review END)"), - Down: ptr("review || ' (from new column)'"), + Up: "(SELECT CASE WHEN review IS NULL THEN product || ' is good' ELSE review END)", + Down: "review || ' (from new column)'", }, }, }, @@ -284,8 +284,8 @@ func TestSetNotNull(t *testing.T) { Table: "employees", Column: "department_id", Nullable: ptr(false), - Up: ptr("(SELECT CASE WHEN department_id IS NULL THEN 1 ELSE department_id END)"), - Down: ptr("department_id"), + Up: "(SELECT CASE WHEN department_id IS NULL THEN 1 ELSE department_id END)", + Down: "department_id", }, }, }, @@ -332,7 +332,7 @@ func TestSetNotNull(t *testing.T) { Table: "users", Column: "name", Nullable: ptr(false), - Up: ptr("(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)"), + Up: "(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)", }, }, }, @@ -399,7 +399,7 @@ func TestSetNotNull(t *testing.T) { Table: "users", Column: "name", Nullable: ptr(false), - Up: ptr("(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)"), + Up: "(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)", }, }, }, @@ -451,8 +451,8 @@ func TestSetNotNull(t *testing.T) { Table: "users", Column: "name", Unique: &migrations.UniqueConstraint{Name: "unique_name"}, - Up: ptr("name"), - Down: ptr("name"), + Up: "name", + Down: "name", }, }, }, @@ -463,7 +463,7 @@ func TestSetNotNull(t *testing.T) { Table: "users", Column: "name", Nullable: ptr(false), - Up: ptr("(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)"), + Up: "(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)", }, }, }, @@ -527,7 +527,7 @@ func TestSetNotNull(t *testing.T) { Table: "users", Column: "name", Nullable: ptr(false), - Up: ptr("(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)"), + Up: "(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)", }, }, }, @@ -592,7 +592,7 @@ func TestSetNotNullValidation(t *testing.T) { Table: "reviews", Column: "review", Nullable: ptr(false), - Down: ptr("review"), + Down: "review", }, }, }, @@ -639,8 +639,8 @@ func TestSetNotNullValidation(t *testing.T) { Table: "reviews", Column: "review", Nullable: ptr(false), - Up: ptr("(SELECT CASE WHEN review IS NULL THEN product || ' is good' ELSE review END)"), - Down: ptr("review"), + Up: "(SELECT CASE WHEN review IS NULL THEN product || ' is good' ELSE review END)", + Down: "review", }, }, }, diff --git a/pkg/migrations/op_set_unique_test.go b/pkg/migrations/op_set_unique_test.go index 9e60b65..2b05a51 100644 --- a/pkg/migrations/op_set_unique_test.go +++ b/pkg/migrations/op_set_unique_test.go @@ -57,7 +57,7 @@ func TestSetColumnUnique(t *testing.T) { Unique: &migrations.UniqueConstraint{ Name: "reviews_review_unique", }, - Up: ptr("review || '-' || (random()*1000000)::integer"), + Up: "review || '-' || (random()*1000000)::integer", }, }, }, @@ -158,8 +158,8 @@ func TestSetColumnUnique(t *testing.T) { Unique: &migrations.UniqueConstraint{ Name: "reviews_review_unique", }, - Up: ptr("review || '-' || (random()*1000000)::integer"), - Down: ptr("review || '!'"), + Up: "review || '-' || (random()*1000000)::integer", + Down: "review || '!'", }, }, }, @@ -220,8 +220,8 @@ func TestSetColumnUnique(t *testing.T) { Unique: &migrations.UniqueConstraint{ Name: "reviews_username_unique", }, - Up: ptr("username"), - Down: ptr("username"), + Up: "username", + Down: "username", }, }, }, @@ -323,8 +323,8 @@ func TestSetColumnUnique(t *testing.T) { Unique: &migrations.UniqueConstraint{ Name: "employees_department_id_unique", }, - Up: ptr("department_id"), - Down: ptr("department_id"), + Up: "department_id", + Down: "department_id", }, }, }, @@ -379,8 +379,8 @@ func TestSetColumnUnique(t *testing.T) { Unique: &migrations.UniqueConstraint{ Name: "reviews_username_unique", }, - Up: ptr("username"), - Down: ptr("username"), + Up: "username", + Down: "username", }, }, }, @@ -442,8 +442,8 @@ func TestSetColumnUnique(t *testing.T) { Unique: &migrations.UniqueConstraint{ Name: "reviews_username_unique", }, - Up: ptr("username"), - Down: ptr("username"), + Up: "username", + Down: "username", }, }, }, @@ -504,8 +504,8 @@ func TestSetColumnUnique(t *testing.T) { Unique: &migrations.UniqueConstraint{ Name: "reviews_username_unique", }, - Up: ptr("username"), - Down: ptr("username"), + Up: "username", + Down: "username", }, }, }, diff --git a/pkg/migrations/types.go b/pkg/migrations/types.go index 77d30bb..e6df431 100644 --- a/pkg/migrations/types.go +++ b/pkg/migrations/types.go @@ -74,7 +74,7 @@ type OpAddColumn struct { Table string `json:"table"` // SQL expression for up migration - Up *string `json:"up,omitempty"` + Up string `json:"up,omitempty"` } // Alter column operation @@ -86,7 +86,7 @@ type OpAlterColumn struct { Column string `json:"column"` // SQL expression for down migration - Down *string `json:"down,omitempty"` + Down string `json:"down,omitempty"` // New name of the column (for rename column operation) Name *string `json:"name,omitempty"` @@ -108,7 +108,7 @@ type OpAlterColumn struct { Unique *UniqueConstraint `json:"unique,omitempty"` // SQL expression for up migration - Up *string `json:"up,omitempty"` + Up string `json:"up,omitempty"` } // Create index operation @@ -141,7 +141,7 @@ type OpDropColumn struct { Column string `json:"column"` // SQL expression for down migration - Down *string `json:"down,omitempty"` + Down string `json:"down,omitempty"` // Name of the table Table string `json:"table"` diff --git a/pkg/roll/execute_test.go b/pkg/roll/execute_test.go index a4df634..bff134f 100644 --- a/pkg/roll/execute_test.go +++ b/pkg/roll/execute_test.go @@ -242,8 +242,8 @@ func TestRollbackOnMigrationStartFailure(t *testing.T) { Table: "table1", Column: "name", Type: ptr("text"), - Up: ptr("invalid"), - Down: ptr("invalid"), + Up: "invalid", + Down: "invalid", }, }, }) diff --git a/schema.json b/schema.json index ce63764..6c0f555 100644 --- a/schema.json +++ b/schema.json @@ -104,6 +104,7 @@ "type": "string" }, "up": { + "default": "", "description": "SQL expression for up migration", "type": "string" } @@ -124,6 +125,7 @@ "type": "string" }, "down": { + "default": "", "description": "SQL expression for down migration", "type": "string" }, @@ -152,6 +154,7 @@ "description": "Add unique constraint to the column" }, "up": { + "default": "", "description": "SQL expression for up migration", "type": "string" } @@ -243,6 +246,7 @@ "type": "string" }, "down": { + "default": "", "description": "SQL expression for down migration", "type": "string" }, @@ -263,6 +267,7 @@ "type": "string" }, "down": { + "default": "", "description": "SQL expression for down migration", "type": "string" },