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

fix: Dont set None audit "context values". #99

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## 0.15

### 0.15.5

- fix: Dont set `None` audit "context values".

### 0.15.4

- fix: Undo accidental inclusion of alembic as required dependencies.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "sqlalchemy-declarative-extensions"
version = "0.15.4"
version = "0.15.5"
authors = [
{name = "Dan Cardin", email = "ddcardin@gmail.com"},
]
Expand Down
3 changes: 3 additions & 0 deletions src/sqlalchemy_declarative_extensions/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ def create_audit_triggers(
def set_context_values(connectable, **values):
"""Set transaction-local context values, to be included on audit-tables."""
for name, value in values.items():
if value is None:
continue

statement = f"SET LOCAL audit.{name} = '{value}'"
connectable.execute(text(statement))

Expand Down
18 changes: 17 additions & 1 deletion tests/audit/test_context_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ class Base(_Base): # type: ignore # type: ignore
__abstract__ = True


context_columns = [Column("username", types.Unicode(), nullable=False)]
context_columns = [
Column("username", types.Unicode(), nullable=False),
Column("nickname", types.Unicode(), nullable=True),
]


@audit(context_columns=context_columns)
Expand Down Expand Up @@ -48,6 +51,19 @@ def test_sets_session_values(pg):
assert audit_row.audit_username == "foo@foo.com"


def test_sets_session_value_to_none(pg):
Base.metadata.create_all(bind=pg.connection())
pg.commit()

set_context_values(pg, username="foo@foo.com", nickname=None)
pg.add(Foo(id=1, name=None, json=None))
pg.commit()

audit_row = pg.execute(Foo.__audit_table__.select()).fetchone()
assert audit_row.audit_username == "foo@foo.com"
assert audit_row.audit_nickname is None


def test_fails_to_set_session_values(pg):
Base.metadata.create_all(bind=pg.connection())
pg.commit()
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading