Skip to content

Commit

Permalink
Add special type for scanning of unused columns (V1) (#132) (#134)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
georgysavva and br3w0r authored Apr 7, 2024
1 parent d706b62 commit e49daf9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
31 changes: 31 additions & 0 deletions dbscan/dbscan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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
}
8 changes: 7 additions & 1 deletion dbscan/rowscanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
}
Expand Down

0 comments on commit e49daf9

Please sign in to comment.