Skip to content

Commit

Permalink
move seeds into separate executable
Browse files Browse the repository at this point in the history
Before this change seeds were run every time we ran cargo run. This was causing
cargo run to fail if seeding had already occured. The new workflow will be to run
scripts/rebuild_db.sh && cargo run --bin seed_db && cargo run
  • Loading branch information
calebbourg committed Mar 28, 2024
1 parent 870959f commit 05c3ce9
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 143 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ clap = { version = "4.4.6", features = ["cargo", "derive", "env"] }
log = "0.4"
simplelog = { version = "0.12", features = ["paris"] }
tokio = "1.33.0"

[[bin]]
name = "seed_db"
path = "src/bin/seed_db.rs"
239 changes: 100 additions & 139 deletions entity_api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use chrono::Utc;
use sea_orm::{prelude::Uuid, ActiveModelTrait, DatabaseConnection, TryIntoModel};
use serde_json::json;
use password_auth::generate_hash;
use sea_orm::{prelude::Uuid, ActiveModelTrait, DatabaseConnection, Set};

use entity::coaching_relationships;
use entity::organizations;
use entity::users;
use log::*;

pub mod coaching_relationship;
pub mod error;
Expand All @@ -15,140 +14,102 @@ pub mod user;
pub async fn seed_database(db: &DatabaseConnection) {
let now = Utc::now();

info!("Seeding database with initial data");
info!("Creating Users");

let jim_hodapp_params = json!({
"email": "[email protected]",
"first_name": "Jim",
"last_name": "Hodapp",
"display_name": "Jim H",
"password": "password",
"github_username": "jhodapp",
"github_profile_url": "https://github.com/jhodapp",
"external_id": Uuid::new_v4(),
"created_at": now,
"updated_at": now,
});

let caleb_bourg_params = json!({
"email": "[email protected]",
"first_name": "Caleb",
"last_name": "Bourg",
"display_name": "Caleb B",
"password": "password",
"github_username": "calebbourg",
"github_profile_url": "https://github.com/calebbourg",
"external_id": Uuid::new_v4(),
"created_at": now,
"updated_at": now,
});

let other_user_params = json!({
"email": "[email protected]",
"first_name": "Other",
"last_name": "User",
"display_name": "Other U",
"password": "password",
"external_id": Uuid::new_v4(),
"created_at": now,
"updated_at": now,
});

let jim_hodapp_model = users::ActiveModel::from_json(jim_hodapp_params)
.unwrap()
.try_into_model()
.unwrap();
let caleb_bourg_model = users::ActiveModel::from_json(caleb_bourg_params)
.unwrap()
.try_into_model()
.unwrap();
let other_user_model = users::ActiveModel::from_json(other_user_params)
.unwrap()
.try_into_model()
.unwrap();

let jim_hodapp = user::create(db, jim_hodapp_model).await.unwrap();
let caleb_bourg = user::create(db, caleb_bourg_model).await.unwrap();
let other_user = user::create(db, other_user_model).await.unwrap();

info!("Creating Organizations");

let jim_hodapp_coaching_params = json!({
"name": "Jim Hodapp's Coaching",
"external_id": Uuid::new_v4(),
"created_at": now,
"updated_at": now,
});

let jim_hodapp_other_org_params = json!({
"name": "Jim Hodapp's Other Organization",
"external_id": Uuid::new_v4(),
"created_at": now,
"updated_at": now,
});

let jim_hodapp_coaching_model =
organizations::ActiveModel::from_json(jim_hodapp_coaching_params)
.unwrap()
.try_into_model()
.unwrap();
let jim_hodapp_other_org_model =
organizations::ActiveModel::from_json(jim_hodapp_other_org_params)
.unwrap()
.try_into_model()
.unwrap();

let jim_hodapp_coaching = organization::create(db, jim_hodapp_coaching_model)
.await
.unwrap();
let jim_hodapp_other_org = organization::create(db, jim_hodapp_other_org_model)
.await
.unwrap();

info!("Creating Coaching Relationships");

let jim_hodapp_coaching_coaching_relationship_params = json!({
"coach_id": jim_hodapp.id,
"coachee_id": caleb_bourg.id,
"organization_id": jim_hodapp_coaching.id,
"external_id": Uuid::new_v4(),
"created_at": now,
"updated_at": now,
});

let jim_hodapp_other_org_coaching_relationship_params = json!({
"coach_id": jim_hodapp.id,
"coachee_id": other_user.id,
"organization_id": jim_hodapp_other_org.id,
"external_id": Uuid::new_v4(),
"created_at": now,
"updated_at": now,
});

let jim_hodapp_coaching_coaching_relationship_model =
coaching_relationships::ActiveModel::from_json(
jim_hodapp_coaching_coaching_relationship_params,
)
.unwrap()
.try_into_model()
.unwrap();
let jim_hodapp_other_org_coaching_relationship_model =
coaching_relationships::ActiveModel::from_json(
jim_hodapp_other_org_coaching_relationship_params,
)
.unwrap()
.try_into_model()
.unwrap();

coaching_relationship::create(db, jim_hodapp_coaching_coaching_relationship_model)
.await
.unwrap()
.try_into_model()
.unwrap();
coaching_relationship::create(db, jim_hodapp_other_org_coaching_relationship_model)
.await
.unwrap()
.try_into_model()
.unwrap();
let jim_hodapp: users::ActiveModel = users::ActiveModel {
external_id: Set(Uuid::new_v4()),
email: Set("[email protected]".to_owned()),
first_name: Set(Some("Jim".to_owned())),
last_name: Set(Some("Hodapp".to_owned())),
display_name: Set(Some("Jim H".to_owned())),
password: Set(generate_hash("password")),
github_username: Set(Some("jhodapp".to_owned())),
github_profile_url: Set(Some("https://github.com/jhodapp".to_owned())),
created_at: Set(now.into()),
updated_at: Set(now.into()),
..Default::default()
}
.save(db)
.await
.unwrap();

let caleb_bourg: users::ActiveModel = users::ActiveModel {
external_id: Set(Uuid::new_v4()),
email: Set("[email protected]".to_owned()),
first_name: Set(Some("Caleb".to_owned())),
last_name: Set(Some("Bourg".to_owned())),
display_name: Set(Some("cbourg2".to_owned())),
password: Set(generate_hash("password")),
github_username: Set(Some("calebbourg".to_owned())),
github_profile_url: Set(Some("https://github.com/calebbourg".to_owned())),
created_at: Set(now.into()),
updated_at: Set(now.into()),
..Default::default()
}
.save(db)
.await
.unwrap();

let other_user: users::ActiveModel = users::ActiveModel {
external_id: Set(Uuid::new_v4()),
email: Set("[email protected]".to_owned()),
first_name: Set(Some("Other".to_owned())),
last_name: Set(Some("User".to_owned())),
display_name: Set(Some("Other U.".to_owned())),
password: Set(generate_hash("password")),
github_username: Set(None),
github_profile_url: Set(None),
created_at: Set(now.into()),
updated_at: Set(now.into()),
..Default::default()
}
.save(db)
.await
.unwrap();

let jim_hodapp_coaching = organizations::ActiveModel {
external_id: Set(Uuid::new_v4()),
name: Set("Jim Hodapp's Coaching".to_owned()),
created_at: Set(now.into()),
updated_at: Set(now.into()),
..Default::default()
}
.save(db)
.await
.unwrap();

let jim_hodapp_other_org = organizations::ActiveModel {
external_id: Set(Uuid::new_v4()),
name: Set("Jim Hodapp's Other Organization".to_owned()),
created_at: Set(now.into()),
updated_at: Set(now.into()),
..Default::default()
}
.save(db)
.await
.unwrap();

coaching_relationships::ActiveModel {
coach_id: Set(jim_hodapp.id.clone().unwrap()),
coachee_id: Set(caleb_bourg.id.clone().unwrap()),
organization_id: Set(jim_hodapp_coaching.id.unwrap()),
external_id: Set(Uuid::new_v4()),
created_at: Set(now.into()),
updated_at: Set(now.into()),
..Default::default()
}
.save(db)
.await
.unwrap();

coaching_relationships::ActiveModel {
coach_id: Set(jim_hodapp.id.clone().unwrap()),
coachee_id: Set(other_user.id.clone().unwrap()),
organization_id: Set(jim_hodapp_other_org.id.unwrap()),
external_id: Set(Uuid::new_v4()),
created_at: Set(now.into()),
updated_at: Set(now.into()),
..Default::default()
}
.save(db)
.await
.unwrap();
}
17 changes: 17 additions & 0 deletions src/bin/seed_db.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use log::info;
use service::{config::Config, logging::Logger, AppState};
use std::sync::Arc;

#[tokio::main]
async fn main() {
let config = Config::new();
Logger::init_logger(&config as &Config);

info!("Seeding Database...");

let db = Arc::new(service::init_database(config.database_uri()).await.unwrap());

let app_state = AppState::new(config, &db);

entity_api::seed_database(app_state.db_conn_ref()).await;
}
2 changes: 0 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ async fn main() {

let app_state = AppState::new(config, &db);

entity_api::seed_database(app_state.db_conn_ref()).await;

web::init_server(app_state).await.unwrap();
}

Expand Down
3 changes: 1 addition & 2 deletions web/src/controller/organization_controller.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::extractors::{
compare_api_version::CompareApiVersion,
authenticated_user::AuthenticatedUser,
authenticated_user::AuthenticatedUser, compare_api_version::CompareApiVersion,
};
use crate::{AppState, Error};
use axum::extract::{Path, Query, State};
Expand Down

0 comments on commit 05c3ce9

Please sign in to comment.