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

Conversation

mlguerrero12
Copy link
Contributor

@mlguerrero12 mlguerrero12 commented Dec 6, 2024

Provide helper Execute query functions to run more complex queries. Clean up existing basic CRUD operations

@openshift-ci openshift-ci bot requested review from browsell and donpenney December 6, 2024 15:46
@mlguerrero12
Copy link
Contributor Author

@alegacy, I believe this will simplify a lot the code. By moving the build of the query in the db repositories, we can easily construct more complex queries with many records. It looks cleaner. Please have a look

@alegacy
Copy link
Collaborator

alegacy commented Dec 6, 2024

@alegacy, I believe this will simplify a lot the code. By moving the build of the query in the db repositories, we can easily construct more complex queries with many records. It looks cleaner. Please have a look

Let's discuss next week. I must be missing something because this seems to have moved what was a relatively manageable set of generic functions back out to the repository and duplicates the logical in dozens of places.

If any particular use cases needed a more complex query there was nothing stopping us from doing that on a case-by-case basis. Having the typical Find/Create/Delete cases handled generically seemed like a better approach short of using a proper ORM to do the heavy lifting.

@mlguerrero12 mlguerrero12 force-pushed the refactorgenerics branch 2 times, most recently from 9313a98 to e40eb90 Compare December 9, 2024 17:18
Provide helper Execute query functions to run more
complex queries. Clean up existing basic CRUD operations

Signed-off-by: Marcelo Guerrero <[email protected]>
@mlguerrero12
Copy link
Contributor Author

@alegacy, I believe this will simplify a lot the code. By moving the build of the query in the db repositories, we can easily construct more complex queries with many records. It looks cleaner. Please have a look

Let's discuss next week. I must be missing something because this seems to have moved what was a relatively manageable set of generic functions back out to the repository and duplicates the logical in dozens of places.

If any particular use cases needed a more complex query there was nothing stopping us from doing that on a case-by-case basis. Having the typical Find/Create/Delete cases handled generically seemed like a better approach short of using a proper ORM to do the heavy lifting.

As we discussed internally, my main concerned was that it seemed we were maintaining a library here. There was no clear convention of what was a basic generic operation. I didn't want us in the future to continue adding arguments to fulfill all needs. For instance, if someone needed more flexibility with the upsert operations (which might be very complex itself) or add support for bulk operations with Insert or Update.

I do understand your point about repeating code. Although, I believe it was ok in this case.

So, as discussed, I left the basic crud operations and emphasized that Insert and Update are for a single record. I cleaned up a bit. I removed the generic functions for upsert. The queries for upsert are now built in the repo file. I also introduced 2 generic functions to execute queries which are used by the existing crud operations and can be used outside to run more complex queries.

Please have a look again, @alegacy

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()

@alegacy alegacy self-requested a review December 9, 2024 20:20
Copy link
Collaborator

@alegacy alegacy left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Dec 9, 2024
Copy link

openshift-ci bot commented Dec 9, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alegacy

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 9, 2024
@openshift-merge-bot openshift-merge-bot bot merged commit acf397c into openshift-kni:main Dec 9, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants