feat(db): add backend shutdown hook for graceful runtime shutdown

Add Database::shutdown() with a default no-op implementation for backward compatibility.

Implement libSQL shutdown via flush_replicator(), treating SyncNotSupported as non-fatal.

Implement Postgres shutdown by closing the pool.
This commit is contained in:
mackaby
2026-03-12 15:06:12 -07:00
committed by Zaki
parent d5828b271d
commit 91e0c2ee62
3 changed files with 24 additions and 0 deletions
+12
View File
@@ -326,6 +326,18 @@ impl Database for LibSqlBackend {
libsql_migrations::run_incremental(&conn).await?;
Ok(())
}
async fn shutdown(&self) -> Result<(), DatabaseError> {
match self.db.flush_replicator().await {
Ok(Some(frame_no)) => {
tracing::debug!("libSQL replicator flushed at frame {}", frame_no);
Ok(())
}
Ok(None) => Ok(()),
Err(libsql::Error::SyncNotSupported(_)) => Ok(()),
Err(error) => Err(DatabaseError::from(error)),
}
}
}
// ==================== Row conversion helpers ====================
+7
View File
@@ -523,6 +523,13 @@ pub trait Database:
{
/// Run schema migrations for this backend.
async fn run_migrations(&self) -> Result<(), DatabaseError>;
/// Shutdown hook for backend-specific drain/flush behavior.
///
/// Default implementation is a no-op so existing backends remain compatible.
async fn shutdown(&self) -> Result<(), DatabaseError> {
Ok(())
}
}
#[cfg(test)]
+5
View File
@@ -61,6 +61,11 @@ impl Database for PgBackend {
async fn run_migrations(&self) -> Result<(), DatabaseError> {
self.store.run_migrations().await
}
async fn shutdown(&self) -> Result<(), DatabaseError> {
self.store.pool().close();
Ok(())
}
}
// ==================== ConversationStore ====================