Skip to content

Commit 2f74a48

Browse files
authored
Merge pull request #1452 from jamisonhyatt/fix-whitelist-table-blacklist-columns
Fix whitelist table blacklist columns
2 parents a80b114 + 6cca1fe commit 2f74a48

File tree

6 files changed

+97
-3
lines changed

6 files changed

+97
-3
lines changed

drivers/sqlboiler-mssql/driver/mssql.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,9 @@ func (m *MSSQLDriver) Columns(schema, tableName string, whitelist, blacklist []s
308308
args = append(args, w)
309309
}
310310
}
311-
} else if len(blacklist) > 0 {
311+
}
312+
313+
if len(blacklist) > 0 {
312314
cols := drivers.ColumnsFromList(blacklist, tableName)
313315
if len(cols) > 0 {
314316
query += fmt.Sprintf(" and c.column_name not in (%s)", strmangle.Placeholders(true, len(cols), 3, 1))

drivers/sqlboiler-mssql/driver/mssql_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,36 @@ func TestDriver(t *testing.T) {
106106
if bytes.Compare(want, got) != 0 {
107107
t.Errorf("want:\n%s\ngot:\n%s\n", want, got)
108108
}
109+
110+
t.Run("whitelist table, blacklist column", func(t *testing.T) {
111+
p := &MSSQLDriver{}
112+
config := drivers.Config{
113+
"user": envUsername,
114+
"pass": envPassword,
115+
"dbname": envDatabase,
116+
"host": envHostname,
117+
"port": envPort,
118+
"sslmode": "disable",
119+
"schema": "dbo",
120+
"whitelist": []string{"magic"},
121+
"blacklist": []string{"magic.string_three"},
122+
}
123+
info, err := p.Assemble(config)
124+
if err != nil {
125+
t.Fatal(err)
126+
}
127+
found := false
128+
for _, tbl := range info.Tables {
129+
if tbl.Name == "magic" {
130+
for _, col := range tbl.Columns {
131+
if col.Name == "string_three" {
132+
found = true
133+
}
134+
}
135+
}
136+
}
137+
if found {
138+
t.Errorf("blacklisted column 'string_three' should not be present in table 'magic'")
139+
}
140+
})
109141
}

drivers/sqlboiler-mysql/driver/mysql.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,9 @@ func (m *MySQLDriver) Columns(schema, tableName string, whitelist, blacklist []s
332332
args = append(args, w)
333333
}
334334
}
335-
} else if len(blacklist) > 0 {
335+
}
336+
337+
if len(blacklist) > 0 {
336338
cols := drivers.ColumnsFromList(blacklist, tableName)
337339
if len(cols) > 0 {
338340
query += fmt.Sprintf(" and c.column_name not in (%s)", strings.Repeat(",?", len(cols))[1:])

drivers/sqlboiler-mysql/driver/mysql_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,32 @@ func TestDriver(t *testing.T) {
113113
require.JSONEq(t, string(want), string(got))
114114
})
115115
}
116+
117+
t.Run("whitelist table, blacklist column", func(t *testing.T) {
118+
p := &MySQLDriver{}
119+
config := drivers.Config{
120+
"user": envUsername,
121+
"pass": envPassword,
122+
"dbname": envDatabase,
123+
"host": envHostname,
124+
"port": envPort,
125+
"sslmode": "false",
126+
"schema": envDatabase,
127+
"whitelist": []string{"magic"},
128+
"blacklist": []string{"magic.string_three"},
129+
}
130+
info, err := p.Assemble(config)
131+
require.NoError(t, err)
132+
found := false
133+
for _, tbl := range info.Tables {
134+
if tbl.Name == "magic" {
135+
for _, col := range tbl.Columns {
136+
if col.Name == "string_three" {
137+
found = true
138+
}
139+
}
140+
}
141+
}
142+
require.False(t, found, "blacklisted column 'string_three' should not be present in table 'magic'")
143+
})
116144
}

drivers/sqlboiler-psql/driver/psql.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,9 @@ func (p *PostgresDriver) Columns(schema, tableName string, whitelist, blacklist
600600
args = append(args, w)
601601
}
602602
}
603-
} else if len(blacklist) > 0 {
603+
}
604+
605+
if len(blacklist) > 0 {
604606
cols := drivers.ColumnsFromList(blacklist, tableName)
605607
if len(cols) > 0 {
606608
query += fmt.Sprintf(" where c.column_name not in (%s)", strmangle.Placeholders(true, len(cols), 3, 1))

drivers/sqlboiler-psql/driver/psql_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,32 @@ func TestAssemble(t *testing.T) {
114114
require.JSONEq(t, string(want), string(got))
115115
})
116116
}
117+
118+
t.Run("whitelist table, blacklist column", func(t *testing.T) {
119+
p := PostgresDriver{}
120+
config := drivers.Config{
121+
"user": envUsername,
122+
"pass": envPassword,
123+
"dbname": envDatabase,
124+
"host": envHostname,
125+
"port": envPort,
126+
"sslmode": "disable",
127+
"schema": "public",
128+
"whitelist": []string{"magic"},
129+
"blacklist": []string{"magic.string_three"},
130+
}
131+
info, err := p.Assemble(config)
132+
require.NoError(t, err)
133+
found := false
134+
for _, tbl := range info.Tables {
135+
if tbl.Name == "magic" {
136+
for _, col := range tbl.Columns {
137+
if col.Name == "string_three" {
138+
found = true
139+
}
140+
}
141+
}
142+
}
143+
require.False(t, found, "blacklisted column 'string_three' should not be present in table 'magic'")
144+
})
117145
}

0 commit comments

Comments
 (0)