Skip to content

Commit 1a9edc7

Browse files
Chore: Add heplful error message when drop operation fails (#5261)
1 parent 8a6b168 commit 1a9edc7

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

sqlmesh/core/schema_diff.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,11 @@ def _drop_operation(
498498
columns = ensure_list(columns)
499499
operations: t.List[TableAlterColumnOperation] = []
500500
column_pos, column_kwarg = self._get_matching_kwarg(columns[-1].name, struct, pos)
501-
assert column_pos is not None
502-
assert column_kwarg
501+
if column_pos is None or not column_kwarg:
502+
raise SQLMeshError(
503+
f"Cannot drop column '{columns[-1].name}' from table '{table_name}' - column not found. "
504+
f"This may indicate a mismatch between the expected and actual table schemas."
505+
)
503506
struct.expressions.pop(column_pos)
504507
operations.append(
505508
TableAlterDropColumnOperation(

tests/core/test_schema_diff.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
TableAlterChangeColumnTypeOperation,
1616
NestedSupport,
1717
)
18+
from sqlmesh.utils.errors import SQLMeshError
1819

1920

2021
def test_schema_diff_calculate():
@@ -2341,3 +2342,27 @@ def test_ignore_additive_array_operations():
23412342
ignore_additive=True,
23422343
)
23432344
assert len(operations_ignore_additive) == 0
2345+
2346+
2347+
def test_drop_operation_missing_column_error():
2348+
schema_differ = SchemaDiffer(
2349+
nested_support=NestedSupport.NONE,
2350+
support_positional_add=False,
2351+
)
2352+
2353+
# a struct that doesn't contain the column we're going to drop
2354+
current_struct = exp.DataType.build("STRUCT<id INT, name STRING>")
2355+
2356+
with pytest.raises(SQLMeshError) as error_message:
2357+
schema_differ._drop_operation(
2358+
columns=[TableAlterColumn.primitive("missing_column")],
2359+
struct=current_struct,
2360+
pos=0,
2361+
root_struct=current_struct,
2362+
table_name="test_table",
2363+
)
2364+
2365+
assert (
2366+
str(error_message.value)
2367+
== "Cannot drop column 'missing_column' from table 'test_table' - column not found. This may indicate a mismatch between the expected and actual table schemas."
2368+
)

0 commit comments

Comments
 (0)