feat: monorepo migration phase 5
App checks / changes (push) Canceled after 0s
App checks / tournament (push) Canceled after 0s
App checks / shifts (push) Canceled after 0s
App checks / changes (push) Canceled after 0s
App checks / tournament (push) Canceled after 0s
App checks / shifts (push) Canceled after 0s
This commit is contained in:
1 parent
d534588e27
commit
66b72d1348
28 files changed
+406
-26
No files matched your search
@@ -0,0 +1,26 @@
|
||||
# CRR Python infrastructure
|
||||
|
||||
Internal uv workspace package, imported as `crr_common`, targeting Python 3.13.
|
||||
Both applications consume it explicitly through workspace dependencies.
|
||||
|
||||
## Proven shared needs
|
||||
|
||||
- `database.create_sync_engine`: both FastAPI applications use synchronous
|
||||
SQLAlchemy and allow SQLite connections to cross request-worker threads.
|
||||
This wraps SQLAlchemy URL handling and supplies `check_same_thread=False` for
|
||||
SQLite, while forwarding caller engine options and honoring explicit connection
|
||||
arguments. Other database dialects receive no SQLite-specific arguments.
|
||||
- `migrations.migration_config`: both applications create an Alembic configuration
|
||||
from their own ini file, migration directory and database setting. It resolves
|
||||
paths, escapes URL interpolation characters, and optionally attaches a caller-
|
||||
owned connection for transactional migrations.
|
||||
|
||||
Schema validation, baseline revisions, upgrades/stamps, transaction ownership,
|
||||
SQLite pragmas, backups, sessions, domain models and authentication belong to
|
||||
each application. The package contains no app imports, switches or domain code.
|
||||
It neither opens migration connections nor runs DDL.
|
||||
|
||||
Run `make python-common-test` from the repository root. `make test` includes
|
||||
these checks, and shared-package changes trigger both app CI jobs. Docker images
|
||||
install the package as a wheel with `uv sync --no-editable`; local uv commands
|
||||
use the workspace package. Its internal version is not a public release stream.
|
||||
@@ -0,0 +1,21 @@
|
||||
[project]
|
||||
name = "crr-python"
|
||||
version = "0.1.0"
|
||||
description = "Shared CRR Python infrastructure"
|
||||
requires-python = ">=3.13,<3.14"
|
||||
dependencies = ["SQLAlchemy>=2.0,<3", "alembic>=1.13,<2"]
|
||||
readme = "README.md"
|
||||
|
||||
[build-system]
|
||||
requires = ["hatchling>=1.26,<2"]
|
||||
build-backend = "hatchling.build"
|
||||
|
||||
[tool.hatch.build.targets.wheel]
|
||||
packages = ["src/crr_common"]
|
||||
|
||||
[dependency-groups]
|
||||
dev = ["pytest>=8,<9"]
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
testpaths = ["tests"]
|
||||
addopts = "-ra"
|
||||
@@ -0,0 +1 @@
|
||||
"""Low-level infrastructure shared by the independently deployed CRR apps."""
|
||||
@@ -0,0 +1,18 @@
|
||||
from collections.abc import Mapping
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import URL, create_engine
|
||||
from sqlalchemy.engine import Engine, make_url
|
||||
|
||||
|
||||
def create_sync_engine(
|
||||
url: str | URL,
|
||||
*,
|
||||
connect_args: Mapping[str, Any] | None = None,
|
||||
**engine_options: Any,
|
||||
) -> Engine:
|
||||
"""Construct a synchronous engine without changing connection/schema policy."""
|
||||
arguments = dict(connect_args or {})
|
||||
if make_url(url).get_backend_name() == "sqlite":
|
||||
arguments.setdefault("check_same_thread", False)
|
||||
return create_engine(url, connect_args=arguments, **engine_options)
|
||||
@@ -0,0 +1,23 @@
|
||||
from pathlib import Path
|
||||
|
||||
from alembic.config import Config
|
||||
from sqlalchemy import URL
|
||||
from sqlalchemy.engine import Connection
|
||||
|
||||
|
||||
def migration_config(
|
||||
ini_path: str | Path,
|
||||
script_location: str | Path,
|
||||
*,
|
||||
database_url: str | URL | None = None,
|
||||
connection: Connection | None = None,
|
||||
) -> Config:
|
||||
"""Prepare Alembic wiring; the caller owns migration policy and connections."""
|
||||
config = Config(str(Path(ini_path).resolve()))
|
||||
config.set_main_option("script_location", str(Path(script_location).resolve()).replace("%", "%%"))
|
||||
if database_url is not None:
|
||||
url = database_url.render_as_string(hide_password=False) if isinstance(database_url, URL) else database_url
|
||||
config.set_main_option("sqlalchemy.url", url.replace("%", "%%"))
|
||||
if connection is not None:
|
||||
config.attributes["connection"] = connection
|
||||
return config
|
||||
@@ -0,0 +1,54 @@
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
from sqlalchemy import URL
|
||||
from sqlalchemy.pool import StaticPool
|
||||
|
||||
from crr_common.database import create_sync_engine
|
||||
|
||||
|
||||
def test_sqlite_connection_can_cross_request_worker_threads(tmp_path):
|
||||
engine = create_sync_engine(URL.create("sqlite", database=str(tmp_path / "threads.sqlite")))
|
||||
try:
|
||||
with engine.connect() as connection, ThreadPoolExecutor(max_workers=1) as pool:
|
||||
assert pool.submit(lambda: connection.exec_driver_sql("SELECT 42").scalar_one()).result() == 42
|
||||
finally:
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_caller_pool_options_and_memory_database_are_preserved():
|
||||
engine = create_sync_engine("sqlite:///:memory:", poolclass=StaticPool)
|
||||
try:
|
||||
assert isinstance(engine.pool, StaticPool)
|
||||
with engine.begin() as connection:
|
||||
connection.exec_driver_sql("CREATE TABLE example(value INTEGER)")
|
||||
connection.exec_driver_sql("INSERT INTO example VALUES(7)")
|
||||
with engine.connect() as connection:
|
||||
assert connection.exec_driver_sql("SELECT value FROM example").scalar_one() == 7
|
||||
finally:
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_shared_factory_leaves_sqlite_pragmas_to_the_consumer(tmp_path):
|
||||
engine = create_sync_engine(URL.create("sqlite", database=str(tmp_path / "policy.sqlite")))
|
||||
try:
|
||||
with engine.connect() as connection:
|
||||
assert connection.exec_driver_sql("PRAGMA foreign_keys").scalar_one() == 0
|
||||
assert connection.exec_driver_sql("PRAGMA journal_mode").scalar_one() == "delete"
|
||||
finally:
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_non_sqlite_urls_and_explicit_connection_options(monkeypatch):
|
||||
from crr_common import database
|
||||
calls = []
|
||||
marker = object()
|
||||
def factory(url, **options):
|
||||
calls.append((url, options))
|
||||
return marker
|
||||
monkeypatch.setattr(database, "create_engine", factory)
|
||||
assert create_sync_engine("postgresql://localhost/example", pool_pre_ping=True) is marker
|
||||
assert calls[-1][1] == {"connect_args": {}, "pool_pre_ping": True}
|
||||
arguments = {"check_same_thread": True, "timeout": 10}
|
||||
assert create_sync_engine("sqlite://", connect_args=arguments) is marker
|
||||
assert calls[-1][1]["connect_args"] == arguments
|
||||
assert arguments == {"check_same_thread": True, "timeout": 10}
|
||||
@@ -0,0 +1,41 @@
|
||||
from sqlalchemy import URL
|
||||
|
||||
from crr_common.database import create_sync_engine
|
||||
from crr_common.migrations import migration_config
|
||||
|
||||
|
||||
def ini(tmp_path):
|
||||
path = tmp_path / "alembic.ini"
|
||||
path.write_text("[alembic]\nscript_location = %(here)s/migrations\n")
|
||||
return path
|
||||
|
||||
|
||||
def test_configuration_resolves_paths_and_preserves_url_percent_escapes(tmp_path):
|
||||
scripts = tmp_path / "migration%files"
|
||||
url = "postgresql://reader:p%25ss@localhost/database"
|
||||
config = migration_config(ini(tmp_path), scripts, database_url=url)
|
||||
assert config.config_file_name == str(tmp_path / "alembic.ini")
|
||||
assert config.get_main_option("script_location") == str(scripts)
|
||||
assert config.get_main_option("sqlalchemy.url") == url
|
||||
assert "connection" not in config.attributes
|
||||
|
||||
|
||||
def test_sqlalchemy_url_objects_are_rendered_without_redacting_connection_credentials(tmp_path):
|
||||
url = URL.create("postgresql", username="test-user", password="test%only", host="localhost", database="example")
|
||||
config = migration_config(ini(tmp_path), tmp_path / "migrations", database_url=url)
|
||||
assert config.get_main_option("sqlalchemy.url") == url.render_as_string(hide_password=False)
|
||||
|
||||
|
||||
def test_configuration_reuses_caller_connection_without_opening_or_committing_it(tmp_path):
|
||||
engine = create_sync_engine("sqlite:///:memory:")
|
||||
try:
|
||||
with engine.connect() as connection:
|
||||
transaction = connection.begin()
|
||||
config = migration_config(ini(tmp_path), tmp_path / "migrations", connection=connection)
|
||||
assert config.attributes["connection"] is connection
|
||||
assert connection.in_transaction()
|
||||
assert not connection.closed
|
||||
assert config.get_main_option("sqlalchemy.url") is None
|
||||
transaction.rollback()
|
||||
finally:
|
||||
engine.dispose()
|
||||
Reference in new issue
Block a user