|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +from contextlib import asynccontextmanager |
| 6 | + |
5 | 7 | import pytest |
6 | 8 |
|
7 | 9 | from adcp.reporting._errors import ReliableReportingConfigurationError |
@@ -146,3 +148,113 @@ async def test_a_managed_service_runs_on_postgres_storage() -> None: |
146 | 148 | assert await engine.list_subscription_principals() == [PrincipalRef(None, AGENCY)] |
147 | 149 | finally: |
148 | 150 | await service.close() |
| 151 | + |
| 152 | + |
| 153 | +def test_no_storage_existence_check_resolves_through_the_search_path() -> None: |
| 154 | + """#1838: ``to_regclass('name')`` finds a table in any schema on the path. |
| 155 | +
|
| 156 | + An existence check that does would read, skip or alter another schema's |
| 157 | + tables; qualify it with ``quote_ident(current_schema())``. |
| 158 | + """ |
| 159 | + import re |
| 160 | + from pathlib import Path |
| 161 | + |
| 162 | + import adcp_reporting_storage |
| 163 | + |
| 164 | + root = Path(adcp_reporting_storage.__file__).parent |
| 165 | + unqualified = re.compile(r"to_regclass\(\s*'") |
| 166 | + found = [ |
| 167 | + f"{path.relative_to(root)}:{number}" |
| 168 | + for path in sorted(root.rglob("*")) |
| 169 | + if path.suffix in {".py", ".sql"} |
| 170 | + for number, line in enumerate(path.read_text().splitlines(), 1) |
| 171 | + if unqualified.search(line) |
| 172 | + ] |
| 173 | + assert found == [] |
| 174 | + |
| 175 | + |
| 176 | +async def _schema(pool) -> str: |
| 177 | + async with pool.connection() as connection: |
| 178 | + return (await (await connection.execute("SELECT current_schema()")).fetchone())[0] |
| 179 | + |
| 180 | + |
| 181 | +async def _catalog(pool, schema: str) -> list[tuple]: |
| 182 | + """Columns, constraints and recorded migrations of ``schema``.""" |
| 183 | + async with pool.connection() as connection: |
| 184 | + columns = await ( |
| 185 | + await connection.execute( |
| 186 | + "SELECT table_name, column_name, data_type FROM information_schema.columns" |
| 187 | + " WHERE table_schema = %s ORDER BY 1, 2", |
| 188 | + (schema,), |
| 189 | + ) |
| 190 | + ).fetchall() |
| 191 | + constraints = await ( |
| 192 | + await connection.execute( |
| 193 | + "SELECT c.conrelid::regclass::text, c.conname, pg_get_constraintdef(c.oid)" |
| 194 | + " FROM pg_constraint c JOIN pg_namespace n ON n.oid = c.connamespace" |
| 195 | + " WHERE n.nspname = %s ORDER BY 1, 2", |
| 196 | + (schema,), |
| 197 | + ) |
| 198 | + ).fetchall() |
| 199 | + recorded = await ( |
| 200 | + await connection.execute("SELECT version FROM adcp_schema_migrations ORDER BY 1") |
| 201 | + ).fetchall() |
| 202 | + return [*columns, *constraints, *recorded] |
| 203 | + |
| 204 | + |
| 205 | +@asynccontextmanager |
| 206 | +async def _search_path(*schemas: str): |
| 207 | + import os |
| 208 | + |
| 209 | + from psycopg_pool import AsyncConnectionPool |
| 210 | + |
| 211 | + async with AsyncConnectionPool( |
| 212 | + os.environ["ADCP_PG_TEST_URL"], |
| 213 | + kwargs={"options": f"-csearch_path={','.join(schemas)}", "autocommit": True}, |
| 214 | + min_size=1, |
| 215 | + max_size=2, |
| 216 | + open=False, |
| 217 | + ) as pool: |
| 218 | + await pool.wait(timeout=10) |
| 219 | + yield pool |
| 220 | + |
| 221 | + |
| 222 | +@pytest.mark.asyncio |
| 223 | +async def test_migrate_upgrades_its_own_schema_beside_a_migrated_one() -> None: |
| 224 | + """#1838: a migrated schema later on the search_path is not this schema's record.""" |
| 225 | + from adcp_reporting_storage.migrations import migrate, migrations, pending_migrations |
| 226 | + |
| 227 | + every = [item.version for item in migrations()] |
| 228 | + async with ( |
| 229 | + isolated_reporting_pool(autocommit=True) as other, |
| 230 | + isolated_reporting_pool(autocommit=True) as own, |
| 231 | + ): |
| 232 | + await migrate(other) |
| 233 | + other_schema, own_schema = await _schema(other), await _schema(own) |
| 234 | + before = await _catalog(other, other_schema) |
| 235 | + async with _search_path(own_schema, other_schema) as shared: |
| 236 | + assert [item.version for item in await pending_migrations(shared)] == every |
| 237 | + assert [item.version for item in await migrate(shared)] == every |
| 238 | + assert await pending_migrations(shared) == [] |
| 239 | + assert await pending_migrations(own) == [] |
| 240 | + assert await _catalog(own, own_schema) == before # the same schema, built in place |
| 241 | + assert await _catalog(other, other_schema) == before |
| 242 | + |
| 243 | + |
| 244 | +@pytest.mark.asyncio |
| 245 | +async def test_create_schema_builds_its_own_schema_beside_a_migrated_one() -> None: |
| 246 | + from adcp_reporting_storage.ledger.pg import PgReportingLedgerStore |
| 247 | + from adcp_reporting_storage.migrations import migrate, pending_migrations |
| 248 | + |
| 249 | + async with ( |
| 250 | + isolated_reporting_pool(autocommit=True) as other, |
| 251 | + isolated_reporting_pool(autocommit=True) as own, |
| 252 | + ): |
| 253 | + await migrate(other) |
| 254 | + other_schema, own_schema = await _schema(other), await _schema(own) |
| 255 | + before = await _catalog(other, other_schema) |
| 256 | + async with _search_path(own_schema, other_schema) as shared: |
| 257 | + await PgReportingLedgerStore(pool=shared).create_schema() |
| 258 | + # create_schema() records its own 0001-0003; the rest stay for ``migrate``. |
| 259 | + assert [item.version for item in await pending_migrations(own)] == ["0004", "0005", "0006"] |
| 260 | + assert await _catalog(other, other_schema) == before |
0 commit comments