Skip to content

Commit

Permalink
rerun seaORM CLI for generating entities
Browse files Browse the repository at this point in the history
  • Loading branch information
calebbourg committed Feb 23, 2024
1 parent 42acde4 commit bf56dba
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 129 deletions.
2 changes: 1 addition & 1 deletion docs/db/refactor_platform_rs.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Table refactor_platform.users {
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
password varchar [not null]
github_username varchar // Specifically GH for now, can generalize later
github_profile_url varchar
created_at timestamptz [default: `now()`]
Expand Down
53 changes: 0 additions & 53 deletions entity/src/coaching_relationship.rs

This file was deleted.

58 changes: 58 additions & 0 deletions entity/src/coaching_relationships.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.3
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(
schema_name = "refactor_platform",
table_name = "coaching_relationships"
)]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
pub id: i32,
#[sea_orm(unique)]
pub external_id: Uuid,
pub organization_id: i32,
pub coach_id: i32,
pub coachee_id: i32,
pub created_at: Option<DateTimeWithTimeZone>,
pub updated_at: Option<DateTimeWithTimeZone>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::organizations::Entity",
from = "Column::OrganizationId",
to = "super::organizations::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
)]
Organizations,
#[sea_orm(
belongs_to = "super::users::Entity",
from = "Column::CoachId",
to = "super::users::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
)]
Users2,
#[sea_orm(
belongs_to = "super::users::Entity",
from = "Column::CoacheeId",
to = "super::users::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
)]
Users1,
}

impl Related<super::organizations::Entity> for Entity {
fn to() -> RelationDef {
Relation::Organizations.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
13 changes: 10 additions & 3 deletions entity/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
pub mod coaching_relationship;
pub mod organization;
pub mod user;
pub mod prelude;

pub mod actions;
pub mod agreements;
pub mod coaching_relationships;
pub mod coaching_sessions;
pub mod notes;
pub mod organizations;
pub mod overarching_goals;
pub mod users;

/// A type alias that represents any Entity's id field data type
pub type Id = i32;
10 changes: 0 additions & 10 deletions entity/src/mod.rs

This file was deleted.

30 changes: 0 additions & 30 deletions entity/src/organization.rs

This file was deleted.

32 changes: 32 additions & 0 deletions entity/src/organizations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.3
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(schema_name = "refactor_platform", table_name = "organizations")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
pub id: i32,
#[sea_orm(unique)]
pub external_id: Uuid,
pub name: Option<String>,
pub logo: Option<String>,
pub created_at: Option<DateTimeWithTimeZone>,
pub updated_at: Option<DateTimeWithTimeZone>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::coaching_relationships::Entity")]
CoachingRelationships,
}

impl Related<super::coaching_relationships::Entity> for Entity {
fn to() -> RelationDef {
Relation::CoachingRelationships.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
9 changes: 3 additions & 6 deletions entity/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.3
pub use super::actions::Entity as Actions;
pub use super::agreements::Entity as Agreements;
pub use super::coaching_sessions::Entity as CoachingSessions;
pub use super::notes::Entity as Notes;
pub use super::overarching_goals::Entity as OverarchingGoals;
pub use super::seaql_migrations::Entity as SeaqlMigrations;
pub use super::coaching_relationships::Entity as CoachingRelationships;
pub use super::organizations::Entity as Organizations;
pub use super::users::Entity as Users;
21 changes: 11 additions & 10 deletions entity/src/user.rs → entity/src/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@
use crate::Id;
use axum_login::AuthUser;
use chrono::{DateTime, Utc};
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Deserialize, Serialize)]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[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)]
#[sea_orm(unique)]
pub external_id: Uuid,
#[sea_orm(unique)]
pub email: String,
pub first_name: String,
pub last_name: String,
pub display_name: String,
pub first_name: Option<String>,
pub last_name: Option<String>,
pub display_name: Option<String>,
pub password: String,
pub github_username: String,
pub github_profile_url: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub github_username: Option<String>,
pub github_profile_url: Option<String>,
pub created_at: Option<DateTimeWithTimeZone>,
pub updated_at: Option<DateTimeWithTimeZone>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand Down
14 changes: 9 additions & 5 deletions entity_api/src/organization.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::error::{EntityApiErrorCode, Error};
use entity::{organization, Id};
use organization::{ActiveModel, Entity, Model};
use entity::{organizations, Id};
use organizations::{ActiveModel, Entity, Model};
use sea_orm::{
entity::prelude::*, ActiveValue, ActiveValue::Set, ActiveValue::Unchanged, DatabaseConnection,
TryIntoModel,
Expand Down Expand Up @@ -34,7 +34,11 @@ pub async fn update(db: &DatabaseConnection, id: Id, model: Model) -> Result<Mod

let active_model: ActiveModel = ActiveModel {
id: Unchanged(organization.id),
external_id: Set(Uuid::new_v4()),
logo: Set(model.logo),
name: Set(model.name),
updated_at: Set(OffsetDateTime::now_utc()),
created_at: Unchanged(organization.created_at),
};
Ok(active_model.update(db).await?.try_into_model()?)
}
Expand Down Expand Up @@ -84,16 +88,16 @@ pub(crate) async fn seed_database(db: &DatabaseConnection) {
];

