Skip to content

Commit 9f25d23

Browse files
Fallback to DROP VIEW if DROP TABLE fails in DuckDB
Signed-off-by: Alessio Giuliano <alessio.giuliano@pix4d.com>
1 parent c5dbce5 commit 9f25d23

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

sqlmesh/core/engine_adapter/duckdb.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,23 @@ def _create_table(
218218
@property
219219
def _is_motherduck(self) -> bool:
220220
return self._extra_config.get("is_motherduck", False)
221+
222+
def drop_table(
223+
self,
224+
table_name: t.Any,
225+
exists: bool = True,
226+
**kwargs: t.Any,
227+
) -> None:
228+
"""
229+
DuckDB will raise an error if you try to DROP TABLE on a view.
230+
Fallback to DROP VIEW if the execution of DROP TABLE fails.
231+
"""
232+
from duckdb import Error
233+
234+
# Safety: Remove 'exists' from kwargs so we don't pass it twice
235+
kwargs.pop("exists", None)
236+
237+
try:
238+
super().drop_table(table_name, exists=exists, **kwargs)
239+
except Error:
240+
self.drop_view(table_name, **kwargs)

0 commit comments

Comments
 (0)