Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
asthamohta committed Dec 26, 2024
1 parent f6f128c commit 70c53eb
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 63 deletions.
12 changes: 11 additions & 1 deletion cmd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/GoogleCloudPlatform/spanner-migration-tool/common/constants"
"github.com/GoogleCloudPlatform/spanner-migration-tool/common/utils"
"github.com/GoogleCloudPlatform/spanner-migration-tool/conversion"
"github.com/GoogleCloudPlatform/spanner-migration-tool/expressions_api"
"github.com/GoogleCloudPlatform/spanner-migration-tool/internal"
"github.com/GoogleCloudPlatform/spanner-migration-tool/logger"
"github.com/GoogleCloudPlatform/spanner-migration-tool/proto/migration"
Expand Down Expand Up @@ -134,7 +135,16 @@ func (cmd *SchemaCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...interfa
return subcommands.ExitFailure
}
} else {
conv, err = convImpl.SchemaConv(cmd.project, sourceProfile, targetProfile, &ioHelper, &conversion.SchemaFromSourceImpl{})
ctx := context.Background()
ddlVerifier, err := expressions_api.NewDDLVerifierImpl(ctx, conv.SpProjectId, conv.SpInstanceId)
if err != nil {
logger.Log.Error(fmt.Sprintf("error trying create ddl verifier: %v", err))
return subcommands.ExitFailure
}
sfs := &conversion.SchemaFromSourceImpl{
DdlVerifier: ddlVerifier,
}
conv, err = convImpl.SchemaConv(cmd.project, sourceProfile, targetProfile, &ioHelper, sfs)
if err != nil {
return subcommands.ExitFailure
}
Expand Down
11 changes: 10 additions & 1 deletion cmd/schema_and_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/GoogleCloudPlatform/spanner-migration-tool/common/constants"
"github.com/GoogleCloudPlatform/spanner-migration-tool/common/utils"
"github.com/GoogleCloudPlatform/spanner-migration-tool/conversion"
"github.com/GoogleCloudPlatform/spanner-migration-tool/expressions_api"
"github.com/GoogleCloudPlatform/spanner-migration-tool/internal"
"github.com/GoogleCloudPlatform/spanner-migration-tool/logger"
"github.com/GoogleCloudPlatform/spanner-migration-tool/profiles"
Expand Down Expand Up @@ -136,7 +137,15 @@ func (cmd *SchemaAndDataCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...
dbURI string
)
convImpl := &conversion.ConvImpl{}
conv, err = convImpl.SchemaConv(cmd.project, sourceProfile, targetProfile, &ioHelper, &conversion.SchemaFromSourceImpl{})
ddlVerifier, err := expressions_api.NewDDLVerifierImpl(ctx, conv.SpProjectId, conv.SpInstanceId)
if err != nil {
logger.Log.Error(fmt.Sprintf("error trying create ddl verifier: %v", err))
return subcommands.ExitFailure
}
sfs := &conversion.SchemaFromSourceImpl{
DdlVerifier: ddlVerifier,
}
conv, err = convImpl.SchemaConv(cmd.project, sourceProfile, targetProfile, &ioHelper, sfs)
if err != nil {
panic(err)
}
Expand Down
11 changes: 4 additions & 7 deletions conversion/conversion_from_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ type SchemaFromSourceInterface interface {
SchemaFromDump(SpProjectId string, SpInstanceId string, driver string, spDialect string, ioHelper *utils.IOStreams, processDump ProcessDumpByDialectInterface) (*internal.Conv, error)
}

type SchemaFromSourceImpl struct{}
type SchemaFromSourceImpl struct {
DdlVerifier expressions_api.DDLVerifier
}

type DataFromSourceInterface interface {
dataFromDatabase(ctx context.Context, migrationProjectId string, sourceProfile profiles.SourceProfile, targetProfile profiles.TargetProfile, config writer.BatchWriterConfig, conv *internal.Conv, client *sp.Client, getInfo GetInfoInterface, dataFromDb DataFromDatabaseInterface, snapshotMigration SnapshotMigrationInterface) (*writer.BatchWriter, error)
Expand Down Expand Up @@ -100,13 +102,8 @@ func (sads *SchemaFromSourceImpl) schemaFromDatabase(migrationProjectId string,
additionalSchemaAttributes := internal.AdditionalSchemaAttributes{
IsSharded: isSharded,
}
ctx := context.Background()
ddlVerifier, err := expressions_api.NewDDLVerifierImpl(ctx, conv.SpProjectId, conv.SpInstanceId)
if err != nil {
return conv, fmt.Errorf("error trying create ddl verifier: %v", err)
}
schemaToSpanner := common.SchemaToSpannerImpl{
DdlV: ddlVerifier,
DdlV: sads.DdlVerifier,
}
return conv, processSchema.ProcessSchema(conv, infoSchema, common.DefaultWorkers, additionalSchemaAttributes, &schemaToSpanner, &common.UtilsOrderImpl{}, &common.InfoSchemaImpl{})
}
Expand Down
106 changes: 54 additions & 52 deletions conversion/conversion_from_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import (
"fmt"
"testing"

"github.com/GoogleCloudPlatform/spanner-migration-tool/expressions_api"
"github.com/GoogleCloudPlatform/spanner-migration-tool/profiles"
"github.com/GoogleCloudPlatform/spanner-migration-tool/sources/common"
"github.com/GoogleCloudPlatform/spanner-migration-tool/sources/mysql"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)


func TestSchemaFromDatabase(t *testing.T) {
targetProfile := profiles.TargetProfile{
Conn: profiles.TargetProfileConnection{
Expand Down Expand Up @@ -65,88 +65,88 @@ func TestSchemaFromDatabase(t *testing.T) {
sourceProfileCloudDefault := profiles.SourceProfile{}
// Avoid getting/setting env variables in the unit tests.
testCases := []struct {
name string
sourceProfile profiles.SourceProfile
getInfoError error
processSchemaError error
errorExpected bool
name string
sourceProfile profiles.SourceProfile
getInfoError error
processSchemaError error
errorExpected bool
}{
{
name: "successful source profile config for bulk migration",
sourceProfile: sourceProfileConfigBulk,
getInfoError: nil,
name: "successful source profile config for bulk migration",
sourceProfile: sourceProfileConfigBulk,
getInfoError: nil,
processSchemaError: nil,
errorExpected: false,
errorExpected: false,
},
{
name: "source profile config for bulk migration: get info error",
sourceProfile: sourceProfileConfigBulk,
getInfoError: fmt.Errorf("error"),
name: "source profile config for bulk migration: get info error",
sourceProfile: sourceProfileConfigBulk,
getInfoError: fmt.Errorf("error"),
processSchemaError: nil,
errorExpected: true,
errorExpected: true,
},
{
name: "source profile config for bulk migration: process schema error",
sourceProfile: sourceProfileConfigBulk,
getInfoError: nil,
name: "source profile config for bulk migration: process schema error",
sourceProfile: sourceProfileConfigBulk,
getInfoError: nil,
processSchemaError: fmt.Errorf("error"),
errorExpected: true,
errorExpected: true,
},
{
name: "successful source profile config for dataflow migration",
sourceProfile: sourceProfileConfigDataflow,
getInfoError: nil,
name: "successful source profile config for dataflow migration",
sourceProfile: sourceProfileConfigDataflow,
getInfoError: nil,
processSchemaError: nil,
errorExpected: false,
errorExpected: false,
},
{
name: "source profile config for dataflow migration: get info error",
sourceProfile: sourceProfileConfigDataflow,
getInfoError: fmt.Errorf("error"),
name: "source profile config for dataflow migration: get info error",
sourceProfile: sourceProfileConfigDataflow,
getInfoError: fmt.Errorf("error"),
processSchemaError: nil,
errorExpected: true,
errorExpected: true,
},
{
name: "source profile config for dms migration",
sourceProfile: sourceProfileConfigDms,
getInfoError: nil,
name: "source profile config for dms migration",
sourceProfile: sourceProfileConfigDms,
getInfoError: nil,
processSchemaError: nil,
errorExpected: true,
errorExpected: true,
},
{
name: "invalid source profile config",
sourceProfile: sourceProfileConfigInvalid,
getInfoError: nil,
name: "invalid source profile config",
sourceProfile: sourceProfileConfigInvalid,
getInfoError: nil,
processSchemaError: nil,
errorExpected: true,
errorExpected: true,
},
{
name: "successful source profile cloud sql",
sourceProfile: sourceProfileCloudSql,
getInfoError: nil,
name: "successful source profile cloud sql",
sourceProfile: sourceProfileCloudSql,
getInfoError: nil,
processSchemaError: nil,
errorExpected: false,
errorExpected: false,
},
{
name: "source profile cloud sql: get info error",
sourceProfile: sourceProfileCloudSql,
getInfoError: fmt.Errorf("error"),
name: "source profile cloud sql: get info error",
sourceProfile: sourceProfileCloudSql,
getInfoError: fmt.Errorf("error"),
processSchemaError: nil,
errorExpected: true,
errorExpected: true,
},
{
name: "successful source profile default",
sourceProfile: sourceProfileCloudDefault,
getInfoError: nil,
name: "successful source profile default",
sourceProfile: sourceProfileCloudDefault,
getInfoError: nil,
processSchemaError: nil,
errorExpected: false,
errorExpected: false,
},
{
name: "source profile default: get info error",
sourceProfile: sourceProfileCloudDefault,
getInfoError: fmt.Errorf("error"),
name: "source profile default: get info error",
sourceProfile: sourceProfileCloudDefault,
getInfoError: fmt.Errorf("error"),
processSchemaError: nil,
errorExpected: true,
errorExpected: true,
},
}

Expand All @@ -159,8 +159,10 @@ func TestSchemaFromDatabase(t *testing.T) {
gim.On("GetInfoSchema", "migration-project-id", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(mysql.InfoSchemaImpl{}, tc.getInfoError)
ps.On("ProcessSchema", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tc.processSchemaError)

s := SchemaFromSourceImpl{}
s := SchemaFromSourceImpl{
DdlVerifier: &expressions_api.MockDDLVerifier{},
}
_, err := s.schemaFromDatabase("migration-project-id", tc.sourceProfile, targetProfile, &gim, &ps)
assert.Equal(t, tc.errorExpected, err != nil, tc.name)
}
}
}
2 changes: 0 additions & 2 deletions expressions_api/expression_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ type ExpressionVerificationAccessorImpl struct {
func NewExpressionVerificationAccessorImpl(ctx context.Context, project string, instance string) (*ExpressionVerificationAccessorImpl, error) {
var spannerAccessor *spanneraccessor.SpannerAccessorImpl
var err error
fmt.Printf("#######")
fmt.Printf(fmt.Sprintf("projects/%s/instances/%s/databases/%s", project, instance, constants.TEMP_DB))
if project != "" && instance != "" {
spannerAccessor, err = spanneraccessor.NewSpannerAccessorClientImplWithSpannerClient(ctx, fmt.Sprintf("projects/%s/instances/%s/databases/%s", project, instance, constants.TEMP_DB))
if err != nil {
Expand Down

0 comments on commit 70c53eb

Please sign in to comment.