for name in organization_names {
let organization = organization::ActiveModel::from_json(json!({
let organization = organizations::ActiveModel::from_json(json!({
"name": name,
}))
.unwrap();

assert_eq!(
organization,
organization::ActiveModel {
organizations::ActiveModel {
id: ActiveValue::NotSet,
name: ActiveValue::Set(name.to_owned()),
name: ActiveValue::Set(Some(name.to_owned())),
}
);

Expand Down
20 changes: 11 additions & 9 deletions entity_api/src/user.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use async_trait::async_trait;
use axum_login::{AuthnBackend, UserId};
use entity::user::{self, Column, Model};
use entity::users::*;
use log::*;
use password_auth::{generate_hash, verify_password};
use sea_orm::{entity::prelude::*, sea_query, ActiveValue, DatabaseConnection};
Expand Down Expand Up @@ -57,7 +57,7 @@ impl AuthnBackend for Backend {
async fn get_user(&self, user_id: &UserId<Self>) -> Result<Option<Self::User>, Self::Error> {
debug!("** get_user(): {:?}", *user_id);

let user: Option<Self::User> = entity::user::Entity::find_by_id(*user_id)
let user: Option<Self::User> = entity::users::Entity::find_by_id(*user_id)
.one(self.db.as_ref())
.await?;

Expand All @@ -71,9 +71,10 @@ pub type AuthSession = axum_login::AuthSession<Backend>;

pub(crate) async fn seed_database(db: &DatabaseConnection) {
let users = vec![
user::ActiveModel {
ActiveModel {
id: ActiveValue::NotSet,
email: ActiveValue::Set("[email protected]".to_owned()),
external_id: ActiveValue::Set(uuid::Uuid::new_v4()),
first_name: ActiveValue::Set("Jim".to_owned()),
last_name: ActiveValue::Set("Hodapp".to_owned()),
display_name: ActiveValue::Set("Jim H".to_owned()),
Expand All @@ -83,9 +84,10 @@ pub(crate) async fn seed_database(db: &DatabaseConnection) {
created_at: ActiveValue::NotSet,
updated_at: ActiveValue::NotSet,
},
user::ActiveModel {
ActiveModel {
id: ActiveValue::NotSet,
email: ActiveValue::Set("[email protected]".to_owned()),
external_id: ActiveValue::Set(uuid::Uuid::new_v4()),
first_name: ActiveValue::Set("Test First".to_owned()),
last_name: ActiveValue::Set("Test Last".to_owned()),
display_name: ActiveValue::Set("Test User".to_owned()),
Expand All @@ -101,13 +103,13 @@ pub(crate) async fn seed_database(db: &DatabaseConnection) {
debug!("user: {:?}", user);

// Upserts seeded user data:
match user::Entity::insert(user)
match users::Entity::insert(user)
.on_conflict(
// on conflict do update
sea_query::OnConflict::column(user::Column::Email)
.update_column(user::Column::FirstName)
.update_column(user::Column::LastName)
.update_column(user::Column::Password)
sea_query::OnConflict::column(Column::Email)
.update_column(Column::FirstName)
.update_column(Column::LastName)
.update_column(Column::Password)
.to_owned(),
)
.exec(db)
Expand Down
4 changes: 2 additions & 2 deletions migration/src/refactor_platform_rs.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- SQL dump generated using DBML (dbml-lang.org)
-- Database: PostgreSQL
-- Generated at: 2024-02-18T13:46:48.698Z
-- Generated at: 2024-02-23T18:01:40.740Z


CREATE TABLE "refactor_platform"."organizations" (
Expand Down Expand Up @@ -29,7 +29,7 @@ CREATE TABLE "refactor_platform"."users" (
"first_name" varchar,
"last_name" varchar,
"display_name" varchar,
"password" varchar,
"password" varchar NOT NULL,
"github_username" varchar,
"github_profile_url" varchar,
"created_at" timestamptz DEFAULT (now()),
Expand Down

0 comments on commit bf56dba

Please sign in to comment.