pub enum DatabasePool {
Postgres(PgPool),
MySQL(MySqlPool),
}Expand description
Database connection pool abstraction.
This enum wraps either a PostgreSQL or MySQL connection pool, providing a unified interface for database operations.
§Database URL Format
- PostgreSQL:
postgres://orpostgresql:// - MySQL:
mysql://
§Examples
use cargo_hammerwork::utils::database::DatabasePool;
// The database type is automatically detected from the URL
let pool = DatabasePool::connect(
"postgresql://user:pass@localhost:5432/hammerwork",
10 // max connections
).await?;Variants§
Implementations§
Source§impl DatabasePool
impl DatabasePool
Sourcepub async fn connect(database_url: &str, pool_size: u32) -> Result<Self>
pub async fn connect(database_url: &str, pool_size: u32) -> Result<Self>
Connect to a database with the specified connection pool size.
The database type is automatically detected from the URL scheme:
postgres://orpostgresql://→ PostgreSQLmysql://→ MySQL
§Arguments
database_url- Database connection URLpool_size- Maximum number of connections in the pool
§Examples
use cargo_hammerwork::utils::database::DatabasePool;
// PostgreSQL with 10 connections
let pg_pool = DatabasePool::connect(
"postgresql://user:pass@localhost/mydb",
10
).await?;
// MySQL with 5 connections
let mysql_pool = DatabasePool::connect(
"mysql://root:pass@localhost/mydb",
5
).await?;Sourcepub fn create_job_queue(self) -> JobQueueWrapper
pub fn create_job_queue(self) -> JobQueueWrapper
Create a job queue from this database pool.
This consumes the pool and returns a wrapped JobQueue that automatically handles database-specific implementations.
§Examples
use cargo_hammerwork::utils::database::DatabasePool;
let pool = DatabasePool::connect("postgresql://localhost/hammerwork", 5).await?;
let job_queue = pool.create_job_queue();
// Use job_queue for enqueuing, processing, etc.Sourcepub async fn migrate(&self, drop_tables: bool) -> Result<()>
pub async fn migrate(&self, drop_tables: bool) -> Result<()>
Run database migrations.
This method runs all pending migrations on the connected database. Optionally drops existing tables before running migrations.
§Arguments
drop_tables- If true, drops all Hammerwork tables before running migrations
§Examples
use cargo_hammerwork::utils::database::DatabasePool;
let pool = DatabasePool::connect("postgresql://localhost/hammerwork", 5).await?;
// Run migrations on existing database
pool.migrate(false).await?;
// Drop tables and run fresh migrations
pool.migrate(true).await?;Auto Trait Implementations§
impl !RefUnwindSafe for DatabasePool
impl !UnwindSafe for DatabasePool
impl Freeze for DatabasePool
impl Send for DatabasePool
impl Sync for DatabasePool
impl Unpin for DatabasePool
impl UnsafeUnpin for DatabasePool
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more