From 9117213b69c17e52a7dd88697cbef9645c9591e4 Mon Sep 17 00:00:00 2001 From: ankitmalikg2 Date: Wed, 18 Nov 2020 23:14:11 +0530 Subject: [PATCH] fix: logger_test linting issue fixed --- README.md | 6 +- api/v1/db.go | 4 +- .../production/config-production.json | 3 +- config-files/staging/config-staging.json | 3 +- config/config.go | 1 + initializer/initializer.go | 6 +- pkg/errors/error_test.go | 68 +++++----- pkg/logger/logger_test.go | 2 +- service/services/config-control.go | 8 +- service/services/config-control_test.go | 4 +- service/services/services.go | 121 ++++++------------ service/services/services_test.go | 26 ++-- service/services/stream.go | 10 +- service/spanner/spanner.go | 33 +---- service/spanner/spanner_test.go | 99 -------------- storage/spanner.go | 27 ---- storage/storage.go | 6 +- 17 files changed, 124 insertions(+), 303 deletions(-) delete mode 100644 service/spanner/spanner_test.go diff --git a/README.md b/README.md index 3895bb6..02b407e 100644 --- a/README.md +++ b/README.md @@ -70,12 +70,14 @@ Add the configuration in the given files: | ------ | ------ | | GoogleProjectID | Your Google Project ID | | SpannerDb | Your Spanner Database Name | +| QueryLimit | Default limit for data| For example: ``` { - "GoogleProjectID" : "first-project", - "SpannerDb" : "test-db" + "GoogleProjectID" : "first-project", + "SpannerDb" : "test-db", + "QueryLimit" : 5000 } ``` diff --git a/api/v1/db.go b/api/v1/db.go index f03f153..eb0dccc 100644 --- a/api/v1/db.go +++ b/api/v1/db.go @@ -180,7 +180,7 @@ func queryResponse(query models.Query, c *gin.Context) { } if query.Limit == 0 { - query.Limit = 5000 + query.Limit = config.ConfigurationMap.QueryLimit } query.ExpressionAttributeNames = ChangeColumnToSpannerExpressionName(query.TableName, query.ExpressionAttributeNames) query = ReplaceHashRangeExpr(query) @@ -550,7 +550,7 @@ func Update(c *gin.Context) { } } -// BatchWriteItem put & delte items in/from table +// BatchWriteItem put & delete items in/from table // @Description Batch Write Item for putting and deleting data in/from table // @Summary Batch Write Items from table // @ID batch-write-rows diff --git a/config-files/production/config-production.json b/config-files/production/config-production.json index e23192a..1ca5ab9 100644 --- a/config-files/production/config-production.json +++ b/config-files/production/config-production.json @@ -1,4 +1,5 @@ { "GoogleProjectID": "Your Google Project ID", - "SpannerDb": "Your Spanner Database Name" + "SpannerDb": "Your Spanner Database Name", + "QueryLimit": 5000 } \ No newline at end of file diff --git a/config-files/staging/config-staging.json b/config-files/staging/config-staging.json index e23192a..1ca5ab9 100644 --- a/config-files/staging/config-staging.json +++ b/config-files/staging/config-staging.json @@ -1,4 +1,5 @@ { "GoogleProjectID": "Your Google Project ID", - "SpannerDb": "Your Spanner Database Name" + "SpannerDb": "Your Spanner Database Name", + "QueryLimit": 5000 } \ No newline at end of file diff --git a/config/config.go b/config/config.go index b5d7731..443ec7e 100644 --- a/config/config.go +++ b/config/config.go @@ -32,6 +32,7 @@ import ( type Configuration struct { GoogleProjectID string SpannerDb string + QueryLimit int64 } var once sync.Once diff --git a/initializer/initializer.go b/initializer/initializer.go index 7fb5b1e..7f99035 100644 --- a/initializer/initializer.go +++ b/initializer/initializer.go @@ -24,11 +24,11 @@ import ( "github.com/cloudspannerecosystem/dynamodb-adapter/storage" ) -// InitAll - this will initiliaze all the project object -// Config, storage and all other global objects are initiliaze +// InitAll - this will initialize all the project object +// Config, storage and all other global objects are initialize func InitAll(box *rice.Box) error { config.InitConfig(box) - storage.InitliazeDriver() + storage.InitializeDriver() err := spanner.ParseDDL(true) if err != nil { return err diff --git a/pkg/errors/error_test.go b/pkg/errors/error_test.go index 233e3c8..4432fb2 100644 --- a/pkg/errors/error_test.go +++ b/pkg/errors/error_test.go @@ -14,36 +14,38 @@ package errors -// func TestInit(t *testing.T) { -// box, err := rice.FindBox("../../errors") -// assert.NoError(t, err) -// Init(box) -// } - -// func TestNew(t *testing.T) { -// e := New("E0001") -// assert.Error(t, e) -// code, err := HTTPResponse(e) -// assert.NotNil(t, err) -// assert.Equal(t, http.StatusInternalServerError, code) -// } - -// func TestHTTPResponse(t *testing.T) { -// e := New("E0001") -// assert.Error(t, e) -// code, err := e.HTTPResponse() -// assert.NotNil(t, err) -// assert.Equal(t, http.StatusInternalServerError, code) -// } - -// func TestNewForExceptionalError(t *testing.T) { -// e := New("E1001") -// assert.Error(t, e) -// } - -// func TestNewForSystemErrors(t *testing.T) { -// code, e := HTTPResponse(errors.New("Test")) -// assert.NotNil(t, e) -// assert.Equal(t, http.StatusInternalServerError, code) - -// } +import ( + "errors" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNew(t *testing.T) { + e := New("E0001") + assert.Error(t, e) + code, err := HTTPResponse(e, nil) + assert.NotNil(t, err) + assert.Equal(t, http.StatusBadRequest, code) +} + +func TestHTTPResponse(t *testing.T) { + e := New("E0001") + assert.Error(t, e) + code, err := e.HTTPResponse(nil) + assert.NotNil(t, err) + assert.Equal(t, http.StatusBadRequest, code) +} + +func TestNewForExceptionalError(t *testing.T) { + e := New("E1001") + assert.Error(t, e) +} + +func TestNewForSystemErrors(t *testing.T) { + code, e := HTTPResponse(errors.New("Test"), nil) + assert.NotNil(t, e) + assert.Equal(t, http.StatusInternalServerError, code) + +} diff --git a/pkg/logger/logger_test.go b/pkg/logger/logger_test.go index de4dbf4..d9a7897 100644 --- a/pkg/logger/logger_test.go +++ b/pkg/logger/logger_test.go @@ -26,6 +26,7 @@ func TestLogError(t *testing.T) { } LogError(err) } + func TestLogInfo(t *testing.T) { info := struct { Info string @@ -44,7 +45,6 @@ func TestLogDebug(t *testing.T) { LogDebug(info) } -// above shows the Benchmark for using LogError in Project func BenchmarkLogError(b *testing.B) { err := struct { Error string diff --git a/service/services/config-control.go b/service/services/config-control.go index 2377a27..0774c2c 100644 --- a/service/services/config-control.go +++ b/service/services/config-control.go @@ -139,10 +139,10 @@ func fetchConfigData() { func parseConfig(table string, config string, count int) { tokens := strings.Split(config, ",") - if tokens[0] == "1" { + if len(tokens) >= 1 && tokens[0] == "1" { models.ConfigController.ReadMap[table] = struct{}{} } - if tokens[1] == "1" { + if len(tokens) >= 2 && tokens[1] == "1" { models.ConfigController.WriteMap[table] = struct{}{} } if len(tokens) > 2 { @@ -158,8 +158,8 @@ func parseConfig(table string, config string, count int) { } } -// IsMyStreamEnabled checks if a table is enabaled for streaming or not -func IsMyStreamEnabled(tableName string) bool { +// IsStreamEnabled checks if a table is enabled for streaming or not +func IsStreamEnabled(tableName string) bool { models.ConfigController.Mux.RLock() defer models.ConfigController.Mux.RUnlock() _, ok := models.ConfigController.StreamEnable[tableName] diff --git a/service/services/config-control_test.go b/service/services/config-control_test.go index e04a6c6..931ddf1 100644 --- a/service/services/config-control_test.go +++ b/service/services/config-control_test.go @@ -33,7 +33,7 @@ func init() { } } -func TestIsMyStreamEnabled(t *testing.T) { +func TestIsStreamEnabled(t *testing.T) { tests := []struct { testName string tableName string @@ -62,7 +62,7 @@ func TestIsMyStreamEnabled(t *testing.T) { } for _, tc := range tests { - got := IsMyStreamEnabled(tc.tableName) + got := IsStreamEnabled(tc.tableName) assert.Equal(t, got, tc.want) } } diff --git a/service/services/services.go b/service/services/services.go index b81ba30..1373725 100644 --- a/service/services/services.go +++ b/service/services/services.go @@ -32,31 +32,6 @@ import ( "github.com/cloudspannerecosystem/dynamodb-adapter/utils" ) -// getProjections makes a projection map -func getProjections(projectionExpression string, expressionAttributeNames map[string]string, pKey string, sKey string) map[string]string { - expressionAttributes := expressionAttributeNames - if len(projectionExpression) == 0 { - return nil - } - projections := strings.Split(projectionExpression, ",") - projectionMap := make(map[string]string) - for _, pro := range projections { - pro = strings.TrimSpace(pro) - if val, ok := expressionAttributes[pro]; ok { - projectionMap[val] = val - } else { - projectionMap[pro] = pro - } - } - if _, ok := projectionMap[pKey]; !ok { - projectionMap[pKey] = pKey - } - if _, ok := projectionMap[sKey]; !ok { - projectionMap[sKey] = sKey - } - return projectionMap -} - // getSpannerProjections makes a projection array of columns func getSpannerProjections(projectionExpression, table string, expressionAttributeNames map[string]string) []string { if projectionExpression == "" { @@ -80,7 +55,7 @@ func getSpannerProjections(projectionExpression, table string, expressionAttribu return projectionCols } -//Put writes an object to Spanner +// Put writes an object to Spanner func Put(ctx context.Context, tableName string, putObj map[string]interface{}, expr *models.UpdateExpressionCondition, conditionExp string, expressionAttr, oldRes map[string]interface{}) (map[string]interface{}, error) { tableConf, err := config.GetTableConf(tableName) if err != nil { @@ -111,7 +86,7 @@ func Put(ctx context.Context, tableName string, putObj map[string]interface{}, e return updateResp, nil } -//Add checks the expression for converting the data +// Add checks the expression for converting the data func Add(ctx context.Context, tableName string, attrMap map[string]interface{}, condExpression string, m, expressionAttr map[string]interface{}, expr *models.UpdateExpressionCondition, oldRes map[string]interface{}) (map[string]interface{}, error) { tableConf, err := config.GetTableConf(tableName) if err != nil { @@ -142,7 +117,7 @@ func Add(ctx context.Context, tableName string, attrMap map[string]interface{}, return updateResp, nil } -//Del checks the expression for saving the data +// Del checks the expression for saving the data func Del(ctx context.Context, tableName string, attrMap map[string]interface{}, condExpression string, expressionAttr map[string]interface{}, expr *models.UpdateExpressionCondition) (map[string]interface{}, error) { logger.LogDebug(expressionAttr) tableConf, err := config.GetTableConf(tableName) @@ -195,13 +170,16 @@ func BatchGet(ctx context.Context, tableName string, keyMapArray []map[string]in return storage.GetStorageInstance().SpannerBatchGet(ctx, tableName, pValues, sValues, nil) } -//BatchPut writes bulk records to Spanner +// BatchPut writes bulk records to Spanner func BatchPut(ctx context.Context, tableName string, arrAttrMap []map[string]interface{}) error { if len(arrAttrMap) <= 0 { return errors.New("ValidationException") } - oldRes, _ := BatchGet(ctx, tableName, arrAttrMap) + oldRes, err := BatchGet(ctx, tableName, arrAttrMap) + if err != nil { + return err + } tableConf, err := config.GetTableConf(tableName) if err != nil { return err @@ -307,18 +285,10 @@ func QueryAttributes(ctx context.Context, query models.Query) (map[string]interf } else { finalResp["LastEvaluatedKey"] = map[string]interface{}{"offset": originalLimit + offset, pKey: last[pKey], tPKey: last[tPKey]} } - if query.StartFrom != nil { - finalResp["Items"] = resp[:length-1] - } else { - finalResp["Items"] = resp[:length-1] - } + finalResp["Items"] = resp[:length-1] } else { - if query.StartFrom != nil { - if length-1 == 1 { - finalResp["Items"] = resp[0] - } else { - finalResp["Items"] = resp - } + if query.StartFrom != nil && length-1 == 1 { + finalResp["Items"] = resp } else { finalResp["Items"] = resp } @@ -362,14 +332,14 @@ func parseSpannerColumns(query *models.Query, tPkey, pKey, sKey string) ([]strin var cols []string if query.ProjectionExpression != "" { cols = getSpannerProjections(query.ProjectionExpression, query.TableName, query.ExpressionAttributeNames) - insertpKey := true + insertPKey := true for i := 0; i < len(cols); i++ { if cols[i] == pKey { - insertpKey = false + insertPKey = false break } } - if insertpKey { + if insertPKey { cols = append(cols, pKey) } if sKey != "" { @@ -432,40 +402,11 @@ func parseSpannerCondition(query *models.Query, pKey, sKey string) (string, map[ } if query.RangeExp != "" { - if whereClause != "WHERE " { - whereClause += " AND " - } - _, _, query.RangeExp = utils.ParseBeginsWith(query.RangeExp) - query.RangeExp = strings.ReplaceAll(query.RangeExp, "begins_with", "STARTS_WITH") - - count := 1 - for k, v := range query.RangeValMap { - if strings.Contains(query.RangeExp, k) { - str := "rangeExp" + strconv.Itoa(count) - query.RangeExp = strings.ReplaceAll(query.RangeExp, k, "@"+str) - params[str] = v - count++ - } - } - whereClause += query.RangeExp + whereClause, query.RangeExp = createWhereClause(whereClause, query.RangeExp, "rangeExp", query.RangeValMap, params) } if query.FilterExp != "" { - if whereClause != "WHERE " { - whereClause += " AND " - } - _, _, query.FilterExp = utils.ParseBeginsWith(query.FilterExp) - query.FilterExp = strings.ReplaceAll(query.FilterExp, "begins_with", "STARTS_WITH") - count := 1 - for k, v := range query.RangeValMap { - if strings.Contains(query.FilterExp, k) { - str := "filter" + strconv.Itoa(count) - query.FilterExp = strings.ReplaceAll(query.FilterExp, k, "@"+str) - params[str] = v - count++ - } - } - whereClause += query.FilterExp + whereClause, query.FilterExp = createWhereClause(whereClause, query.FilterExp, "filterExp", query.RangeValMap, params) } if whereClause == "WHERE " { @@ -474,6 +415,26 @@ func parseSpannerCondition(query *models.Query, pKey, sKey string) (string, map[ return whereClause, params } +func createWhereClause(whereClause string, expression string, queryVar string, RangeValueMap map[string]interface{}, params map[string]interface{}) (string, string) { + _, _, expression = utils.ParseBeginsWith(expression) + expression = strings.ReplaceAll(expression, "begins_with", "STARTS_WITH") + + if whereClause != "WHERE " { + whereClause += " AND " + } + count := 1 + for k, v := range RangeValueMap { + if strings.Contains(expression, k) { + str := queryVar + strconv.Itoa(count) + expression = strings.ReplaceAll(expression, k, "@"+str) + params[str] = v + count++ + } + } + whereClause += expression + return whereClause, expression +} + func parseOffset(query *models.Query) (string, int64) { logger.LogDebug(query) if query.StartFrom != nil { @@ -535,7 +496,7 @@ func BatchGetWithProjection(ctx context.Context, tableName string, keyMapArray [ return storage.GetStorageInstance().SpannerBatchGet(ctx, tableName, pValues, sValues, projectionCols) } -//Delete service +// Delete service func Delete(ctx context.Context, tableName string, primaryKeyMap map[string]interface{}, condExpression string, attrMap map[string]interface{}, expr *models.UpdateExpressionCondition) error { tableConf, err := config.GetTableConf(tableName) if err != nil { @@ -549,7 +510,7 @@ func Delete(ctx context.Context, tableName string, primaryKeyMap map[string]inte return storage.GetStorageInstance().SpannerDelete(ctx, tableName, primaryKeyMap, e, expr) } -//BatchDelete service +// BatchDelete service func BatchDelete(ctx context.Context, tableName string, keyMapArray []map[string]interface{}) error { tableConf, err := config.GetTableConf(tableName) if err != nil { @@ -577,13 +538,13 @@ func BatchDelete(ctx context.Context, tableName string, keyMapArray []map[string return nil } -//Scan service +// Scan service func Scan(ctx context.Context, scanData models.ScanMeta) (map[string]interface{}, error) { query := models.Query{} query.TableName = scanData.TableName query.Limit = scanData.Limit if query.Limit == 0 { - query.Limit = 5000 + query.Limit = config.ConfigurationMap.QueryLimit } query.StartFrom = scanData.StartFrom query.RangeValMap = scanData.ExpressionAttributeMap @@ -607,7 +568,7 @@ func scanSpanerTable(ctx context.Context, tableName, pKey, sKey string) ([]map[s var result []map[string]interface{} query := models.Query{} query.TableName = tableName - var originalLimit int64 = 5000 + var originalLimit int64 = config.ConfigurationMap.QueryLimit query.Limit = originalLimit + 1 for { query.StartFrom = startFrom diff --git a/service/services/services_test.go b/service/services/services_test.go index 1becb49..aa61e89 100644 --- a/service/services/services_test.go +++ b/service/services/services_test.go @@ -286,9 +286,9 @@ func Test_createSpannerQuery(t *testing.T) { "first", "second", spanner.Statement{ - SQL: "SELECT testTable.`first`,testTable.`second` FROM testTable WHERE second is not null AND fourth > @filter1 ORDER BY second DESC LIMIT 5000 ", + SQL: "SELECT testTable.`first`,testTable.`second` FROM testTable WHERE second is not null AND fourth > @filterExp1 ORDER BY second DESC LIMIT 5000 ", Params: map[string]interface{}{ - "filter1": float64(5), + "filterExp1": float64(5), }, }, []string{"first", "second"}, @@ -312,10 +312,10 @@ func Test_createSpannerQuery(t *testing.T) { "first", "second", spanner.Statement{ - SQL: "SELECT testTable.`first`,testTable.`second` FROM testTable WHERE second is not null AND first > @rangeExp1 AND fourth > @filter1 ORDER BY second DESC LIMIT 5000 ", + SQL: "SELECT testTable.`first`,testTable.`second` FROM testTable WHERE second is not null AND first > @rangeExp1 AND fourth > @filterExp1 ORDER BY second DESC LIMIT 5000 ", Params: map[string]interface{}{ - "filter1": float64(5), - "rangeExp1": float64(4), + "filterExp1": float64(5), + "rangeExp1": float64(4), }, }, []string{"first", "second"}, @@ -340,10 +340,10 @@ func Test_createSpannerQuery(t *testing.T) { "first", "second", spanner.Statement{ - SQL: "SELECT testTable.`first`,testTable.`second` FROM testTable WHERE second is not null AND first > @rangeExp1 AND fourth > @filter1 ORDER BY second DESC LIMIT 100", + SQL: "SELECT testTable.`first`,testTable.`second` FROM testTable WHERE second is not null AND first > @rangeExp1 AND fourth > @filterExp1 ORDER BY second DESC LIMIT 100", Params: map[string]interface{}{ - "filter1": float64(5), - "rangeExp1": float64(4), + "filterExp1": float64(5), + "rangeExp1": float64(4), }, }, []string{"first", "second"}, @@ -608,9 +608,9 @@ func Test_parseSpannerCondition(t *testing.T) { }, "first", "second", - "WHERE second is not null AND fourth = @filter1", + "WHERE second is not null AND fourth = @filterExp1", map[string]interface{}{ - "filter1": float64(61), + "filterExp1": float64(61), }, }, { @@ -626,10 +626,10 @@ func Test_parseSpannerCondition(t *testing.T) { }, "first", "second", - "WHERE second is not null AND fourth = @rangeExp1 AND fourth = @filter1", + "WHERE second is not null AND fourth = @rangeExp1 AND fourth = @filterExp1", map[string]interface{}{ - "filter1": float64(34), - "rangeExp1": float64(61), + "filterExp1": float64(34), + "rangeExp1": float64(61), }, }, } diff --git a/service/services/stream.go b/service/services/stream.go index 29eae12..01bf60b 100644 --- a/service/services/stream.go +++ b/service/services/stream.go @@ -42,7 +42,7 @@ func InitStream() { // StreamDataToThirdParty for streaming data to any third party source func StreamDataToThirdParty(oldImage, newImage map[string]interface{}, tableName string) { - if !IsMyStreamEnabled(tableName) { + if !IsStreamEnabled(tableName) { return } if (oldImage == nil || len(oldImage) == 0) && newImage == nil || len(newImage) == 0 { @@ -86,6 +86,7 @@ func connectors(streamObj *models.StreamDataModel) { } func pubsubPublish(streamObj *models.StreamDataModel) { + var err error topicName, status := IsPubSubAllowed(streamObj.Table) if !status { return @@ -99,8 +100,11 @@ func pubsubPublish(streamObj *models.StreamDataModel) { mClients[topicName] = topic } message := &pubsub.Message{} - message.Data, _ = json.Marshal(streamObj) - _, err := topic.Publish(context.Background(), message).Get(ctx) + message.Data, err = json.Marshal(streamObj) + if err != nil { + logger.LogError(err) + } + _, err = topic.Publish(context.Background(), message).Get(ctx) if err != nil { logger.LogError(err) } diff --git a/service/spanner/spanner.go b/service/spanner/spanner.go index 1599e27..6f9b027 100644 --- a/service/spanner/spanner.go +++ b/service/spanner/spanner.go @@ -35,15 +35,14 @@ var specialCharRg = regexp.MustCompile("[" + ss + "]+") // global map object which is used to read and write data into spanner tables func ParseDDL(updateDB bool) error { - stmt1 := spanner.Statement{} - stmt1.SQL = "SELECT * FROM dynamodb_adapter_table_ddl" - ms, err := storage.GetStorageInstance().ExecuteSpannerQuery(context.Background(), "dynamodb_adapter_table_ddl", []string{"tableName", "column", "dataType", "originalColumn"}, false, stmt1) + stmt := spanner.Statement{} + stmt.SQL = "SELECT * FROM dynamodb_adapter_table_ddl" + ms, err := storage.GetStorageInstance().ExecuteSpannerQuery(context.Background(), "dynamodb_adapter_table_ddl", []string{"tableName", "column", "dataType", "originalColumn"}, false, stmt) if err != nil { return err } - dataAvailable := len(ms) > 0 - if dataAvailable { + if len(ms) > 0 { for i := 0; i < len(ms); i++ { tableName := ms[i]["tableName"].(string) column := ms[i]["column"].(string) @@ -69,27 +68,3 @@ func ParseDDL(updateDB bool) error { } return nil } - -func getColNameAndType(stmt string) (string, string) { - if stmt == "" { - return "", "" - } - stmt = strings.TrimSpace(stmt) - tokens := strings.Split(stmt, " ") - if len(tokens) < 2 { - return "", "" - } - tokens[0] = strings.Trim(tokens[0], "`") - return tokens[0], tokens[1] -} - -func getTableName(stmt string) string { - if stmt == "" { - return "" - } - tokens := strings.Split(stmt, " ") - if len(tokens) < 3 { - return "" - } - return tokens[2] -} diff --git a/service/spanner/spanner_test.go b/service/spanner/spanner_test.go deleted file mode 100644 index 6bc148c..0000000 --- a/service/spanner/spanner_test.go +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright 2020 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package spanner - -import ( - "testing" - - "gopkg.in/go-playground/assert.v1" -) - -func Test_getColNameAndType(t *testing.T) { - tests := []struct { - inputStr string - want1 string - want2 string - }{ - { - "", - "", - "", - }, - { - "oneValue", - "", - "", - }, - { - "name STRING", - "name", - "STRING", - }, - { - "subjects ARRAY", - "subjects", - "ARRAY", - }, - { - "date_of_birth DATE", - "date_of_birth", - "DATE", - }, - { - "Available BOOL", - "Available", - "BOOL", - }, - } - - for _, tc := range tests { - got1, got2 := getColNameAndType(tc.inputStr) - assert.Equal(t, got1, tc.want1) - assert.Equal(t, got2, tc.want2) - } -} - -func Test_getTableName(t *testing.T) { - tests := []struct { - inputStr string - want string - }{ - { - "", - "", - }, - { - "oneValue", - "", - }, - { - "two value", - "", - }, - { - "three value here", - "here", - }, - { - "create table Employee", - "Employee", - }, - } - - for _, tc := range tests { - got := getTableName(tc.inputStr) - assert.Equal(t, got, tc.want) - } -} diff --git a/storage/spanner.go b/storage/spanner.go index e3f40d6..52cd90a 100755 --- a/storage/spanner.go +++ b/storage/spanner.go @@ -33,39 +33,12 @@ import ( "github.com/cloudspannerecosystem/dynamodb-adapter/utils" "cloud.google.com/go/spanner" - database "cloud.google.com/go/spanner/admin/database/apiv1" "github.com/ahmetb/go-linq" "google.golang.org/api/iterator" - databasepb "google.golang.org/genproto/googleapis/spanner/admin/database/v1" ) var base64Regexp = regexp.MustCompile("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$") -// ReadDatabaseSchema returns array of statments to create table -func ReadDatabaseSchema() ([]string, error) { - ctx := context.Background() - cli, err := database.NewDatabaseAdminClient(ctx) - if err != nil { - return nil, errors.New(err.Error()) - } - defer cli.Close() - m := map[string]struct{}{} - for _, v := range models.SpannerTableMap { - m[v] = struct{}{} - } - var statments []string - for k := range m { - req := &databasepb.GetDatabaseDdlRequest{} - req.Database = "projects/" + config.ConfigurationMap.GoogleProjectID + "/instances/" + k + "/databases/" + config.ConfigurationMap.SpannerDb - ddlResp, err := cli.GetDatabaseDdl(ctx, req) - if err != nil { - return nil, errors.New(err.Error()) - } - statments = append(statments, ddlResp.GetStatements()...) - } - return statments, nil -} - // SpannerBatchGet - fetch all rows func (s Storage) SpannerBatchGet(ctx context.Context, tableName string, pKeys, sKeys []interface{}, projectionCols []string) ([]map[string]interface{}, error) { var keySet []spanner.KeySet diff --git a/storage/storage.go b/storage/storage.go index 90b2266..85c102b 100755 --- a/storage/storage.go +++ b/storage/storage.go @@ -69,8 +69,8 @@ func initSpannerDriver(instance string, m map[string]*gjson.Result) *spanner.Cli return Client } -// InitliazeDriver - this will Initliaze databases object in global map -func InitliazeDriver() { +// InitializeDriver - this will Initialize databases object in global map +func InitializeDriver() { storage = new(Storage) storage.spannerClient = make(map[string]*spanner.Client) @@ -100,7 +100,7 @@ var once sync.Once func GetStorageInstance() *Storage { once.Do(func() { if storage == nil { - InitliazeDriver() + InitializeDriver() } })