-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
870959f
commit 05c3ce9
Showing
5 changed files
with
122 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters