Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions sqlmesh/core/schema_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,11 @@ def _drop_operation(
columns = ensure_list(columns)
operations: t.List[TableAlterColumnOperation] = []
column_pos, column_kwarg = self._get_matching_kwarg(columns[-1].name, struct, pos)
assert column_pos is not None
assert column_kwarg
if column_pos is None or not column_kwarg:
raise SQLMeshError(
f"Cannot drop column '{columns[-1].name}' from table '{table_name}' - column not found. "
f"This may indicate a mismatch between the expected and actual table schemas."
)
struct.expressions.pop(column_pos)
operations.append(
TableAlterDropColumnOperation(
Expand Down
25 changes: 25 additions & 0 deletions tests/core/test_schema_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
TableAlterChangeColumnTypeOperation,
NestedSupport,
)
from sqlmesh.utils.errors import SQLMeshError


def test_schema_diff_calculate():
Expand Down Expand Up @@ -2341,3 +2342,27 @@ def test_ignore_additive_array_operations():
ignore_additive=True,
)
assert len(operations_ignore_additive) == 0


def test_drop_operation_missing_column_error():
schema_differ = SchemaDiffer(
nested_support=NestedSupport.NONE,
support_positional_add=False,
)

# a struct that doesn't contain the column we're going to drop
current_struct = exp.DataType.build("STRUCT<id INT, name STRING>")

with pytest.raises(SQLMeshError) as error_message:
schema_differ._drop_operation(
columns=[TableAlterColumn.primitive("missing_column")],
struct=current_struct,
pos=0,
root_struct=current_struct,
table_name="test_table",
)

assert (
str(error_message.value)
== "Cannot drop column 'missing_column' from table 'test_table' - column not found. This may indicate a mismatch between the expected and actual table schemas."
)