From: Louis-Amaury Chaib Date: Mon, 19 May 2025 11:51:37 +0000 (+0200) Subject: chore: move handling of if_exists to base.py X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F1627%2Fhead;p=thirdparty%2Fsqlalchemy%2Falembic.git chore: move handling of if_exists to base.py --- diff --git a/alembic/ddl/base.py b/alembic/ddl/base.py index f2411c90..50d8268f 100644 --- a/alembic/ddl/base.py +++ b/alembic/ddl/base.py @@ -196,7 +196,9 @@ def visit_rename_table( def visit_add_column(element: AddColumn, compiler: DDLCompiler, **kw) -> str: return "%s %s" % ( alter_table(compiler, element.table_name, element.schema), - add_column(compiler, element.column, **kw), + add_column( + compiler, element.column, if_not_exists=element.if_not_exists, **kw + ), ) @@ -204,7 +206,9 @@ def visit_add_column(element: AddColumn, compiler: DDLCompiler, **kw) -> str: def visit_drop_column(element: DropColumn, compiler: DDLCompiler, **kw) -> str: return "%s %s" % ( alter_table(compiler, element.table_name, element.schema), - drop_column(compiler, element.column.name, **kw), + drop_column( + compiler, element.column.name, if_exists=element.if_exists, **kw + ), ) @@ -323,16 +327,29 @@ def alter_table( return "ALTER TABLE %s" % format_table_name(compiler, name, schema) -def drop_column(compiler: DDLCompiler, name: str, **kw) -> str: - return "DROP COLUMN %s" % format_column_name(compiler, name) +def drop_column( + compiler: DDLCompiler, name: str, if_exists: Optional[bool] = None, **kw +) -> str: + return "DROP COLUMN %s%s" % ( + "IF EXISTS " if if_exists else "", + format_column_name(compiler, name), + ) def alter_column(compiler: DDLCompiler, name: str) -> str: return "ALTER COLUMN %s" % format_column_name(compiler, name) -def add_column(compiler: DDLCompiler, column: Column[Any], **kw) -> str: - text = "ADD COLUMN %s" % compiler.get_column_specification(column, **kw) +def add_column( + compiler: DDLCompiler, + column: Column[Any], + if_not_exists: Optional[bool] = None, + **kw, +) -> str: + text = "ADD COLUMN %s%s" % ( + "IF NOT EXISTS " if if_not_exists else "", + compiler.get_column_specification(column, **kw), + ) const = " ".join( compiler.process(constraint) for constraint in column.constraints diff --git a/alembic/ddl/mssql.py b/alembic/ddl/mssql.py index 47c997e1..257abda5 100644 --- a/alembic/ddl/mssql.py +++ b/alembic/ddl/mssql.py @@ -10,6 +10,7 @@ from typing import List from typing import Optional from typing import TYPE_CHECKING from typing import Union +import warnings from sqlalchemy import types as sqltypes from sqlalchemy.schema import Column @@ -206,6 +207,13 @@ class MSSQLImpl(DefaultImpl): if_exists: Optional[bool] = None, **kw, ) -> None: + if if_exists is not None: + # if exists is kept for compatibility with parent API + # but not supported by mssql + warnings.warn( + "The `if_exists` flag is not supported by mssql" + "and will be ignored" + ) drop_default = kw.pop("mssql_drop_default", False) if drop_default: self._exec( @@ -223,9 +231,7 @@ class MSSQLImpl(DefaultImpl): drop_fks = kw.pop("mssql_drop_foreign_key", False) if drop_fks: self._exec(_ExecDropFKConstraint(table_name, column, schema)) - super().drop_column( - table_name, column, schema=schema, if_exists=if_exists, **kw - ) + super().drop_column(table_name, column, schema=schema, **kw) def compare_server_default( self, diff --git a/alembic/ddl/mysql.py b/alembic/ddl/mysql.py index 47e9d808..c7b3905c 100644 --- a/alembic/ddl/mysql.py +++ b/alembic/ddl/mysql.py @@ -12,14 +12,12 @@ from typing import Union from sqlalchemy import schema from sqlalchemy import types as sqltypes -from .base import AddColumn from .base import alter_table from .base import AlterColumn from .base import ColumnDefault from .base import ColumnName from .base import ColumnNullable from .base import ColumnType -from .base import DropColumn from .base import format_column_name from .base import format_server_default from .impl import DefaultImpl @@ -393,29 +391,6 @@ def _mysql_alter_default( ) -@compiles(AddColumn, "mysql", "mariadb") -def _mysql_add_column( - element: AddColumn, compiler: MySQLDDLCompiler, **kw -) -> str: - - return "%s ADD COLUMN %s%s" % ( - alter_table(compiler, element.table_name, element.schema), - "IF NOT EXISTS " if element.if_not_exists else "", - compiler.get_column_specification(element.column, **kw), - ) - - -@compiles(DropColumn, "mysql", "mariadb") -def _mysql_drop_column( - element: DropColumn, compiler: MySQLDDLCompiler, **kw -) -> str: - return "%s DROP COLUMN %s%s" % ( - alter_table(compiler, element.table_name, element.schema), - "IF EXISTS " if element.if_exists else "", - format_column_name(compiler, element.column.name), - ) - - @compiles(MySQLModifyColumn, "mysql", "mariadb") def _mysql_modify_column( element: MySQLModifyColumn, compiler: MySQLDDLCompiler, **kw diff --git a/alembic/ddl/postgresql.py b/alembic/ddl/postgresql.py index 244281a2..10f606fb 100644 --- a/alembic/ddl/postgresql.py +++ b/alembic/ddl/postgresql.py @@ -27,18 +27,15 @@ from sqlalchemy.dialects.postgresql import BIGINT from sqlalchemy.dialects.postgresql import ExcludeConstraint from sqlalchemy.dialects.postgresql import INTEGER from sqlalchemy.schema import CreateIndex -from sqlalchemy.sql.compiler import DDLCompiler from sqlalchemy.sql.elements import ColumnClause from sqlalchemy.sql.elements import TextClause from sqlalchemy.sql.functions import FunctionElement from sqlalchemy.types import NULLTYPE -from .base import AddColumn from .base import alter_column from .base import alter_table from .base import AlterColumn from .base import ColumnComment -from .base import DropColumn from .base import format_column_name from .base import format_table_name from .base import format_type @@ -516,34 +513,6 @@ class PostgresqlColumnType(AlterColumn): self.using = using -@compiles(AddColumn, "postgresql") -def visit_add_column(element: AddColumn, compiler: PGDDLCompiler, **kw) -> str: - return "%s %s" % ( - alter_table(compiler, element.table_name, element.schema), - add_column( - compiler, - element.column, - if_not_exists=element.if_not_exists, - **kw, - ), - ) - - -@compiles(DropColumn, "postgresql") -def visit_drop_column( - element: DropColumn, compiler: PGDDLCompiler, **kw -) -> str: - return "%s %s" % ( - alter_table(compiler, element.table_name, element.schema), - drop_column( - compiler, - element.column.name, - if_exists=element.if_exists, - **kw, - ), - ) - - @compiles(RenameTable, "postgresql") def visit_rename_table( element: RenameTable, compiler: PGDDLCompiler, **kw @@ -880,39 +849,3 @@ def _render_potential_column( autogen_context, wrap_in_element=isinstance(value, (TextClause, FunctionElement)), ) - - -def add_column( - compiler: DDLCompiler, - column: Column[Any], - *, - if_not_exists: Optional[bool] = None, - **kw, -) -> str: - text = "ADD COLUMN " - if if_not_exists: - text += "IF NOT EXISTS " - - text += compiler.get_column_specification(column, **kw) - - const = " ".join( - compiler.process(constraint) for constraint in column.constraints - ) - if const: - text += " " + const - - return text - - -def drop_column( - compiler: DDLCompiler, - name: str, - *, - if_exists: Optional[bool] = None, - **kw, -) -> str: - text = "DROP COLUMN " - if if_exists: - text += "IF EXISTS " - text += format_column_name(compiler, name) - return text