Skip to content

Commit

Permalink
fix: remove schema name from t.Name during bun-schema inspection
Browse files Browse the repository at this point in the history
  • Loading branch information
bevzzz committed Nov 10, 2024
1 parent 6b0ddc1 commit 31ed582
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
21 changes: 21 additions & 0 deletions internal/dbtest/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,5 +626,26 @@ func TestBunModelInspector_Inspect(t *testing.T) {
return
}
})

t.Run("separates schema and table name", func(t *testing.T) {
type Model struct {
bun.BaseModel `bun:"table:custom_schema.model"`
}

tables := schema.NewTables(dialect)
tables.Register((*Model)(nil))
inspector := sqlschema.NewBunModelInspector(tables)

got, err := inspector.Inspect(context.Background())
require.NoError(t, err)

gotTables := got.GetTables()
require.Equal(t, 1, gotTables.Len())
for _, table := range gotTables.FromOldest() {
require.Equal(t, "custom_schema", table.GetSchema(), "wrong schema name")
require.Equal(t, "model", table.GetName(), "wrong table name")
return
}
})
})
}
14 changes: 8 additions & 6 deletions internal/dbtest/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ func testUnique(t *testing.T, db *bun.DB) {

func testUniqueRenamedTable(t *testing.T, db *bun.DB) {
type TableBefore struct {
bun.BaseModel `bun:"table:before"`
bun.BaseModel `bun:"table:automigrate.before"`
FirstName string `bun:"first_name,unique:full_name"`
LastName string `bun:"last_name,unique:full_name"`
Birthday string `bun:"birthday,unique"`
Expand All @@ -866,7 +866,7 @@ func testUniqueRenamedTable(t *testing.T, db *bun.DB) {
}

type TableAfter struct {
bun.BaseModel `bun:"table:after"`
bun.BaseModel `bun:"table:automigrate.after"`
// Expand full_name unique group and rename it.
FirstName string `bun:"first_name,unique:birth_certificate"`
LastName string `bun:"last_name,unique:birth_certificate"`
Expand All @@ -881,7 +881,7 @@ func testUniqueRenamedTable(t *testing.T, db *bun.DB) {
orderedmap.Pair[string, sqlschema.Table]{
Key: "after",
Value: &sqlschema.BaseTable{
Schema: db.Dialect().DefaultSchema(),
Schema: "automigrate",
Name: "after",
Columns: orderedmap.New[string, sqlschema.Column](orderedmap.WithInitialData(
orderedmap.Pair[string, sqlschema.Column]{
Expand Down Expand Up @@ -931,6 +931,7 @@ func testUniqueRenamedTable(t *testing.T, db *bun.DB) {

ctx := context.Background()
inspect := inspectDbOrSkip(t, db)
mustCreateSchema(t, ctx, db, "automigrate")
mustResetModel(t, ctx, db, (*TableBefore)(nil))
mustDropTableOnCleanup(t, ctx, db, (*TableAfter)(nil))
m := newAutoMigratorOrSkip(t, db, migrate.WithModel((*TableAfter)(nil)))
Expand All @@ -946,7 +947,7 @@ func testUniqueRenamedTable(t *testing.T, db *bun.DB) {
func testUpdatePrimaryKeys(t *testing.T, db *bun.DB) {
// Has a composite primary key.
type DropPKBefore struct {
bun.BaseModel `bun:"table:drop_your_pks"`
bun.BaseModel `bun:"table:please.drop_your_pks"`
FirstName string `bun:"first_name,pk"`
LastName string `bun:"last_name,pk"`
}
Expand All @@ -970,7 +971,7 @@ func testUpdatePrimaryKeys(t *testing.T, db *bun.DB) {

// Doesn't have any primary keys.
type DropPKAfter struct {
bun.BaseModel `bun:"table:drop_your_pks"`
bun.BaseModel `bun:"table:please.drop_your_pks"`
FirstName string `bun:"first_name,notnull"`
LastName string `bun:"last_name,notnull"`
}
Expand All @@ -994,7 +995,7 @@ func testUpdatePrimaryKeys(t *testing.T, db *bun.DB) {
orderedmap.Pair[string, sqlschema.Table]{
Key: "drop_your_pks",
Value: &sqlschema.BaseTable{
Schema: db.Dialect().DefaultSchema(),
Schema: "please",
Name: "drop_your_pks",
Columns: orderedmap.New[string, sqlschema.Column](orderedmap.WithInitialData(
orderedmap.Pair[string, sqlschema.Column]{
Expand Down Expand Up @@ -1074,6 +1075,7 @@ func testUpdatePrimaryKeys(t *testing.T, db *bun.DB) {

ctx := context.Background()
inspect := inspectDbOrSkip(t, db)
mustCreateSchema(t, ctx, db, "please")
mustResetModel(t, ctx, db,
(*DropPKBefore)(nil),
(*AddNewPKBefore)(nil),
Expand Down
11 changes: 9 additions & 2 deletions migrate/sqlschema/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,17 @@ func (bmi *BunModelInspector) Inspect(ctx context.Context) (Database, error) {
pk = &PrimaryKey{Columns: NewColumns(columns...)}
}

state.Tables.Set(t.Name, &BunTable{
// In cases where a table is defined in a non-default schema in the `bun:table` tag,
// schema.Table only extracts the name of the schema, but passes the entire tag value to t.Name
// for backwads-compatibility. For example, a bun model like this:
// type Model struct { bun.BaseModel `bun:"table:favourite.books` }
// produces
// schema.Table{ Schema: "favourite", Name: "favourite.books" }
tableName := strings.TrimPrefix(t.Name, t.Schema+".")
state.Tables.Set(tableName, &BunTable{
BaseTable: BaseTable{
Schema: t.Schema,
Name: t.Name,
Name: tableName,
Columns: columns,
UniqueConstraints: unique,
PrimaryKey: pk,
Expand Down

0 comments on commit 31ed582

Please sign in to comment.