Update schema.json to correctly describe the 'alter column' operation (#261)

Update `schema.json` to correctly describe the 'alter column' operation.

The [alter column
](https://github.com/xataio/pgroll/tree/main/docs#alter-column)
operation is used to perform different alter column operations according
to which fields are present.

The rules are:
* `table` and `column` are always required.
* Exactly one of `name`, `type`, `check`, `unique`, `references`,
`nullable` must be present.
* If `type`, `check`, `unique`, `references` or `nullable` is present,
then `up` and `down` must also be present.
* If `name` is present then neither `up` or `down` is allowed.

This PR updates `schema.json` to capture these dependencies.

This change causes churn in the test code as some fields that previously
`string` have become `*string`.

Fixes #222
This commit is contained in:
Andrew Farries 2024-02-02 09:10:22 +00:00 committed by GitHub
parent bb79eecc3b
commit 5a6a83a65c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 177 additions and 141 deletions

View File

@ -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 != "" {
if o.Up != nil {
return NoUpSQLAllowedError{}
}
if o.Down != "" {
if o.Down != nil {
return NoDownSQLAllowedError{}
}
}
@ -67,20 +67,20 @@ func (o *OpAlterColumn) Validate(ctx context.Context, s *schema.Schema) error {
func (o *OpAlterColumn) innerOperation() Operation {
switch {
case o.Name != "":
case o.Name != nil:
return &OpRenameColumn{
Table: o.Table,
From: o.Column,
To: o.Name,
To: *o.Name,
}
case o.Type != "":
case o.Type != nil:
return &OpChangeType{
Table: o.Table,
Column: o.Column,
Type: o.Type,
Up: o.Up,
Down: o.Down,
Type: *o.Type,
Up: ptrToStr(o.Up),
Down: ptrToStr(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: o.Up,
Down: o.Down,
Up: ptrToStr(o.Up),
Down: ptrToStr(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: o.Up,
Down: o.Down,
Up: ptrToStr(o.Up),
Down: ptrToStr(o.Down),
}
case o.Nullable != nil && !*o.Nullable:
return &OpSetNotNull{
Table: o.Table,
Column: o.Column,
Up: o.Up,
Down: o.Down,
Up: ptrToStr(o.Up),
Down: ptrToStr(o.Down),
}
case o.Nullable != nil && *o.Nullable:
return &OpDropNotNull{
Table: o.Table,
Column: o.Column,
Up: o.Up,
Down: o.Down,
Up: ptrToStr(o.Up),
Down: ptrToStr(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: o.Up,
Down: o.Down,
Up: ptrToStr(o.Up),
Down: ptrToStr(o.Down),
}
}
return nil
@ -134,10 +134,10 @@ func (o *OpAlterColumn) innerOperation() Operation {
func (o *OpAlterColumn) numChanges() int {
fieldsSet := 0
if o.Name != "" {
if o.Name != nil {
fieldsSet++
}
if o.Type != "" {
if o.Type != nil {
fieldsSet++
}
if o.Check != nil {
@ -155,3 +155,10 @@ func (o *OpAlterColumn) numChanges() int {
return fieldsSet
}
func ptrToStr(s *string) string {
if s == nil {
return ""
}
return *s
}

View File

@ -60,7 +60,7 @@ func TestAlterColumnValidation(t *testing.T) {
&migrations.OpAlterColumn{
Table: "doesntexist",
Column: "title",
Name: "renamed_title",
Name: ptr("renamed_title"),
},
},
},
@ -77,7 +77,7 @@ func TestAlterColumnValidation(t *testing.T) {
&migrations.OpAlterColumn{
Table: "posts",
Column: "doesntexist",
Name: "renamed_title",
Name: ptr("renamed_title"),
},
},
},
@ -94,8 +94,8 @@ func TestAlterColumnValidation(t *testing.T) {
&migrations.OpAlterColumn{
Table: "posts",
Column: "title",
Name: "renamed_title",
Up: "some up sql",
Name: ptr("renamed_title"),
Up: ptr("some up sql"),
},
},
},
@ -112,8 +112,8 @@ func TestAlterColumnValidation(t *testing.T) {
&migrations.OpAlterColumn{
Table: "posts",
Column: "title",
Name: "renamed_title",
Down: "some down sql",
Name: ptr("renamed_title"),
Down: ptr("some down sql"),
},
},
},
@ -146,8 +146,8 @@ func TestAlterColumnValidation(t *testing.T) {
&migrations.OpAlterColumn{
Table: "posts",
Column: "title",
Name: "renamed_title",
Type: "varchar(255)",
Name: ptr("renamed_title"),
Type: ptr("varchar(255)"),
},
},
},
@ -172,7 +172,7 @@ func TestAlterColumnValidation(t *testing.T) {
&migrations.OpAlterColumn{
Table: "orders",
Column: "quantity",
Name: "renamed_quantity",
Name: ptr("renamed_quantity"),
},
},
},

View File

@ -53,9 +53,9 @@ func TestChangeColumnType(t *testing.T) {
&migrations.OpAlterColumn{
Table: "reviews",
Column: "rating",
Type: "integer",
Up: "CAST (rating AS integer)",
Down: "CAST (rating AS text)",
Type: ptr("integer"),
Up: ptr("CAST (rating AS integer)"),
Down: ptr("CAST (rating AS text)"),
},
},
},
@ -205,9 +205,9 @@ func TestChangeColumnType(t *testing.T) {
&migrations.OpAlterColumn{
Table: "employees",
Column: "department_id",
Type: "bigint",
Up: "department_id",
Down: "department_id",
Type: ptr("bigint"),
Up: ptr("department_id"),
Down: ptr("department_id"),
},
},
},
@ -253,9 +253,9 @@ func TestChangeColumnType(t *testing.T) {
&migrations.OpAlterColumn{
Table: "users",
Column: "username",
Type: "varchar(255)",
Up: "username",
Down: "username",
Type: ptr("varchar(255)"),
Up: ptr("username"),
Down: ptr("username"),
},
},
},
@ -321,9 +321,9 @@ func TestChangeColumnType(t *testing.T) {
&migrations.OpAlterColumn{
Table: "users",
Column: "username",
Type: "varchar(255)",
Up: "username",
Down: "username",
Type: ptr("varchar(255)"),
Up: ptr("username"),
Down: ptr("username"),
},
},
},
@ -374,9 +374,9 @@ func TestChangeColumnType(t *testing.T) {
&migrations.OpAlterColumn{
Table: "users",
Column: "username",
Type: "varchar(255)",
Up: "username",
Down: "username",
Type: ptr("varchar(255)"),
Up: ptr("username"),
Down: ptr("username"),
},
},
},
@ -425,9 +425,9 @@ func TestChangeColumnType(t *testing.T) {
&migrations.OpAlterColumn{
Table: "users",
Column: "username",
Type: "varchar(255)",
Up: "username",
Down: "username",
Type: ptr("varchar(255)"),
Up: ptr("username"),
Down: ptr("username"),
},
},
},
@ -502,8 +502,8 @@ func TestChangeColumnTypeValidation(t *testing.T) {
&migrations.OpAlterColumn{
Table: "reviews",
Column: "rating",
Type: "integer",
Down: "CAST (rating AS text)",
Type: ptr("integer"),
Down: ptr("CAST (rating AS text)"),
},
},
},
@ -520,8 +520,8 @@ func TestChangeColumnTypeValidation(t *testing.T) {
&migrations.OpAlterColumn{
Table: "reviews",
Column: "rating",
Type: "integer",
Up: "CAST (rating AS integer)",
Type: ptr("integer"),
Up: ptr("CAST (rating AS integer)"),
},
},
},

