Skip to content

Commit

Permalink
corrected few nits
Browse files Browse the repository at this point in the history
  • Loading branch information
NirnayaSindhuSuthari committed Aug 6, 2024
1 parent 69a2860 commit 51cb3a9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func GetOrCreateClient(ctx context.Context, dbURI string) (*sp.Client, error) {
spannermetadataClient, err = newClient(ctx, dbURI)
})
if err != nil {
return nil, fmt.Errorf("failed to create spanner database client: %v", err)
return nil, fmt.Errorf("failed to create spanner metadata database client: %v", err)
}
return spannermetadataClient, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func TestGetOrCreateClient_Basic(t *testing.T) {
newClient = func(ctx context.Context, database string, opts ...option.ClientOption) (*sp.Client, error) {
return &sp.Client{}, nil
}
c, err := GetOrCreateClient(ctx, "testURI")
assert.NotNil(t, c)
client, err := GetOrCreateClient(ctx, "testURI")
assert.NotNil(t, client)
assert.Nil(t, err)
}

Expand All @@ -63,17 +63,17 @@ func TestGetOrCreateClient_OnlyOnceViaSync(t *testing.T) {
newClient = func(ctx context.Context, database string, opts ...option.ClientOption) (*sp.Client, error) {
return &sp.Client{}, nil
}
c, err := GetOrCreateClient(ctx, "testURI")
assert.NotNil(t, c)
client, err := GetOrCreateClient(ctx, "testURI")
assert.NotNil(t, client)
assert.Nil(t, err)
// Explicitly set the client to nil. Running GetOrCreateClient should not create a
// new client since sync would already be executed.
spannermetadataClient = nil
newClient = func(ctx context.Context, database string, opts ...option.ClientOption) (*sp.Client, error) {
return nil, fmt.Errorf("test error")
}
c, err = GetOrCreateClient(ctx, "testURI")
assert.Nil(t, c)
client, err = GetOrCreateClient(ctx, "testURI")
assert.Nil(t, client)
assert.Nil(t, err)
}

