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
+ ),
)
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
+ ),
)
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
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
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(
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,
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
)
-@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
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
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
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