Skip to content

AlterColumnOp.modify_name is ignored #1635

@lenvk

Description

@lenvk

In previous discussion #1632 I was asked to open an issue and here it is

Describe the bug
We have an AlterColumnOp in our env.py

@writer.rewrites(ops.DropColumnOp)
def drop_column(context, revision, op):
    return [
        ops.AlterColumnOp(
            op.table_name,
            op.column_name,
            modify_name=f'del_{op.column_name}',
        )
    ]

but when we delete a field from model and run alembic revision --autogenerate -m "whatever", modify_name is absent in the autogenerated migration

Expected behavior
autogenerated migration contains op.alter_column('awesome_table', 'redundant_field', new_column_name='del_redundant_field')

Actual behavior
autogenerated migration contains op.alter_column('awesome_table', 'redundant_field')

To Reproduce
If you need a full example (pipfile included, etc) - please, let me know, because I've skipped some non-relevant parts

models.py

from sqlalchemy.orm import DeclarativeBase, mapped_column, Mapped


class BaseModel(DeclarativeBase):
    pass


class AwesomeModel(BaseModel):
    __tablename__ = "awesome_model"

    id: Mapped[int] = mapped_column(primary_key=True)
    redundant_field: Mapped[str] = mapped_column(comment='delete me', nullable=True)

alembic.ini (with sqlite as requested :D)

. . .
sqlalchemy.url = sqlite:////tmp/test_alembic.sqlite3
. . .

env.py

from logging.config import fileConfig
from alembic.operations import ops
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
from alembic.autogenerate import rewriter
from models import BaseModel
. . .
target_metadata = BaseModel.metadata
writer = rewriter.Rewriter()


@writer.rewrites(ops.DropColumnOp)
def drop_column(context, revision, op):
    return [
        ops.AlterColumnOp(
            op.table_name,
            op.column_name,
            modify_name=f'del_{op.column_name}',
        )
    ]


def run_migrations_online() -> None:
    . . .
    with connectable.connect() as connection:
        context.configure(
            connection=connection,
            target_metadata=target_metadata,
            process_revision_directives=writer,
        )

        with context.begin_transaction():
            context.run_migrations()
. . .

Versions.

  • OS: ubuntu 20.04
  • Python: 3.11
  • Alembic: 1.13.1
  • SQLAlchemy: 2.0.32
  • Database: postgresql
  • DBAPI: asyncpg 0.29.0

Additional context
Please see discussion to get the whole context

Have a nice day!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions