Skip to content

Commit

Permalink
Fix the end of sync query when not data is present (#483)
Browse files Browse the repository at this point in the history
When the resync ends and no data exists in the k8s API for some resource
then the "NotIn" clause on the database query throws an error since the
list passed to it is empty.  If no data exists in the API server then we
simply want to delete ALL records therefore we don't need to supply a
where clause.

Signed-off-by: Allain Legacy <[email protected]>
  • Loading branch information
alegacy authored Jan 20, 2025
1 parent 6ad6c09 commit 05f7e9f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 9 additions & 2 deletions internal/service/cluster/db/repo/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/google/uuid"
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/dialect/psql"

"github.com/openshift-kni/oran-o2ims/internal/service/cluster/db/models"
Expand Down Expand Up @@ -36,7 +37,10 @@ func (r *ClusterRepository) GetNodeClusters(ctx context.Context) ([]models.NodeC
// GetNodeClustersNotIn returns the list of NodeCluster records not matching the list of keys provided, or an empty list
// if none exist; otherwise an error
func (r *ClusterRepository) GetNodeClustersNotIn(ctx context.Context, keys []any) ([]models.NodeCluster, error) {
e := psql.Quote(models.NodeCluster{}.PrimaryKey()).NotIn(psql.Arg(keys...))
var e bob.Expression = nil
if len(keys) > 0 {
e = psql.Quote(models.NodeCluster{}.PrimaryKey()).NotIn(psql.Arg(keys...))
}
return utils.Search[models.NodeCluster](ctx, r.Db, e)
}

Expand Down Expand Up @@ -110,7 +114,10 @@ func (r *ClusterRepository) GetClusterResources(ctx context.Context) ([]models.C
// GetClusterResourcesNotIn returns the list of ClusterResource records not matching the list of keys provided, or an
// empty list if none exist; otherwise an error
func (r *ClusterRepository) GetClusterResourcesNotIn(ctx context.Context, keys []any) ([]models.ClusterResource, error) {
e := psql.Quote(models.ClusterResource{}.PrimaryKey()).NotIn(psql.Arg(keys...))
var e bob.Expression = nil
if len(keys) > 0 {
e = psql.Quote(models.ClusterResource{}.PrimaryKey()).NotIn(psql.Arg(keys...))
}
return utils.Search[models.ClusterResource](ctx, r.Db, e)
}

Expand Down
6 changes: 5 additions & 1 deletion internal/service/resources/db/repo/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/google/uuid"
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/dialect/psql"

"github.com/openshift-kni/oran-o2ims/internal/service/common/repo"
Expand All @@ -24,7 +25,10 @@ func (r *ResourcesRepository) GetDeploymentManagers(ctx context.Context) ([]mode
// GetDeploymentManagersNotIn returns the list of DeploymentManager records not matching the list of keys provided, or
// an empty list if none exist; otherwise an error
func (r *ResourcesRepository) GetDeploymentManagersNotIn(ctx context.Context, keys []any) ([]models.DeploymentManager, error) {
e := psql.Quote(models.DeploymentManager{}.PrimaryKey()).NotIn(psql.Arg(keys...))
var e bob.Expression = nil
if len(keys) > 0 {
e = psql.Quote(models.DeploymentManager{}.PrimaryKey()).NotIn(psql.Arg(keys...))
}
return utils.Search[models.DeploymentManager](ctx, r.Db, e)
}

Expand Down

0 comments on commit 05f7e9f

Please sign in to comment.