Skip to content

Commit ec48875

Browse files
committed
fix MD attach error handlnig
style
1 parent 1d6daf9 commit ec48875

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

sqlmesh/core/config/connection.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,14 @@ def init(cursor: duckdb.DuckDBPyConnection) -> None:
276276
# set it as the default catalog.
277277
# If a user tried to attach a MotherDuck database/share which has already by attached via
278278
# `ATTACH 'md:'`, then we don't want to raise since this is expected.
279-
if not (
280-
'database with name "memory" already exists' in str(e)
281-
and path_options == ":memory:"
282-
) and f'database with name "{alias}" already exists' not in str(e):
279+
if (
280+
not (
281+
'database with name "memory" already exists' in str(e)
282+
and path_options == ":memory:"
283+
)
284+
and f"""database with name "{path_options.path.replace('md:', '')}" already exists"""
285+
not in str(e)
286+
):
283287
raise e
284288
if i == 0 and not getattr(self, "database", None):
285289
cursor.execute(f"USE {alias}")
@@ -373,12 +377,17 @@ def to_sql(self, alias: str) -> str:
373377
options.append(f"TYPE {self.type.upper()}")
374378
if self.read_only:
375379
options.append("READ_ONLY")
380+
options_sql = f" ({', '.join(options)})" if options else ""
381+
alias_sql = ""
376382
# TODO: Add support for Postgres schema. Currently adding it blocks access to the information_schema
377-
alias_sql = (
383+
if self.type == "motherduck":
378384
# MotherDuck does not support aliasing
379-
f" AS {alias}" if not (self.type == "motherduck" or self.path.startswith("md:")) else ""
380-
)
381-
options_sql = f" ({', '.join(options)})" if options else ""
385+
if (md_db := self.path.replace("md:", "")) != alias.replace('"', ""):
386+
raise ConfigError(
387+
f"MotherDuck does not support assigning an alias different from the database name {md_db}."
388+
)
389+
else:
390+
alias_sql += f" AS {alias}"
382391
return f"ATTACH '{self.path}'{alias_sql}{options_sql}"
383392

384393

0 commit comments

Comments
 (0)