snowflake-sqlalchemy icon indicating copy to clipboard operation
snowflake-sqlalchemy copied to clipboard

SNOW-159471 Alembic op.alter_column generates invalid SQL for rename

Open ms32035 opened this issue 5 years ago • 2 comments

Please answer these questions before submitting your issue. Thanks!

  1. What version of Python are you using (python --version)?

3.7

  1. What operating system and processor architecture are you using (python -c 'import platform; print(platform.platform())')?

  2. What are the component versions in the environment (pip list)?

Package                    Version
-------------------------- ---------
alembic                    1.3.2
snowflake-connector-python 2.1.2
snowflake-sqlalchemy       1.1.17
sqlacodegen                2.1.0
SQLAlchemy                 1.3.12
  1. What did you do? alembic migration with the following spec
    op.alter_column(
        "t",
        "c1",
        new_column_name="c2",
        schema="s",
    )
  1. What did you expect to see?

Correctly changed table name

  1. What did you see instead?

Invalid SQL: RENAME instead of RENAME COLUMN

  1. Can you set logging to DEBUG and collect the logs?
sqlalchemy.exc.ProgrammingError: (snowflake.connector.errors.ProgrammingError) 001003 (42000): SQL compilation error:
syntax error line 1 at position 34 unexpected 'c1'.
[SQL: ALTER TABLE s.t RENAME c1 TO c2]

BTW, yes I know this can be worked around with op.execute

ms32035 avatar Dec 20 '19 11:12 ms32035

We are seeing this issue, too. Any updates?

DannyNemer avatar May 08 '20 22:05 DannyNemer

Hey people, I triaged the issue and I'm sad to say that I can't fix this for you as we don't have a snowflake-alembic package.

However, I can tell you how to fix the behaviour yourself.

Just add the following code to the class we tell you to create here: https://github.com/snowflakedb/snowflake-sqlalchemy#alembic-support

    @compiles(ColumnName)
    def visit_column_name(element, compiler, **kw):
        return "%s RENAME COLUMN %s TO %s" % (
            alter_table(compiler, element.table_name, element.schema),
            format_column_name(compiler, element.column_name),
            format_column_name(compiler, element.newname),
        )

and don't forget to:

from alembic.ddl.base import alter_table, format_column_name, ColumnName
from sqlalchemy.ext.compiler import compiles

sfc-gh-mkeller avatar May 10 '20 18:05 sfc-gh-mkeller

To clean up and re-prioritize bugs and feature requests we are closing all issues older than 6 months as of Apr 1, 2023. If there are any issues or feature requests that you would like us to address, please re-create them. For urgent issues, opening a support case with this link Snowflake Community is the fastest way to get a response

github-actions[bot] avatar Apr 05 '23 01:04 github-actions[bot]