|
1 | 1 | import logging |
2 | 2 |
|
| 3 | +from typing import Optional |
| 4 | + |
3 | 5 | from sqlalchemy.ext.compiler import compiles |
| 6 | +from sqlalchemy.sql.base import Executable |
| 7 | +from sqlalchemy.sql.elements import ClauseElement |
| 8 | + |
4 | 9 | from alembic.ddl import DefaultImpl |
5 | 10 | from alembic.ddl.base import ColumnNullable |
6 | 11 | from alembic.ddl.base import ColumnType |
@@ -56,25 +61,43 @@ def compare_server_default( |
56 | 61 | rendered_inspector_default, |
57 | 62 | ) |
58 | 63 |
|
59 | | - def correct_for_autogen_constraints( |
| 64 | + def drop_column( |
60 | 65 | self, |
61 | | - conn_unique_constraints, |
62 | | - conn_indexes, |
63 | | - metadata_unique_constraints, |
64 | | - metadata_indexes, |
65 | | - ): |
66 | | - |
67 | | - doubled_constraints = { |
68 | | - index |
69 | | - for index in conn_indexes |
70 | | - if index.info.get("duplicates_constraint") |
71 | | - } |
72 | | - |
73 | | - for ix in doubled_constraints: |
74 | | - conn_indexes.remove(ix) |
| 66 | + table_name: str, |
| 67 | + column: Column, |
| 68 | + schema: Optional[str] = None, |
| 69 | + **kw, |
| 70 | + ) -> None: |
| 71 | + column_name = column.name |
| 72 | + fkeys = self.dialect.get_foreign_keys(self.connection, table_name, schema) |
| 73 | + fkey = [ |
| 74 | + fkey["name"] for fkey in fkeys if column_name in fkey["constrained_columns"] |
| 75 | + ] |
| 76 | + if len(fkey) == 1: |
| 77 | + self._exec(_ExecDropForeignKey(table_name, fkey[0], schema)) |
| 78 | + super().drop_column(table_name, column, schema, **kw) |
| 79 | + |
| 80 | + |
| 81 | +class _ExecDropForeignKey(Executable, ClauseElement): |
| 82 | + inherit_cache = False |
| 83 | + |
| 84 | + def __init__( |
| 85 | + self, table_name: str, foreignkey_name: Column, schema: Optional[str] |
| 86 | + ) -> None: |
| 87 | + self.table_name = table_name |
| 88 | + self.foreignkey_name = foreignkey_name |
| 89 | + self.schema = schema |
| 90 | + |
| 91 | + |
| 92 | +@compiles(_ExecDropForeignKey, "iris") |
| 93 | +def _exec_drop_foreign_key( |
| 94 | + element: _ExecDropForeignKey, compiler: IRISDDLCompiler, **kw |
| 95 | +) -> str: |
| 96 | + return "%s DROP FOREIGN KEY %s" % ( |
| 97 | + alter_table(compiler, element.table_name, element.schema), |
| 98 | + format_column_name(compiler, element.foreignkey_name), |
| 99 | + ) |
75 | 100 |
|
76 | | - # if not sqla_compat.sqla_2: |
77 | | - # self._skip_functional_indexes(metadata_indexes, conn_indexes) |
78 | 101 |
|
79 | 102 | @compiles(ColumnNullable, "iris") |
80 | 103 | def visit_column_nullable( |
|
0 commit comments