15
15
package assessment
16
16
17
17
import (
18
+ "context"
19
+ "fmt"
20
+
18
21
assessment "github.com/GoogleCloudPlatform/spanner-migration-tool/assessment/collectors"
19
22
"github.com/GoogleCloudPlatform/spanner-migration-tool/assessment/utils"
20
23
"github.com/GoogleCloudPlatform/spanner-migration-tool/internal"
21
24
"github.com/GoogleCloudPlatform/spanner-migration-tool/logger"
22
25
"github.com/GoogleCloudPlatform/spanner-migration-tool/profiles"
26
+ "go.uber.org/zap"
23
27
)
24
28
25
29
type assessmentCollectors struct {
26
- sampleCollector assessment.SampleCollector
27
- infoSchemaCollector assessment.InfoSchemaCollector
30
+ sampleCollector * assessment.SampleCollector
31
+ infoSchemaCollector * assessment.InfoSchemaCollector
32
+ appAssessmentCollector * assessment.MigrationSummarizer
28
33
}
29
34
30
- func PerformAssessment (conv * internal.Conv , sourceProfile profiles.SourceProfile ) (utils.AssessmentOutput , error ) {
35
+ func PerformAssessment (conv * internal.Conv , sourceProfile profiles.SourceProfile , assessmentConfig map [ string ] string , projectId string ) (utils.AssessmentOutput , error ) {
31
36
32
37
logger .Log .Info ("performing assessment" )
38
+ logger .Log .Info (fmt .Sprintf ("assessment config %+v" , assessmentConfig ))
39
+ logger .Log .Info (fmt .Sprintf ("project id %+v" , projectId ))
40
+
41
+ ctx := context .Background ()
33
42
34
43
output := utils.AssessmentOutput {}
35
44
// Initialize collectors
36
- c , err := initializeCollectors (conv , sourceProfile )
45
+ c , err := initializeCollectors (conv , sourceProfile , assessmentConfig , projectId , ctx )
37
46
if err != nil {
38
47
logger .Log .Error ("unable to initialize collectors" )
39
48
return output , err
@@ -45,32 +54,86 @@ func PerformAssessment(conv *internal.Conv, sourceProfile profiles.SourceProfile
45
54
// Select the highest confidence output for each attribute
46
55
// Populate assessment struct
47
56
48
- output .SchemaAssessment , err = performSchemaAssessment (c )
57
+ output .SchemaAssessment , err = performSchemaAssessment (ctx , c )
58
+
49
59
return output , err
50
60
}
51
61
52
62
// Initilize collectors. Take a decision here on which collectors are mandatory and which are optional
53
- func initializeCollectors (conv * internal.Conv , sourceProfile profiles.SourceProfile ) (assessmentCollectors , error ) {
63
+ func initializeCollectors (conv * internal.Conv , sourceProfile profiles.SourceProfile , assessmentConfig map [ string ] string , projectId string , ctx context. Context ) (assessmentCollectors , error ) {
54
64
c := assessmentCollectors {}
55
65
sampleCollector , err := assessment .CreateSampleCollector ()
56
66
if err != nil {
57
67
return c , err
58
68
}
59
- c .sampleCollector = sampleCollector
69
+ c .sampleCollector = & sampleCollector
60
70
infoSchemaCollector , err := assessment .CreateInfoSchemaCollector (conv , sourceProfile )
61
71
if infoSchemaCollector .IsEmpty () {
62
72
return c , err
63
73
}
64
- c .infoSchemaCollector = infoSchemaCollector
74
+ c .infoSchemaCollector = & infoSchemaCollector
75
+
76
+ //Initiialize App Assessment Collector
77
+
78
+ codeDirectory , exists := assessmentConfig ["codeDirectory" ]
79
+ if exists {
80
+ logger .Log .Info ("initializing app collector" )
81
+
82
+ mysqlSchema := "" // TODO fetch from conv
83
+ spannerSchema := "" // TODO fetch from conv
84
+ mysqlSchemaPath , exists := assessmentConfig ["mysqlSchemaPath" ]
85
+ if exists {
86
+ logger .Log .Info (fmt .Sprintf ("overriding mysql schema from file %s" , mysqlSchemaPath ))
87
+ mysqlSchema , err := utils .ReadFile (mysqlSchemaPath )
88
+ if err != nil {
89
+ logger .Log .Debug ("error reading MySQL schema file:" , zap .Error (err ))
90
+ }
91
+ logger .Log .Debug (mysqlSchema )
92
+ }
93
+
94
+ spannerSchemaPath , exists := assessmentConfig ["spannerSchemaPath" ]
95
+ if exists {
96
+ logger .Log .Info (fmt .Sprintf ("overriding spanner schema from file %s" , spannerSchemaPath ))
97
+ spannerSchema , err := utils .ReadFile (spannerSchemaPath )
98
+ if err != nil {
99
+ logger .Log .Debug ("error reading Spanner schema file:" , zap .Error (err ))
100
+ }
101
+ logger .Log .Info (spannerSchema )
102
+ }
103
+
104
+ summarizer , err := assessment .NewMigrationSummarizer (ctx , nil , projectId , assessmentConfig ["location" ], mysqlSchema , spannerSchema , codeDirectory )
105
+ if err != nil {
106
+ logger .Log .Error ("error initiating migration summarizer" )
107
+ return c , err
108
+ }
109
+ c .appAssessmentCollector = summarizer
110
+ logger .Log .Info ("initialized app collector" )
111
+ } else {
112
+ logger .Log .Info ("app code info unavailable" )
113
+ }
114
+
65
115
return c , err
66
116
}
67
117
68
- func performSchemaAssessment (collectors assessmentCollectors ) (utils.SchemaAssessmentOutput , error ) {
118
+ func performSchemaAssessment (ctx context. Context , collectors assessmentCollectors ) (utils.SchemaAssessmentOutput , error ) {
69
119
schemaOut := utils.SchemaAssessmentOutput {}
70
120
schemaOut .SourceTableDefs , schemaOut .SpannerTableDefs = collectors .infoSchemaCollector .ListTables ()
71
121
schemaOut .SourceIndexDef , schemaOut .SpannerIndexDef = collectors .infoSchemaCollector .ListIndexes ()
72
122
schemaOut .Triggers = collectors .infoSchemaCollector .ListTriggers ()
73
123
schemaOut .SourceColDefs , schemaOut .SpannerColDefs = collectors .infoSchemaCollector .ListColumnDefinitions ()
74
124
schemaOut .StoredProcedureAssessmentOutput = collectors .infoSchemaCollector .ListStoredProcedures ()
125
+
126
+ if collectors .appAssessmentCollector != nil {
127
+ logger .Log .Info ("adding app assessment details" )
128
+ codeAssessment , err := collectors .appAssessmentCollector .AnalyzeProject (ctx )
129
+
130
+ if err != nil {
131
+ logger .Log .Error ("error analyzing project" , zap .Error (err ))
132
+ return schemaOut , err
133
+ }
134
+
135
+ logger .Log .Info (fmt .Sprintf ("snippets %+v" , codeAssessment .Snippets ))
136
+ schemaOut .CodeSnippets = & codeAssessment .Snippets
137
+ }
75
138
return schemaOut , nil
76
139
}
0 commit comments