A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
Show empty databases, but only for defined duckdb connections (#6119)
Shahmir Varqha452f59e349a0
diff --git a/frontend/src/components/datasources/datasources.tsx b/frontend/src/components/datasources/datasources.tsxindex 850273a9d5..d7f2dcf0de 100644--- a/frontend/src/components/datasources/datasources.tsx+++ b/frontend/src/components/datasources/datasources.tsx@@ -30,7 +30,11 @@ import {type SQLTableContext,useDataSourceActions,} from "@/core/datasets/data-source-connections";-import { DUCKDB_ENGINE, INTERNAL_SQL_ENGINES } from "@/core/datasets/engines";+import {+ DEFAULT_DUCKDB_DATABASE,+ DUCKDB_ENGINE,+ INTERNAL_SQL_ENGINES,+} from "@/core/datasets/engines";import {PreviewSQLTable,PreviewSQLTableList,@@ -101,9 +105,22 @@ const connectionsAtom = atom((get) => {const dataConnections = new Map(get(dataConnectionsMapAtom));// Filter out the internal engines if it has no databases+ // Or if it has only the in-memory database and no schemasfor (const engine of INTERNAL_SQL_ENGINES) {const connection = dataConnections.get(engine);- if (connection && connection.databases.length === 0) {+ if (!connection) {+ continue;+ }++ if (connection.databases.length === 0) {+ dataConnections.delete(engine);+ }++ if (+ connection.databases.length === 1 &&+ connection.databases[0].name === DEFAULT_DUCKDB_DATABASE &&+ connection.databases[0].schemas.length === 0+ ) {dataConnections.delete(engine);}}diff --git a/frontend/src/core/datasets/engines.ts b/frontend/src/core/datasets/engines.tsindex 978b5f74f0..646f11ae51 100644--- a/frontend/src/core/datasets/engines.ts+++ b/frontend/src/core/datasets/engines.ts@@ -8,3 +8,4 @@ export type ConnectionName = TypedString<"ConnectionName">;// Keep this in sync with the backend nameexport const DUCKDB_ENGINE = "__marimo_duckdb" as ConnectionName;export const INTERNAL_SQL_ENGINES = new Set([DUCKDB_ENGINE]);+export const DEFAULT_DUCKDB_DATABASE = "memory";diff --git a/marimo/_data/get_datasets.py b/marimo/_data/get_datasets.pyindex 65447940d9..e68647434c 100644--- a/marimo/_data/get_datasets.py+++ b/marimo/_data/get_datasets.py@@ -115,9 +115,19 @@ def _get_databases_from_duckdb_internal(tables_result = duckdb.execute("SHOW ALL TABLES").fetchall()else:tables_result = connection.execute("SHOW ALL TABLES").fetchall()- if not len(tables_result):- # No tables- return []++ if len(tables_result) == 0:+ # Return empty databases if there are no tables+ all_dbs = _get_duckdb_database_names(connection)+ return [+ Database(+ name=database,+ dialect="duckdb",+ schemas=[],+ engine=engine_name,+ )+ for database in all_dbs+ ]# Group tables by database and schema# databases_dict[database][schema] = [table1, table2, ...]diff --git a/tests/_data/test_get_datasets.py b/tests/_data/test_get_datasets.pyindex 8103749b26..3c19ce62fc 100644--- a/tests/_data/test_get_datasets.py+++ b/tests/_data/test_get_datasets.py@@ -388,8 +388,6 @@ DROP SCHEMA s2 CASCADE;@pytest.mark.skipif(not HAS_DEPS, reason="optional dependencies not installed")def test_get_databases() -> None:- assert get_databases_from_duckdb(connection=None) == []-import duckdbduckdb.execute(sql_query)@@ -410,6 +408,30 @@ def test_get_databases() -> None:duckdb.execute(cleanup_query)[email protected](not HAS_DEPS, reason="optional dependencies not installed")+def test_get_databases_with_no_tables() -> None:+ import duckdb++ in_memory_database = Database(+ name="memory",+ dialect="duckdb",+ schemas=[],+ engine=None,+ )++ # No connection, return in-memory database+ connection = None+ assert get_databases_from_duckdb(connection=connection) == [+ in_memory_database+ ]++ # Custom connection with no tables+ connection = duckdb.connect(":memory:")+ assert get_databases_from_duckdb(connection=connection) == [+ in_memory_database+ ]++@pytest.mark.skipif(not HAS_DEPS, reason="optional dependencies not installed")def test_get_databases_with_connection() -> None:import duckdbdiff --git a/tests/_sql/test_duckdb.py b/tests/_sql/test_duckdb.pyindex ea07851cda..1a6fe66b99 100644--- a/tests/_sql/test_duckdb.py+++ b/tests/_sql/test_duckdb.py@@ -150,7 +150,7 @@ def test_duckdb_engine_get_databases_no_conn() -> None:include_table_details=False,include_tables=False,)- assert initial_databases == []+ assert initial_databases == [Database(name="memory", dialect="duckdb")]assert engine.get_default_database() == "memory"assert engine.get_default_schema() == "main"diff --git a/tests/_sql/test_get_engines.py b/tests/_sql/test_get_engines.pyindex b32ef5281a..50a35112e7 100644--- a/tests/_sql/test_get_engines.py+++ b/tests/_sql/test_get_engines.py@@ -51,7 +51,7 @@ def test_engine_to_data_source_connection() -> None:assert connection.display_name == "duckdb (my_duckdb)"assert connection.default_database == "memory"assert connection.default_schema == "main"- assert connection.databases == []+ assert connection.databases == [Database(name="memory", dialect="duckdb")]# Test with ClickhouseEmbedded engineclickhouse_engine = ClickhouseEmbedded(None)