View File

@ -47,8 +47,8 @@ func TestDropConstraint(t *testing.T) {
Name: "check_title_length",
Constraint: "length(title) > 3",
},
Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)",
Down: "title",
Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"),
Down: ptr("title"),
},
},
},
@ -176,8 +176,8 @@ func TestDropConstraint(t *testing.T) {
Name: "check_title_length",
Constraint: "length(title) > 3",
},
Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)",
Down: "title",
Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"),
Down: ptr("title"),
},
},
},
@ -262,8 +262,8 @@ func TestDropConstraint(t *testing.T) {
Table: "users",
Column: "id",
},
Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)",
Down: "user_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"),
},
},
},
@ -637,8 +637,8 @@ func TestDropConstraint(t *testing.T) {
Name: "check_title_length",
Constraint: "length(title) > 3",
},
Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)",
Down: "title",
Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"),
Down: ptr("title"),
},
},
},
@ -706,8 +706,8 @@ func TestDropConstraint(t *testing.T) {
Name: "check_title_length",
Constraint: "length(title) > 3",
},
Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)",
Down: "title",
Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"),
Down: ptr("title"),
},
},
},
@ -836,8 +836,8 @@ func TestDropConstraintValidation(t *testing.T) {
Name: "check_title_length",
Constraint: "length(title) > 3",
},
Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)",
Down: "title",
Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"),
Down: ptr("title"),
},
},
}

