use anyhow::Result;
use async_trait::async_trait;
use sqlx::PgPool;
use super::Seeder;
pub struct SeedBankTransactionSeeder;
impl SeedBankTransactionSeeder {
pub fn new() -> Self {
Self
}
}
impl Default for SeedBankTransactionSeeder {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl Seeder for SeedBankTransactionSeeder {
fn name(&self) -> &'static str {
"SeedBankTransactionSeeder"
}
fn order(&self) -> i32 {
5
}
async fn should_run(&self, pool: &PgPool) -> Result<bool> {
let count: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM banking.bank_transactions")
.fetch_one(pool)
.await?;
Ok(count.0 == 0)
}
async fn run(&self, pool: &PgPool) -> Result<()> {
let sql = include_str!("../../migrations/seeds/bank_transaction_seed.sql");
sqlx::raw_sql(sql).execute(pool).await?;
Ok(())
}
async fn rollback(&self, pool: &PgPool) -> Result<()> {
sqlx::query("DELETE FROM banking.bank_transactions")
.execute(pool)
.await?;
Ok(())
}
}