Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One cannot add a column with a comment within batch operation context #761

Closed
cansjt opened this issue Nov 27, 2020 · 3 comments
Closed
Labels
batch migrations bug Something isn't working

Comments

@cansjt
Copy link
Contributor

cansjt commented Nov 27, 2020

Describe the bug
Batch operation op.batch_alter_table(...).add_column() method cannot add a column that comes with a comment.

Comment support is fairly new, maybe this is a "feature" due to some constraint I am not aware of ?

Expected behavior

The column is added to the table and the appropriate comment is defined for the column.

To Reproduce

I believe the following migration script, the one I found the issue with, stripped of anything unrelated to the bug, should provide the minimal amount of code to reproduce the bug:

from alembic import op
import sqlalchemy as sa

revision = '792d2e012c4a'
down_revision = None  # Obviously there exists other migrations that created the table in the first place

def upgrade():
    with op.batch_alter_table('some_table', schema='some_schema') as batch_op:
        batch_op.add_column(sa.Column('column_name', sa.Integer, nullable=True, comment="some comment"))
        # More operations may come here

Error

  File "~/.virtualenvs/project/bin/alembic", line 8, in <module>
    sys.exit(main())
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/config.py", line 581, in main
    CommandLine(prog=prog).main(argv=argv)
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/config.py", line 575, in main
    self.run_cmd(cfg, options)
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/config.py", line 555, in run_cmd
    **dict((k, getattr(options, k, None)) for k in kwarg)
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/command.py", line 298, in upgrade
    script.run_env()
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/script/base.py", line 489, in run_env
    util.load_python_file(self.dir, "env.py")
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/util/pyfiles.py", line 98, in load_python_file
    module = load_module_py(module_id, path)
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/util/compat.py", line 184, in load_module_py
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "src/dags/stats/migrations/env.py", line 145, in <module>
    run_migrations_online()
  File "src/dags/stats/migrations/env.py", line 138, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/runtime/environment.py", line 846, in run_migrations
    self.get_context().run_migrations(**kw)
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/runtime/migration.py", line 522, in run_migrations
    step.migration_fn(**kw)
  File "src/dags/stats/migrations/versions/792d2e012c4a_migration_script_.py", line 51, in upgrade
    batch_op.add_column(sa.Column('column_name', sa.Integer, nullable=True, comment="some comment"))
  File "<string>", line 3, in add_column
  File "~/.virtualenvs/jfdags/lib/python3.7/site-packages/alembic/operations/ops.py", line 1956, in batch_add_column
    return operations.invoke(op)
  File "~/.virtualenvs/jfdags/lib/python3.7/site-packages/alembic/operations/base.py", line 373, in invoke
    return fn(self, operation)
  File "~/.virtualenvs/jfdags/lib/python3.7/site-packages/alembic/operations/toimpl.py", line 146, in add_column
    operations.impl.create_column_comment(column)
AttributeError: 'BatchOperationsImpl' object has no attribute 'create_column_comment'

Versions.

  • OS: Ubuntu 2020.04
  • Python: 3.7.9
  • Alembic: 1.4.3
  • SQLAlchemy: 1.3.20
  • Database: Postgresql
  • DBAPI: psycopg2

Additional context

Have a nice day!
You too !

@cansjt cansjt added the requires triage New issue that requires categorization label Nov 27, 2020
@zzzeek zzzeek added batch migrations bug Something isn't working and removed requires triage New issue that requires categorization labels Nov 27, 2020
@sqla-tester
Copy link
Collaborator

Mike Bayer has proposed a fix for this issue in the master branch:

Implement column comments for batch https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/2385

@zzzeek
Copy link
Member

zzzeek commented Nov 27, 2020

we're on a slightly delayed path for relase as it's 1.5, feel free to test alembic master to see if that works on your end thanks!

@cansjt
Copy link
Contributor Author

cansjt commented Dec 18, 2020

Thank you for the blazing fast fix. 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
batch migrations bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants