bun icon indicating copy to clipboard operation
bun copied to clipboard

feat: add mechanism for prefixing queries with a comment string

Open kylemcc opened this issue 1 year ago • 1 comments

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 avatar Jul 01 '24 04:07 kylemcc

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

vmihailenco avatar Aug 29 '24 13:08 vmihailenco

@kylemcc any chance you can finish this?

vmihailenco avatar Oct 26 '24 06:10 vmihailenco

@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

kylemcc avatar Nov 12 '24 17:11 kylemcc

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!

github-actions[bot] avatar Dec 13 '24 03:12 github-actions[bot]