]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
chore: move handling of if_exists to base.py 1627/head
authorLouis-Amaury Chaib <louisamaury.chaib@partoo.fr>
Mon, 19 May 2025 11:51:37 +0000 (13:51 +0200)
committerLouis-Amaury Chaib <louisamaury.chaib@partoo.fr>
Mon, 19 May 2025 11:51:37 +0000 (13:51 +0200)
alembic/ddl/base.py
alembic/ddl/mssql.py
alembic/ddl/mysql.py
alembic/ddl/postgresql.py

index f2411c903eaf311a05ee4074b764686cd6e42da8..50d8268f4914e7ee19120e17c41f07b110af8e06 100644 (file)
@@ -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
index 47c997e176d6e46dfa13606ed80bc4355e3a8044..257abda58cc16fcec9255d30e03ef3a27eb95842 100644 (file)
@@ -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,
index 47e9d80871e3781cb6391e0825d4d92517bfbe23..c7b3905cba35d99777a42432ae2ae555bb54e95d 100644 (file)
@@ -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
index 244281a2d61b9f08ce9e4793198475ca7c34a5b4..10f606fb12beadf0ca49a94b811cb1b0e3fa5d78 100644 (file)
@@ -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