View File

@ -55,7 +55,7 @@ func TestDropNotNull(t *testing.T) {
Table: "reviews",
Column: "review",
Nullable: ptr(true),
Down: "(SELECT CASE WHEN review IS NULL THEN product || ' is good' ELSE review END)",
Down: ptr("(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: "(SELECT CASE WHEN review IS NULL THEN product || ' is good' ELSE review END)",
Up: "review || ' (from the old column)'",
Down: ptr("(SELECT CASE WHEN review IS NULL THEN product || ' is good' ELSE review END)"),
Up: ptr("review || ' (from the old column)'"),
},
},
},
@ -277,8 +277,8 @@ func TestDropNotNull(t *testing.T) {
Table: "employees",
Column: "department_id",
Nullable: ptr(true),
Down: "(SELECT CASE WHEN department_id IS NULL THEN 1 ELSE department_id END)",
Up: "department_id",
Down: ptr("(SELECT CASE WHEN department_id IS NULL THEN 1 ELSE department_id END)"),
Up: ptr("department_id"),
},
},
},
@ -325,8 +325,8 @@ func TestDropNotNull(t *testing.T) {
Table: "users",
Column: "name",
Nullable: ptr(true),
Up: "name",
Down: "(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)",
Up: ptr("name"),
Down: ptr("(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: "name",
Down: "(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)",
Up: ptr("name"),
Down: ptr("(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)"),
},
},
},
@ -447,8 +447,8 @@ func TestDropNotNull(t *testing.T) {
Table: "users",
Column: "name",
Nullable: ptr(true),
Up: "name",
Down: "(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)",
Up: ptr("name"),
Down: ptr("(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)"),
},
},
},
@ -515,7 +515,7 @@ func TestDropNotNullValidation(t *testing.T) {
Table: "users",
Column: "name",
Nullable: ptr(true),
Up: "name",
Up: ptr("name"),
},
},
},
@ -552,8 +552,8 @@ func TestDropNotNullValidation(t *testing.T) {
Table: "users",
Column: "name",
Nullable: ptr(true),
Up: "name",
Down: "(SELECT CASE WHEN name IS NULL THEN 'placeholder' ELSE name END)",
Up: ptr("name"),
Down: ptr("(SELECT CASE WHEN name IS NULL THEN 'placeholder' ELSE name END)"),
},
},
},

View File

@ -42,7 +42,7 @@ func TestRenameColumn(t *testing.T) {
&migrations.OpAlterColumn{
Table: "users",
Column: "username",
Name: "name",
Name: ptr("name"),
},
},
},

View File

@ -47,8 +47,8 @@ func TestSetCheckConstraint(t *testing.T) {
Name: "check_title_length",
Constraint: "length(title) > 3",
},
Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)",
Down: "title",
Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"),
Down: ptr("title"),
},
},
},
@ -180,8 +180,8 @@ func TestSetCheckConstraint(t *testing.T) {
Name: "check_title_length",
Constraint: "length(title) > 3",
},
Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)",
Down: "title",
Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"),
Down: ptr("title"),
},
},
},
@ -277,8 +277,8 @@ func TestSetCheckConstraint(t *testing.T) {
Name: "check_valid_department_id",
Constraint: "department_id > 1",
},
Up: "(SELECT CASE WHEN department_id <= 1 THEN 2 ELSE department_id END)",
Down: "department_id",
Up: ptr("(SELECT CASE WHEN department_id <= 1 THEN 2 ELSE department_id END)"),
Down: ptr("department_id"),
},
},
},
@ -334,8 +334,8 @@ func TestSetCheckConstraint(t *testing.T) {
Name: "check_body_length",
Constraint: "length(body) > 3",
},
Up: "(SELECT CASE WHEN length(body) <= 3 THEN LPAD(body, 4, '-') ELSE body END)",
Down: "body",
Up: ptr("(SELECT CASE WHEN length(body) <= 3 THEN LPAD(body, 4, '-') ELSE body END)"),
Down: ptr("body"),
},
},
},
@ -392,8 +392,8 @@ func TestSetCheckConstraint(t *testing.T) {
Name: "check_title_length",
Constraint: "length(title) > 3",
},
Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)",
Down: "title",
Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"),
Down: ptr("title"),
},
},
},
@ -446,8 +446,8 @@ func TestSetCheckConstraint(t *testing.T) {
Name: "check_title_length",
Constraint: "length(title) > 3",
},
Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)",
Down: "title",
Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"),
Down: ptr("title"),
},
},
},
@ -517,8 +517,8 @@ func TestSetCheckConstraintValidation(t *testing.T) {
Check: &migrations.CheckConstraint{
Constraint: "length(title) > 3",
},
Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)",
Down: "title",
Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"),
Down: ptr("title"),
},
},
},
@ -539,7 +539,7 @@ func TestSetCheckConstraintValidation(t *testing.T) {
Name: "check_title_length",
Constraint: "length(title) > 3",
},
Down: "title",
Down: ptr("title"),
},
},
},
@ -560,7 +560,7 @@ func TestSetCheckConstraintValidation(t *testing.T) {
Name: "check_title_length",
Constraint: "length(title) > 3",
},
Up: "(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)",
Up: ptr("(SELECT CASE WHEN length(title) <= 3 THEN LPAD(title, 4, '-') ELSE title END)"),
},
},
},

