feat: add conflict handling on create/Many/AndReturn (Upsert) and add support for composite PKs - #45
Merged
Merged
Conversation
… support for composite primary keys 1. model_create.gotpl: - Define OnConflict() on the create/Many/AndReturnBuilder that returns a model specific ConflictBuilder - Replace the single idCol with a pkCols []string, either generated from a scalar field marked as IsID or a composite PK (fixes the bug in categoryToPost id="" resulting in WHERE "" = ?) 2- relations_runtime.gotpl: - change the signature of buildBulkInsertSQL to accept pkCols, conflict Tagret/Action 3- model_predicate.gotpl: - Generate package-level instances of CompositeUniqueConstraint for all model compound indexes/unique constraints, and defined aliases for package Upsert and ConflictBuilder - Add helper for compound primary key, simmilar to compound unique constraint functionality-wise, but to differentiate between compound unique 4- client.gotpl: - Modify the Dialect interface to receive conflict targets and custom updates in ConflictClause and remove InsertPrefix, to support SQL-compliant ON CONFLICT clause generation for all dialects, replacing the older dialect-specific prefixes, without having to duplicate much - Implement UniqueColumns() (from UniqueConstraintTarget inteface) on UniqueField and StringUniqueField to allow standard unique fields generated on delegates to act directly as UniqueConstraintTarget params in the OnConflict() builder 5- builders_create.gotpl: - Replace the bool skipDuplicates func with conflictTarget to allow create builders to support generic conflict actions (ignore, UpdateNewValues and custom update func) - replace row.Scan in execute insert with a buffered query stream (next, scan, close) to prevent connection leaks and ensure rows are properly closed as it was erroring on conflict without properly returning nil for records but retruning sql.ErrNoRows - Update the fallback SQL generation to check all primary key columns to support dialects that don't have RETURNING when fetching with composite PKs 6- runtime.gotpl: - Implement computeNonConflictCols to determine non-identifying cols that should be automatically set when UpdateNewValues() is called avoiding updating the PK and the conflicting cols - Add buildConflictClause() to generate dialect-specific ON CONFLICT CLAUSE (define a common interface to retrieve col names on conflict, have unique fields and constraints implement it, so it's easy to use in the OnConflict func, define OnConflict itself on create builders, define Ignore(), UpdateNewValues() and custom Update()m handle the conflict logic in sql insertion, fix ID to slice instead of scalar resolving to scalar in case of scalar BUT, supporting composite ids now)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
2- relations_runtime.gotpl:
3- model_predicate.gotpl:
4- client.gotpl:
5- builders_create.gotpl:
6- runtime.gotpl:
(basically define a common interface to retrieve col names on conflict, have unique fields and constraints implement it, so it's easy to use in the OnConflict func, define OnConflict itself on create builders, define Ignore(), UpdateNewValues() and custom Update()m handle the conflict logic in sql insertion, fix ID to slice instead of scalar resolving to scalar in case of scalar BUT, supporting composite ids now)