Skip to content

Commit

Permalink
fixed bigquery schema bug
Browse files Browse the repository at this point in the history
Signed-off-by: adarsh-jaiss <[email protected]>
  • Loading branch information
Adarsh-jaiss committed May 23, 2024
1 parent 7a96a2e commit 729a463
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 39 deletions.
28 changes: 5 additions & 23 deletions databases/bigquery/bigquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var GOOGLE_APPLICATION_CREDENTIALS = "GOOGLE_APPLICATION_CREDENTIALS"

const (
BigQuery_SCHEMA_QUERY = "SELECT * FROM %s LIMIT 1"
BigQuery_SCHEMA_QUERY = "SELECT column_name, data_type FROM %s.INFORMATION_SCHEMA.COLUMNS WHERE table_name='%s'"
BigQuery_TABLES_QUERY = "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = '%s'"
)

Expand Down Expand Up @@ -58,9 +58,8 @@ func NewBigQueryWithConfig(cfg *config.Config) (types.ISQL, error) {
// this function extarcts the schema of a table in BigQuery.
// It takes table name as input and returns a Table struct and an error.
func (b *BigQuery) Schema(table string) (types.Table, error) {

// execute the sql statement
rows, err := b.Client.Query(fmt.Sprintf(BigQuery_SCHEMA_QUERY, table))
rows, err := b.Client.Query(fmt.Sprintf(BigQuery_SCHEMA_QUERY, b.Config.Database, table))
if err != nil {
return types.Table{}, fmt.Errorf("error executing sql statement: %v", err)
}
Expand All @@ -71,27 +70,11 @@ func (b *BigQuery) Schema(table string) (types.Table, error) {
}
}()

// scanning the result into a variable and append it into a the slice
var columns []types.Column
columnNames, err := rows.Columns()
if err != nil {
return types.Table{}, fmt.Errorf("error getting column names: %v", err)
}
columnTypes, err := rows.ColumnTypes()
if err != nil {
return types.Table{}, fmt.Errorf("error getting column types: %v", err)
}
for i, name := range columnNames {
for rows.Next() {
var column types.Column
column.Name = name
column.Type = columnTypes[i].DatabaseTypeName()
var ISNullable, _ = columnTypes[i].Nullable()
column.IsNullable = fmt.Sprintf("%v", ISNullable)
var length int64
length, _ = columnTypes[i].Length()
column.CharacterMaximumLength = sql.NullInt64{
Int64: length,
Valid: length != 0,
if err := rows.Scan(&column.Name, &column.Type); err != nil {
return types.Table{}, fmt.Errorf("error scanning row: %v", err)
}
columns = append(columns, column)
}
Expand All @@ -102,7 +85,6 @@ func (b *BigQuery) Schema(table string) (types.Table, error) {
Dataset: b.Config.Database,
ColumnCount: int64(len(columns)),
}, nil

}

// Execute executes a query on BigQuery.
Expand Down
32 changes: 17 additions & 15 deletions databases/bigquery/bigquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,38 @@ func MockDB() (*sql.DB, sqlmock.Sqlmock) {
// It creates a mock instance of BigQuery, sets the expected return values, and calls the method under test.
// It then asserts the expected return values and checks if the method was called with the correct arguments.
func TestSchema(t *testing.T) {
db, mock := MockDB() // create a new mock database connection
// create a new mock database connection
db, mock := MockDB()
defer func() {
if err := db.Close(); err != nil {
fmt.Println(err)
}
}() // close the connection when the function returns
}()

tableName := "user" // table name to be used in the test
mockRows := sqlmock.NewRows([]string{"Field", "Type", "Null", "Key", "Default", "Extra"}).AddRow("id", "int", "NO", "PRI", nil, "auto_increment") // mock rows to be returned by the query
table_name := "user" // table name to be used in the test

mock.ExpectQuery(regexp.QuoteMeta(fmt.Sprintf(BigQuery_SCHEMA_QUERY, tableName))).WillReturnRows(mockRows) // set the expected return values for the query
// mock rows to be returned by the query
columns := []string{"column", "type"}
mockRows := sqlmock.NewRows(columns).AddRow("id", "int").AddRow("name", "varchar")
// set the expected return values for the query
expectedQuery := fmt.Sprintf("SELECT column_name, data_type FROM %s.INFORMATION_SCHEMA.COLUMNS WHERE table_name='%s'", "", table_name)
mock.ExpectQuery(regexp.QuoteMeta(expectedQuery)).WillReturnRows(mockRows)

// we then create a new instance of our BigQuery object and test the function
m, err := NewBigQuery(db)
// we then create a new instance of our Redshift object and test the function
b, err := NewBigQuery(db)
if err != nil {
t.Errorf("error initialising bigquery: %s", err)
t.Errorf("error initialising redshift: %s", err)
}
response, err := m.Schema(tableName) // call the Schema method
response, err := b.Schema(table_name) // call the Schema method
if err != nil {
t.Errorf("error executing query: %s", err)
t.Errorf("error executing query : %v", err)
}

fmt.Printf("Table schema : %+v\n", response)
fmt.Printf("Table schema: %+v\n", response)

// we make sure that all expectations were met, otherwise an error will be reported
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
t.Errorf("there was unfulfilled expectations: %s", err)
}

}

// TestTables is a unit test function that tests the Tables method of the BigQuery struct.
Expand Down Expand Up @@ -95,7 +98,6 @@ func TestTables(t *testing.T) {
}
}


// TestExecute is a unit test function that tests the Execute method of the BigQuery struct.
// It creates a mock instance of BigQuery, sets the expected return values, and calls the method under test.
// It then asserts the expected return values and checks if the method was called with the correct arguments.
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/thesaas-company/xray

go 1.22
go 1.22.2

toolchain go1.22.3

require (
github.com/DATA-DOG/go-sqlmock v1.5.2
Expand Down Expand Up @@ -47,6 +49,7 @@ require (
github.com/aws/smithy-go v1.14.2 // indirect
github.com/danieljoos/wincred v1.1.2 // indirect
github.com/dvsekhvalnov/jose2go v1.6.0 // indirect
github.com/elgs/gosqlcrud v0.0.0-20240405131937-de90abf1755a // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-logr/logr v1.4.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ github.com/dnaeon/go-vcr v1.2.0 h1:zHCHvJYTMh1N7xnV7zf1m1GPBF9Ad0Jk/whtQ1663qI=
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
github.com/dvsekhvalnov/jose2go v1.6.0 h1:Y9gnSnP4qEI0+/uQkHvFXeD2PLPJeXEL+ySMEA2EjTY=
github.com/dvsekhvalnov/jose2go v1.6.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU=
github.com/elgs/gosqlcrud v0.0.0-20240405131937-de90abf1755a h1:5fRodZBvUatkP8rF+eM0mnWfzQnsX4soUNBKpqmH+g8=
github.com/elgs/gosqlcrud v0.0.0-20240405131937-de90abf1755a/go.mod h1:1aQxDp0l1rY6JkTM6mhgsV0c3hfow5WlfWIaoeydhWc=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down

0 comments on commit 729a463

Please sign in to comment.