From e49daf95ce4fc56bd09abf66ce4e6f2d827089c4 Mon Sep 17 00:00:00 2001 From: Georgy Savva Date: Sun, 7 Apr 2024 08:34:56 -0400 Subject: [PATCH] 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 --- .github/workflows/test.yml | 8 ++++---- dbscan/dbscan_test.go | 31 +++++++++++++++++++++++++++++++ dbscan/rowscanner.go | 8 +++++++- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 97bbb90..ffe593a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -27,8 +27,8 @@ jobs: steps: - name: Download CockroachDB Binary run: | - wget -qO- https://binaries.cockroachdb.com/cockroach-v20.1.3.linux-amd64.tgz | tar xvz - sudo cp -i cockroach-v20.1.3.linux-amd64/cockroach /usr/local/bin/ + wget -qO- https://binaries.cockroachdb.com/cockroach-v23.2.3.linux-amd64.tgz | tar xvz + sudo cp -i cockroach-v23.2.3.linux-amd64/cockroach /usr/local/bin/ - name: Install Go uses: actions/setup-go@v3 @@ -56,12 +56,12 @@ jobs: run: go test --tags with_mssql -v -race -coverprofile=coverage.txt -covermode=atomic ./... --cockroach-binary cockroach - name: Upload Codecov - uses: codecov/codecov-action@v3 + uses: codecov/codecov-action@v4 with: + token: ${{ secrets.CODECOV_TOKEN }} files: ./coverage.txt flags: unittests env_vars: GO name: codecov-umbrella fail_ci_if_error: true - path_to_write_report: ./codecov_report.txt verbose: true diff --git a/dbscan/dbscan_test.go b/dbscan/dbscan_test.go index 2dd617e..6a26a4f 100644 --- a/dbscan/dbscan_test.go +++ b/dbscan/dbscan_test.go @@ -374,6 +374,25 @@ func TestScanRow_withAllowUnknownColumns_returnsRow(t *testing.T) { assert.Equal(t, expected, *got) } +func TestScanRow_withAllowUnknownColumns_unknownColumnType(t *testing.T) { + t.Parallel() + rows := queryRows(t, ` + SELECT 'foo val' AS foo, 'test_val_1'::test_enum_type AS bar + `) + defer rows.Close() //nolint: errcheck + rows.Next() + + got := &struct{ Foo string }{} + testAPIWithUnknownColumns, err := getAPI(dbscan.WithAllowUnknownColumns(true)) + require.NoError(t, err) + err = testAPIWithUnknownColumns.ScanRow(got, rows) + require.NoError(t, err) + requireNoRowsErrorsAndClose(t, rows) + + expected := struct{ Foo string }{Foo: "foo val"} + assert.Equal(t, expected, *got) +} + func TestMain(m *testing.M) { exitCode := func() int { flag.Parse() @@ -387,6 +406,10 @@ func TestMain(m *testing.M) { panic(err) } defer testDB.Close() + err = prepareTestDB(testDB) + if err != nil { + panic(err) + } testAPI, err = getAPI() if err != nil { panic(err) @@ -395,3 +418,11 @@ func TestMain(m *testing.M) { }() os.Exit(exitCode) } + +func prepareTestDB(testDB *pgxpool.Pool) (err error) { + _, err = testDB.Exec(ctx, ` + CREATE TYPE test_enum_type AS ENUM ('test_val_1', 'test_val_2'); + `) + + return err +} diff --git a/dbscan/rowscanner.go b/dbscan/rowscanner.go index b1df194..685f72f 100644 --- a/dbscan/rowscanner.go +++ b/dbscan/rowscanner.go @@ -124,6 +124,12 @@ func startScanner(rs *RowScanner, dstValue reflect.Value) error { ) } +type noOpScanType struct{} + +func (*noOpScanType) Scan(value interface{}) error { + return nil +} + func (rs *RowScanner) scanStruct(structValue reflect.Value) error { if rs.scans == nil { rs.scans = make([]interface{}, len(rs.columns)) @@ -132,7 +138,7 @@ func (rs *RowScanner) scanStruct(structValue reflect.Value) error { fieldIndex, ok := rs.columnToFieldIndex[column] if !ok { if rs.api.allowUnknownColumns { - var tmp interface{} + var tmp noOpScanType rs.scans[i] = &tmp continue }