bun
bun copied to clipboard
feat: add mechanism for prefixing queries with a comment string
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: https://github.com/uptrace/bun/issues/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.
@kylemcc This looks useful, but I have a minor refactoring request. Please take a look when you have time.
@kylemcc any chance you can finish this?
@kylemcc any chance you can finish this?
Apologies @vmihailenco, got busy with other things. Let me see if I can get this cleaned up today or tomorrow
This pull request has been automatically marked as stale because it has not had activity in the last 30 days. If there is no update within the next 7 days, this pr will be closed. Please feel free to give a status update now, ping for review, when it's ready. Thank you for your contributions!