Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: store return nil on datasource, environment, instance, project, repository, setting entity non-existence (#313)

spinningbot68a192c78dc3
diff --git a/server/activity_manager.go b/server/activity_manager.go
index 9a64d497af..64e47c858c 100644
--- a/server/activity_manager.go
+++ b/server/activity_manager.go
@@ -75,6 +75,9 @@ func (m *ActivityManager) CreateActivity(ctx context.Context, create *api.Activi
if err != nil {
return nil, fmt.Errorf("failed to find project for posting webhook event after changing the issue status: %v, error: %w", meta.issue.Name, err)
}
+ if meta.issue.Project == nil {
+ return nil, fmt.Errorf("failed to find project ID %v for posting webhook event after changing the issue status %q", meta.issue.ProjectID, meta.issue.Name)
+ }
}
principalFind := &api.PrincipalFind{
diff --git a/server/database.go b/server/database.go
index f4fa0e2b9b..56f6e29008 100644
--- a/server/database.go
+++ b/server/database.go
@@ -29,6 +29,10 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find instance").SetInternal(err)
}
+ if instance == nil {
+ err := fmt.Errorf("Instance ID not found %v", databaseCreate.InstanceID)
+ return echo.NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
+ }
databaseCreate.EnvironmentID = instance.EnvironmentID
database, err := s.DatabaseService.CreateDatabase(ctx, databaseCreate)
diff --git a/server/environment.go b/server/environment.go
index d61bbc5802..adf92162ac 100644
--- a/server/environment.go
+++ b/server/environment.go
@@ -148,6 +148,9 @@ func (s *Server) composeEnvironmentByID(ctx context.Context, id int) (*api.Envir
if err != nil {
return nil, err
}
+ if environment == nil {
+ return nil, fmt.Errorf("environment ID not found %v", id)
+ }
if err := s.composeEnvironmentRelationship(ctx, environment); err != nil {
return nil, err
diff --git a/server/instance.go b/server/instance.go
index d5cfcca949..1bcc2e67c7 100644
--- a/server/instance.go
+++ b/server/instance.go
@@ -133,11 +133,11 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
}
instance, err = s.InstanceService.FindInstance(ctx, instanceFind)
if err != nil {
- if common.ErrorCode(err) == common.NotFound {
- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", id))
- }
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch instance ID: %v", id)).SetInternal(err)
}
+ if instance == nil {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", id))
+ }
dataSourceType := api.Admin
dataSourceFind := &api.DataSourceFind{
@@ -148,6 +148,10 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch data source for instance: %v", instance.Name)).SetInternal(err)
}
+ if adminDataSource == nil {
+ err := fmt.Errorf("data source not found for instance ID %v, name %q and type %q", instance.ID, instance.Name, dataSourceType)
+ return echo.NewHTTPError(http.StatusInternalServerError, err.Error()).SetInternal(err)
+ }
dataSourcePatch := &api.DataSourcePatch{
ID: adminDataSource.ID,
@@ -439,6 +443,9 @@ func (s *Server) composeInstanceByID(ctx context.Context, id int) (*api.Instance
if err != nil {
return nil, err
}
+ if instance == nil {
+ return nil, fmt.Errorf("instance ID not found %v", id)
+ }
if err := s.composeInstanceRelationship(ctx, instance); err != nil {
return nil, err
diff --git a/server/issue.go b/server/issue.go
index 5628e5b875..abe69a7b2e 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -364,6 +364,9 @@ func (s *Server) createIssue(ctx context.Context, issueCreate *api.IssueCreate,
if err != nil {
return nil, fmt.Errorf("failed to fetch instance in issue creation: %v", err)
}
+ if instance == nil {
+ return nil, fmt.Errorf("instance ID not found %v", taskCreate.InstanceID)
+ }
if taskCreate.Type == api.TaskDatabaseSchemaUpdate {
payload := api.TaskDatabaseSchemaUpdatePayload{}
payload.MigrationType = taskCreate.MigrationType
diff --git a/server/project.go b/server/project.go
index f33290379f..16abf15c2a 100644
--- a/server/project.go
+++ b/server/project.go
@@ -531,6 +531,9 @@ func (s *Server) composeProjectByID(ctx context.Context, id int) (*api.Project,
if err != nil {
return nil, err
}
+ if project == nil {
+ return nil, fmt.Errorf("project ID not found %v", id)
+ }
if err := s.composeProjectRelationship(ctx, project); err != nil {
return nil, err
diff --git a/server/project_webhook.go b/server/project_webhook.go
index ceba11f02d..d0928499fa 100644
--- a/server/project_webhook.go
+++ b/server/project_webhook.go
@@ -194,11 +194,11 @@ func (s *Server) registerProjectWebhookRoutes(g *echo.Group) {
}
project, err := s.ProjectService.FindProject(ctx, projectFind)
if err != nil {
- if common.ErrorCode(err) == common.NotFound {
- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project ID not found: %d", projectID))
- }
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch project ID: %v", projectID)).SetInternal(err)
}
+ if project == nil {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project ID not found: %d", projectID))
+ }
id, err := strconv.Atoi(c.Param("webhookID"))
if err != nil {
diff --git a/server/task.go b/server/task.go
index 4c8aabfbc4..8fa2f1590d 100644
--- a/server/task.go
+++ b/server/task.go
@@ -530,7 +530,10 @@ func (s *Server) changeTaskStatusWithPatch(ctx context.Context, task *api.Task,
ID: &task.InstanceID,
})
if err != nil {
- return nil, fmt.Errorf("invalid instance ID: %v", task.InstanceID)
+ return nil, fmt.Errorf("failed to find instance: %v", task.InstanceID)
+ }
+ if instance == nil {
+ return nil, fmt.Errorf("instance ID not found %v", task.InstanceID)
}
databaseCreate := &api.DatabaseCreate{
CreatorID: taskStatusPatch.UpdaterID,
diff --git a/server/task_executor_schema_update.go b/server/task_executor_schema_update.go
index e924f78de9..c3541ac339 100644
--- a/server/task_executor_schema_update.go
+++ b/server/task_executor_schema_update.go
@@ -81,6 +81,9 @@ func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Serve
if err != nil {
return true, nil, fmt.Errorf("failed to find linked repository for database %q", databaseName)
}
+ if repository == nil {
+ return true, nil, fmt.Errorf("repository not found with project ID %v", task.Database.ProjectID)
+ }
mi, err = db.ParseMigrationInfo(
payload.VCSPushEvent.FileCommit.Added,
diff --git a/server/task_scheduler.go b/server/task_scheduler.go
index 5d3576aa1e..c15e3f7df8 100644
--- a/server/task_scheduler.go
+++ b/server/task_scheduler.go
@@ -267,6 +267,9 @@ func (s *TaskScheduler) ScheduleIfNeeded(ctx context.Context, task *api.Task) (*
if err != nil {
return nil, err
}
+ if instance == nil {
+ return nil, fmt.Errorf("instance ID not found %v", task.InstanceID)
+ }
// For now we only supported MySQL dialect syntax and compatibility check
if instance.Engine == db.MySQL || instance.Engine == db.TiDB {
pass, err = s.server.passCheck(ctx, s.server, task, api.TaskCheckDatabaseStatementSyntax)
diff --git a/server/webhook.go b/server/webhook.go
index 83c0b53243..4062d07ca8 100644
--- a/server/webhook.go
+++ b/server/webhook.go
@@ -50,11 +50,11 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
}
repository, err := s.RepositoryService.FindRepository(ctx, repositoryFind)
if err != nil {
- if common.ErrorCode(err) == common.NotFound {
- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Endpoint not found: %v", webhookEndpointID))
- }
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to respond webhook event for endpoint: %v", webhookEndpointID)).SetInternal(err)
}
+ if repository == nil {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Endpoint not found: %v", webhookEndpointID))
+ }
if err := s.composeRepositoryRelationship(ctx, repository); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch repository relationship: %v", repository.Name)).SetInternal(err)
diff --git a/store/data_source.go b/store/data_source.go
index 5096ceabd0..d62cb25f63 100644
--- a/store/data_source.go
+++ b/store/data_source.go
@@ -68,7 +68,6 @@ func (s *DataSourceService) FindDataSourceList(ctx context.Context, find *api.Da
}
// FindDataSource retrieves a single dataSource based on find.
-// Returns ENOTFOUND if no matching record.
// Returns ECONFLICT if finding more than 1 matching records.
func (s *DataSourceService) FindDataSource(ctx context.Context, find *api.DataSourceFind) (*api.DataSource, error) {
tx, err := s.db.BeginTx(ctx, nil)
@@ -80,8 +79,10 @@ func (s *DataSourceService) FindDataSource(ctx context.Context, find *api.DataSo
list, err := s.findDataSourceList(ctx, tx, find)
if err != nil {
return nil, err
- } else if len(list) == 0 {
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("data source 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 data sources with filter %+v, expect 1", len(list), find)}
}
diff --git a/store/environment.go b/store/environment.go
index 025fab15e2..c75c1fa819 100644
--- a/store/environment.go
+++ b/store/environment.go
@@ -76,7 +76,6 @@ func (s *EnvironmentService) FindEnvironmentList(ctx context.Context, find *api.
}
// FindEnvironment retrieves a single environment based on find.
-// Returns ENOTFOUND if no matching record.
// Returns ECONFLICT if finding more than 1 matching records.
func (s *EnvironmentService) FindEnvironment(ctx context.Context, find *api.EnvironmentFind) (*api.Environment, error) {
if find.ID != nil {
@@ -99,16 +98,16 @@ func (s *EnvironmentService) FindEnvironment(ctx context.Context, find *api.Envi
list, err := s.findEnvironmentList(ctx, tx, find)
if err != nil {
return nil, err
- } else if len(list) == 0 {
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("environment 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 environments with filter %+v, expect 1", len(list), find)}
}
-
if err := s.cache.UpsertCache(api.EnvironmentCache, list[0].ID, list[0]); err != nil {
return nil, err
}
-
return list[0], nil
}
diff --git a/store/instance.go b/store/instance.go
index 24881cf904..2abb8085db 100644
--- a/store/instance.go
+++ b/store/instance.go
@@ -109,7 +109,6 @@ func (s *InstanceService) FindInstanceList(ctx context.Context, find *api.Instan
}
// FindInstance retrieves a single instance based on find.
-// Returns ENOTFOUND if no matching record.
// Returns ECONFLICT if finding more than 1 matching records.
func (s *InstanceService) FindInstance(ctx context.Context, find *api.InstanceFind) (*api.Instance, error) {
if find.ID != nil {
@@ -132,16 +131,16 @@ func (s *InstanceService) FindInstance(ctx context.Context, find *api.InstanceFi
list, err := findInstanceList(ctx, tx, find)
if err != nil {
return nil, err
- } else if len(list) == 0 {
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("instance 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 instances with filter %+v, expect 1", len(list), find)}
}
-
if err := s.cache.UpsertCache(api.InstanceCache, list[0].ID, list[0]); err != nil {
return nil, err
}
-
return list[0], nil
}
diff --git a/store/project.go b/store/project.go
index fdd6a9c76d..a3e5114f09 100644
--- a/store/project.go
+++ b/store/project.go
@@ -77,7 +77,6 @@ func (s *ProjectService) FindProjectList(ctx context.Context, find *api.ProjectF
}
// FindProject retrieves a single project based on find.
-// Returns ENOTFOUND if no matching record.
// Returns ECONFLICT if finding more than 1 matching records.
func (s *ProjectService) FindProject(ctx context.Context, find *api.ProjectFind) (*api.Project, error) {
if find.ID != nil {
@@ -100,16 +99,16 @@ func (s *ProjectService) FindProject(ctx context.Context, find *api.ProjectFind)
list, err := findProjectList(ctx, tx, find)
if err != nil {
return nil, err
- } else if len(list) == 0 {
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("project 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 projects with filter %+v, expect 1", len(list), find)}
}
-
if err := s.cache.UpsertCache(api.ProjectCache, list[0].ID, list[0]); err != nil {
return nil, err
}
-
return list[0], nil
}
diff --git a/store/repository.go b/store/repository.go
index 4c46e856f7..811f41778b 100644
--- a/store/repository.go
+++ b/store/repository.go
@@ -64,7 +64,6 @@ func (s *RepositoryService) FindRepositoryList(ctx context.Context, find *api.Re
}
// FindRepository retrieves a single repository based on find.
-// Returns ENOTFOUND if no matching record.
// Returns ECONFLICT if finding more than 1 matching records.
func (s *RepositoryService) FindRepository(ctx context.Context, find *api.RepositoryFind) (*api.Repository, error) {
tx, err := s.db.BeginTx(ctx, nil)
@@ -76,8 +75,10 @@ func (s *RepositoryService) FindRepository(ctx context.Context, find *api.Reposi
list, err := findRepositoryList(ctx, tx, find)
if err != nil {
return nil, err
- } else if len(list) == 0 {
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("repository 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 repositories with filter %+v, expect 1", len(list), find)}
}
diff --git a/store/setting.go b/store/setting.go
index 67a944396a..4e5f7dd5a4 100644
--- a/store/setting.go
+++ b/store/setting.go
@@ -34,26 +34,26 @@ func (s *SettingService) CreateSettingIfNotExist(ctx context.Context, create *ap
}
setting, err := s.FindSetting(ctx, find)
if err != nil {
- if common.ErrorCode(err) == common.NotFound {
- tx, err := s.db.BeginTx(ctx, nil)
- if err != nil {
- return nil, FormatError(err)
- }
- defer tx.Rollback()
-
- setting, err = createSetting(ctx, tx, create)
- if err != nil {
- return nil, err
- }
-
- if err := tx.Commit(); err != nil {
- return nil, FormatError(err)
- }
-
- return setting, nil
- }
return nil, err
}
+ if setting == nil {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer tx.Rollback()
+
+ setting, err = createSetting(ctx, tx, create)
+ if err != nil {
+ return nil, err
+ }
+
+ if err := tx.Commit(); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return setting, nil
+ }
return setting, nil
}
@@ -74,7 +74,6 @@ func (s *SettingService) FindSettingList(ctx context.Context, find *api.SettingF
}
// FindSetting retrieves a single setting based on find.
-// Returns ENOTFOUND if no matching record.
// Returns ECONFLICT if finding more than 1 matching records.
func (s *SettingService) FindSetting(ctx context.Context, find *api.SettingFind) (*api.Setting, error) {
tx, err := s.db.BeginTx(ctx, nil)
@@ -86,8 +85,10 @@ func (s *SettingService) FindSetting(ctx context.Context, find *api.SettingFind)
list, err := findSettingList(ctx, tx, find)
if err != nil {
return nil, err
- } else if len(list) == 0 {
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("setting 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 activities with filter %+v, expect 1. ", len(list), find)}
}
diff --git a/store/task.go b/store/task.go
index a6c63cc9d5..6bbfb3714e 100644
--- a/store/task.go
+++ b/store/task.go
@@ -226,8 +226,10 @@ func (s *TaskService) findTask(ctx context.Context, tx *Tx, find *api.TaskFind)
list, err := s.findTaskList(ctx, tx, find)
if err != nil {
return nil, err
- } else if len(list) == 0 {
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("task 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 tasks with filter %+v, expect 1", len(list), find)}
}