8000 Fix schema.Schema.NormalizeTableName by k1LoW · Pull Request #490 · k1LoW/tbls · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix schema.Schema.NormalizeTableName #490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2023
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
2 changes: 1 addition & 1 deletion schema/schema.go
8000
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