-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a 'set comment' sub-operation to 'alter column' (#344)
Allow 'alter column' operations to set the comment on a column: ```json { "name": "02_change_comment", "operations": [ { "alter_column": { "table": "events", "column": "name", "comment": "The full name of the event" } } ] } ``` This is a versioned migration so the column to which the comment is applied is duplicated and backfilled according to the `up` and `down` SQL supplied with the 'alter column' operation. `up` and `down` default to a simple copy of the field between new and old columns. The intention is that this can be combined with a 'change type' sub-operation if there is some column metadata that should be updated as part of the type change: ```json { "name": "35_alter_column_multiple", "operations": [ { "alter_column": { "table": "events", "column": "name", "name": "event_name", "type": "text", "comment": "{type: some-metadata}", "up": "name", "down": "name" } } ] } ``` Fixes #328
- Loading branch information
1 parent
afad3d8
commit 4fbfdf7
Showing
9 changed files
with
271 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package migrations | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
|
||
"github.com/xataio/pgroll/pkg/schema" | ||
) | ||
|
||
type OpSetComment struct { | ||
Table string `json:"table"` | ||
Column string `json:"column"` | ||
Comment string `json:"comment"` | ||
Up string `json:"up"` | ||
Down string `json:"down"` | ||
} | ||
|
||
var _ Operation = (*OpSetComment)(nil) | ||
|
||
func (o *OpSetComment) Start(ctx context.Context, conn *sql.DB, stateSchema string, tr SQLTransformer, s *schema.Schema, cbs ...CallbackFn) (*schema.Table, error) { | ||
tbl := s.GetTable(o.Table) | ||
|
||
return tbl, addCommentToColumn(ctx, conn, o.Table, TemporaryName(o.Column), o.Comment) | ||
} | ||
|
||
func (o *OpSetComment) Complete(ctx context.Context, conn *sql.DB, tr SQLTransformer, s *schema.Schema) error { | ||
return nil | ||
} | ||
|
||
func (o *OpSetComment) Rollback(ctx context.Context, conn *sql.DB, tr SQLTransformer) error { | ||
return nil | ||
} | ||
|
||
func (o *OpSetComment) Validate(ctx context.Context, s *schema.Schema) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package migrations_test | ||
|
||
import ( | ||
"database/sql" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/xataio/pgroll/pkg/migrations" | ||
) | ||
|
||
func TestSetComment(t *testing.T) { | ||
t.Parallel() | ||
|
||
ExecuteTests(t, TestCases{ | ||
{ | ||
name: "set column comment with default up and down SQL", | ||
migrations: []migrations.Migration{ | ||
{ | ||
Name: "01_add_table", | ||
Operations: migrations.Operations{ | ||
&migrations.OpCreateTable{ | ||
Name: "users", | ||
Columns: []migrations.Column{ | ||
{ | ||
Name: "id", | ||
Type: "serial", | ||
Pk: ptr(true), | ||
}, | ||
{ | ||
Name: "name", | ||
Type: "text", | ||
Comment: ptr("apples"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "02_set_comment", | ||
Operations: migrations.Operations{ | ||
&migrations.OpAlterColumn{ | ||
Table: "users", | ||
Column: "name", | ||
Comment: ptr("name of the user"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
afterStart: func(t *testing.T, db *sql.DB, schema string) { | ||
// Inserting a row into the new schema succeeds | ||
MustInsert(t, db, schema, "02_set_comment", "users", map[string]string{ | ||
"name": "alice", | ||
}) | ||
|
||
// Inserting a row into the old schema succeeds | ||
MustInsert(t, db, schema, "01_add_table", "users", map[string]string{ | ||
"name": "bob", | ||
}) | ||
|
||
// The old column should have the old comment. | ||
ColumnMustHaveComment(t, db, schema, "users", "name", "apples") | ||
|
||
// The new column should have the new comment. | ||
ColumnMustHaveComment(t, db, schema, "users", migrations.TemporaryName("name"), "name of the user") | ||
|
||
// The old schema view has the expected rows | ||
rows := MustSelect(t, db, schema, "01_add_table", "users") | ||
assert.Equal(t, []map[string]any{ | ||
{"id": 1, "name": "alice"}, | ||
{"id": 2, "name": "bob"}, | ||
}, rows) | ||
|
||
// The new schema view has the expected rows | ||
rows = MustSelect(t, db, schema, "02_set_comment", "users") | ||
assert.Equal(t, []map[string]any{ | ||
{"id": 1, "name": "alice"}, | ||
{"id": 2, "name": "bob"}, | ||
}, rows) | ||
}, | ||
afterRollback: func(t *testing.T, db *sql.DB, schema string) { | ||
// The column should have the old comment. | ||
ColumnMustHaveComment(t, db, schema, "users", "name", "apples") | ||
}, | ||
afterComplete: func(t *testing.T, db *sql.DB, schema string) { | ||
// The new column should have the new comment. | ||
ColumnMustHaveComment(t, db, schema, "users", "name", "name of the user") | ||
|
||
// The new schema view has the expected rows | ||
rows := MustSelect(t, db, schema, "02_set_comment", "users") | ||
assert.Equal(t, []map[string]any{ | ||
{"id": 1, "name": "alice"}, | ||
{"id": 2, "name": "bob"}, | ||
}, rows) | ||
}, | ||
}, | ||
{ | ||
name: "set column comment with user-supplied up and down SQL", | ||
migrations: []migrations.Migration{ | ||
{ | ||
Name: "01_add_table", | ||
Operations: migrations.Operations{ | ||
&migrations.OpCreateTable{ | ||
Name: "users", | ||
Columns: []migrations.Column{ | ||
{ | ||
Name: "id", | ||
Type: "serial", | ||
Pk: ptr(true), | ||
}, | ||
{ | ||
Name: "name", | ||
Type: "text", | ||
Comment: ptr("apples"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "02_set_comment", | ||
Operations: migrations.Operations{ | ||
&migrations.OpAlterColumn{ | ||
Table: "users", | ||
Column: "name", | ||
Comment: ptr("name of the user"), | ||
Up: "'rewritten by up SQL'", | ||
Down: "'rewritten by down SQL'", | ||
}, | ||
}, | ||
}, | ||
}, | ||
afterStart: func(t *testing.T, db *sql.DB, schema string) { | ||
// Inserting a row into the new schema succeeds | ||
MustInsert(t, db, schema, "02_set_comment", "users", map[string]string{ | ||
"name": "alice", | ||
}) | ||
|
||
// Inserting a row into the old schema succeeds | ||
MustInsert(t, db, schema, "01_add_table", "users", map[string]string{ | ||
"name": "bob", | ||
}) | ||
|
||
// The old column should have the old comment. | ||
ColumnMustHaveComment(t, db, schema, "users", "name", "apples") | ||
|
||
// The new column should have the new comment. | ||
ColumnMustHaveComment(t, db, schema, "users", migrations.TemporaryName("name"), "name of the user") | ||
|
||
// The old schema view has the expected rows | ||
rows := MustSelect(t, db, schema, "01_add_table", "users") | ||
assert.Equal(t, []map[string]any{ | ||
{"id": 1, "name": "rewritten by down SQL"}, | ||
{"id": 2, "name": "bob"}, | ||
}, rows) | ||
|
||
// The new schema view has the expected rows | ||
rows = MustSelect(t, db, schema, "02_set_comment", "users") | ||
assert.Equal(t, []map[string]any{ | ||
{"id": 1, "name": "alice"}, | ||
{"id": 2, "name": "rewritten by up SQL"}, | ||
}, rows) | ||
}, | ||
afterRollback: func(t *testing.T, db *sql.DB, schema string) { | ||
// The column should have the old comment. | ||
ColumnMustHaveComment(t, db, schema, "users", "name", "apples") | ||
}, | ||
afterComplete: func(t *testing.T, db *sql.DB, schema string) { | ||
// The new column should have the new comment. | ||
ColumnMustHaveComment(t, db, schema, "users", "name", "name of the user") | ||
|
||
// The new schema view has the expected rows | ||
rows := MustSelect(t, db, schema, "02_set_comment", "users") | ||
assert.Equal(t, []map[string]any{ | ||
{"id": 1, "name": "rewritten by up SQL"}, | ||
{"id": 2, "name": "rewritten by up SQL"}, | ||
}, rows) | ||
}, | ||
}, | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters