Skip to content

Commit

Permalink
Update driver to work without dataset
Browse files Browse the repository at this point in the history
For running management queries on
a bigquery instance, ie. create/delete
datasets, we need the bigquery driver
to work with connection-strings where we
don't provide the dataset.
This diff solves that with minimal
changes, to keep the fork not deviate too
much from the OSS driver.
  • Loading branch information
d1xlord committed Jul 18, 2023
1 parent 299ce22 commit 3bbc5e6
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 519 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/

.idea/
5 changes: 3 additions & 2 deletions adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package bigquery

import (
"database/sql/driver"
"gorm.io/driver/bigquery/adaptor"
"reflect"

"github.com/scaledata/bigquery/adaptor"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"reflect"
)

type bigQuerySchemaAdaptor struct {
Expand Down
11 changes: 6 additions & 5 deletions bigquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package bigquery
import (
"database/sql"
"fmt"
"gorm.io/driver/bigquery/adaptor"
_ "gorm.io/driver/bigquery/driver"
"reflect"
"regexp"
"strings"

"github.com/scaledata/bigquery/adaptor"
_ "github.com/scaledata/bigquery/driver"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/logger"
"gorm.io/gorm/migrator"
"gorm.io/gorm/schema"
"reflect"
"regexp"
"strings"
)

type Dialector struct {
Expand Down
2 changes: 1 addition & 1 deletion callbacks.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package bigquery

import (
"gorm.io/driver/bigquery/adaptor"
"github.com/scaledata/bigquery/adaptor"
"gorm.io/gorm"
"gorm.io/gorm/callbacks"
)
Expand Down
3 changes: 2 additions & 1 deletion driver/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"encoding/json"

"cloud.google.com/go/bigquery"
"gorm.io/driver/bigquery/adaptor"

"github.com/scaledata/bigquery/adaptor"
)

type bigQuerySchema interface {
Expand Down
10 changes: 8 additions & 2 deletions driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func configFromUri(uri string) (*bigQueryConfig, error) {
return nil, fmt.Errorf("invalid prefix, expected bigquery:// got: %s", uri)
}

if u.Path == "" {
if u.Hostname() == "" {
return nil, invalidConnectionStringError(uri)
}

Expand All @@ -75,9 +75,15 @@ func configFromUri(uri string) (*bigQueryConfig, error) {
return nil, invalidConnectionStringError(uri)
}

// Check if dataset was provided
datasetName := ""
if len(fields) >= 1 {
datasetName = fields[len(fields)-1]
}

config := &bigQueryConfig{
projectID: u.Hostname(),
dataSet: fields[len(fields)-1],
dataSet: datasetName,
scopes: getScopes(u.Query()),
endpoint: u.Query().Get("endpoint"),
disableAuth: u.Query().Get("disable_auth") == "true",
Expand Down
5 changes: 3 additions & 2 deletions driver/rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package driver

import (
"database/sql/driver"
"google.golang.org/api/iterator"
"gorm.io/driver/bigquery/adaptor"
"io"

"github.com/scaledata/bigquery/adaptor"
"google.golang.org/api/iterator"
)

type bigQueryRows struct {
Expand Down
6 changes: 4 additions & 2 deletions driver/source.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package driver

import (
"cloud.google.com/go/bigquery"
"errors"
"gorm.io/driver/bigquery/adaptor"
"io"

"cloud.google.com/go/bigquery"

"github.com/scaledata/bigquery/adaptor"
)

type bigQuerySource interface {
Expand Down
6 changes: 4 additions & 2 deletions driver/statement.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package driver

import (
"cloud.google.com/go/bigquery"
"context"
"database/sql/driver"
"errors"

"cloud.google.com/go/bigquery"
"github.com/sirupsen/logrus"
"gorm.io/driver/bigquery/adaptor"

"github.com/scaledata/bigquery/adaptor"
)

type bigQueryStatement struct {
Expand Down
18 changes: 5 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
module gorm.io/driver/bigquery
module github.com/scaledata/bigquery

go 1.16

require (
cloud.google.com/go/bigquery v1.42.0
cloud.google.com/go/iam v0.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/googleapis/gax-go/v2 v2.6.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
cloud.google.com/go/bigquery v1.12.0
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.0
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
google.golang.org/api v0.99.0
google.golang.org/genproto v0.0.0-20221014213838-99cd37c6964a // indirect
google.golang.org/grpc v1.50.1 // indirect
github.com/sirupsen/logrus v1.6.0
github.com/stretchr/testify v1.4.0
google.golang.org/api v0.32.0
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gorm.io/gorm v1.24.0
)
529 changes: 42 additions & 487 deletions go.sum

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions test/common_text.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package test

import (
"log"

"github.com/scaledata/bigquery"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/suite"
"gorm.io/driver/bigquery"
"gorm.io/gorm"
"log"
)

type GormTestSuite struct {
Expand Down

0 comments on commit 3bbc5e6

Please sign in to comment.