Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions pkg/migrations/op_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ func ColumnMustNotHaveComment(t *testing.T, db *sql.DB, schema, table, column st
}
}

func ColumnMustHaveDefault(t *testing.T, db *sql.DB, schema, table, column, expectedDefault string) {
t.Helper()
if !columnHasDefault(t, db, schema, table, column, &expectedDefault) {
t.Fatalf("Expected column %q to have default value %q", column, expectedDefault)
}
}

func ColumnMustBePK(t *testing.T, db *sql.DB, schema, table, column string) {
t.Helper()
if !columnMustBePK(t, db, schema, table, column) {
Expand Down Expand Up @@ -710,6 +717,27 @@ func columnHasComment(t *testing.T, db *sql.DB, schema, table, column string, ex
return actualComment != nil && *expectedComment == *actualComment
}

func columnHasDefault(t *testing.T, db *sql.DB, schema, table, column string, expectedDefault *string) bool {
t.Helper()

var actualDefault *string
err := db.QueryRow(`
SELECT column_default
FROM information_schema.columns
WHERE table_schema = $1
AND table_name = $2
AND column_name = $3
`,
schema, table, column).Scan(&actualDefault)
if err != nil {
t.Fatal(err)
}
if expectedDefault == nil {
return actualDefault == nil
}
return actualDefault != nil && *expectedDefault == *actualDefault
}

func columnMustBePK(t *testing.T, db *sql.DB, schema, table, column string) bool {
t.Helper()

Expand Down
99 changes: 99 additions & 0 deletions pkg/migrations/op_create_constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,105 @@ func TestCreateConstraintInMultiOperationMigrations(t *testing.T) {
TableMustBeCleanedUp(t, db, schema, "products", "name")
},
},
{
name: "create table with comment and default on column, add unique constraint",
migrations: []migrations.Migration{
{
Name: "01_create_table",
Operations: migrations.Operations{
&migrations.OpCreateTable{
Name: "items",
Columns: []migrations.Column{
{
Name: "id",
Type: "int",
Pk: true,
},
{
Name: "name",
Type: "text",
Default: ptr("'Pixel'"),
Comment: ptr("my important comment"),
},
},
},
&migrations.OpCreateConstraint{
Table: "items",
Type: migrations.OpCreateConstraintTypeUnique,
Name: "unique_item_name",
Columns: []string{"name"},
Up: map[string]string{
"name": "name",
},
Down: map[string]string{
"name": "name",
},
},
},
},
},
afterStart: func(t *testing.T, db *sql.DB, schema string) {
// Column must have comment set
ColumnMustHaveComment(t, db, schema, "items", "name", "my important comment")
// Column must have default value
ColumnMustHaveDefault(t, db, schema, "items", "name", "'Pixel'::text")
// Can insert a row into the new schema
MustInsert(t, db, schema, "01_create_table", "items", map[string]string{
"id": "1",
"name": "apple",
})

// Can insert a row into the new schema that meets the constraint
MustInsert(t, db, schema, "01_create_table", "items", map[string]string{
"id": "2",
"name": "banana",
})

// Can't insert a row into the new schema that violates the constraint
MustNotInsert(t, db, schema, "01_create_table", "items", map[string]string{
"id": "3",
"name": "apple",
}, testutils.UniqueViolationErrorCode)

// The new view has the expected rows
rows := MustSelect(t, db, schema, "01_create_table", "items")
assert.Equal(t, []map[string]any{
{"id": 1, "name": "apple"},
{"id": 2, "name": "banana"},
}, rows)

// The old view has the expected rows
rows = MustSelect(t, db, schema, "01_create_table", "items")
assert.Equal(t, []map[string]any{
{"id": 1, "name": "apple"},
{"id": 2, "name": "banana"},
}, rows)
},
afterRollback: func(t *testing.T, db *sql.DB, schema string) {},
afterComplete: func(t *testing.T, db *sql.DB, schema string) {
// Column comment must be preserved
ColumnMustHaveComment(t, db, schema, "items", "name", "my important comment")
// Column must have default value
ColumnMustHaveDefault(t, db, schema, "items", "name", "'Pixel'::text")
// Can insert a row into the new schema that meets the constraint
MustInsert(t, db, schema, "01_create_table", "items", map[string]string{
"id": "3",
"name": "carrot",
})

// Can't insert a row into the new schema that violates the constraint
MustNotInsert(t, db, schema, "01_create_table", "items", map[string]string{
"id": "4",
"name": "carrot",
}, testutils.UniqueViolationErrorCode)

// The new view has the expected rows
rows := MustSelect(t, db, schema, "01_create_table", "items")
assert.Equal(t, []map[string]any{
{"id": 3, "name": "carrot"},
}, rows)
},
},
})
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/migrations/op_create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,14 @@ func (o *OpCreateTable) updateSchema(s *schema.Schema) *schema.Schema {
Unique: col.Unique,
Nullable: col.Nullable,
Type: col.Type,
Default: col.Default,
}
if col.Pk {
primaryKeys = append(primaryKeys, col.Name)
}
if col.Comment != nil {
columns[col.Name].Comment = *col.Comment
}
}

uniqueConstraints := make(map[string]*schema.UniqueConstraint, 0)
Expand Down