Fix startup hook to fire after metadata and schema tables are populated (#2666)

* Fix startup hook to fire after metadata and schema tables are populated

Previously, the startup() plugin hook fired before internal database
tables were populated from metadata.yaml and before catalog schema
tables were filled. This meant plugins couldn't read or modify metadata
during startup. Now invoke_startup() calls refresh_schemas() before
firing startup hooks, ensuring metadata and catalog tables are available.

* Fix startup hook to fire after metadata and schema tables are populated

Previously, the startup() plugin hook fired before internal database
tables were populated from metadata.yaml and before catalog schema
tables were filled. This meant plugins couldn't read or modify metadata
during startup. Now invoke_startup() calls _refresh_schemas() before
firing startup hooks, ensuring metadata and catalog tables are available.

Updated test_tracer to reflect that internal DB creation SQL now runs
during startup rather than during the first traced request.

* Move check_databases before invoke_startup in CLI serve

Since invoke_startup now calls _refresh_schemas() which queries each
database, the spatialite connection check must run first to provide
the friendly error message instead of a raw OperationalError.

https://claude.ai/code/session_01KL4t5FZYb32rZY7xaqrrZU
This commit is contained in:
Simon Willison 2026-03-16 17:56:40 -07:00 committed by GitHub
commit 7f93353549
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 35 additions and 22 deletions

View file

@ -127,6 +127,18 @@ def startup(datasette):
internal_db = datasette.get_internal_database()
result = await internal_db.execute("select 1 + 1")
datasette._startup_hook_calculation = result.first()[0]
# Check that metadata tables have been populated before startup fires
metadata_rows = await internal_db.execute(
"select key, value from metadata_instance"
)
datasette._startup_metadata_keys = [row["key"] for row in metadata_rows]
# Check that catalog/schema tables have been populated before startup fires
catalog_rows = await internal_db.execute(
"select database_name from catalog_databases"
)
datasette._startup_catalog_databases = [
row["database_name"] for row in catalog_rows
]
return inner