From d8d1d94be26d33f5671e5f800c9383a9105770ad Mon Sep 17 00:00:00 2001 From: DanCardin Date: Tue, 4 Mar 2025 16:07:59 -0500 Subject: [PATCH] Add TextClause to alter_column's server_default fields. Given sqlalchemy models with server_defaults, alembic may autogenerate `existing_server_default=sa.text("...")`, which fails typechecking on the resultant migration because `TextClause` is not currently a valid annotated type. Closes: #1577 Pull-request: https://github.com/sqlalchemy/alembic/pull/1577 Pull-request-sha: e21d07e55847f1e78e53f4f04bc2e458945f83fe Change-Id: I583748e16f3fccb4d58d8c4aa0e33c00b029523c --- alembic/op.pyi | 4 ++-- alembic/operations/base.py | 6 ++++-- alembic/operations/ops.py | 8 +++++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/alembic/op.pyi b/alembic/op.pyi index d86bef46..13a5852f 100644 --- a/alembic/op.pyi +++ b/alembic/op.pyi @@ -146,12 +146,12 @@ def alter_column( *, nullable: Optional[bool] = None, comment: Union[str, Literal[False], None] = False, - server_default: Any = False, + server_default: Union[str, bool, Identity, Computed, TextClause] = False, new_column_name: Optional[str] = None, type_: Union[TypeEngine[Any], Type[TypeEngine[Any]], None] = None, existing_type: Union[TypeEngine[Any], Type[TypeEngine[Any]], None] = None, existing_server_default: Union[ - str, bool, Identity, Computed, None + str, bool, Identity, Computed, TextClause, None ] = False, existing_nullable: Optional[bool] = None, existing_comment: Optional[str] = None, diff --git a/alembic/operations/base.py b/alembic/operations/base.py index 456d1c75..13993114 100644 --- a/alembic/operations/base.py +++ b/alembic/operations/base.py @@ -705,14 +705,16 @@ class Operations(AbstractOperations): *, nullable: Optional[bool] = None, comment: Union[str, Literal[False], None] = False, - server_default: Any = False, + server_default: Union[ + str, bool, Identity, Computed, TextClause + ] = False, new_column_name: Optional[str] = None, type_: Union[TypeEngine[Any], Type[TypeEngine[Any]], None] = None, existing_type: Union[ TypeEngine[Any], Type[TypeEngine[Any]], None ] = None, existing_server_default: Union[ - str, bool, Identity, Computed, None + str, bool, Identity, Computed, TextClause, None ] = False, existing_nullable: Optional[bool] = None, existing_comment: Optional[str] = None, diff --git a/alembic/operations/ops.py b/alembic/operations/ops.py index bb4d825b..eebc2323 100644 --- a/alembic/operations/ops.py +++ b/alembic/operations/ops.py @@ -1840,14 +1840,16 @@ class AlterColumnOp(AlterTableOp): *, nullable: Optional[bool] = None, comment: Optional[Union[str, Literal[False]]] = False, - server_default: Any = False, + server_default: Union[ + str, bool, Identity, Computed, TextClause + ] = False, new_column_name: Optional[str] = None, type_: Optional[Union[TypeEngine[Any], Type[TypeEngine[Any]]]] = None, existing_type: Optional[ Union[TypeEngine[Any], Type[TypeEngine[Any]]] ] = None, - existing_server_default: Optional[ - Union[str, bool, Identity, Computed] + existing_server_default: Union[ + str, bool, Identity, Computed, TextClause, None ] = False, existing_nullable: Optional[bool] = None, existing_comment: Optional[str] = None, -- 2.47.3