-
-
Notifications
You must be signed in to change notification settings - Fork 90
feat(psql merge): implement MERGE statement support with comprehensive test cases (issue #593) #623
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
base: main
Are you sure you want to change the base?
Conversation
atzedus
commented
Jan 30, 2026
- Added Merge function to create MERGE queries in psql dialect.
- Introduced mm package for building MERGE query components including Into, Using, WhenMatched, and WhenNotMatched clauses.
- Implemented various actions for matched and not matched conditions, including updates, inserts, and deletions.
- Created extensive test cases covering multiple scenarios for the MERGE statement, ensuring correct SQL generation and argument handling.
…e test cases - Added Merge function to create MERGE queries in psql dialect. - Introduced mm package for building MERGE query components including Into, Using, WhenMatched, and WhenNotMatched clauses. - Implemented various actions for matched and not matched conditions, including updates, inserts, and deletions. - Created extensive test cases covering multiple scenarios for the MERGE statement, ensuring correct SQL generation and argument handling.
|
Thank you so so much for this PR. From my first overview it seems good. However, I think I'd like to do some reorganisation of some of the things placed in the In addition, remember to add a note in the |
…utomatic RETURNING support
|
@stephenafamo what do you think about this commit? 46eb21a BackgroundThe MERGE statement implementation in psql requires awareness of the PostgreSQL version because certain features are only available in specific versions:
When using Alternatives Considered1. Field in dialect structtype dialect struct {
Version int
}
func SetVersion(version int) {
Dialect.Version = version
}Pros: Centralized, can be set once at startup 2. Field in Table structtype Table[...] struct {
Version int
}
table.Version = 17Pros: Per-table configuration, explicit in code 3. Functional Options at Table CreationNewTablex(..., WithVersion(17))Pros: Explicit configuration, immutable after creation 4. Context-based Configuration (Chosen)ctx := psql.SetVersion(ctx, 17)
results, err := table.Merge(...).All(ctx, db)Pros: Thread-safe, supports different versions per request, no global state, follows Go idioms DecisionContext-based configuration was chosen because:
Usage Examples// Set version in context
ctx := psql.SetVersion(context.Background(), 17)
// MERGE will automatically add RETURNING for version 17+
results, err := table.Merge(...).All(ctx, db)API Reference// SetVersion sets the major version in the context.
func SetVersion(ctx context.Context, version int) context.Context
// GetVersion returns the major version from the context (0 if not set).
func GetVersion(ctx context.Context) int
// VersionAtLeast checks if the version in context is at least the given version.
func VersionAtLeast(ctx context.Context, minVersion int) bool |
|
I think the versioning method is quite elegant. Would work quite nicely for me since I usually set my parent context at the very beginning of |
|
Thank you! The context-based approach was chosen specifically to avoid global state and make version configuration thread-safe and flexible. For MERGE specifically, setting version to 17+ enables automatic A similar versioning approach could potentially be applied to other dialects (MySQL, SQLite) that have version-specific features as well. Additionally, for PostgreSQL itself, it might be useful for supporting other PostgreSQL 17+ features like the extended I understand that the changes in this PR are quite extensive, so please take your time to review and think through the code. To be honest, I'm not entirely sure I've done everything correctly myself, so I would really appreciate your feedback and guidance. I'm happy to address any concerns or make adjustments as needed. |
|
Thanks again. I'll need to find a block of time to be able to properly review this, so it may take a bit. But feel free to nudge me if you feel I'm taking too long 😅 |
3b49cef to
f8f30c5
Compare