Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ All notable changes to this project are documented in this file. The format is b
- SMTP preserves long `List-Unsubscribe` URLs within the 998-character line limit without RFC 2047 encoding, while retaining encoded non-ASCII headers and SMTPUTF8 support for internationalized mailboxes (#1053).
- SMTP refolds long UTF-8 headers to respect the 998-byte wire-line limit (#1053).
- Store OAuth access and refresh tokens as unbounded TEXT so long Microsoft Entra tokens can sign in on Postgres; migration `a1054e9c7b62` widens both columns (#1054).
- **Behaviour change:** When reusing a schema, PostgreSQL test fixtures empty model tables with one `TRUNCATE ... RESTART IDENTITY RESTRICT`, so row-level `DELETE` triggers cannot block between-test resets. Foreign-key references from tables outside the reset list reject the reset, and lock waits fail after five seconds. Unowned public-schema sequences still restart at 1; statement-level `TRUNCATE` triggers still apply (#1052).

## [0.0.48] — 2026-11-19

Expand Down
15 changes: 14 additions & 1 deletion docs/testing/fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,20 @@ local `sm_test` database) and runs 4 workers by default. Under xdist each worker

Each test starts with empty tables. The schema is built once per process and
rebuilt only when changed; Postgres fixtures do not create a separate database
per test. Connections use `NullPool` because pytest-asyncio gives each test
per test. When reusing a schema, all model tables are emptied in one
`TRUNCATE <tables> RESTART IDENTITY RESTRICT` statement. This bypasses row-level
`DELETE` triggers, including guards that reject deletion of immutable rows.
Statement-level `TRUNCATE` triggers (including `BEFORE TRUNCATE`) still fire and
can reject the reset. Public-schema sequences are also reset to 1, including
sequences not owned by a table column, which `RESTART IDENTITY` does not reset.
Foreign-key references from tables outside the model-table list reject the
reset rather than implicitly emptying those tables. `TRUNCATE` takes exclusive
table locks until the reset transaction completes; a transaction-local five-second
lock timeout makes leaked read transactions fail setup rather than hang it.
Keep using separate databases per xdist worker. The default in-memory SQLite
fixtures copy a schema template into fresh databases instead of running this reset.

Connections use `NullPool` because pytest-asyncio gives each test
its own event loop. Use `-p no:anyio` when invoking pytest directly on Postgres;
the Make target already includes it.

Expand Down
18 changes: 11 additions & 7 deletions framework/testing/simple_module_test/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ async def prepare_schema_once(engine: Any, tables: list[Any], build: Any) -> Non
per-process copy of the first build (``_sqlite_template``), a file one runs
``build(engine)``.
Postgres: when this process already built the schema and the catalog still
matches it, delete every row and restart the sequences (~0.03 s) instead of
dropping and rebuilding it (~0.25 s). Anything else (the first test, a
matches it, empty the tables with one TRUNCATE and restart the sequences
instead of dropping and rebuilding it. Anything else (the first test, a
direct ``reset_schema`` call, a test's own DDL) resets and rebuilds.
"""
global _reset_for, _built_schema
Expand Down Expand Up @@ -181,13 +181,17 @@ async def _fingerprint(engine: Any) -> str:


async def _empty_tables(engine: Any, tables: list[Any]) -> None:
from sqlalchemy.schema import sort_tables
"""Empty a reused Postgres schema; SQLite is handled before this call."""

async with engine.begin() as conn:
quote = conn.dialect.identifier_preparer.format_table
# Children first, so no foreign key refuses a parent's delete.
for table in reversed(sort_tables(tables)):
await conn.execute(text(f"DELETE FROM {quote(table)}"))
# A leaked reader must fail setup instead of waiting forever for TRUNCATE.
await conn.execute(text("SET LOCAL lock_timeout = '5s'"))
if tables:
quote = conn.dialect.identifier_preparer.format_table
names = ", ".join(quote(table) for table in tables)
# Refuse foreign-key dependants outside the caller's reset boundary.
await conn.execute(text(f"TRUNCATE {names} RESTART IDENTITY RESTRICT"))
# RESTART IDENTITY excludes unowned sequences; preserve their reset too.
await conn.execute(_RESTART_SEQUENCES)


Expand Down
114 changes: 112 additions & 2 deletions framework/testing/tests/test_schema_reuse_pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@

from __future__ import annotations

import asyncio

import pytest
from simple_module_test import database
from sqlalchemy import Column, Integer, MetaData, String, Table, func, select, text
from sqlalchemy import (
Column,
ForeignKey,
Identity,
Integer,
MetaData,
String,
Table,
func,
select,
text,
)
from sqlalchemy.exc import DBAPIError
from sqlalchemy.ext.asyncio import create_async_engine

pytestmark = pytest.mark.postgres
Expand All @@ -13,7 +27,7 @@
_item = Table(
"reuse_probe_item",
_metadata,
Column("id", Integer, primary_key=True),
Column("id", Integer, Identity(), primary_key=True),
Column("name", String(20)),
)

Expand Down Expand Up @@ -73,3 +87,99 @@ async def test_a_test_that_changes_the_schema_forces_a_rebuild(engine):
assert builds == [1, 1]
async with engine.connect() as conn:
assert await conn.scalar(text("SELECT to_regclass('reuse_probe_extra')")) is None


async def test_reset_bypasses_delete_row_triggers_and_restarts_identity(engine):
await _next_test(engine, [])
child = Table(
"reset probe child",
MetaData(),
Column("item_id", Integer, ForeignKey(_item.c.id), primary_key=True),
schema="public",
)
async with engine.begin() as conn:
await conn.run_sync(child.create)
await conn.execute(
text("""
CREATE FUNCTION refuse_probe_delete() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
RAISE EXCEPTION 'probe rows are immutable';
END;
$$
""")
)
await conn.execute(
text("""
CREATE TRIGGER refuse_probe_delete BEFORE DELETE ON reuse_probe_item
FOR EACH ROW EXECUTE FUNCTION refuse_probe_delete()
""")
)
assert [await _insert(engine), await _insert(engine)] == [1, 2]
async with engine.begin() as conn:
await conn.execute(child.insert().values(item_id=1))

# A quoted, schema-qualified child and parent-first input need no DELETE ordering.
await database._empty_tables(engine, [_item, child])

async with engine.connect() as conn:
assert await conn.scalar(select(func.count()).select_from(_item)) == 0
assert await conn.scalar(select(func.count()).select_from(child)) == 0
assert await _insert(engine) == 1


async def test_reset_restarts_sequences_without_a_table_owner(engine):
await _next_test(engine, [])
async with engine.begin() as conn:
await conn.execute(text("CREATE SEQUENCE reuse_probe_unowned"))
assert await conn.scalar(text("SELECT nextval('reuse_probe_unowned')")) == 1
assert await conn.scalar(text("SELECT nextval('reuse_probe_unowned')")) == 2

await database._empty_tables(engine, [_item])

async with engine.connect() as conn:
assert await conn.scalar(text("SELECT nextval('reuse_probe_unowned')")) == 1


async def test_reset_refuses_to_empty_tables_outside_the_passed_list(engine):
await _next_test(engine, [])
outside = Table(
"outside_probe",
MetaData(),
Column("id", Integer, primary_key=True),
Column("item_id", Integer, ForeignKey(_item.c.id)),
)
async with engine.begin() as conn:
await conn.run_sync(outside.create)
# DELETE on the empty parent would leave this unrelated row untouched.
await conn.execute(outside.insert().values(id=1, item_id=None))

with pytest.raises(DBAPIError, match="foreign key"):
await database._empty_tables(engine, [_item])

async with engine.connect() as conn:
assert await conn.scalar(select(outside.c.id)) == 1


async def test_reset_times_out_when_a_previous_reader_holds_a_lock(engine):
await _next_test(engine, [])
async with engine.connect() as reader:
await reader.execute(select(_item))
try:
with pytest.raises(DBAPIError, match="lock timeout"):
await asyncio.wait_for(database._empty_tables(engine, [_item]), timeout=10)
finally:
await reader.rollback()


async def test_reset_with_no_tables_preserves_the_migration_stamp(engine):
await _next_test(engine, [])
async with engine.begin() as conn:
await conn.execute(text("CREATE TABLE alembic_version (version_num text)"))
await conn.execute(text("INSERT INTO alembic_version VALUES ('probe')"))

await database._empty_tables(engine, [])
await database._empty_tables(engine, [_item])

async with engine.connect() as conn:
assert await conn.scalar(text("SELECT version_num FROM alembic_version")) == "probe"
Loading