Skip to content

Commit

Permalink
Make the rebuild.sh script more OS-agnostic. Also migrate the codebas…
Browse files Browse the repository at this point in the history
…e to work with UUID and timestamps with timezone support..
  • Loading branch information
jhodapp committed Feb 18, 2024
1 parent b9a41dd commit e95bfa7
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 96 deletions.
10 changes: 7 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 7 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,31 @@ The platform itself is useful for professional independent coaches, informal men

## Running the Database Setup Script

1. Ensure you have PostgreSQL installed and running on your machine. If you're using macOS, you can install it with Homebrew:
1. Ensure you have PostgreSQL installed and running on your machine. If you're using macOS, you can use
[Postgres.app](https://postgresapp.com/) or install it with Homebrew:

```shell
brew install postgresql
brew services start postgresql
```

2. Make sure you have the `dbml2sql` tool installed. You can install it with npm:

```shell
npm install -g dbml2sql
npm install -g @dbml/cli
```

3. Make the script executable:
3. Run the script with default settings:

```shell
chmod +x rebuild_db.sh
```

4. Run the script with default settings:

```shell
./rebuild_db.sh
./scripts/rebuild_db.sh
```

This will create a database named `refactor_platform`, a user named `refactor`, and a schema named `refactor_platform`.

5. If you want to use different settings, you can provide them as arguments to the script:
4. If you want to use different settings, you can provide them as arguments to the script:

```shell
./rebuild_db.sh my_database my_user my_schema
./scripts/rebuild_db.sh my_database my_user my_schema
```

This will create a database named `my_database`, a user named `my_user`, and a schema named `my_schema`.
Expand Down
57 changes: 28 additions & 29 deletions docs/db/refactor_platform_rs.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +2,85 @@
// Docs: https://dbml.dbdiagram.io/docs

Table refactor_platform.organizations {
id uuid [primary key, unique, not null]
id uuid [primary key, unique, not null, default: `gen_random_uuid()`]
name varchar [note: 'The name of the organization that the coach <--> coachee belong to']
logo varchar [note: 'A URI pointing to the organization\'s logo icon file']
created_at timestamp
updated_at timestamp [note: 'The last date and time fields were changed']
created_at timestamptz [default: `now()`]
updated_at timestamptz [default: `now()`, note: 'The last date and time fields were changed']
}

// Coaching relationship type belonging to the refactor_platform schema
// from the perspective of the coach
Table refactor_platform.coaching_relationships {
id uuid [primary key, unique, not null]
id uuid [primary key, unique, not null, default: `gen_random_uuid()`]
organization_id uuid [not null, note: 'The organization associated with this coaching relationship']
coach_id uuid [not null, note: 'The coach associated with this coaching relationship']
coachee_id uuid [not null, note: 'The coachee associated with this coaching relationship']
created_at timestamp
updated_at timestamp [note: 'The last date and time fields were changed']
created_at timestamptz [default: `now()`]
updated_at timestamptz [default: `now()`, note: 'The last date and time fields were changed']
}

Table refactor_platform.users {
id uuid [primary key, unique, not null]
email varchar
id uuid [primary key, unique, not null, default: `gen_random_uuid()`]
email varchar [unique, not null]
first_name varchar
last_name varchar
display_name varchar [note: 'If a user wants to go by something other than first & last names']
password varchar
timezone varchar
github_username varchar // Specifically GH for now, can generalize later
github_profile_url varchar
created_at timestamp
updated_at timestamp [note: 'The last date and time fields were changed']
created_at timestamptz [default: `now()`]
updated_at timestamptz [default: `now()`, note: 'The last date and time fields were changed']
}

Table refactor_platform.coaching_sessions {
id uuid [primary key, unique, not null]
id uuid [primary key, unique, not null, default: `gen_random_uuid()`]
coaching_relationship_id uuid [not null, note: 'The coaching relationship (i.e. what coach & coachee under what organization) associated with this coaching session']
date timestamp [note: 'The date and time of a session']
timezone varchar [note: 'The baseline timezone used for the `date` field']
created_at timestamp
updated_at timestamp [note: 'The last date and time fields were changed']
created_at timestamptz [default: `now()`]
updated_at timestamptz [default: `now()`, note: 'The last date and time fields were changed']
}

Table refactor_platform.overarching_goals {
id uuid [primary key, unique, not null]
id uuid [primary key, unique, not null, default: `gen_random_uuid()`]
coaching_session_id uuid [note: 'The coaching session that an overarching goal is associated with']
title varchar [note: 'A short description of an overarching goal']
details varchar [note: 'A long description of an overarching goal']
completed_at timestamp [note: 'The date and time an overarching goal was completed']
created_at timestamp
updated_at timestamp [note: 'The last date and time fields were changed']
completed_at timestamptz [note: 'The date and time an overarching goal was completed']
created_at timestamptz [default: `now()`]
updated_at timestamptz [default: `now()`, note: 'The last date and time fields were changed']
}

Table refactor_platform.notes {
id uuid [primary key, unique, not null]
id uuid [primary key, unique, not null, default: `gen_random_uuid()`]
coaching_session_id uuid [not null]
body varchar [note: 'Main text of the note supporting Markdown']
user_id uuid [not null, note: 'User that created (owns) the note']
created_at timestamp
updated_at timestamp [note: 'The last date and time an overarching note\'s fields were changed']
created_at timestamptz [default: `now()`]
updated_at timestamptz [default: `now()`, note: 'The last date and time an overarching note\'s fields were changed']
}

Table refactor_platform.agreements {
id uuid [primary key, unique, not null]
id uuid [primary key, unique, not null, default: `gen_random_uuid()`]
coaching_session_id uuid [not null]
details varchar [note: 'Either a short or long description of an agreement reached between coach and coachee in a coaching session']
user_id uuid [not null, note: 'User that created (owns) the agreement']
created_at timestamp
updated_at timestamp [note: 'The last date and time an overarching agreement\'s fields were changed']
created_at timestamptz [default: `now()`]
updated_at timestamptz [default: `now()`, note: 'The last date and time an overarching agreement\'s fields were changed']
}

Table refactor_platform.actions {
id uuid [primary key, unique, not null]
id uuid [primary key, unique, not null, default: `gen_random_uuid()`]
// The first session where this action was created
// It will carry forward to every future session until
// its due_by is passed or it was completed by the coachee
coaching_session_id uuid [not null]
due_by timestamp
due_by timestamptz
completed boolean // May be unnecessary if there's a valid completed_at timestamp
completed_at timestamp
created_at timestamp
updated_at timestamp
completed_at timestamptz
created_at timestamp [default: `now()`]
updated_at timestamp [default: `now()`]
}

// coaching_relationships relationships
Expand Down
4 changes: 3 additions & 1 deletion entity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ path = "src/lib.rs"

[dependencies]
axum-login = "0.12.0"
chrono = { version = "0.4.34", features = ["serde"] }
serde = { version = "1", features = ["derive"] }

uuid = "1.7.0"

[dependencies.sea-orm]
version = "0.12"
features = [ "with-uuid" ]
5 changes: 3 additions & 2 deletions entity/src/coaching_relationship.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.3
use crate::Id;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Deserialize, Serialize)]
#[sea_orm(
schema_name = "refactor_platform_rs",
schema_name = "refactor_platform",
table_name = "coaching_relationships"
)]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
pub id: i32,
pub id: Id,
pub coachee_id: String,
pub coach_id: String,
pub organization_id: String,
Expand Down
4 changes: 3 additions & 1 deletion entity/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use uuid::Uuid;

