Skip to content

Commit

Permalink
Merge pull request #1 from mbobrovskyi/feature/bigqeury-test
Browse files Browse the repository at this point in the history
Feature/bigqeury test
  • Loading branch information
mbobrovskyi authored Feb 15, 2023
2 parents decd290 + e764e1d commit 54219d7
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 23 deletions.
2 changes: 1 addition & 1 deletion database/bigquery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* [Examples](./examples)

### Usage
`bigquery://https://www.googleapis.com/bigquery/v2:443?x-migrations-table=schema_migrations&x-statement-timeout=0&credentials_filename=./myproject-XXXXXXXXXXXXX-XXXXXXXXXXXX.json&project_id=myproject-XXXXXXXXXXXXX&dataset_id=mydataset`
`bigquery://https://bigquery.googleapis.com/bigquery/v2/?x-migrations-table=schema_migrations&x-statement-timeout=0&credentials_filename=./myproject-XXXXXXXXXXXXX-XXXXXXXXXXXX.json&project_id=myproject-XXXXXXXXXXXXX&dataset_id=mydataset`


### System variables reference
Expand Down
14 changes: 9 additions & 5 deletions database/bigquery/bigquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"google.golang.org/api/option"
"io"
nurl "net/url"
"os"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -148,12 +149,14 @@ func (b *BigQuery) Open(url string) (database.Driver, error) {
}
}

//By default, try to get from environment variable GOOGLE_APPLICATION_CREDENTIALS
if q.Has("credentials_filename") {
opts = append(opts, option.WithCredentialsFile(q.Get("credentials_filename")))
} else if os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") != "" {
opts = append(opts, option.WithCredentialsFile(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")))
} else {
opts = append(opts, option.WithoutAuthentication())
}

// By default, try to get from credentials file
projectID := bigquery.DetectProjectID
if q.Has("project_id") {
projectID = q.Get("project_id")
Expand All @@ -175,6 +178,8 @@ func (b *BigQuery) Open(url string) (database.Driver, error) {
config.TimeZone = q.Get("time_zone")
}

opts = append(opts, option.WithEndpoint(fmt.Sprintf("%s%s", purl.Host, purl.Path)))

client, err := bigquery.NewClient(ctx, projectID, opts...)
if err != nil {
return nil, err
Expand Down Expand Up @@ -267,10 +272,9 @@ func (b *BigQuery) SetVersion(version int, dirty bool) error {
ctx := context.Background()

query := fmt.Sprintf(`
SET @@dataset_id = '%[1]s';
BEGIN TRANSACTION;
DELETE FROM %[2]s WHERE true;
INSERT INTO %[2]s (version, dirty) VALUES (%[3]d, %[4]t);
DELETE FROM `+"`%[1]s.%[2]s`"+` WHERE true;
INSERT INTO `+"`%[1]s.%[2]s`"+` (version, dirty) VALUES (%[3]d, %[4]t);
COMMIT TRANSACTION;
`, b.config.DatasetID, b.config.MigrationsTable, version, dirty)

Expand Down
100 changes: 83 additions & 17 deletions database/bigquery/bigquery_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package bigquery

import (
"context"
"errors"
"github.com/golang-migrate/migrate/v4/database"
"strings"
"testing"
)

const (
connectionUrl = "bigquery://https://www.googleapis.com/bigquery/v2:443?x-migrations-table=schema_migrations&x-statement-timeout=0&credentials_filename=./tmp/myproject-XXXXXXXXXXXXX-XXXXXXXXXXXX.json&dataset_id=mydataset"
//connectionUrl = "bigquery://https://bigquery.googleapis.com/bigquery/v2/?x-migrations-table=schema_migrations&x-statement-timeout=0&credentials_filename=./tmp/myproject-XXXXXXXXXXXXX-XXXXXXXXXXXX.json&dataset_id=mydataset"
connectionUrl = "bigquery://http://0.0.0.0:9050/?x-migrations-table=schema_migrations&project_id=myproject&dataset_id=mydataset"
)

func openConnection() (database.Driver, error) {
Expand All @@ -21,66 +24,117 @@ func openConnection() (database.Driver, error) {
return driver, nil
}

func TestWithInstanceWithoutClient(t *testing.T) {
driver, err := WithInstance(context.Background(), nil, &Config{})
if err == nil {
t.Errorf("expected `no client`, got nil")
return
}
if !errors.Is(err, ErrNoClient) {
t.Errorf("expected `no client`, got %s", err.Error())
return
}
if driver != nil {
t.Errorf("driver should be nil")
return
}
}

func TestOpen(t *testing.T) {
_, err := openConnection()
driver, err := openConnection()
if err != nil {
t.Error(err)
return
}

defer func() {
err := driver.Close()
if err != nil {
t.Error(err)
}
}()
}

func TestVersion(t *testing.T) {
func TestClose(t *testing.T) {
driver, err := openConnection()
if err != nil {
t.Error(err)
return
}

version, dirty, err := driver.Version()
defer func() {
err := driver.Close()
if err != nil {
t.Error(err)
}
}()

err = driver.Close()
if err != nil {
t.Error(err)
return
}

t.Log(version, dirty)
}

func TestDrop(t *testing.T) {
func TestVersion(t *testing.T) {
driver, err := openConnection()
if err != nil {
t.Error(err)
return
}

err = driver.Drop()
defer func() {
err := driver.Close()
if err != nil {
t.Error(err)
}
}()

version, dirty, err := driver.Version()
if err != nil {
t.Error(err)
return
}

t.Log(version, dirty)
}

func TestClose(t *testing.T) {
func TestSetVersion(t *testing.T) {
driver, err := openConnection()
if err != nil {
t.Error(err)
return
}

err = driver.Close()
defer func() {
err := driver.Close()
if err != nil {
t.Error(err)
}
}()

err = driver.SetVersion(-1, false)
if err != nil {
t.Error(err)
return
}
}

func TestSetVersion(t *testing.T) {
func TestDrop(t *testing.T) {
driver, err := openConnection()
if err != nil {
t.Error(err)
return
}

err = driver.SetVersion(-1, false)
defer func() {
err := driver.Close()
if err != nil {
t.Error(err)
}
}()

err = driver.Drop()
if err != nil {
t.Error(err)
return
Expand All @@ -94,6 +148,13 @@ func TestRun(t *testing.T) {
return
}

defer func() {
err := driver.Close()
if err != nil {
t.Error(err)
}
}()

err = driver.Run(strings.NewReader(`
CREATE TABLE IF NOT EXISTS users (
first_name STRING,
Expand All @@ -112,16 +173,21 @@ func TestRunWithError(t *testing.T) {
return
}

defer func() {
err := driver.Close()
if err != nil {
t.Error(err)
}
}()

err = driver.Run(strings.NewReader(`
CREATE TABLE IF NOT EXISTS users (
first_name STRINGa,
last_name STRING
)`))
if err != nil {
t.Log(err)
return
if err == nil {
t.Error("expected 'googleapi: Error 400: Query error: Type not found: STRINGa at [4:36], invalidQuery' got nil")
}

t.Error("error is nil, should be 'googleapi: Error 400: Query error: Type not found: STRINGa at [4:36], invalidQuery'")
return
t.Log(err)
}

0 comments on commit 54219d7

Please sign in to comment.