Skip to content

fix: alembic column rename #1098

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion sqlalchemy_bigquery/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1331,11 +1331,26 @@ def __init__(self, *args, **kwargs):
pass
else:
from alembic.ddl import impl
from alembic.ddl.base import ColumnType, format_type, alter_table, alter_column
from alembic.ddl.base import (
ColumnName,
ColumnType,
format_column_name,
format_type,
alter_table,
alter_column,
)

class SqlalchemyBigqueryImpl(impl.DefaultImpl):
__dialect__ = "bigquery"

@compiles(ColumnName, "bigquery")
def visit_column_name(element: ColumnName, compiler: DDLCompiler, **kw) -> str:
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),
)

@compiles(ColumnType, "bigquery")
def visit_column_type(element: ColumnType, compiler: DDLCompiler, **kw) -> str:
"""Replaces the visit_column_type() function in alembic/alembic/ddl/base.py.
Expand Down
3 changes: 2 additions & 1 deletion tests/system/test_alembic.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,11 @@ def test_alembic_scenario(alembic_table):
op.add_column(
"account", Column("last_transaction_date", DateTime, comment="when updated")
)
op.alter_column("account", "name", new_column_name="friendly_name")

assert alembic_table("account", "schema") == [
SchemaField("id", "INTEGER", "REQUIRED"),
SchemaField("name", "STRING(50)", "REQUIRED", description="The name"),
SchemaField("friendly_name", "STRING(50)", "REQUIRED", description="The name"),
SchemaField("description", "STRING(200)"),
SchemaField("last_transaction_date", "DATETIME", description="when updated"),
]
Expand Down