db_testkit/
backend.rs

1use async_trait::async_trait;
2
3use crate::{error::Result, pool::PoolConfig, template::DatabaseName};
4
5/// A trait for database connections that can be pooled
6#[async_trait]
7pub trait Connection: Send {
8    /// Check if the connection is valid
9    async fn is_valid(&self) -> bool;
10
11    /// Reset the connection state
12    async fn reset(&mut self) -> Result<()>;
13
14    /// Execute a SQL query
15    async fn execute(&mut self, sql: &str) -> Result<()>;
16}
17
18/// A trait for database backends that can create and manage databases
19#[async_trait]
20pub trait DatabaseBackend: Send + Sync + Clone {
21    /// The type of connection this backend provides
22    type Connection: Connection;
23    /// The type of pool this backend provides
24    type Pool: DatabasePool<Connection = Self::Connection>;
25
26    /// Create a new database with the given name
27    async fn create_database(&self, name: &DatabaseName) -> Result<()>;
28
29    /// Drop a database with the given name
30    async fn drop_database(&self, name: &DatabaseName) -> Result<()>;
31
32    /// Create a new connection pool for the given database
33    async fn create_pool(&self, name: &DatabaseName, config: &PoolConfig) -> Result<Self::Pool>;
34
35    /// Terminate all connections to the given database
36    async fn terminate_connections(&self, name: &DatabaseName) -> Result<()>;
37
38    /// Create a new database from a template
39    async fn create_database_from_template(
40        &self,
41        name: &DatabaseName,
42        template: &DatabaseName,
43    ) -> Result<()>;
44}
45
46/// A trait for database pools that can be used to acquire and release connections
47#[async_trait]
48pub trait DatabasePool: Send + Sync + Clone {
49    /// The type of connection this pool provides
50    type Connection: Connection;
51
52    /// Acquire a connection from the pool
53    async fn acquire(&self) -> Result<Self::Connection>;
54
55    /// Release a connection back to the pool
56    async fn release(&self, conn: Self::Connection) -> Result<()>;
57}