Skip to main content

Module database

Module database 

Source
Expand description

Database connection and migration utilities.

This module provides abstractions for working with both PostgreSQL and MySQL databases in the Hammerwork CLI. It handles connection pooling, automatic database type detection, and migration management.

§Examples

§Connecting to a Database

use cargo_hammerwork::utils::database::DatabasePool;

// Connect to PostgreSQL
let pg_pool = DatabasePool::connect(
    "postgresql://localhost/hammerwork",
    5  // connection pool size
).await?;

// Connect to MySQL
let mysql_pool = DatabasePool::connect(
    "mysql://localhost/hammerwork",
    5
).await?;

§Running Migrations

use cargo_hammerwork::utils::database::DatabasePool;

let pool = DatabasePool::connect("postgresql://localhost/hammerwork", 5).await?;

// Run all pending migrations
pool.migrate(false).await?;

// Drop existing tables and run migrations from scratch
pool.migrate(true).await?;

§Creating a Job Queue

use cargo_hammerwork::utils::database::DatabasePool;

let pool = DatabasePool::connect("postgresql://localhost/hammerwork", 5).await?;
let job_queue = pool.create_job_queue();

// Now you can use the job queue for operations
// The wrapper automatically handles PostgreSQL vs MySQL differences

Enums§

DatabasePool
Database connection pool abstraction.
JobQueueWrapper
Wrapper for database-specific JobQueue implementations.