Skip to content

Commit e49daf9

Browse files
georgysavvabr3w0r
andauthored
Add special type for scanning of unused columns (V1) (#132) (#134)
* add noOpScanType * remove Value method * add test for scanning of unknown enum * fix tests * update db version * fix codecov * use token in codecov --------- Co-authored-by: br3w0r <daniildirun123@gmail.com>
1 parent d706b62 commit e49daf9

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ jobs:
2727
steps:
2828
- name: Download CockroachDB Binary
2929
run: |
30-
wget -qO- https://binaries.cockroachdb.com/cockroach-v20.1.3.linux-amd64.tgz | tar xvz
31-
sudo cp -i cockroach-v20.1.3.linux-amd64/cockroach /usr/local/bin/
30+
wget -qO- https://binaries.cockroachdb.com/cockroach-v23.2.3.linux-amd64.tgz | tar xvz
31+
sudo cp -i cockroach-v23.2.3.linux-amd64/cockroach /usr/local/bin/
3232
3333
- name: Install Go
3434
uses: actions/setup-go@v3
@@ -56,12 +56,12 @@ jobs:
5656
run: go test --tags with_mssql -v -race -coverprofile=coverage.txt -covermode=atomic ./... --cockroach-binary cockroach
5757

5858
- name: Upload Codecov
59-
uses: codecov/codecov-action@v3
59+
uses: codecov/codecov-action@v4
6060
with:
61+
token: ${{ secrets.CODECOV_TOKEN }}
6162
files: ./coverage.txt
6263
flags: unittests
6364
env_vars: GO
6465
name: codecov-umbrella
6566
fail_ci_if_error: true
66-
path_to_write_report: ./codecov_report.txt
6767
verbose: true

dbscan/dbscan_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,25 @@ func TestScanRow_withAllowUnknownColumns_returnsRow(t *testing.T) {
374374
assert.Equal(t, expected, *got)
375375
}
376376

377+
func TestScanRow_withAllowUnknownColumns_unknownColumnType(t *testing.T) {
378+
t.Parallel()
379+
rows := queryRows(t, `
380+
SELECT 'foo val' AS foo, 'test_val_1'::test_enum_type AS bar
381+
`)
382+
defer rows.Close() //nolint: errcheck
383+
rows.Next()
384+
385+
got := &struct{ Foo string }{}
386+
testAPIWithUnknownColumns, err := getAPI(dbscan.WithAllowUnknownColumns(true))
387+
require.NoError(t, err)
388+
err = testAPIWithUnknownColumns.ScanRow(got, rows)
389+
require.NoError(t, err)
390+
requireNoRowsErrorsAndClose(t, rows)
391+
392+
expected := struct{ Foo string }{Foo: "foo val"}
393+
assert.Equal(t, expected, *got)
394+
}
395+
377396
func TestMain(m *testing.M) {
378397
exitCode := func() int {
379398
flag.Parse()
@@ -387,6 +406,10 @@ func TestMain(m *testing.M) {
387406
panic(err)
388407
}
389408
defer testDB.Close()
409+
err = prepareTestDB(testDB)
410+
if err != nil {
411+
panic(err)
412+
}
390413
testAPI, err = getAPI()
391414
if err != nil {
392415
panic(err)
@@ -395,3 +418,11 @@ func TestMain(m *testing.M) {
395418
}()
396419
os.Exit(exitCode)
397420
}
421+
422+
func prepareTestDB(testDB *pgxpool.Pool) (err error) {
423+
_, err = testDB.Exec(ctx, `
424+
CREATE TYPE test_enum_type AS ENUM ('test_val_1', 'test_val_2');
425+
`)
426+
427+
return err
428+
}

dbscan/rowscanner.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ func startScanner(rs *RowScanner, dstValue reflect.Value) error {
124124
)
125125
}
126126

127+
type noOpScanType struct{}
128+
129+
func (*noOpScanType) Scan(value interface{}) error {
130+
return nil
131+
}
132+
127133
func (rs *RowScanner) scanStruct(structValue reflect.Value) error {
128134
if rs.scans == nil {
129135
rs.scans = make([]interface{}, len(rs.columns))
@@ -132,7 +138,7 @@ func (rs *RowScanner) scanStruct(structValue reflect.Value) error {
132138
fieldIndex, ok := rs.columnToFieldIndex[column]
133139
if !ok {
134140
if rs.api.allowUnknownColumns {
135-
var tmp interface{}
141+
var tmp noOpScanType
136142
rs.scans[i] = &tmp
137143
continue
138144
}

0 commit comments

Comments
 (0)