Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: return nil when sheet not found (#18554)
Danny Xu7dce444464f4
diff --git a/backend/api/v1/plan_service.go b/backend/api/v1/plan_service.goindex 599d3e4682..82e1281b13 100644--- a/backend/api/v1/plan_service.go+++ b/backend/api/v1/plan_service.go@@ -608,10 +608,13 @@ func (s *PlanService) UpdatePlan(ctx context.Context, request *connect.Request[vreturn nil}- _, err = s.store.GetSheetMetadata(ctx, newSheetSha256)+ sheet, err := s.store.GetSheetMetadata(ctx, newSheetSha256)if err != nil {return connect.NewError(connect.CodeInternal, errors.Errorf("failed to get sheet %q: %v", oldSheetName, err))}+ if sheet == nil {+ return connect.NewError(connect.CodeNotFound, errors.Errorf("sheet %q not found", oldSheetName))+ }doUpdate = truetaskPatch.SheetSha256 = &newSheetSha256@@ -797,10 +800,7 @@ func (s *PlanService) ListPlanCheckRuns(ctx context.Context, request *connect.Reif err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to list plan check runs, error: %v", err))}- converted, err := convertToPlanCheckRuns(ctx, s.store, projectID, planUID, planCheckRuns)- if err != nil {- return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to convert plan check runs, error: %v", err))- }+ converted := convertToPlanCheckRuns(projectID, planUID, planCheckRuns)return connect.NewResponse(&v1pb.ListPlanCheckRunsResponse{PlanCheckRuns: converted,diff --git a/backend/api/v1/plan_service_converter.go b/backend/api/v1/plan_service_converter.goindex 38df92cbb0..59639ab8ad 100644--- a/backend/api/v1/plan_service_converter.go+++ b/backend/api/v1/plan_service_converter.go@@ -292,40 +292,27 @@ func convertPlanSpecExportDataConfig(config *v1pb.Plan_Spec_ExportDataConfig) *s}}-func convertToPlanCheckRuns(ctx context.Context, s *store.Store, projectID string, planUID int64, runs []*store.PlanCheckRunMessage) ([]*v1pb.PlanCheckRun, error) {+func convertToPlanCheckRuns(projectID string, planUID int64, runs []*store.PlanCheckRunMessage) []*v1pb.PlanCheckRun {var planCheckRuns []*v1pb.PlanCheckRunfor _, run := range runs {- converted, err := convertToPlanCheckRun(ctx, s, projectID, planUID, run)- if err != nil {- return nil, errors.Wrapf(err, "failed to convert plan check run")- }+ converted := convertToPlanCheckRun(projectID, planUID, run)planCheckRuns = append(planCheckRuns, converted)}- return planCheckRuns, nil+ return planCheckRuns}-func convertToPlanCheckRun(ctx context.Context, s *store.Store, projectID string, planUID int64, run *store.PlanCheckRunMessage) (*v1pb.PlanCheckRun, error) {+func convertToPlanCheckRun(projectID string, planUID int64, run *store.PlanCheckRunMessage) *v1pb.PlanCheckRun {converted := &v1pb.PlanCheckRun{Name: common.FormatPlanCheckRun(projectID, planUID, int64(run.UID)),CreateTime: timestamppb.New(run.CreatedAt),Type: convertToPlanCheckRunType(run.Type),Status: convertToPlanCheckRunStatus(run.Status),Target: common.FormatDatabase(run.Config.InstanceId, run.Config.DatabaseName),- Sheet: "",+ Sheet: common.FormatSheet(projectID, run.Config.GetSheetSha256()),Results: convertToPlanCheckRunResults(run.Result.Results),Error: run.Result.Error,}-- if sheetSha256 := run.Config.GetSheetSha256(); sheetSha256 != "" {- _, err := s.GetSheetMetadata(ctx, sheetSha256)- if err != nil {- return nil, errors.Wrapf(err, "failed to get sheet")- }- // Sheets are now project-agnostic, no need to check projectID- converted.Sheet = common.FormatSheet(projectID, sheetSha256)- }-- return converted, nil+ return converted}func convertToPlanCheckRunType(t store.PlanCheckRunType) v1pb.PlanCheckRun_Type {diff --git a/backend/api/v1/plan_service_plan_check.go b/backend/api/v1/plan_service_plan_check.goindex a99724f2ce..1b50162576 100644--- a/backend/api/v1/plan_service_plan_check.go+++ b/backend/api/v1/plan_service_plan_check.go@@ -172,11 +172,13 @@ func getPlanCheckRunsFromChangeDatabaseConfigForDatabase(ctx context.Context, sif instance == nil {return nil, errors.Errorf("instance %q not found", database.InstanceID)}- _, err = s.GetSheetMetadata(ctx, sheetSha256)+ sheet, err := s.GetSheetMetadata(ctx, sheetSha256)if err != nil {return nil, errors.Wrapf(err, "failed to get sheet %s", sheetSha256)}- // Sheets are now project-agnostic, no need to check projectID+ if sheet == nil {+ return nil, errors.Errorf("sheet %s not found", sheetSha256)+ }enableSDL := config.Type == storepb.PlanConfig_ChangeDatabaseConfig_SDLdiff --git a/backend/api/v1/release_service.go b/backend/api/v1/release_service.goindex eb98fa9d8f..b74c77662b 100644--- a/backend/api/v1/release_service.go+++ b/backend/api/v1/release_service.go@@ -120,11 +120,13 @@ func (s *ReleaseService) CreateRelease(ctx context.Context, req *connect.Requestif err != nil {return nil, errors.Wrapf(err, "failed to get sheetSha256 from %q", f.Sheet)}- _, err = s.store.GetSheetMetadata(ctx, sheetSha256)+ sheet, err := s.store.GetSheetMetadata(ctx, sheetSha256)if err != nil {return nil, errors.Wrapf(err, "failed to get sheet %q", f.Sheet)}- // Sheets are now project-agnostic, no need to check projectID+ if sheet == nil {+ return nil, errors.Errorf("sheet %q not found", f.Sheet)+ }releaseMessage.Payload.Files = append(releaseMessage.Payload.Files, &storepb.ReleasePayload_File{Id: f.Id,@@ -435,6 +437,9 @@ func convertToRelease(ctx context.Context, s *store.Store, release *store.Releasif err != nil {return nil, errors.Wrapf(err, "failed to get sheet with sha256 %q", f.SheetSha256)}+ if sheet == nil {+ return nil, errors.Errorf("sheet %q not found", f.SheetSha256)+ }// Sheets are now project-agnostic, no need to check projectIDr.Files = append(r.Files, &v1pb.Release_File{Id: f.Id,@@ -507,6 +512,9 @@ func validateAndSanitizeReleaseFiles(ctx context.Context, s *store.Store, filesif err != nil {return nil, errors.Wrapf(err, "failed to get sheet %q", f.Sheet)}+ if sheet == nil {+ return nil, errors.Errorf("sheet %q not found", f.Sheet)+ }// Sheets are now project-agnostic, no need to check projectIDf.Statement = []byte(sheet.Statement)f.SheetSha256 = sheet.Sha256diff --git a/backend/api/v1/revision_service.go b/backend/api/v1/revision_service.goindex 3d32f7f88f..f82e02eb81 100644--- a/backend/api/v1/revision_service.go+++ b/backend/api/v1/revision_service.go@@ -277,6 +277,9 @@ func convertToRevision(ctx context.Context, s *store.Store, parent string, revisif err != nil {return nil, errors.Wrapf(err, "failed to get sheet %q", sheetSha256)}+ if sheet == nil {+ return nil, errors.Errorf("sheet %q not found", sheetSha256)+ }database, err := s.GetDatabase(ctx, &store.FindDatabaseMessage{InstanceID: &revision.InstanceID,diff --git a/backend/runner/approval/runner.go b/backend/runner/approval/runner.goindex 841e78dcdf..3eb62c93c7 100644--- a/backend/runner/approval/runner.go+++ b/backend/runner/approval/runner.go@@ -506,6 +506,9 @@ func (r *Runner) buildCELVariablesForDatabaseChange(ctx context.Context, issue *if err != nil {return nil, true, errors.Wrapf(err, "failed to get statement in sheet %v", sheetSha256)}+ if sheet == nil {+ return nil, true, errors.Errorf("sheet %v not found", sheetSha256)+ }taskStatement = sheet.Statement}diff --git a/backend/runner/plancheck/ghost_sync_executor.go b/backend/runner/plancheck/ghost_sync_executor.goindex b79d306ed6..4b289890f6 100644--- a/backend/runner/plancheck/ghost_sync_executor.go+++ b/backend/runner/plancheck/ghost_sync_executor.go@@ -87,6 +87,9 @@ func (e *GhostSyncExecutor) Run(ctx context.Context, config *storepb.PlanCheckRuif err != nil {return nil, errors.Wrapf(err, "failed to get sheet %s", config.SheetSha256)}+ if sheet == nil {+ return nil, errors.Errorf("sheet %s not found", config.SheetSha256)+ }statement := sheet.Statement// Database secrets feature has been removeddiff --git a/backend/runner/plancheck/statement_advise_executor.go b/backend/runner/plancheck/statement_advise_executor.goindex 59bd0b2338..b8eef9aa86 100644--- a/backend/runner/plancheck/statement_advise_executor.go+++ b/backend/runner/plancheck/statement_advise_executor.go@@ -49,6 +49,9 @@ func (e *StatementAdviseExecutor) Run(ctx context.Context, config *storepb.PlanCif err != nil {return nil, errors.Wrapf(err, "failed to get sheet %s", config.SheetSha256)}+ if sheet == nil {+ return nil, errors.Errorf("sheet %s not found", config.SheetSha256)+ }if sheet.Size > common.MaxSheetCheckSize {return []*storepb.PlanCheckRunResult_Result{{@@ -63,6 +66,9 @@ func (e *StatementAdviseExecutor) Run(ctx context.Context, config *storepb.PlanCif err != nil {return nil, err}+ if fullSheet == nil {+ return nil, errors.Errorf("sheet full %s not found", config.SheetSha256)+ }statement := fullSheet.StatementenablePriorBackup := config.EnablePriorBackupenableGhost := config.EnableGhostdiff --git a/backend/runner/plancheck/statement_report_executor.go b/backend/runner/plancheck/statement_report_executor.goindex 15164f5678..12f5d778b8 100644--- a/backend/runner/plancheck/statement_report_executor.go+++ b/backend/runner/plancheck/statement_report_executor.go@@ -52,6 +52,9 @@ func (e *StatementReportExecutor) Run(ctx context.Context, config *storepb.PlanCif err != nil {return nil, errors.Wrapf(err, "failed to get sheet %s", config.SheetSha256)}+ if sheet == nil {+ return nil, errors.Errorf("sheet %s not found", config.SheetSha256)+ }if sheet.Size > common.MaxSheetCheckSize {return []*storepb.PlanCheckRunResult_Result{{@@ -66,6 +69,9 @@ func (e *StatementReportExecutor) Run(ctx context.Context, config *storepb.PlanCif err != nil {return nil, err}+ if fullSheet == nil {+ return nil, errors.Errorf("sheet full %s not found", config.SheetSha256)+ }statement := fullSheet.Statementinstance, err := e.store.GetInstance(ctx, &store.FindInstanceMessage{ResourceID: &config.InstanceId})diff --git a/backend/runner/taskrun/data_export_executor.go b/backend/runner/taskrun/data_export_executor.goindex d28986f523..f54f6ec2ca 100644--- a/backend/runner/taskrun/data_export_executor.go+++ b/backend/runner/taskrun/data_export_executor.go@@ -78,6 +78,9 @@ func (exec *DataExportExecutor) RunOnce(ctx context.Context, _ context.Context,if err != nil {return true, nil, err}+ if sheet == nil {+ return true, nil, errors.Errorf("sheet not found: %s", task.Payload.GetSheetSha256())+ }statement := sheet.StatementdataSource := apiv1.GetQueriableDataSource(instance)diff --git a/backend/runner/taskrun/database_create_executor.go b/backend/runner/taskrun/database_create_executor.goindex 2cc202f34e..5ef7e5c5db 100644--- a/backend/runner/taskrun/database_create_executor.go+++ b/backend/runner/taskrun/database_create_executor.go@@ -45,6 +45,9 @@ func (exec *DatabaseCreateExecutor) RunOnce(ctx context.Context, driverCtx conteif err != nil {return true, nil, errors.Wrapf(err, "failed to get sheet: %s", task.Payload.GetSheetSha256())}+ if sheet == nil {+ return true, nil, errors.Errorf("sheet not found: %s", task.Payload.GetSheetSha256())+ }statement := sheet.Statementstatement = strings.TrimSpace(statement)diff --git a/backend/runner/taskrun/database_migrate_executor.go b/backend/runner/taskrun/database_migrate_executor.goindex 2bd4fd7137..a836645124 100644--- a/backend/runner/taskrun/database_migrate_executor.go+++ b/backend/runner/taskrun/database_migrate_executor.go@@ -66,6 +66,9 @@ func (exec *DatabaseMigrateExecutor) runMigrationWithPriorBackup(ctx context.Conif err != nil {return true, nil, err}+ if sheet == nil {+ return true, nil, errors.Errorf("sheet not found: %s", task.Payload.GetSheetSha256())+ }statement := sheet.Statement// Handle prior backup if enabled.@@ -137,6 +140,9 @@ func (exec *DatabaseMigrateExecutor) runGhostMigration(ctx context.Context, drivif err != nil {return true, nil, err}+ if sheet == nil {+ return true, nil, errors.Errorf("sheet not found: %s", task.Payload.GetSheetSha256())+ }statement := sheet.Statementflags := task.Payload.GetFlags()diff --git a/backend/runner/taskrun/executor.go b/backend/runner/taskrun/executor.goindex c6a4534515..74fc69c1cb 100644--- a/backend/runner/taskrun/executor.go+++ b/backend/runner/taskrun/executor.go@@ -65,9 +65,7 @@ type migrateContext struct {instance *store.InstanceMessagedatabase *store.DatabaseMessage// nullable if type=baseline- sheet *store.SheetMessage- // empty if type=baseline- sheetName string+ sheetSha256 stringtask *store.TaskMessagetaskRunUID int@@ -198,12 +196,7 @@ func getMigrationInfo(ctx context.Context, stores *store.Store, profile *config.}if sheetID != nil {- sheet, err := stores.GetSheetMetadata(ctx, *sheetID)- if err != nil {- return nil, errors.Wrapf(err, "failed to get sheet")- }- mc.sheet = sheet- mc.sheetName = common.FormatSheet(pipeline.ProjectID, sheet.Sha256)+ mc.sheetSha256 = *sheetID}if isChangeDatabaseTask(task) {@@ -441,7 +434,7 @@ func beginMigration(ctx context.Context, stores *store.Store, mc *migrateContextPayload: &storepb.ChangelogPayload{TaskRun: mc.taskRunName,Revision: 0,- SheetSha256: mc.sheet.Sha256,+ SheetSha256: mc.sheetSha256,Version: mc.version,Type: changelogType,GitCommit: mc.profile.GitCommit,@@ -482,7 +475,7 @@ func endMigration(ctx context.Context, storeInstance *store.Store, mc *migrateCoPayload: &storepb.RevisionPayload{Release: mc.release.release,File: mc.release.file,- SheetSha256: "",+ SheetSha256: mc.sheetSha256,TaskRun: mc.taskRunName,Type: storepb.SchemaChangeType_VERSIONED,},@@ -490,9 +483,6 @@ func endMigration(ctx context.Context, storeInstance *store.Store, mc *migrateCoif mc.task.Type == storepb.Task_DATABASE_SDL {r.Payload.Type = storepb.SchemaChangeType_DECLARATIVE}- if mc.sheet != nil {- r.Payload.SheetSha256 = mc.sheet.Sha256- }revision, err := storeInstance.CreateRevision(ctx, r)if err != nil {diff --git a/backend/runner/taskrun/schema_update_sdl_executor.go b/backend/runner/taskrun/schema_update_sdl_executor.goindex 95fd52f75c..f35a5015b7 100644--- a/backend/runner/taskrun/schema_update_sdl_executor.go+++ b/backend/runner/taskrun/schema_update_sdl_executor.go@@ -55,6 +55,9 @@ func (exec *SchemaDeclareExecutor) RunOnce(ctx context.Context, driverCtx contexif err != nil {return true, nil, err}+ if sheet == nil {+ return true, nil, errors.Errorf("sheet not found: %s", task.Payload.GetSheetSha256())+ }sheetContent := sheet.StatementexecFunc := func(ctx context.Context, execStatement string, driver db.Driver, opts db.ExecuteOptions) error {@@ -163,6 +166,9 @@ func getPreviousSuccessfulSDLAndSchema(ctx context.Context, s *store.Store, instif err != nil {return "", nil, errors.Wrapf(err, "failed to get sheet statement for previous SDL changelog sheet %s", sheetSha256)}+ if sheet == nil {+ return "", nil, errors.Errorf("sheet %s not found", sheetSha256)+ }previousUserSDLText = sheet.Statement}diff --git a/backend/store/sheet.go b/backend/store/sheet.goindex fa6461c364..f4e393b354 100644--- a/backend/store/sheet.go+++ b/backend/store/sheet.go@@ -35,6 +35,9 @@ func (s *Store) GetSheetMetadata(ctx context.Context, sha256Hex string) (*SheetMif err != nil {return nil, err}+ if sheet == nil {+ return nil, nil+ }s.sheetMetadataCache.Add(sha256Hex, sheet)return sheet, nil@@ -52,6 +55,9 @@ func (s *Store) GetSheetFull(ctx context.Context, sha256Hex string) (*SheetMessaif err != nil {return nil, err}+ if sheet == nil {+ return nil, nil+ }s.sheetFullCache.Add(sha256Hex, sheet)return sheet, nil@@ -109,7 +115,7 @@ func (s *Store) getSheet(ctx context.Context, sha256Hex string, loadFull bool) (}if sheet == nil {- return nil, errors.Errorf("sheet not found with sha256 %s", sha256Hex)+ return nil, nil}return sheet, nil