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