Expand Down Expand Up @@ -110,7 +110,7 @@ func TestGetOrCreateClient_Error(t *testing.T) {
newClient = func(ctx context.Context, database string, opts ...option.ClientOption) (*sp.Client, error) {
return nil, fmt.Errorf("test error")
}
c, err := GetOrCreateClient(ctx, "testURI")
assert.Nil(t, c)
client, err := GetOrCreateClient(ctx, "testURI")
assert.Nil(t, client)
assert.NotNil(t, err)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ import (
)

type SpannerMetadataAccessor interface {
// IsSpannerSupportedStatement checks whether the statement from Source database is supported by Spanner or not.
// IsSpannerSupportedStatement checks if the given statement is supported by Spanner.
IsSpannerSupportedStatement(SpProjectId string, SpInstanceId string, statement string, columntype string) bool
// isValidSpannerStatement fires query to spanner to cast statement based on spanner column type.
isValidSpannerStatement(db string, defaultval string, ty string) error
// isValidSpannerStatement queries spanner and checks if statement evaluates to a data corresponding to given type.
isValidSpannerStatement(db string, defaultval string, datatype string) error
}

type SpannerMetadataAccessorImpl struct{}

func (spm *SpannerMetadataAccessorImpl) IsSpannerSupportedStatement(SpProjectId string, SpInstanceId string, statement string, columntype string) bool {
db := getSpannerUri(SpProjectId, SpInstanceId)
db := getSpannerMetadataDbUri(SpProjectId, SpInstanceId)
if SpProjectId == "" || SpInstanceId == "" {
return false
}
Expand All @@ -44,18 +44,18 @@ func (spm *SpannerMetadataAccessorImpl) IsSpannerSupportedStatement(SpProjectId
return true
}
}
func (spm *SpannerMetadataAccessorImpl) isValidSpannerStatement(db string, statement string, ty string) error {
func (spm *SpannerMetadataAccessorImpl) isValidSpannerStatement(db string, statement string, datatype string) error {
ctx := context.Background()
spmClient, err := spannermetadataclient.GetOrCreateClient(ctx, db)
if err != nil {
return err
}

if spmClient == nil {
return fmt.Errorf("Client is nil")
return fmt.Errorf("spannerMetadataClient is nil")
}
stmt := spanner.Statement{
SQL: "SELECT CAST(" + statement + " AS " + ty + ") AS ConvertedDefaultval",
SQL: "SELECT CAST(" + statement + " AS " + datatype + ") AS statementValue",
}
iter := spmClient.Single().Query(ctx, stmt)
defer iter.Stop()
Expand All @@ -71,6 +71,6 @@ func (spm *SpannerMetadataAccessorImpl) isValidSpannerStatement(db string, state
}
}

func getSpannerUri(projectId string, instanceId string) string {
func getSpannerMetadataDbUri(projectId string, instanceId string) string {
return fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectId, instanceId, constants.METADATA_DB)
}
7 changes: 5 additions & 2 deletions sources/common/toddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,17 @@ func (ss *SchemaToSpannerImpl) SchemaToSpannerDDLHelper(SpProjectId string, SpIn
}

defaultVal := ddl.DefaultValue{
IsPresent: srcCol.DefaultValue.IsPresent,
Value: srcCol.DefaultValue.Value,
IsPresent: false,
Value: "",
}

if srcCol.DefaultValue.IsPresent {
spM := spannermetadataaccessor.SpannerMetadataAccessorImpl{}

Check failure on line 155 in sources/common/toddl.go

View workflow job for this annotation

GitHub Actions / coverage

srcCol.DefaultValue undefined (type schema.Column has no field or method DefaultValue)

Check failure on line 155 in sources/common/toddl.go

View workflow job for this annotation

GitHub Actions / lint

srcCol.DefaultValue undefined (type schema.Column has no field or method DefaultValue)

Check failure on line 155 in sources/common/toddl.go

View workflow job for this annotation

GitHub Actions / integration-tests

srcCol.DefaultValue undefined (type schema.Column has no field or method DefaultValue)
defaultVal.IsPresent = spM.IsSpannerSupportedStatement(SpProjectId, SpInstanceId, srcCol.DefaultValue.Value, ty.Name)
}

Check failure on line 157 in sources/common/toddl.go

View workflow job for this annotation

GitHub Actions / coverage

srcCol.DefaultValue undefined (type schema.Column has no field or method DefaultValue)

Check failure on line 157 in sources/common/toddl.go

View workflow job for this annotation

GitHub Actions / lint

srcCol.DefaultValue undefined (type schema.Column has no field or method DefaultValue)

Check failure on line 157 in sources/common/toddl.go

View workflow job for this annotation

GitHub Actions / integration-tests

srcCol.DefaultValue undefined (type schema.Column has no field or method DefaultValue)
if defaultVal.IsPresent {
defaultVal.Value = srcCol.DefaultValue.Value
}

Check failure on line 160 in sources/common/toddl.go

View workflow job for this annotation

GitHub Actions / coverage

srcCol.DefaultValue undefined (type schema.Column has no field or method DefaultValue)

Check failure on line 160 in sources/common/toddl.go

View workflow job for this annotation

GitHub Actions / lint

srcCol.DefaultValue undefined (type schema.Column has no field or method DefaultValue)

Check failure on line 160 in sources/common/toddl.go

View workflow job for this annotation

GitHub Actions / integration-tests

srcCol.DefaultValue undefined (type schema.Column has no field or method DefaultValue)

spColDef[srcColId] = ddl.ColumnDef{
Name: colName,
Expand Down
2 changes: 1 addition & 1 deletion spanner/ddl/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func (cd ColumnDef) PrintColumnDef(c Config) (string, string) {
s += " NOT NULL "
}
if cd.DefaultValue.IsPresent {
s += " DEFAULT (" + cd.DefaultValue.Value + ") "
s += " DEFAULT (CAST(" + (cd.DefaultValue.Value) + " as " +(cd.T.Name)+ ")) "
}
s += cd.AutoGen.PrintAutoGenCol()
}
Expand Down

0 comments on commit 51cb3a9

Please sign in to comment.