Skip to content

Commit 12a70ee

Browse files
committed
fix: show Databases folder for MotherDuck
- Rename database field to default_database (stored in options) - This keeps tcp_endpoint.database empty so tree shows Databases folder - Add get_databases() to list all MotherDuck databases - Add get_tables/get_views() with database filtering
1 parent 72dc5c5 commit 12a70ee

3 files changed

Lines changed: 56 additions & 8 deletions

File tree

sqlit/domains/connections/providers/motherduck/adapter.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from typing import TYPE_CHECKING, Any
66

7+
from sqlit.domains.connections.providers.adapters.base import TableInfo
78
from sqlit.domains.connections.providers.duckdb.adapter import DuckDBAdapter
89

910
if TYPE_CHECKING:
@@ -36,10 +37,8 @@ def connect(self, config: ConnectionConfig) -> Any:
3637
package_name=self.install_package,
3738
)
3839

39-
# Get database from options or tcp_endpoint
40-
database = config.get_option("database", "")
41-
if not database and config.tcp_endpoint:
42-
database = config.tcp_endpoint.database
40+
# Get default database from options
41+
database = config.get_option("default_database", "")
4342

4443
# Get token from tcp_endpoint.password (stored in keyring)
4544
token = ""
@@ -56,6 +55,51 @@ def connect(self, config: ConnectionConfig) -> Any:
5655
duckdb_any: Any = duckdb
5756
return duckdb_any.connect(conn_str)
5857

58+
def get_databases(self, conn: Any) -> list[str]:
59+
"""List all MotherDuck databases."""
60+
result = conn.execute("SELECT database_name FROM duckdb_databases() WHERE NOT internal")
61+
return [row[0] for row in result.fetchall()]
62+
63+
def get_tables(self, conn: Any, database: str | None = None) -> list[TableInfo]:
64+
"""Get tables from a specific MotherDuck database."""
65+
if database:
66+
result = conn.execute(
67+
"SELECT table_schema, table_name FROM information_schema.tables "
68+
"WHERE table_catalog = ? "
69+
"AND table_type = 'BASE TABLE' "
70+
"AND table_schema NOT IN ('pg_catalog', 'information_schema') "
71+
"ORDER BY table_schema, table_name",
72+
(database,),
73+
)
74+
else:
75+
result = conn.execute(
76+
"SELECT table_schema, table_name FROM information_schema.tables "
77+
"WHERE table_type = 'BASE TABLE' "
78+
"AND table_schema NOT IN ('pg_catalog', 'information_schema') "
79+
"ORDER BY table_schema, table_name"
80+
)
81+
return [(row[0], row[1]) for row in result.fetchall()]
82+
83+
def get_views(self, conn: Any, database: str | None = None) -> list[TableInfo]:
84+
"""Get views from a specific MotherDuck database."""
85+
if database:
86+
result = conn.execute(
87+
"SELECT table_schema, table_name FROM information_schema.tables "
88+
"WHERE table_catalog = ? "
89+
"AND table_type = 'VIEW' "
90+
"AND table_schema NOT IN ('pg_catalog', 'information_schema') "
91+
"ORDER BY table_schema, table_name",
92+
(database,),
93+
)
94+
else:
95+
result = conn.execute(
96+
"SELECT table_schema, table_name FROM information_schema.tables "
97+
"WHERE table_type = 'VIEW' "
98+
"AND table_schema NOT IN ('pg_catalog', 'information_schema') "
99+
"ORDER BY table_schema, table_name"
100+
)
101+
return [(row[0], row[1]) for row in result.fetchall()]
102+
59103
def build_select_query(
60104
self, table: str, limit: int, database: str | None = None, schema: str | None = None
61105
) -> str:

sqlit/domains/connections/providers/motherduck/schema.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
display_name="MotherDuck",
1212
fields=(
1313
SchemaField(
14-
name="database",
15-
label="Database",
14+
name="default_database",
15+
label="Default Database",
1616
placeholder="my_database",
1717
required=True,
1818
),
@@ -24,6 +24,6 @@
2424
),
2525
),
2626
supports_ssh=False,
27-
is_file_based=False, # Not file-based, uses database + token
27+
is_file_based=False,
2828
requires_auth=True,
2929
)

tests/unit/test_motherduck_adapter.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,17 @@ def test_motherduck_schema_uses_password_field():
3636
from sqlit.domains.connections.providers.motherduck.schema import SCHEMA
3737

3838
field_names = [f.name for f in SCHEMA.fields]
39-
assert "database" in field_names
39+
assert "default_database" in field_names
4040
assert "password" in field_names # Uses standard password field for token
4141

4242
# Password field should be labeled as "Access Token"
4343
password_field = next(f for f in SCHEMA.fields if f.name == "password")
4444
assert password_field.label == "Access Token"
4545

46+
# Database field should be labeled as "Default Database"
47+
db_field = next(f for f in SCHEMA.fields if f.name == "default_database")
48+
assert db_field.label == "Default Database"
49+
4650

4751
def test_motherduck_supports_multiple_databases():
4852
"""Test MotherDuck reports support for multiple databases."""

0 commit comments

Comments
 (0)