-
-
Notifications
You must be signed in to change notification settings - Fork 407
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 support for header comments in ORM-generated queries #2011
Conversation
feat: Add query comments to headers for improved debugging This feature enhances debugging by including query comments in headers, allowing developers to trace the query's origin. By matching the query comment in the header, it's easier to identify the specific location in the code where the query was executed.
I reverted the changes in db_test that were made by mistake. Should I squash the commits? |
I think this is a reasonable thing to add, but I don't think it should be via the context provider as that makes things a bit odd. Let's say I want to pass something like both the trace ID and the current user's account ID in, I would have to create a new context just for that and add the value; with at least one of those values already being on the context in another shape (the trace ID via otel at least). And as I understand it; unwrapping the context is not expensive, but its not cheap either? What would you think of just adding a function to the query struct that would do what youre already doing, but would allow an arbitrary string to be passed to it and maybe escaped? That way the way the comment gets onto the query is relatively implementation agnostic, someone could still pass data via context down and simply plop it in via before query. Or someone could add it only for specific queries? |
I wouldn't worry about squashing commits, github can take care of that |
Sure
I will check how I could implement it in the Query struct. I will keep you updated |
Hi @elliotcourant, |
Now it works this way
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great to me so far, Would you be able to add some tests to make sure that its escaping things properly and then I think this should be fine to merge.
Thank you @elliotcourant! |
Hi Everyone!
A common challenge when using ORMs is the difficulty of correlating an executed query in the DBMS with the lines of code that triggered it. To solve this issue, it would be helpful to add a comment in the header of the generated query. In this implementation, if a context with a comment is provided, we’ll include that as a comment in the query.
Since I couldn’t find an existing feature to add header comments to queries, I’ve built this functionality myself.
Here’s an example of how to use it:
This will generate an INSERT statement with the following comment:
I’ve added two public functions:
SetQueryComment
andGetQueryComment
. On the one hand, we could keep onlySetQueryComment
(and potentially remove GetQueryComment), but on the other hand,GetQueryComment
may be useful for integration with other wrappers and services that require access to the query comment from context