Skip to content

Add example of setting session vars with a session, engine, and conne… #7

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ SQLAlchemy Postgresql Audit
example
alembic-integration
naming-conventions
session-settings
contributing
roadmap
contributors
Expand Down
42 changes: 42 additions & 0 deletions docs/source/session-settings.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Session Settings
----------------

In order to enrich the audit tables with information, we can set session settings that live for the duration of a transaction.


**SQLAlchemy Session**

.. code-block:: python

from sqlalchemy_postgresql_audit import set_session_vars
from sqlalchemy.orm import sessionmaker

Session = sessionmaker(bind=engine)
session = Session()

set_session_vars(session, username='huntcsg')
session.execute(insert_stmt, { ... values ... })
session.commit()



**SQLAlchemy Connection**

.. code-block:: python

from sqlalchemy_postgresql_audit import set_session_vars

with engine.connect() as conn:
with conn.begin() as trans:
set_session_vars(conn, username='huntcsg')
conn.execute(insert_stmt, { ... values ... })


**SQLAlchemy Engine**

.. code-block:: python

from sqlalchemy_postgresql_audit import set_session_vars
with engine.begin() as conn:
set_session_vars(conn, username='huntcsg')
conn.execute(insert_stmt, { ... values ... })
20 changes: 20 additions & 0 deletions examples/alembic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Instructions

1. Install package and dependencies
```bash
$ pip install sqlalchemy-postgresql-audit
$ pip install psycopg2
$ pip install alembic
$ pip install -e test-app

2. Run

```bash
$ docker-compose up -d

3. Run

```bash
$ alembic upgrade head

4. Database now has tables, audit tables, and triggers.
19 changes: 19 additions & 0 deletions examples/plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Instructions

1. Install package and dependencies
```bash
$ pip install sqlalchemy-postgresql-audit
$ pip install psycopg2

2. Run

```bash
$ docker-compose up -d

3. Run

```bash
$ python plugin.py

4. Database now has tables, audit tables, and triggers.

18 changes: 18 additions & 0 deletions examples/simple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Instructions

1. Install package and dependencies
```bash
$ pip install sqlalchemy-postgresql-audit
$ pip install psycopg2

2. Run

```bash
$ docker-compose up -d

3. Run

```bash
$ python simple.py

4. Database now has tables, audit tables, and triggers.