Skip to content

Commit

Permalink
Refactor DB generics
Browse files Browse the repository at this point in the history
Provide helper Execute query functions to run more
complex queries. Clean up existing basic CRUD operations

Signed-off-by: Marcelo Guerrero <[email protected]>
  • Loading branch information
mlguerrero12 committed Dec 9, 2024
1 parent d4afb17 commit 128880a
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 168 deletions.
49 changes: 37 additions & 12 deletions internal/service/alarms/internal/db/repo/alarms_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/dialect/psql"
"github.com/stephenafamo/bob/dialect/psql/dialect"
"github.com/stephenafamo/bob/dialect/psql/im"

"github.com/openshift-kni/oran-o2ims/internal/service/alarms/internal/db/models"
"github.com/openshift-kni/oran-o2ims/internal/service/common/utils"
Expand Down Expand Up @@ -45,35 +47,58 @@ func (ar *AlarmsRepository) DeleteAlarmDefinitionsNotIn(ctx context.Context, ids

// UpsertAlarmDictionary inserts or updates an alarm dictionary record
func (ar *AlarmsRepository) UpsertAlarmDictionary(ctx context.Context, record models.AlarmDictionary) ([]models.AlarmDictionary, error) {
tags := utils.GetDBTagsFromStructFields(record, "AlarmDictionaryVersion", "EntityType", "Vendor", "ResourceTypeID")
dbModel := models.AlarmDictionary{}

// Important to keep the order. The order of tags.Columns().. is not deterministic
values := []bob.Expression{psql.Arg(record.AlarmDictionaryVersion, record.EntityType, record.Vendor, record.ResourceTypeID)}
records, err := utils.UpsertOnConflict[models.AlarmDictionary](ctx, ar.Db, []string{tags["AlarmDictionaryVersion"], tags["EntityType"], tags["Vendor"], tags["ResourceTypeID"]}, values)
tags := utils.GetDBTagsFromStructFields(dbModel, "AlarmDictionaryVersion", "EntityType", "Vendor", "ResourceTypeID")

columns := []string{tags["AlarmDictionaryVersion"], tags["EntityType"], tags["Vendor"], tags["ResourceTypeID"]}

query := psql.Insert(
im.Into(record.TableName(), columns...),
im.Values(psql.Arg(record.AlarmDictionaryVersion, record.EntityType, record.Vendor, record.ResourceTypeID)),
im.OnConflict(record.OnConflict()).DoUpdate(
im.SetExcluded(columns...)),
im.Returning(record.PrimaryKey()),
)

sql, args, err := query.Build()
if err != nil {
return nil, fmt.Errorf("failed to upsert alarm dictionary: %w", err)
return nil, fmt.Errorf("failed to build query: %w", err)
}

return records, nil
return utils.ExecuteCollectRows[models.AlarmDictionary](ctx, ar.Db, sql, args)
}

// UpsertAlarmDefinitions inserts or updates alarm definition records
func (ar *AlarmsRepository) UpsertAlarmDefinitions(ctx context.Context, records []models.AlarmDefinition) ([]models.AlarmDefinition, error) {
dbModel := models.AlarmDefinition{}

if len(records) == 0 {
return nil, nil
}

tags := utils.GetDBTagsFromStructFields(records[0], "AlarmName", "AlarmLastChange", "AlarmDescription", "ProposedRepairActions", "AlarmAdditionalFields", "AlarmDictionaryID", "Severity")
var values []bob.Expression
columns := []string{tags["AlarmName"], tags["AlarmLastChange"], tags["AlarmDescription"], tags["ProposedRepairActions"], tags["AlarmAdditionalFields"], tags["AlarmDictionaryID"], tags["Severity"]}

modInsert := []bob.Mod[*dialect.InsertQuery]{
im.Into(dbModel.TableName(), columns...),
im.OnConflictOnConstraint(dbModel.OnConflict()).DoUpdate(
im.SetExcluded(columns...)),
im.Returning(dbModel.PrimaryKey()),
}

for _, record := range records {
// Important to keep the order. The order of tags.Columns().. is not deterministic
values = append(values, psql.Arg(record.AlarmName, record.AlarmLastChange, record.AlarmDescription, record.ProposedRepairActions, record.AlarmAdditionalFields, record.AlarmDictionaryID, record.Severity))
modInsert = append(modInsert, im.Values(psql.Arg(record.AlarmName, record.AlarmLastChange, record.AlarmDescription, record.ProposedRepairActions, record.AlarmAdditionalFields, record.AlarmDictionaryID, record.Severity)))
}

records, err := utils.UpsertOnConflictConstraint[models.AlarmDefinition](ctx, ar.Db, []string{tags["AlarmName"], tags["AlarmLastChange"], tags["AlarmDescription"], tags["ProposedRepairActions"], tags["AlarmAdditionalFields"], tags["AlarmDictionaryID"], tags["Severity"]}, values)
query := psql.Insert(
modInsert...,
)

sql, args, err := query.Build()
if err != nil {
return nil, fmt.Errorf("failed to upsert alarm dictionary: %w", err)
return nil, fmt.Errorf("failed to build query: %w", err)
}

return records, nil
return utils.ExecuteCollectRows[models.AlarmDefinition](ctx, ar.Db, sql, args)
}
Loading

0 comments on commit 128880a

Please sign in to comment.