Skip to content

Commit

Permalink
Add tests for the rename column operation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-farries committed Aug 18, 2023
1 parent 819df1d commit 4d3a125
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions pkg/migrations/op_rename_column_test.go
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)
},
}})
}

0 comments on commit 4d3a125

Please sign in to comment.