Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(data): record the schema= keyword on Alembic create_table providers (#4192)

yi111cfbb2c5edafd
diff --git a/packages/core/src/repowise/core/workspace/extractors/data/orm_models.py b/packages/core/src/repowise/core/workspace/extractors/data/orm_models.py
index 6dd8d85e2..b3a0b0107 100644
--- a/packages/core/src/repowise/core/workspace/extractors/data/orm_models.py
+++ b/packages/core/src/repowise/core/workspace/extractors/data/orm_models.py
@@ -19,10 +19,11 @@ from typing import TYPE_CHECKING
from repowise.core.workspace.origins import ContractOrigin
-from ..base import ScanContext
+from ..base import ScanContext, line_at
from ..langs import CSHARP, JAVA, KOTLIN, PYTHON, RUBY
-from .dialect import dedup_emit, found_names
-from .names import pluralize, snake_case
+from ..strings import call_arguments
+from .dialect import _Found, dedup_emit, found_names
+from .names import normalize_table_name, pluralize, snake_case
if TYPE_CHECKING:
from repowise.core.workspace.contracts import Contract
@@ -39,8 +40,44 @@ class SqlAlchemyDjangoDialect:
_TABLENAME_RE = re.compile(r"^\s*__tablename__\s*[:=]\s*.*?['\"](\S+?)['\"]", re.MULTILINE)
_DB_TABLE_RE = re.compile(r"^\s*db_table\s*=\s*['\"](\S+?)['\"]", re.MULTILINE)
- _CREATE_TABLE_RE = re.compile(r"\bop\.create_table\(\s*['\"](\w+)['\"]")
+ _CREATE_TABLE_RE = re.compile(r"\bop\.create_table\s*\(")
_SQLMODEL_RE = re.compile(r"\bclass\s+(\w+)\([^)]*\btable\s*=\s*True[^)]*\)")
+ _BARE_STRING_RE = re.compile(r"['\"](\w+)['\"]")
+ _SCHEMA_KWARG_RE = re.compile(r"schema\s*=\s*['\"](\w+)['\"]")
+
+ def _created_tables(self, content: str) -> list[_Found]:
+ r"""The tables each ``op.create_table`` call declares, schema-qualified.
+
+ Alembic takes the target schema as a ``schema=`` keyword that usually
+ trails the column list, so the call's first string literal is the bare
+ table name and the qualifier has to come from the keyword: a
+ schema-qualified migration would otherwise be recorded as ``owners``
+ where its SQL consumers say ``core.owners``, and the two keys would
+ never meet. The argument list is split at top level
+ (:func:`..strings.call_arguments`) so a ``schema=`` belonging to a
+ later statement, or one on a nested call inside the argument list,
+ cannot lend its schema to this table. The qualified token goes through
+ :func:`normalize_table_name` so the case folding - and the default
+ schema drop - is the SQL consumer side's own.
+ """
+ found: list[_Found] = []
+ for m in self._CREATE_TABLE_RE.finditer(content):
+ args = call_arguments(content, m.end() - 1) # m.end()-1 is the '('
+ if not args:
+ continue
+ name = self._BARE_STRING_RE.fullmatch(args[0])
+ if name is None:
+ continue
+ table = name.group(1)
+ for arg in args[1:]:
+ schema = self._SCHEMA_KWARG_RE.fullmatch(arg)
+ if schema is not None:
+ table = f"{schema.group(1)}.{table}"
+ break
+ raw = normalize_table_name(table)
+ if raw is not None:
+ found.append((raw, ContractOrigin.DDL, line_at(content, m.start())))
+ return found
def extract(self, ctx: ScanContext) -> list[Contract]:
# A migration's `create_table` defines the schema; the rest model it.
@@ -49,7 +86,7 @@ class SqlAlchemyDjangoDialect:
content = ctx.content
seen: set[str] = set()
created = (
- found_names(self._CREATE_TABLE_RE, content, ContractOrigin.DDL)
+ self._created_tables(content)
if ctx.has_any(("op.create_table",))
else []
)
diff --git a/tests/unit/workspace/test_data_contracts.py b/tests/unit/workspace/test_data_contracts.py
index 52d82aa2f..290be6bbd 100644
--- a/tests/unit/workspace/test_data_contracts.py
+++ b/tests/unit/workspace/test_data_contracts.py
@@ -149,6 +149,80 @@ class TestOrmProviders:
# A migration defines the schema, which marks the owning repo.
assert orders.meta["schema"] == "create"
+ def test_alembic_create_table_records_the_schema_keyword(self, tmp_path: Path) -> None:
+ # `schema=` trails the column list, so the name has to come off the
+ # call's argument list rather than the string right after the paren.
+ _write(
+ tmp_path,
+ "alembic/versions/abc_init.py",
+ "def upgrade():\n"
+ " op.create_table(\n"
+ ' "owners",\n'
+ ' sa.Column("id", sa.Integer, primary_key=True),\n'
+ ' schema="core",\n'
+ " )\n",
+ )
+ contracts = DataExtractor().extract(tmp_path, "api")
+ ids = {c.contract_id for c in contracts}
+ assert "data::core.owners" in ids
+ # The bare key must go: it is what let the two sides drift apart.
+ assert "data::owners" not in ids
+
+ def test_alembic_schema_keyword_matches_the_qualified_sql_consumer(
+ self, tmp_path: Path
+ ) -> None:
+ # The end-to-end shape from the issue: a schema-qualified migration and
+ # a schema-qualified consumer previously never met (2 of 48 consumers).
+ backend = tmp_path / "backend"
+ worker = tmp_path / "worker"
+ _write(
+ backend,
+ "alembic/versions/abc_init.py",
+ 'def upgrade():\n op.create_table("owners", sa.Column("id"), schema="core")\n',
+ )
+ _write(worker, "jobs/process.py", 'q = "SELECT id FROM core.owners WHERE done = 0"\n')
+
+ contracts = DataExtractor().extract(backend, "backend")
+ contracts += DataExtractor().extract(worker, "worker")
+ links = match_contracts(contracts)
+
+ assert len(links) == 1
+ assert links[0].provider_repo == "backend"
+ assert links[0].consumer_repo == "worker"
+
+ def test_alembic_default_schema_keyword_still_normalizes_away(self, tmp_path: Path) -> None:
+ _write(
+ tmp_path,
+ "alembic/versions/abc_init.py",
+ 'op.create_table("owners", sa.Column("id"), schema="public")\n',
+ )
+ contracts = DataExtractor().extract(tmp_path, "api")
+ assert "data::owners" in {c.contract_id for c in contracts}
+
+ def test_alembic_schema_keyword_does_not_leak_to_the_next_call(
+ self, tmp_path: Path
+ ) -> None:
+ _write(
+ tmp_path,
+ "alembic/versions/abc_init.py",
+ "def upgrade():\n"
+ ' op.create_table("owners", sa.Column("id"), schema="core")\n'
+ ' op.create_table("visits", sa.Column("id"))\n',
+ )
+ contracts = DataExtractor().extract(tmp_path, "api")
+ ids = {c.contract_id for c in contracts}
+ assert {"data::core.owners", "data::visits"} <= ids
+
+ def test_alembic_schema_in_another_statement_does_not_leak(self, tmp_path: Path) -> None:
+ _write(
+ tmp_path,
+ "alembic/versions/abc_init.py",
+ 'op.create_table("owners", sa.Column("id"))\n'
+ 'op.execute(sa.text(\'ALTER TABLE x SET SCHEMA "core"\'))\n',
+ )
+ contracts = DataExtractor().extract(tmp_path, "api")
+ assert "data::owners" in {c.contract_id for c in contracts}
+
def test_sqlmodel_table_true(self, tmp_path: Path) -> None:
_write(
tmp_path,