docbox_management/
database.rs

1use docbox_database::{DbPool, DbResult};
2
3/// Provider to get database access for the management tool
4///
5/// Expects that the access granted to the database is sufficient
6/// for creation and deletion of tables
7pub trait DatabaseProvider: Send + Sync + 'static {
8    /// Connect to the provided `database` providing back a [DbPool]
9    fn connect(&self, database: &str) -> impl Future<Output = DbResult<DbPool>> + Send;
10}
11
12pub struct CloseOnDrop(DbPool);
13
14pub fn close_pool_on_drop(pool: &DbPool) -> CloseOnDrop {
15    CloseOnDrop(pool.clone())
16}
17
18impl Drop for CloseOnDrop {
19    fn drop(&mut self) {
20        let pool = self.0.clone();
21
22        tokio::spawn(async move {
23            tracing::debug!("closing dropped pool");
24            pool.close().await;
25            tracing::debug!("closed dropped pool");
26        });
27    }
28}