Skip to content

Commit f99abbf

Browse files
committed
fix(mssql): skip dbo on drop schema
Signed-off-by: Cortland Goffena <30168413+cmgoffena13@users.noreply.github.com>
1 parent 869248c commit f99abbf

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

sqlmesh/core/engine_adapter/mssql.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,10 @@ def drop_schema(
180180
**drop_args: t.Dict[str, exp.Expr],
181181
) -> None:
182182
"""
183-
MsSql doesn't support CASCADE clause and drops schemas unconditionally.
183+
MsSql doesn't support CASCADE clause so objects are dropped individually.
184+
185+
SQL Server also forbids dropping the built-in ``dbo`` schema (error 15150).
186+
Objects inside it are still dropped when cascade=True, but the schema itself is left in place.
184187
"""
185188
if cascade:
186189
objects = self._get_data_objects(schema_name)
@@ -199,6 +202,11 @@ def drop_schema(
199202
object_table,
200203
exists=ignore_if_not_exists,
201204
)
205+
206+
schema = schema_name.db if isinstance(schema_name, exp.Table) else schema_name
207+
if schema.lower() == "dbo":
208+
return
209+
202210
super().drop_schema(schema_name, ignore_if_not_exists=ignore_if_not_exists, cascade=False)
203211

204212
def merge(

tests/core/engine_adapter/test_mssql.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,26 @@ def test_drop_schema(make_mocked_engine_adapter: t.Callable):
714714
]
715715

716716

717+
def test_drop_schema_skips_dbo(make_mocked_engine_adapter: t.Callable):
718+
adapter = make_mocked_engine_adapter(MSSQLEngineAdapter)
719+
720+
adapter._get_data_objects = mock.Mock()
721+
adapter._get_data_objects.return_value = [
722+
DataObject(
723+
catalog="test_catalog",
724+
schema="dbo",
725+
name="test_view",
726+
type=DataObjectType.from_str("VIEW"),
727+
)
728+
]
729+
730+
adapter.drop_schema("dbo", cascade=True)
731+
732+
sql_calls = to_sql_calls(adapter)
733+
assert """DROP VIEW IF EXISTS [dbo].[test_view];""" in sql_calls
734+
assert """DROP SCHEMA IF EXISTS [dbo];""" not in sql_calls
735+
736+
717737
def test_drop_schema_with_special_identifiers(make_mocked_engine_adapter: t.Callable):
718738
adapter = make_mocked_engine_adapter(MSSQLEngineAdapter)
719739

0 commit comments

Comments
 (0)