-
Notifications
You must be signed in to change notification settings - Fork 54
Pass autocomit to the janitor's load methods - closes #902 #1383
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
base: main
Are you sure you want to change the base?
Changes from all commits
04c1be5
2f03adb
e9bd138
d7ce811
3c475da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Load functions passed to proc/noproc fixtures have to accept new `autocommit` argument. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| `postgresql_noproc` has an additional parameter `load_autocommit` which is grouped together with `load` making | ||
| positional arguments filled till `depends_on` incompatible. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Allow running the SQL loader connection with autocommit enabled, so ``.sql`` files containing statements that cannot run inside a transaction block (such as ``CREATE DATABASE``) can be loaded. This is configurable without a custom loader in three ways: the ``postgresql_load_autocommit`` ini option, the ``--postgresql-load-autocommit`` command line flag, or the ``load_autocommit`` argument of the ``postgresql_proc`` / ``postgresql_noproc`` factories (which also maps to an ``autocommit`` argument on ``DatabaseJanitor`` / ``AsyncDatabaseJanitor``). |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| """Asserts that load_autocommit is True. | ||
|
|
||
| That shows that it is not the default (False), and that we parsed it as a bool. | ||
| """ | ||
|
|
||
| from pytest import FixtureRequest | ||
|
|
||
| from pytest_postgresql.config import get_config | ||
|
|
||
|
|
||
| def test_assert_load_autocommit_is_true(request: FixtureRequest) -> None: | ||
| """Asserts that load_autocommit is True.""" | ||
| config = get_config(request) | ||
| assert config.load_autocommit is True |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,26 +7,29 @@ | |
| from pytest_postgresql.executor_noop import NoopExecutor | ||
|
|
||
|
|
||
| def load_schema(host: str, port: int, user: str, dbname: str, password: str | None) -> None: | ||
| def load_schema(host: str, port: int, user: str, dbname: str, password: str | None, autocommit: bool) -> None: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win Make Ruff flags each new boolean positional parameter ( -def load_schema(host: str, port: int, user: str, dbname: str, password: str | None, autocommit: bool) -> None:
+def load_schema(host: str, port: int, user: str, dbname: str, password: str | None, *, autocommit: bool) -> None:Apply the same change to Also applies to: 19-19, 29-29 🧰 Tools🪛 Ruff (0.15.21)[warning] 10-10: Boolean-typed positional argument in function definition (FBT001) 🤖 Prompt for AI AgentsSource: Linters/SAST tools |
||
| """Load schema into the database.""" | ||
| with psycopg.connect(host=host, port=port, user=user, dbname=dbname, password=password) as conn: | ||
| conn.autocommit = autocommit | ||
| with conn.cursor() as cur: | ||
| cur.execute("CREATE TABLE schema_table (id serial PRIMARY KEY, name varchar);") | ||
| conn.commit() | ||
|
|
||
|
|
||
| def load_data(host: str, port: int, user: str, dbname: str, password: str | None) -> None: | ||
| def load_data(host: str, port: int, user: str, dbname: str, password: str | None, autocommit: bool) -> None: | ||
| """Load the first layer of data into the database.""" | ||
| with psycopg.connect(host=host, port=port, user=user, dbname=dbname, password=password) as conn: | ||
| conn.autocommit = autocommit | ||
| with conn.cursor() as cur: | ||
| cur.execute("INSERT INTO schema_table (name) VALUES ('data_layer');") | ||
| cur.execute("CREATE TABLE data_table (id serial PRIMARY KEY, val varchar);") | ||
| conn.commit() | ||
|
|
||
|
|
||
| def load_more_data(host: str, port: int, user: str, dbname: str, password: str | None) -> None: | ||
| def load_more_data(host: str, port: int, user: str, dbname: str, password: str | None, autocommit: bool) -> None: | ||
| """Load the second layer of data into the database.""" | ||
| with psycopg.connect(host=host, port=port, user=user, dbname=dbname, password=password) as conn: | ||
| conn.autocommit = autocommit | ||
| with conn.cursor() as cur: | ||
| cur.execute("INSERT INTO schema_table (name) VALUES ('more_data_layer');") | ||
| cur.execute("CREATE TABLE more_data_table (id serial PRIMARY KEY, extra varchar);") | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.