MCPcopy Create free account
hub / github.com/bytebase/bytebase / DeriveCheckTargets

Function DeriveCheckTargets

backend/runner/plancheck/derive.go:16–89  ·  view source on GitHub ↗

DeriveCheckTargets derives check targets from a plan and optional database group. This replaces the stored config by computing targets at runtime.

(ctx context.Context, s *store.Store, project *store.ProjectMessage, plan *store.PlanMessage, databaseGroup *v1pb.DatabaseGroup)

Source from the content-addressed store, hash-verified

14// DeriveCheckTargets derives check targets from a plan and optional database group.
15// This replaces the stored config by computing targets at runtime.
16func DeriveCheckTargets(ctx context.Context, s *store.Store, project *store.ProjectMessage, plan *store.PlanMessage, databaseGroup *v1pb.DatabaseGroup) ([]*CheckTarget, error) {
17 var targets []*CheckTarget
18
19 for _, spec := range plan.Config.Specs {
20 switch config := spec.Config.(type) {
21 case *storepb.PlanConfig_Spec_CreateDatabaseConfig:
22 // No checks for create database.
23 case *storepb.PlanConfig_Spec_ExportDataConfig:
24 // No checks for export data.
25 case *storepb.PlanConfig_Spec_ChangeDatabaseConfig:
26 // Skip plan checks for releases.
27 if config.ChangeDatabaseConfig.Release != "" {
28 continue
29 }
30
31 var databases []string
32 if len(config.ChangeDatabaseConfig.Targets) == 1 && databaseGroup != nil && config.ChangeDatabaseConfig.Targets[0] == databaseGroup.Name {
33 for _, m := range databaseGroup.MatchedDatabases {
34 databases = append(databases, m.Name)
35 }
36 } else {
37 databases = config.ChangeDatabaseConfig.Targets
38 }
39
40 // Apply sampling upfront
41 if samplingSize := project.Setting.GetCiSamplingSize(); samplingSize > 0 {
42 if len(databases) > int(samplingSize) {
43 databases = databases[:samplingSize]
44 }
45 }
46
47 // Parse ghost config from sheet content
48 var enableGhost bool
49 var ghostFlags map[string]string
50
51 sheetContent, err := getSheetContent(ctx, s, config.ChangeDatabaseConfig.SheetSha256)
52 if err != nil {
53 return nil, errors.Wrapf(err, "failed to get sheet content")
54 }
55 if sheetContent != "" {
56 enableGhost = ghost.IsGhostEnabled(sheetContent)
57 if enableGhost {
58 ghostFlags, err = ghost.ParseGhostDirective(sheetContent)
59 if err != nil {
60 return nil, errors.Wrapf(err, "failed to parse ghost directive")
61 }
62 }
63 }
64
65 for _, target := range databases {
66 types := []storepb.PlanCheckType{
67 storepb.PlanCheckType_PLAN_CHECK_TYPE_STATEMENT_ADVISE,
68 storepb.PlanCheckType_PLAN_CHECK_TYPE_STATEMENT_SUMMARY_REPORT,
69 }
70 if enableGhost {
71 types = append(types, storepb.PlanCheckType_PLAN_CHECK_TYPE_GHOST_SYNC)
72 }
73

Callers 2

getPlanCheckRunFromPlanFunction · 0.92
runPlanCheckRunMethod · 0.85

Calls 5

IsGhostEnabledFunction · 0.92
ParseGhostDirectiveFunction · 0.92
getSheetContentFunction · 0.85
ErrorfMethod · 0.80
GetCiSamplingSizeMethod · 0.45

Tested by

no test coverage detected