-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for the rename column operation
- Loading branch information
1 parent
819df1d
commit 4d3a125
Showing
1 changed file
with
81 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package migrations_test | ||
|
||
import ( | ||
"database/sql" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"pg-roll/pkg/migrations" | ||
) | ||
|
||
func TestRenameColumn(t *testing.T) { | ||
t.Parallel() | ||
|
||
ExecuteTests(t, TestCases{{ | ||
name: "rename column", | ||
migrations: []migrations.Migration{ | ||
{ | ||
Name: "01_add_table", | ||
Operations: migrations.Operations{ | ||
&migrations.OpCreateTable{ | ||
Name: "users", | ||
Columns: []migrations.Column{ | ||
{ | ||
Name: "id", | ||
Type: "serial", | ||
PrimaryKey: true, | ||
}, | ||
{ | ||
Name: "username", | ||
Type: "varchar(255)", | ||
Nullable: false, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "02_rename_column", | ||
Operations: migrations.Operations{ | ||
&migrations.OpRenameColumn{ | ||
Table: "users", | ||
From: "username", | ||
To: "name", | ||
}, | ||
}, | ||
}, | ||
}, | ||
afterStart: func(t *testing.T, db *sql.DB) { | ||
// The column in the underlying table has not been renamed. | ||
ColumnMustExist(t, db, "public", "users", "username") | ||
|
||
// Insertions to the new column name in the new version schema should work. | ||
MustInsert(t, db, "public", "02_rename_column", "users", map[string]string{"name": "alice"}) | ||
|
||
// Insertions to the old column name in the old version schema should work. | ||
MustInsert(t, db, "public", "01_add_table", "users", map[string]string{"username": "bob"}) | ||
|
||
// Data can be read from the view in the new version schema. | ||
rows := MustSelect(t, db, "public", "02_rename_column", "users") | ||
assert.Equal(t, []map[string]any{ | ||
{"id": 1, "name": "alice"}, | ||
{"id": 2, "name": "bob"}, | ||
}, rows) | ||
}, | ||
afterRollback: func(t *testing.T, db *sql.DB) { | ||
// no-op | ||
}, | ||
afterComplete: func(t *testing.T, db *sql.DB) { | ||
// The column in the underlying table has been renamed. | ||
ColumnMustExist(t, db, "public", "users", "name") | ||
|
||
// Data can be read from the view in the new version schema. | ||
rows := MustSelect(t, db, "public", "02_rename_column", "users") | ||
assert.Equal(t, []map[string]any{ | ||
{"id": 1, "name": "alice"}, | ||
{"id": 2, "name": "bob"}, | ||
}, rows) | ||
}, | ||
}}) | ||
} |