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(code_origin): recognise prefixed migration directories under db/ (#3462)

ump45nosef477bc937a02
diff --git a/packages/core/src/repowise/core/code_origin.py b/packages/core/src/repowise/core/code_origin.py
index 036e3ddae..1e6812208 100644
--- a/packages/core/src/repowise/core/code_origin.py
+++ b/packages/core/src/repowise/core/code_origin.py
@@ -693,6 +693,13 @@ _TOOLING_ROOT_DIRS = frozenset(
# directory with another name is recognised only by its sibling ``env.py``,
# which a path-only classifier cannot see.
_MIGRATION_DIR = "migrations"
+# Rails also keeps migration-like scripts in ``db/`` directories named with a
+# prefix: retired one-shot scripts in ``db/old_migrations/``, data migrations in
+# ``db/data_migrations/``. Only directly under ``db/``: elsewhere a name ending
+# in ``_migrations`` is as likely a package's own source
+# (``src/django_test_migrations/``).
+_MIGRATION_DB_DIR = "db"
+_MIGRATION_DB_SUBDIR_SUFFIX = "_migrations"
_MIGRATION_DIR_PAIRS = frozenset({("db", "migrate"), ("alembic", "versions")})
_TOOLING_ROOT_NAMES = frozenset({"fabfile.py"})
# Benchmarks kept beside the code they measure (abseil's
@@ -755,7 +762,14 @@ def is_vendored_or_generated_path(path: str) -> bool:
def _is_migration_dir(dirs: list[str]) -> bool:
- return _MIGRATION_DIR in dirs or any(pair in _MIGRATION_DIR_PAIRS for pair in pairwise(dirs))
+ return (
+ _MIGRATION_DIR in dirs
+ or any(pair in _MIGRATION_DIR_PAIRS for pair in pairwise(dirs))
+ or any(
+ parent == _MIGRATION_DB_DIR and child.endswith(_MIGRATION_DB_SUBDIR_SUFFIX)
+ for parent, child in pairwise(dirs)
+ )
+ )
def is_migration_path(path: str) -> bool:
diff --git a/tests/unit/test_code_origin.py b/tests/unit/test_code_origin.py
index 2a9e9fcfc..51588dcda 100644
--- a/tests/unit/test_code_origin.py
+++ b/tests/unit/test_code_origin.py
@@ -96,6 +96,8 @@ _OWN_HEADER = (
("fabfile.py", "tooling"),
("eslint.config.mjs", "tooling"),
("db/migrate/20240101_add_users.rb", "tooling"),
+ ("db/data_migrations/20240101_backfill.rb", "tooling"),
+ ("src/django_test_migrations/logic.py", "production"),
("src/__generated__/schema.ts", "generated"),
("api/user_pb2.py", "generated"),
("api/user_grpc.pb.go", "generated"),
@@ -528,6 +530,16 @@ def test_migration_paths() -> None:
assert not is_migration_path("src/migrate.py")
+def test_prefixed_migration_dirs_under_db() -> None:
+ # Rails keeps retired and data migrations in prefixed directories under db/.
+ assert is_migration_path("db/old_migrations/x.rb")
+ assert is_migration_path("db/data_migrations/x.rb")
+ # Elsewhere a name containing or ending in "migrations" is ordinary source.
+ assert not is_migration_path("app/migrations_helper/x.rb")
+ assert not is_migration_path("src/django_test_migrations/logic.py")
+ assert not is_migration_path("django_test_migrations/contrib/unittest_case.py")
+
+
def test_ship_rank_puts_product_code_first_and_tests_last() -> None:
assert ship_rank("src/app.py") == 0
assert ship_rank("build.gradle.kts") == 1