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: store return nil on database entity non-existence (#314)
spinningbot336e540f5596
diff --git a/server/backup_runner.go b/server/backup_runner.goindex c051c14841..131506829f 100644--- a/server/backup_runner.go+++ b/server/backup_runner.go@@ -81,6 +81,14 @@ func (s *BackupRunner) Run() error {zap.Error(err))continue}+ if database == nil {+ err := fmt.Errorf("Failed to get database for backup setting, database ID not found %v", backupSetting.DatabaseID)+ s.l.Error(err.Error(),+ zap.Int("id", backupSetting.ID),+ zap.Int("databaseID", backupSetting.DatabaseID),+ zap.Error(err))+ continue+ }backupSetting.Database = databasebackupName := fmt.Sprintf("%s-%s-%s-autobackup", api.ProjectShortSlug(database.Project), api.EnvSlug(database.Instance.Environment), t.Format("20060102T030405"))diff --git a/server/database.go b/server/database.goindex 56f6e29008..2dba150dff 100644--- a/server/database.go+++ b/server/database.go@@ -129,11 +129,11 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {}database, err := s.composeDatabaseByFind(ctx, databaseFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))- }return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch database ID: %v", id)).SetInternal(err)}+ if database == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))+ }c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)if err := jsonapi.MarshalPayload(c.Response().Writer, database); err != nil {@@ -161,11 +161,11 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {ID: &id,})if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))- }return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to patch database ID: %v", id)).SetInternal(err)}+ if database == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))+ }if databasePatch.ProjectID != nil {toProject, err := s.composeProjectByID(ctx, *databasePatch.ProjectID)@@ -223,11 +223,11 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {ID: &databasePatch.ID,})if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))- }return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to patch database ID: %v", id)).SetInternal(err)}+ if existingDatabase == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))+ }}database, err = s.DatabaseService.PatchDatabase(ctx, databasePatch)@@ -314,11 +314,11 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {}database, err := s.composeDatabaseByFind(ctx, databaseFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))- }return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch database ID: %v", id)).SetInternal(err)}+ if database == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))+ }tableFind := &api.TableFind{DatabaseID: &id,@@ -372,11 +372,11 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {}database, err := s.composeDatabaseByFind(ctx, databaseFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))- }return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch database ID: %v", id)).SetInternal(err)}+ if database == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))+ }tableName := c.Param("tableName")tableFind := &api.TableFind{@@ -434,11 +434,11 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {}database, err := s.composeDatabaseByFind(ctx, databaseFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))- }return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch database ID: %v", id)).SetInternal(err)}+ if database == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))+ }viewFind := &api.ViewFind{DatabaseID: &id,@@ -481,11 +481,11 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {}database, err := s.composeDatabaseByFind(ctx, databaseFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))- }return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch database ID: %v", id)).SetInternal(err)}+ if database == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))+ }backupCreate.Path, err = getAndCreateBackupPath(s.dataDir, database, backupCreate.Name)if err != nil {@@ -573,13 +573,13 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {databaseFind := &api.DatabaseFind{ID: &id,}- _, err = s.composeDatabaseByFind(ctx, databaseFind)+ database, err := s.composeDatabaseByFind(ctx, databaseFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))- }return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch database ID: %v", id)).SetInternal(err)}+ if database == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))+ }backupFind := &api.BackupFind{DatabaseID: &id,@@ -620,11 +620,11 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {}db, err := s.composeDatabaseByFind(ctx, databaseFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))- }return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch database ID: %v", id)).SetInternal(err)}+ if db == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))+ }backupSettingUpsert.EnvironmentID = db.Instance.Environment.IDbackupSetting, err := s.BackupService.UpsertBackupSetting(ctx, backupSettingUpsert)@@ -649,13 +649,13 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {databaseFind := &api.DatabaseFind{ID: &id,}- _, err = s.composeDatabaseByFind(ctx, databaseFind)+ database, err := s.composeDatabaseByFind(ctx, databaseFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))- }return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch database ID: %v", id)).SetInternal(err)}+ if database == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))+ }backupSettingFind := &api.BackupSettingFind{DatabaseID: &id,@@ -715,8 +715,10 @@ func (s *Server) composeDatabaseByFind(ctx context.Context, find *api.DatabaseFireturn nil, err}- if err := s.composeDatabaseRelationship(ctx, database); err != nil {- return nil, err+ if database != nil {+ if err := s.composeDatabaseRelationship(ctx, database); err != nil {+ return nil, err+ }}return database, nildiff --git a/server/issue.go b/server/issue.goindex abe69a7b2e..c1d87c930e 100644--- a/server/issue.go+++ b/server/issue.go@@ -783,11 +783,11 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.I}database, err := s.composeDatabaseByFind(ctx, databaseFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- return nil, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", d.DatabaseID))- }return nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch database ID: %v", d.DatabaseID)).SetInternal(err)}+ if database == nil {+ return nil, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", d.DatabaseID))+ }taskCreate, err := getSchemaUpdateTask(database, m.MigrationType, m.VCSPushEvent, d)if err != nil {diff --git a/server/task.go b/server/task.goindex 8fa2f1590d..4514222026 100644--- a/server/task.go+++ b/server/task.go@@ -400,6 +400,9 @@ func (s *Server) composeTaskRelationship(ctx context.Context, task *api.Task) erif err != nil {return err}+ if task.Database == nil {+ return fmt.Errorf("database ID not found %v", task.DatabaseID)+ }}return nildiff --git a/server/task_check_executor_database_connect.go b/server/task_check_executor_database_connect.goindex eb450c701b..2e5a97151e 100644--- a/server/task_check_executor_database_connect.go+++ b/server/task_check_executor_database_connect.go@@ -48,6 +48,9 @@ func (exec *TaskCheckDatabaseConnectExecutor) Run(ctx context.Context, server *Sif err != nil {return []api.TaskCheckResult{}, common.Errorf(common.Internal, err)}+ if database == nil {+ return []api.TaskCheckResult{}, common.Errorf(common.Internal, fmt.Errorf("database ID not found %v", task.DatabaseID))+ }driver, err := getDatabaseDriver(ctx, database.Instance, database.Name, exec.l)if err != nil {diff --git a/server/task_check_scheduler.go b/server/task_check_scheduler.goindex 7335274b1f..c96918b077 100644--- a/server/task_check_scheduler.go+++ b/server/task_check_scheduler.go@@ -261,6 +261,9 @@ func (s *TaskCheckScheduler) ScheduleCheckIfNeeded(ctx context.Context, task *apif err != nil {return nil, err}+ if database == nil {+ return nil, fmt.Errorf("database ID not found %v", task.DatabaseID)+ }_, err = s.server.TaskCheckRunService.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{CreatorID: creatorID,diff --git a/server/task_executor_database_restore.go b/server/task_executor_database_restore.goindex 3898419113..a74b9adc61 100644--- a/server/task_executor_database_restore.go+++ b/server/task_executor_database_restore.go@@ -10,7 +10,6 @@ import ("strconv""github.com/bytebase/bytebase/api"- "github.com/bytebase/bytebase/common""github.com/bytebase/bytebase/plugin/db""go.uber.org/zap")@@ -65,6 +64,9 @@ func (exec *DatabaseRestoreTaskExecutor) RunOnce(ctx context.Context, server *Seif err != nil {return true, nil, fmt.Errorf("failed to find database for the backup: %w", err)}+ if sourceDatabase == nil {+ return true, nil, fmt.Errorf("source database ID not found %v", backup.DatabaseID)+ }targetDatabaseFind := &api.DatabaseFind{InstanceID: &task.InstanceID,@@ -72,11 +74,11 @@ func (exec *DatabaseRestoreTaskExecutor) RunOnce(ctx context.Context, server *Se}targetDatabase, err := server.composeDatabaseByFind(ctx, targetDatabaseFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- return true, nil, fmt.Errorf("target database %q not found in instance %q: %w", payload.DatabaseName, task.Instance.Name, err)- }return true, nil, fmt.Errorf("failed to find target database %q in instance %q: %w", payload.DatabaseName, task.Instance.Name, err)}+ if targetDatabase == nil {+ return true, nil, fmt.Errorf("target database %q not found in instance %q: %w", payload.DatabaseName, task.Instance.Name, err)+ }exec.l.Debug("Start database restore from backup...",zap.String("source_instance", sourceDatabase.Instance.Name),diff --git a/store/database.go b/store/database.goindex 388c21e24b..e5cfb92ad7 100644--- a/store/database.go+++ b/store/database.go@@ -121,7 +121,6 @@ func (s *DatabaseService) FindDatabaseList(ctx context.Context, find *api.Databa}// FindDatabase retrieves a single database based on find.-// Returns ENOTFOUND if no matching record.// Returns ECONFLICT if finding more than 1 matching records.func (s *DatabaseService) FindDatabase(ctx context.Context, find *api.DatabaseFind) (*api.Database, error) {if find.ID != nil {@@ -144,8 +143,10 @@ func (s *DatabaseService) FindDatabase(ctx context.Context, find *api.DatabaseFilist, err := s.findDatabaseList(ctx, tx, find)if err != nil {return nil, err- } else if len(list) == 0 {- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("database not found: %+v", find)}+ }++ if len(list) == 0 {+ return nil, nil} else if len(list) > 1 {return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d databases with filter %+v, expect 1. ", len(list), find)}}