Skip to content

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

refactor: store return nil on column, view, table non-existence (#304)

spinningbotfc8ca3467f2f
diff --git a/server/database.go b/server/database.go
index d3b4735276..f4fa0e2b9b 100644
--- a/server/database.go
+++ b/server/database.go
@@ -383,6 +383,9 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch table for database id: %d, table name: %s", id, tableName)).SetInternal(err)
}
+ if table == nil {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("table %q not found from database %v", tableName, id)).SetInternal(err)
+ }
table.Database = database
diff --git a/server/sql.go b/server/sql.go
index 3f8fd538ac..52d4b3de9b 100644
--- a/server/sql.go
+++ b/server/sql.go
@@ -271,27 +271,27 @@ func (s *Server) syncEngineVersionAndSchema(ctx context.Context, instance *api.I
TableID: &upsertedTable.ID,
Name: &column.Name,
}
- _, err := s.ColumnService.FindColumn(ctx, columnFind)
+ col, err := s.ColumnService.FindColumn(ctx, columnFind)
if err != nil {
- if common.ErrorCode(err) == common.NotFound {
- columnCreate := &api.ColumnCreate{
- CreatorID: api.SystemBotID,
- DatabaseID: database.ID,
- TableID: upsertedTable.ID,
- Name: column.Name,
- Position: column.Position,
- Default: column.Default,
- Nullable: column.Nullable,
- Type: column.Type,
- CharacterSet: column.CharacterSet,
- Collation: column.Collation,
- Comment: column.Comment,
- }
- if err := createColumn(database, upsertedTable, columnCreate); err != nil {
- return err
- }
- } else {
- return fmt.Errorf("failed to sync column for instance: %s, database: %s, table: %s. Error %w", instance.Name, database.Name, upsertedTable.Name, err)
+ return fmt.Errorf("failed to sync column for instance: %s, database: %s, table: %s. Error %w", instance.Name, database.Name, upsertedTable.Name, err)
+ }
+ // Create column if not exists yet.
+ if col == nil {
+ columnCreate := &api.ColumnCreate{
+ CreatorID: api.SystemBotID,
+ DatabaseID: database.ID,
+ TableID: upsertedTable.ID,
+ Name: column.Name,
+ Position: column.Position,
+ Default: column.Default,
+ Nullable: column.Nullable,
+ Type: column.Type,
+ CharacterSet: column.CharacterSet,
+ Collation: column.Collation,
+ Comment: column.Comment,
+ }
+ if err := createColumn(database, upsertedTable, columnCreate); err != nil {
+ return err
}
}
}
diff --git a/store/column.go b/store/column.go
index 62fde4e9c7..ed484a9f01 100644
--- a/store/column.go
+++ b/store/column.go
@@ -63,7 +63,6 @@ func (s *ColumnService) FindColumnList(ctx context.Context, find *api.ColumnFind
}
// FindColumn retrieves a single column based on find.
-// Returns ENOTFOUND if no matching record.
// Returns ECONFLICT if finding more than 1 matching records.
func (s *ColumnService) FindColumn(ctx context.Context, find *api.ColumnFind) (*api.Column, error) {
tx, err := s.db.BeginTx(ctx, nil)
@@ -75,8 +74,10 @@ func (s *ColumnService) FindColumn(ctx context.Context, find *api.ColumnFind) (*
list, err := s.findColumnList(ctx, tx, find)
if err != nil {
return nil, err
- } else if len(list) == 0 {
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("column 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 columns with filter %+v, expect 1", len(list), find)}
}
diff --git a/store/table.go b/store/table.go
index 612cf6742d..23bbd689e5 100644
--- a/store/table.go
+++ b/store/table.go
@@ -62,7 +62,6 @@ func (s *TableService) FindTableList(ctx context.Context, find *api.TableFind) (
}
// FindTable retrieves a single table based on find.
-// Returns ENOTFOUND if no matching record.
// Returns ECONFLICT if finding more than 1 matching records.
func (s *TableService) FindTable(ctx context.Context, find *api.TableFind) (*api.Table, error) {
tx, err := s.db.BeginTx(ctx, nil)
@@ -74,8 +73,10 @@ func (s *TableService) FindTable(ctx context.Context, find *api.TableFind) (*api
list, err := s.findTableList(ctx, tx, find)
if err != nil {
return nil, err
- } else if len(list) == 0 {
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("table 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 tables with filter %+v, expect 1", len(list), find)}
}
diff --git a/store/view.go b/store/view.go
index 1c1643c950..cf22ea6b77 100644
--- a/store/view.go
+++ b/store/view.go
@@ -62,7 +62,6 @@ func (s *ViewService) FindViewList(ctx context.Context, find *api.ViewFind) ([]*
}
// FindView retrieves a single view based on find.
-// Returns ENOTFOUND if no matching record.
// Returns ECONFLICT if finding more than 1 matching records.
func (s *ViewService) FindView(ctx context.Context, find *api.ViewFind) (*api.View, error) {
tx, err := s.db.BeginTx(ctx, nil)
@@ -74,8 +73,10 @@ func (s *ViewService) FindView(ctx context.Context, find *api.ViewFind) (*api.Vi
list, err := s.findViewList(ctx, tx, find)
if err != nil {
return nil, err
- } else if len(list) == 0 {
- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("view 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 views with filter %+v, expect 1", len(list), find)}
}