Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor DB generics #382

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's really unfortunately that they sometimes accept any and other times they accept string because here we could have used tags.Columns()


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
Loading