pub mod coaching_relationship;
pub mod organization;
pub mod user;

/// A type alias that represents any Entity's id field data type
pub type Id = i32; // TODO: consider changing this to a u64
pub type Id = Uuid;
7 changes: 3 additions & 4 deletions entity/src/organization.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.3
use crate::Id;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Default, PartialEq, DeriveEntityModel, Eq, Deserialize, Serialize)]
#[sea_orm(schema_name = "refactor_platform_rs", table_name = "organizations")]
#[sea_orm(schema_name = "refactor_platform", table_name = "organizations")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
// TODO: consider changing this to a u64
pub id: i32,
pub id: Id,
pub name: String,
}

Expand Down
12 changes: 9 additions & 3 deletions entity/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@
use crate::Id;
use axum_login::AuthUser;
use chrono::{DateTime, Utc};
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)]
#[sea_orm(schema_name = "refactor_platform_rs", table_name = "users")]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Deserialize, Serialize)]
#[sea_orm(schema_name = "refactor_platform", table_name = "users")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
pub id: Id,
#[sea_orm(unique, indexed)]
pub email: String,
pub password: String,
pub first_name: String,
pub last_name: String,
pub display_name: String,
pub password: String,
pub github_username: String,
pub github_profile_url: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand Down
18 changes: 16 additions & 2 deletions entity_api/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,32 @@ pub(crate) async fn seed_database(db: &DatabaseConnection) {
email: ActiveValue::Set("[email protected]".to_owned()),
first_name: ActiveValue::Set("Jim".to_owned()),
last_name: ActiveValue::Set("Hodapp".to_owned()),
display_name: ActiveValue::Set("Jim H".to_owned()),
password: ActiveValue::Set(generate_hash("password1").to_owned()),
github_username: ActiveValue::Set("jhodapp".to_owned()),
github_profile_url: ActiveValue::Set("https://github.com/jhodapp".to_owned()),
created_at: ActiveValue::NotSet,
updated_at: ActiveValue::NotSet,
},
user::ActiveModel {
id: ActiveValue::NotSet,
email: ActiveValue::Set("[email protected]".to_owned()),
first_name: ActiveValue::Set("Test First".to_owned()),
last_name: ActiveValue::Set("Test Last".to_owned()),
display_name: ActiveValue::Set("Test User".to_owned()),
password: ActiveValue::Set(generate_hash("password2").to_owned()),
github_username: ActiveValue::Set("test".to_owned()),
github_profile_url: ActiveValue::Set("https://github.com/test".to_owned()),
created_at: ActiveValue::NotSet,
updated_at: ActiveValue::NotSet,
},
];

for user in users {
debug!("user: {:?}", user);

// Upserts seeded user data:
let _res = user::Entity::insert(user)
match user::Entity::insert(user)
.on_conflict(
// on conflict do update
sea_query::OnConflict::column(user::Column::Email)
Expand All @@ -101,6 +111,10 @@ pub(crate) async fn seed_database(db: &DatabaseConnection) {
.to_owned(),
)
.exec(db)
.await;
.await
{
Ok(_) => info!("Succeeded in seeding user data."),
Err(e) => error!("Failed to insert or update user when seeding user data: {e}"),
};
}
}
4 changes: 2 additions & 2 deletions migration/src/m20240211_174355_base_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ impl MigrationTrait for Migration {
Ok(())
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> {
Ok(())
}
}

// format sql as valid sql statements
fn process_sql(sql: &str) -> String {
sql.replace(";", ";\n")
sql.replace(';', ";\n")
.lines()
.filter(|line| !line.trim().starts_with("--"))
.collect::<Vec<_>>()
Expand Down
Loading

0 comments on commit e95bfa7

Please sign in to comment.