Skip to content

Commit

Permalink
Merge pull request #490 from k1LoW/fix-NormalizeTableName
Browse files Browse the repository at this point in the history
Fix schema.Schema.NormalizeTableName
  • Loading branch information
k1LoW committed Jun 16, 2023
2 parents da35dd7 + 290d23c commit 62855a3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ type Schema struct {
}

func (s *Schema) NormalizeTableName(name string) string {
if s.Driver != nil && (s.Driver.Name == "postgres" || s.Driver.Name == "redshift") && !strings.Contains(name, ".") {
if s.Driver != nil && s.Driver.Meta != nil && s.Driver.Meta.CurrentSchema != "" && (s.Driver.Name == "postgres" || s.Driver.Name == "redshift") && !strings.Contains(name, ".") {
return fmt.Sprintf("%s.%s", s.Driver.Meta.CurrentSchema, name)
}
return name
Expand Down
23 changes: 23 additions & 0 deletions schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,36 @@ package schema
import (
"database/sql"
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestNormalizeTableName(t *testing.T) {
tests := []struct {
s *Schema
name string
want string
}{
{&Schema{}, "testtable", "testtable"},
{&Schema{Driver: &Driver{Name: "postgres", Meta: &DriverMeta{CurrentSchema: "public"}}}, "testtable", "public.testtable"},
{&Schema{Driver: &Driver{Name: "mysql", Meta: &DriverMeta{CurrentSchema: "public"}}}, "testtable", "testtable"},
{&Schema{Driver: &Driver{Name: "postgres", Meta: &DriverMeta{CurrentSchema: ""}}}, "testtable", "testtable"},
{&Schema{Driver: &Driver{Name: "postgres", Meta: &DriverMeta{CurrentSchema: "public"}}}, "other.testtable", "other.testtable"},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
got := tt.s.NormalizeTableName(tt.name)
if got != tt.want {
t.Errorf("got %v\nwant %v", got, tt.want)
}
})
}
}

func TestSchema_FindTableByName(t *testing.T) {
schema := Schema{
Name: "testschema",
Expand Down

0 comments on commit 62855a3

Please sign in to comment.