Skip to content

Commit

Permalink
Added ability to seed via rust code
Browse files Browse the repository at this point in the history
  • Loading branch information
DenuxPlays committed Dec 7, 2024
1 parent 35cb4ed commit 7c80db3
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
//! This module defines functions and operations related to the application's
//! database interactions.
use std::{collections::HashMap, fs::File, io::Write, path::Path, sync::OnceLock, time::Duration};

use chrono::{DateTime, Utc};
use duct::cmd;
use fs_err::{self as fs, create_dir_all};
Expand All @@ -14,6 +12,8 @@ use sea_orm::{
DatabaseConnection, DbConn, EntityTrait, IntoActiveModel, Statement,
};
use sea_orm_migration::MigratorTrait;
use std::future::Future;
use std::{collections::HashMap, fs::File, io::Write, path::Path, sync::OnceLock, time::Duration};
use tracing::info;

use super::Result as AppResult;
Expand All @@ -35,6 +35,16 @@ fn get_extract_db_name() -> &'static Regex {
EXTRACT_DB_NAME.get_or_init(|| Regex::new(r"/([^/]+)$").unwrap())
}

/// A trait for seeding the database with initial data.
/// Seeders should be kept in `src/fixtures`.
pub trait Seeder {
/// The unique name of the seeder.
fn name(&self) -> String;

/// Seeds the database with initial data.
fn seed(&self, db: &DatabaseConnection) -> impl Future<Output = AppResult<()>>;
}

#[derive(Default, Clone, Debug)]
pub struct MultiDb {
pub db: HashMap<String, DatabaseConnection>,
Expand Down Expand Up @@ -277,6 +287,15 @@ where
Ok(())
}

/// Seed the database with the given Seeder.
///
/// # Errors
///
/// Returns an error if the seeder fails.
pub async fn seed_via_seeder(db: &DatabaseConnection, seeder: &impl Seeder) -> AppResult<()> {
seeder.seed(db).await
}

/// Function to reset auto-increment
/// # Errors
/// Returns error if it fails
Expand Down

0 comments on commit 7c80db3

Please sign in to comment.