View File

@ -67,8 +67,8 @@ func TestSetForeignKey(t *testing.T) {
Table: "users",
Column: "id",
},
Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)",
Down: "user_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"),
},
},
},
@ -232,8 +232,8 @@ func TestSetForeignKey(t *testing.T) {
Table: "users",
Column: "id",
},
Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)",
Down: "user_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"),
},
},
},
@ -325,8 +325,8 @@ func TestSetForeignKey(t *testing.T) {
Table: "users",
Column: "id",
},
Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)",
Down: "user_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"),
},
},
},
@ -341,8 +341,8 @@ func TestSetForeignKey(t *testing.T) {
Table: "users",
Column: "id",
},
Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)",
Down: "user_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"),
},
},
},
@ -413,8 +413,8 @@ func TestSetForeignKey(t *testing.T) {
Table: "users",
Column: "id",
},
Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)",
Down: "user_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"),
},
},
},
@ -496,8 +496,8 @@ func TestSetForeignKey(t *testing.T) {
Table: "users",
Column: "id",
},
Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)",
Down: "user_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"),
},
},
},
@ -571,8 +571,8 @@ func TestSetForeignKey(t *testing.T) {
Table: "users",
Column: "id",
},
Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)",
Down: "user_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"),
},
},
},
@ -677,8 +677,8 @@ func TestSetForeignKeyValidation(t *testing.T) {
Table: "users",
Column: "id",
},
Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)",
Down: "user_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"),
},
},
},
@ -704,8 +704,8 @@ func TestSetForeignKeyValidation(t *testing.T) {
Table: "doesntexist",
Column: "id",
},
Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)",
Down: "user_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"),
},
},
},
@ -731,8 +731,8 @@ func TestSetForeignKeyValidation(t *testing.T) {
Table: "users",
Column: "doesntexist",
},
Up: "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)",
Down: "user_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"),
},
},
},

View File

@ -55,7 +55,7 @@ func TestSetNotNull(t *testing.T) {
Table: "reviews",
Column: "review",
Nullable: ptr(false),
Up: "(SELECT CASE WHEN review IS NULL THEN product || ' is good' ELSE review END)",
Up: ptr("(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: "(SELECT CASE WHEN review IS NULL THEN product || ' is good' ELSE review END)",
Down: "review || ' (from new column)'",
Up: ptr("(SELECT CASE WHEN review IS NULL THEN product || ' is good' ELSE review END)"),
Down: ptr("review || ' (from new column)'"),
},
},
},
@ -283,8 +283,8 @@ func TestSetNotNull(t *testing.T) {
Table: "employees",
Column: "department_id",
Nullable: ptr(false),
Up: "(SELECT CASE WHEN department_id IS NULL THEN 1 ELSE department_id END)",
Down: "department_id",
Up: ptr("(SELECT CASE WHEN department_id IS NULL THEN 1 ELSE department_id END)"),
Down: ptr("department_id"),
},
},
},
@ -331,7 +331,7 @@ func TestSetNotNull(t *testing.T) {
Table: "users",
Column: "name",
Nullable: ptr(false),
Up: "(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)",
Up: ptr("(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)"),
},
},
},
@ -398,7 +398,7 @@ func TestSetNotNull(t *testing.T) {
Table: "users",
Column: "name",
Nullable: ptr(false),
Up: "(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)",
Up: ptr("(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)"),
},
},
},
@ -451,7 +451,7 @@ func TestSetNotNull(t *testing.T) {
Table: "users",
Column: "name",
Nullable: ptr(false),
Up: "(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)",
Up: ptr("(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)"),
},
},
},
@ -530,7 +530,7 @@ func TestSetNotNullValidation(t *testing.T) {
Table: "reviews",
Column: "review",
Nullable: ptr(false),
Down: "review",
Down: ptr("review"),
},
},
},
@ -577,8 +577,8 @@ func TestSetNotNullValidation(t *testing.T) {
Table: "reviews",
Column: "review",
Nullable: ptr(false),
Up: "(SELECT CASE WHEN review IS NULL THEN product || ' is good' ELSE review END)",
Down: "review",
Up: ptr("(SELECT CASE WHEN review IS NULL THEN product || ' is good' ELSE review END)"),
Down: ptr("review"),
},
},
},

