Skip to content

feat: auto-derive naming rules from MetaData.naming_convention#7

Merged
AlexeyShalaev merged 1 commit into
masterfrom
feature/naming-convention-from-metadata
May 16, 2026
Merged

feat: auto-derive naming rules from MetaData.naming_convention#7
AlexeyShalaev merged 1 commit into
masterfrom
feature/naming-convention-from-metadata

Conversation

@AlexeyShalaev

Copy link
Copy Markdown
Contributor

Summary

  • test_naming_conventions now reads orm_metadata.naming_convention (already a required fixture) and auto-derives prefix/suffix rules from it
  • Rules are merged in three layers — built-in defaults → convention → explicit class attributes (last wins)
  • The SQLAlchemy built-in default {"ix": "ix_%(column_0_label)s"} is ignored so projects without a custom convention keep existing behaviour
  • New alembic_gauntlet/utils/convention.py with rules_from_metadata() and NamingConventionRules

Zero-config usage

DB_NAMING_CONVENTION = {
    "ix": "%(column_0_label)s_idx",
    "uq": "%(table_name)s_%(column_0_name)s_key",
    "ck": "%(table_name)s_%(constraint_name)s_check",
    "fk": "%(table_name)s_%(column_0_name)s_fkey",
    "pk": "%(table_name)s_pkey",
}
Base = declarative_base(metadata=MetaData(naming_convention=DB_NAMING_CONVENTION))

class TestMyMigrations(MigrationTestBase):
    @pytest.fixture
    def orm_metadata(self) -> MetaData:
        return Base.metadata  # naming rules derived automatically — no class attributes needed

Test plan

  • Existing integration test passes (no naming_convention → falls back to defaults)
  • Unit tests for _extract_prefix, _extract_suffix, rules_from_metadata
  • SQLAlchemy default convention ignored
  • Fully dynamic templates (no literal prefix/suffix) skipped gracefully
  • make fmt && make test green (78 passed, coverage 63.96%)

MigrationNamingMixin.test_naming_conventions now reads orm_metadata
(already a required fixture) and extracts prefix/suffix rules from
MetaData.naming_convention. Rules are merged in three layers:
  1. built-in defaults
  2. derived from naming_convention (overrides defaults)
  3. explicit class attributes (always win)

The SQLAlchemy built-in default convention {ix: ...} is ignored so
projects without a custom naming_convention keep existing behaviour.
@codecov

codecov Bot commented May 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 6.66667% with 42 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
alembic_gauntlet/utils/convention.py 6.66% 42 Missing ⚠️

📢 Thoughts on this report? Let us know!

@AlexeyShalaev AlexeyShalaev merged commit b673665 into master May 16, 2026
6 of 7 checks passed
@AlexeyShalaev AlexeyShalaev deleted the feature/naming-convention-from-metadata branch May 16, 2026 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant