Skip to content

Commit

Permalink
Adds a setting to define the correlation id placement injected in the…
Browse files Browse the repository at this point in the history
… SQL statement. (#68)
  • Loading branch information
CaueP authored and dbaty committed Sep 20, 2023
1 parent 286411b commit 6b5d688
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
4 changes: 3 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ History
2.4 (unreleased)
++++++++++++++++

- Nothing changed yet.
- Add `CID_SQL_STATEMENT_TEMPLATE` setting to customize the position
of the correlation relative to the original SQL statement.
Contributed by Cauê Garcia Polimanti (@CaueP).


2.3 (2022-06-13)
Expand Down
7 changes: 6 additions & 1 deletion cid/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


DEFAULT_CID_SQL_COMMENT_TEMPLATE = 'cid: {cid}'
DEFAULT_SQL_STATEMENT_TEMPLATE = '/* {cid} */\n{sql}'


class CidCursorWrapper:
Expand Down Expand Up @@ -31,12 +32,16 @@ def add_comment(self, sql):
cid_sql_template = getattr(
settings, 'CID_SQL_COMMENT_TEMPLATE', DEFAULT_CID_SQL_COMMENT_TEMPLATE
)
sql_statement_template = getattr(
settings, 'CID_SQL_STATEMENT_TEMPLATE', DEFAULT_SQL_STATEMENT_TEMPLATE
)
cid = get_cid()
if not cid:
return sql
cid = cid.replace('/*', r'\/\*').replace('*/', r'\*\/')
cid = cid_sql_template.format(cid=cid)
return f"/* {cid} */\n{sql}"
statement = sql_statement_template.format(cid=cid, sql=sql)
return statement

# The following methods cannot be implemented in __getattr__, because the
# code must run when the method is invoked, not just when it is accessed.
Expand Down
9 changes: 9 additions & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,15 @@ a string with a ``cid`` format parameter:
CID_SQL_COMMENT_TEMPLATE = 'correlation={cid}'
Also, you may change the position of the correlation id injected in
the statement by defining a ``CID_SQL_STATEMENT_TEMPLATE`` that is
a string with a ``cid`` and a ``sql`` format parameter:

.. code-block:: python
CID_SQL_STATEMENT_TEMPLATE = '/* {cid} */\n{sql}'
Inclusion of the correlation id in templates
--------------------------------------------

Expand Down
10 changes: 10 additions & 0 deletions tests/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ def test_adds_comment_setting_overriden(self, get_cid):
self.cursor_wrapper.add_comment("SELECT 1;")
)

@override_settings(CID_SQL_STATEMENT_TEMPLATE='{sql}\n/* {cid} */')
@mock.patch('cid.cursor.get_cid')
def test_adds_comment_with_statement_template_setting_overriden(self, get_cid):
get_cid.return_value = 'testing-cursor-after-sql-statement'
expected = "SELECT 1;\n/* cid: testing-cursor-after-sql-statement */"
self.assertEqual(
expected,
self.cursor_wrapper.add_comment("SELECT 1;")
)

@mock.patch('cid.cursor.get_cid')
def test_no_comment_when_cid_is_none(self, get_cid):
get_cid.return_value = None
Expand Down

0 comments on commit 6b5d688

Please sign in to comment.