Skip to content

Fix: preserve quoted_name(quote=...) semantics when rendering schema in autogenerate - #1843

Open
pranjalm37 wants to merge 1 commit into
sqlalchemy:mainfrom
pranjalm37:fix/quoted-name-schema-render-1526
Open

Fix: preserve quoted_name(quote=...) semantics when rendering schema in autogenerate#1843
pranjalm37 wants to merge 1 commit into
sqlalchemy:mainfrom
pranjalm37:fix/quoted-name-schema-render-1526

Conversation

@pranjalm37

Copy link
Copy Markdown

Summary

Fixes #1526.

When a table/index/constraint's schema is set using an explicit
quoted_name(name, quote=True) or quoted_name(name, quote=False), the
autogenerate render code (_ident() in alembic/autogenerate/render.py)
collapsed the value down to a plain string before formatting it with %r,
so the caller's explicit quoting preference was silently dropped from the
generated migration file.

Fix

Adds _render_schema_ident(), used at every schema= render site
(create/drop table, index, column, check/unique constraint, drop_constraint,
alter_column, and the batch_alter_table header):

  • If the schema is a quoted_name with quote explicitly set (True/False),
    render it as quoted_name('name', True/False) and add the necessary import
    to the generated migration file.
  • Otherwise, falls back to repr() of the original value, preserving prior
    behavior — including for objects with a custom __repr__ (there's an
    existing test, test_drop_unique_constraint_schema_reprobj, covering that
    case, which still passes).

Test plan

  • Added test_render_table_w_quoted_name_schema, matching the repro from
    Migration autogeneration should respect schemas defined using quoted_name #1526, asserting both the rendered code and that the quoted_name import
    is added.
  • Full test suite passes locally: pytest tests/ --ignore=tests/test_post_write.py --ignore=tests/test_mssql.py --ignore=tests/test_oracle.py --ignore=tests/test_mysql.py --ignore=tests/test_postgresql.py → 1562 passed, 133 skipped (DB-specific suites skipped, no local DB available; test_post_write.py failures are pre-existing/unrelated — they fail identically on main without this change, due to black not being installed in the hook environment).
  • black --check and flake8 pass on both changed files.
  • Added changelog fragment at docs/build/unreleased/1526.rst.

…in autogenerate

When a table's schema is set using an explicit quoted_name(name, quote=True/False),
the autogenerate render code collapsed it down to a plain string literal, silently
dropping the caller's explicit quoting preference in the generated migration file.

Adds _render_schema_ident() which preserves the quoted_name(...) wrapper when
quote is explicitly set, and otherwise preserves prior behavior (including
support for custom repr()-able schema objects). Applied consistently across all
schema= render sites: create/drop table, index, column, constraints, and the
batch_alter_table header.

Fixes sqlalchemy#1526
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.

Migration autogeneration should respect schemas defined using quoted_name

1 participant