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

feat: add mechanism for prefixing queries with a comment string #1002

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kylemcc
Copy link
Contributor

@kylemcc kylemcc commented Jul 1, 2024

This change proposes a new Comment(string) method and ContextWithComment(context.Context, string) function for prepending a comment to a SQL query.

Here's simple example:

// using the query API
err := db.NewSelect().
    Model(&products).
    Where("description like ?", d).
    Comment("customer:"+customerID).
    Scan(ctx)

// using a context
qctx := bun.ContextWithComment(ctx, "customer:"+customerID)
err := db.NewSelect().
    Model(&products).
    Where("description LIKE ?", d).
    Scan(qctx)

This would produce a query that looks like:

/*customer:01J1P97P3AP7V287B57P7055CZ*/ SELECT * FROM products WHERE description LIKE '%red%'

This may warrant a bit of discussion first, but I wanted to include the code with the proposal. I have had this code running in production since April with no problems. It appears that others may be interested in this as well: #998

My immediate use case for this is to add a comment to a query with tracing/debug information that will show up in the process list/pg_stat_activity, but there are certainly other uses for this functionality.

query_base.go Outdated
q.comment = c
}

func (q *baseQuery) appendComment(b []byte) []byte {
Copy link
Member

Choose a reason for hiding this comment

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

Could you please rework this to accept ctx context.Context and then extract the comment from the context in this function?

Then we can remove commentFromContext.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review @vmihailenco! Updated as you requested and rebased on top of master.

@vmihailenco
Copy link
Member

@kylemcc This looks useful, but I have a minor refactoring request. Please take a look when you have time.

if c, _ := ctx.Value(queryCommentCtxKey{}).(string); c == "" {
return b
} else {
q.comment = c
Copy link
Member

Choose a reason for hiding this comment

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

Can you please change this so it does not modify the baseQuery? So the following is possible:

q := NewSelectQuery()
q.AppendQuery(ctxWithComment1) // one comment
q.AppendQuery(ctxWithComment2) // another comment

Also, one might think that both comments must be appended: one from the query and one from the context. I will leave it to you to decide what to do in such case.

Copy link
Member

Choose a reason for hiding this comment

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

If multiple comments are allowed, then we can also consider supporting this: NewSelect().Comment("comment1").Comment("comment2").

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you please change this so it does not modify the baseQuery? So the following is possible:

q := NewSelectQuery()
q.AppendQuery(ctxWithComment1) // one comment
q.AppendQuery(ctxWithComment2) // another comment

Also, one might think that both comments must be appended: one from the query and one from the context. I will leave it to you to decide what to do in such case.

Ah, yes, good catch. Didn't think about the AppendQuery use case.

If multiple comments are allowed, then we can also consider supporting this: NewSelect().Comment("comment1").Comment("comment2").

This seems reasonable, too. Let me experiment with the formatting a bit to see what looks best.

@vmihailenco
Copy link
Member

@kylemcc any chance you can finish this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants