Skip to content

Add add-database and remove-database lifecycle events - #2870

Draft
asg017 wants to merge 1 commit into
mainfrom
asg017/db-lifecycle-events
Draft

Add add-database and remove-database lifecycle events#2870
asg017 wants to merge 1 commit into
mainfrom
asg017/db-lifecycle-events

Conversation

@asg017

@asg017 asg017 commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Adds two new plugin-visible events that fire when a database is attached to or detached from a running Datasette instance:

  • add-database — fired by Datasette.add_database() (and therefore add_memory_database())
  • remove-database — fired by Datasette.remove_database()

Both events carry database (the attached name), path (resolved absolute filesystem path, None for in-memory databases) and is_memory. actor is None for programmatic calls.

Motivation

Plugins that manage databases have no way to learn about databases added or removed after startup. The motivating consumer is datasette-litestream, which runs a long-lived replication daemon: any database attached at runtime (e.g. by datasette-upload-dbs) is silently not replicated — a data-durability gap. With these events it can register new databases with the daemon immediately, and trigger a final sync on removal.

Delivery semantics

  • Best-effort, fire-and-forget: dispatch happens via loop.create_task() (with strong references held until completion, so tasks can't be garbage-collected mid-flight). Listeners run shortly after the change, not before the method returns.
  • Runtime-only: events fire only after invoke_startup() has completed and while an event loop is running. Databases attached during startup fire nothing — plugins that need those can iterate datasette.databases in their own startup hook. This also avoids the track_event() assertion failing before event classes are registered.
  • remove-database fires after close(): close() drains queued writes first, so a listener doing a final read of the file sees everything. The database file is never deleted (the exception is temp-disk databases, which delete their backing file on close — documented).
  • Rapid successive changes to the same name may reach listeners interleaved; documented as something listeners must tolerate.

Changes

  • datasette/events.py — new AddDatabaseEvent / RemoveDatabaseEvent dataclasses, registered in the core register_events() hookimpl
  • datasette/app.py_track_event_soon() helper + dispatch from add_database() / remove_database()
  • docs/events.md, docs/internals.rst — event docs and the delivery contract
  • Tests: registration, file/memory databases, remove (file untouched on disk), pre-startup and no-running-loop no-ops, auto-renamed names in events

Out of scope (possible follow-ups)

  • HTTP API for attaching/detaching databases (permissions, allowlisted directory, persistence story)
  • Any file deletion on removal — removal is detach-only

🤖 Generated with Claude Code

Fire plugin-visible events from Datasette.add_database() and
.remove_database() so plugins can react to databases being attached or
detached at runtime. Dispatch is best-effort fire-and-forget: events only
fire when startup has completed and an event loop is running, and
remove-database fires after close() so queued writes are flushed before
listeners run.

Motivating consumer is datasette-litestream, which needs to register newly
attached databases with its replication daemon.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@codecov

codecov Bot commented Aug 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 30 lines in your changes missing coverage. Please review.
✅ Project coverage is 0.00%. Comparing base (e889403) to head (c6bbcc3).

Files with missing lines Patch % Lines
datasette/app.py 0.00% 18 Missing ⚠️
datasette/events.py 0.00% 12 Missing ⚠️
Additional details and impacted files
@@          Coverage Diff          @@
##            main   #2870   +/-   ##
=====================================
  Coverage   0.00%   0.00%           
=====================================
  Files         73      73           
  Lines      12279   12307   +28     
=====================================
- Misses     12279   12307   +28     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant