From 9d0103be7ca0a6a7a9c675b5172f732b25eb054b Mon Sep 17 00:00:00 2001 From: Hunter Senft-Grupp Date: Mon, 24 Jun 2019 03:15:26 -0400 Subject: [PATCH 1/2] Add example of setting session vars with a session, engine, and connection --- docs/source/index.rst | 1 + docs/source/session-settings.rst | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 docs/source/session-settings.rst diff --git a/docs/source/index.rst b/docs/source/index.rst index 49b18b6..be5ea87 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -10,6 +10,7 @@ SQLAlchemy Postgresql Audit example alembic-integration naming-conventions + session-settings contributing roadmap contributors diff --git a/docs/source/session-settings.rst b/docs/source/session-settings.rst new file mode 100644 index 0000000..65a529a --- /dev/null +++ b/docs/source/session-settings.rst @@ -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 ... }) From f7b17244138370d62b530f2ac484fe22d2ad0e18 Mon Sep 17 00:00:00 2001 From: Hunter Senft-Grupp Date: Mon, 24 Jun 2019 03:22:20 -0400 Subject: [PATCH 2/2] Add example READMEs --- examples/alembic/README.md | 20 ++++++++++++++++++++ examples/plugin/README.md | 19 +++++++++++++++++++ examples/simple/README.md | 18 ++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 examples/alembic/README.md create mode 100644 examples/plugin/README.md create mode 100644 examples/simple/README.md diff --git a/examples/alembic/README.md b/examples/alembic/README.md new file mode 100644 index 0000000..1c53b7f --- /dev/null +++ b/examples/alembic/README.md @@ -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. diff --git a/examples/plugin/README.md b/examples/plugin/README.md new file mode 100644 index 0000000..c32ee16 --- /dev/null +++ b/examples/plugin/README.md @@ -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. + diff --git a/examples/simple/README.md b/examples/simple/README.md new file mode 100644 index 0000000..825aa1d --- /dev/null +++ b/examples/simple/README.md @@ -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.