Add add-database and remove-database lifecycle events - #2870
Draft
asg017 wants to merge 1 commit into
Draft
Conversation
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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds two new plugin-visible events that fire when a database is attached to or detached from a running Datasette instance:
add-database— fired byDatasette.add_database()(and thereforeadd_memory_database())remove-database— fired byDatasette.remove_database()Both events carry
database(the attached name),path(resolved absolute filesystem path,Nonefor in-memory databases) andis_memory.actorisNonefor 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. bydatasette-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
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.invoke_startup()has completed and while an event loop is running. Databases attached during startup fire nothing — plugins that need those can iteratedatasette.databasesin their ownstartuphook. This also avoids thetrack_event()assertion failing before event classes are registered.remove-databasefires afterclose():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).Changes
datasette/events.py— newAddDatabaseEvent/RemoveDatabaseEventdataclasses, registered in the coreregister_events()hookimpldatasette/app.py—_track_event_soon()helper + dispatch fromadd_database()/remove_database()docs/events.md,docs/internals.rst— event docs and the delivery contractOut of scope (possible follow-ups)
🤖 Generated with Claude Code