View File

@ -57,7 +57,7 @@ func TestSetColumnUnique(t *testing.T) {
Unique: &migrations.UniqueConstraint{
Name: "reviews_review_unique",
},
Up: "review || '-' || (random()*1000000)::integer",
Up: ptr("review || '-' || (random()*1000000)::integer"),
},
},
},
@ -158,8 +158,8 @@ func TestSetColumnUnique(t *testing.T) {
Unique: &migrations.UniqueConstraint{
Name: "reviews_review_unique",
},
Up: "review || '-' || (random()*1000000)::integer",
Down: "review || '!'",
Up: ptr("review || '-' || (random()*1000000)::integer"),
Down: ptr("review || '!'"),
},
},
},
@ -220,8 +220,8 @@ func TestSetColumnUnique(t *testing.T) {
Unique: &migrations.UniqueConstraint{
Name: "reviews_username_unique",
},
Up: "username",
Down: "username",
Up: ptr("username"),
Down: ptr("username"),
},
},
},
@ -322,8 +322,8 @@ func TestSetColumnUnique(t *testing.T) {
Unique: &migrations.UniqueConstraint{
Name: "employees_department_id_unique",
},
Up: "department_id",
Down: "department_id",
Up: ptr("department_id"),
Down: ptr("department_id"),
},
},
},
@ -378,8 +378,8 @@ func TestSetColumnUnique(t *testing.T) {
Unique: &migrations.UniqueConstraint{
Name: "reviews_username_unique",
},
Up: "username",
Down: "username",
Up: ptr("username"),
Down: ptr("username"),
},
},
},
@ -441,8 +441,8 @@ func TestSetColumnUnique(t *testing.T) {
Unique: &migrations.UniqueConstraint{
Name: "reviews_username_unique",
},
Up: "username",
Down: "username",
Up: ptr("username"),
Down: ptr("username"),
},
},
},

View File

@ -75,10 +75,10 @@ type OpAlterColumn struct {
Column string `json:"column"`
// SQL expression for down migration
Down string `json:"down"`
Down *string `json:"down,omitempty"`
// New name of the column (for rename column operation)
Name string `json:"name"`
Name *string `json:"name,omitempty"`
// Indicates if the column is nullable (for add/remove not null constraint
// operation)
@ -91,13 +91,13 @@ type OpAlterColumn struct {
Table string `json:"table"`
// New type of the column (for change type operation)
Type string `json:"type"`
Type *string `json:"type,omitempty"`
// Add unique constraint to the column
Unique *UniqueConstraint `json:"unique,omitempty"`
// SQL expression for up migration
Up string `json:"up"`
Up *string `json:"up,omitempty"`
}
// Create index operation

View File

@ -150,7 +150,36 @@
"type": "string"
}
},
"required": ["column", "down", "name", "table", "type", "up"],
"required": ["table", "column"],
"oneOf": [
{
"required": ["up", "down"],
"oneOf": [
{"required": ["check"]},
{"required": ["type"]},
{"required": ["nullable"]},
{"required": ["unique"]},
{"required": ["references"]}
],
"not": {
"required": ["name"]
}
},
{
"required": ["name"],
"not": {
"anyOf": [
{ "required": [ "up" ] },
{ "required": [ "down" ] },
{ "required": [ "check" ] },
{ "required": [ "type" ] },
{ "required": [ "nullable" ] },
{ "required": [ "unique" ] },
{ "required": [ "references" ] }
]
}
}
],
"type": "object"
},
"OpCreateIndex": {