Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions sqlit/domains/query/ui/mixins/autocomplete_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@

SCHEMA_PROCESS_BATCH_SIZE = 200

# With no database selected, autocomplete indexes every user database on the
# server. Past this many databases that means hundreds of metadata queries
# queued on the single connection executor (blocking the explorer and user
# queries for minutes) and millions of entries processed on the UI thread, so
# indexing waits until the user picks a database.
MAX_SERVER_WIDE_INDEX_DATABASES = 20


def _too_many_databases_message(count: int) -> str:
return (
f"Autocomplete not indexed: the server has {count} databases. "
"Select a database in the explorer (Enter on it) to index it."
)


def _dedupe_routines(routines: list[Any]) -> list[Any]:
"""Deduplicate routines without collapsing equal names in other schemas."""
Expand Down Expand Up @@ -346,6 +360,11 @@ def _on_databases_loaded(self: AutocompleteMixinHost, databases: list) -> None:
return
caps = provider.capabilities

if len(databases) > MAX_SERVER_WIDE_INDEX_DATABASES:
self._stop_schema_spinner()
self.notify(_too_many_databases_message(len(databases)), severity="warning")
return

self._schema_pending_dbs = databases
self._schema_total_jobs = len(databases) * 3 # tables, views, procedures per db
supports_procedures = caps.supports_stored_procedures and isinstance(provider.schema_inspector, ProcedureInspector)
Expand Down Expand Up @@ -821,6 +840,9 @@ async def run_db_call(fn: Any, *args: Any, **kwargs: Any) -> Any:
all_dbs = await run_db_call(inspector.get_databases, connection)
system_dbs = {s.lower() for s in caps.system_databases}
databases = [d for d in all_dbs if d.lower() not in system_dbs]
if len(databases) > MAX_SERVER_WIDE_INDEX_DATABASES:
self.notify(_too_many_databases_message(len(databases)), severity="warning")
return
else:
databases = []
else:
Expand Down
70 changes: 70 additions & 0 deletions tests/unit/test_autocomplete_many_databases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""Server-wide autocomplete indexing is skipped on servers with many databases."""

from __future__ import annotations

from types import SimpleNamespace
from typing import Any

from sqlit.domains.query.ui.mixins.autocomplete_schema import (
MAX_SERVER_WIDE_INDEX_DATABASES,
AutocompleteSchemaMixin,
)


class _Host(AutocompleteSchemaMixin):
def __init__(self) -> None:
self.current_provider = SimpleNamespace(
capabilities=SimpleNamespace(supports_stored_procedures=False),
schema_inspector=object(),
)
self.current_connection = object()
self.notifications: list[tuple[str, str]] = []
self.loaded: list[tuple[str, Any]] = []
self.spinner_stopped = False

def notify(self, message: str, severity: str = "information") -> None:
self.notifications.append((message, severity))

def _stop_schema_spinner(self) -> None:
self.spinner_stopped = True

def _load_tables_job(self, database: Any) -> None:
self.loaded.append(("tables", database))

def _load_views_job(self, database: Any) -> None:
self.loaded.append(("views", database))

def _schema_job_complete(self) -> None:
pass


def test_many_databases_skip_indexing_and_warn() -> None:
host = _Host()
databases = [f"db{i}" for i in range(MAX_SERVER_WIDE_INDEX_DATABASES + 1)]

host._on_databases_loaded(databases)

assert host.loaded == []
assert host.spinner_stopped
assert len(host.notifications) == 1
message, severity = host.notifications[0]
assert severity == "warning"
assert str(len(databases)) in message


def test_few_databases_are_indexed() -> None:
host = _Host()
databases = [f"db{i}" for i in range(MAX_SERVER_WIDE_INDEX_DATABASES)]

host._on_databases_loaded(databases)

assert ("tables", "db0") in host.loaded
assert host.notifications == []


def test_selected_database_is_always_indexed() -> None:
host = _Host()

host._on_databases_loaded(["AppDb"])

assert host.loaded == [("tables", "AppDb"), ("views", "AppDb")]