Skip to content

Commit 6df1fef

Browse files
authored
NameTuple.MatchesPattern() now performs case insenstive match. (#1379)
1 parent f6e5c11 commit 6df1fef

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

yb-voyager/src/namereg/namereg_test.go

+54-2
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ func TestNameTupleMatchesPattern(t *testing.T) {
7979
pattern string
8080
match bool
8181
}{
82-
{"table1", false}, // effectively: <defaultSchema>.table1 i.e. public.table1
82+
{"table1", true}, // effectively: <defaultSchema>.table1 i.e. public.table1
8383
{"table2", false},
8484
{"table", false},
8585
{"TABLE1", true},
8686
{"TABLE2", false},
8787
{"TABLE", false},
8888
{"TABLE*", true},
89-
{"table*", false},
89+
{"table*", true},
9090
{"SAKILA.TABLE1", true},
9191
{"SAKILA.TABLE2", false},
9292
{"SAKILA.TABLE", false},
@@ -105,6 +105,58 @@ func TestNameTupleMatchesPattern(t *testing.T) {
105105
}
106106
}
107107

108+
func TestNameMatchesPattern(t *testing.T) {
109+
assert := assert.New(t)
110+
require := require.New(t)
111+
112+
reg := &NameRegistry{
113+
SourceDBType: ORACLE,
114+
role: SOURCE_DB_EXPORTER_ROLE,
115+
SourceDBSchemaNames: []string{"TEST_SCHEMA"},
116+
DefaultSourceDBSchemaName: "TEST_SCHEMA",
117+
SourceDBTableNames: map[string][]string{
118+
"TEST_SCHEMA": {
119+
"C", "C1", "C2", "Case_Sensitive_Columns", "EMPLOYEES", "FOO", "Mixed_Case_Table_Name_Test",
120+
"RESERVED_COLUMN", "SESSION_LOG", "SESSION_LOG1", "SESSION_LOG2", "SESSION_LOG3", "SESSION_LOG4",
121+
"TEST_TIMEZONE", "TRUNC_TEST", "check", "group",
122+
},
123+
},
124+
}
125+
// Prepare a list of all NamedTuples.
126+
ntups := make([]*sqlname.NameTuple, 0)
127+
for _, tableName := range reg.SourceDBTableNames["TEST_SCHEMA"] {
128+
ntup, err := reg.LookupTableName(tableName)
129+
require.Nil(err)
130+
ntups = append(ntups, ntup)
131+
}
132+
// Write a table-driven test to test the MatchesPattern() method using following patterns:
133+
// session_log,session_log?,"group","check",test*,"*Case*",c*
134+
var testCases = []struct {
135+
pattern string
136+
expected []string
137+
}{
138+
{"session_log", []string{"TEST_SCHEMA.SESSION_LOG"}},
139+
{"session_log?", []string{"TEST_SCHEMA.SESSION_LOG1", "TEST_SCHEMA.SESSION_LOG2", "TEST_SCHEMA.SESSION_LOG3", "TEST_SCHEMA.SESSION_LOG4"}},
140+
{"group", []string{"TEST_SCHEMA.group"}},
141+
{"check", []string{"TEST_SCHEMA.check"}},
142+
{"test*", []string{"TEST_SCHEMA.TEST_TIMEZONE"}},
143+
{"*Case*", []string{"TEST_SCHEMA.Case_Sensitive_Columns", "TEST_SCHEMA.Mixed_Case_Table_Name_Test"}},
144+
{"c*", []string{"TEST_SCHEMA.C", "TEST_SCHEMA.C1", "TEST_SCHEMA.C2", "TEST_SCHEMA.Case_Sensitive_Columns", "TEST_SCHEMA.check"}},
145+
}
146+
for _, tc := range testCases {
147+
for _, ntup := range ntups {
148+
match, err := ntup.MatchesPattern(tc.pattern)
149+
require.Nil(err)
150+
tableName := ntup.CurrentName.Qualified.Unquoted
151+
if match {
152+
assert.Contains(tc.expected, tableName, "pattern: %s, tableName: %s", tc.pattern, tableName)
153+
} else {
154+
assert.NotContains(tc.expected, tableName, "pattern: %s, tableName: %s", tc.pattern, tableName)
155+
}
156+
}
157+
}
158+
}
159+
108160
func TestNameRegistrySuccessfulLookup(t *testing.T) {
109161
assert := assert.New(t)
110162
require := require.New(t)

yb-voyager/src/utils/sqlname/nametuple.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (nv *ObjectName) MatchesPattern(pattern string) (bool, error) {
7979
default:
8080
return false, fmt.Errorf("invalid pattern: %s", pattern)
8181
}
82-
match1, err := filepath.Match(pattern, nv.Unqualified.Unquoted)
82+
match1, err := filepath.Match(strings.ToLower(pattern), strings.ToLower(nv.Unqualified.Unquoted))
8383
if err != nil {
8484
return false, fmt.Errorf("invalid pattern: %s", pattern)
8585
}

0 commit comments

Comments
 (0)