@@ -169,6 +169,7 @@ class TableAlterOperation(PydanticModel):
169169 expected_table_struct : exp .DataType
170170 add_position : t .Optional [TableAlterColumnPosition ] = None
171171 current_type : t .Optional [exp .DataType ] = None
172+ cascade : bool = False
172173
173174 @classmethod
174175 def add (
@@ -192,13 +193,15 @@ def drop(
192193 columns : t .Union [TableAlterColumn , t .List [TableAlterColumn ]],
193194 expected_table_struct : t .Union [str , exp .DataType ],
194195 column_type : t .Optional [t .Union [str , exp .DataType ]] = None ,
196+ cascade : bool = False ,
195197 ) -> TableAlterOperation :
196198 column_type = exp .DataType .build (column_type ) if column_type else exp .DataType .build ("INT" )
197199 return cls (
198200 op = TableAlterOperationType .DROP ,
199201 columns = ensure_list (columns ),
200202 column_type = column_type ,
201203 expected_table_struct = exp .DataType .build (expected_table_struct ),
204+ cascade = cascade ,
202205 )
203206
204207 @classmethod
@@ -274,7 +277,9 @@ def expression(
274277 return alter_table
275278 elif self .is_drop :
276279 alter_table = exp .Alter (this = exp .to_table (table_name ), kind = "TABLE" )
277- drop_column = exp .Drop (this = self .column (array_element_selector ), kind = "COLUMN" )
280+ drop_column = exp .Drop (
281+ this = self .column (array_element_selector ), kind = "COLUMN" , cascade = self .cascade
282+ )
278283 alter_table .set ("actions" , [drop_column ])
279284 return alter_table
280285 else :
@@ -313,6 +318,7 @@ class SchemaDiffer(PydanticModel):
313318 into a FLOAT64 column just fine.
314319 support_coercing_compatible_types: Whether or not the engine for which the diff is being computed supports direct
315320 coercion of compatible types.
321+ drop_cascade: Whether to add CASCADE modifier when dropping a column.
316322 parameterized_type_defaults: Default values for parameterized data types. Dict key is a sqlglot exp.DataType.Type,
317323 but in the engine adapter specification we build it from the dialect string instead of specifying it directly.
318324 Example: `exp.DataType.build("STRING", dialect=DIALECT).this` instead of the underlying `exp.DataType.Type.TEXT`
@@ -336,6 +342,7 @@ class SchemaDiffer(PydanticModel):
336342 default_factory = dict , alias = "coerceable_types"
337343 )
338344 support_coercing_compatible_types : bool = False
345+ drop_cascade : bool = False
339346 parameterized_type_defaults : t .Dict [
340347 exp .DataType .Type , t .List [t .Tuple [t .Union [int , float ], ...]]
341348 ] = {}
@@ -458,7 +465,9 @@ def _drop_operation(
458465 assert column_kwarg
459466 struct .expressions .pop (column_pos )
460467 operations .append (
461- TableAlterOperation .drop (columns , root_struct .copy (), column_kwarg .args ["kind" ])
468+ TableAlterOperation .drop (
469+ columns , root_struct .copy (), column_kwarg .args ["kind" ], cascade = self .drop_cascade
470+ )
462471 )
463472 return operations
464473
0 commit comments