Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Design and implement the Bakery Chain schema #34

Merged
merged 38 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
255aeb7
ERD of the Bakery domain to begin with
samsamai Jun 24, 2021
b8f5854
Bakery chain domain model
samsamai Jun 25, 2021
fce0b04
Use more datatypes in the model
samsamai Jun 25, 2021
318665d
Start creating the table for Bakery and Baker objects
samsamai Jun 26, 2021
bd2e7a9
Baker belongs to a bakery
samsamai Jun 26, 2021
1de4d0c
Create Customer and Order
samsamai Jun 26, 2021
c765571
Create Lineitem
samsamai Jun 27, 2021
0011f5e
Create Cake and CakesBakers
samsamai Jun 27, 2021
59322d3
Merge branch 'master' into ss/bakery
samsamai Jun 28, 2021
8f66968
Use find_related in place of find_xxx
samsamai Jun 29, 2021
ba65d4f
Update customer has_many relation
samsamai Jun 29, 2021
88bc1e1
Merge branch 'master' into ss/bakery
samsamai Jun 29, 2021
2b0424b
Update ERD jpg
samsamai Jun 29, 2021
d103671
Agnostic backend for creating table
samsamai Jun 29, 2021
c4fc1b1
Add the rest of the relations
samsamai Jun 29, 2021
6104770
Pull table creation into its own module
samsamai Jun 29, 2021
391fb08
Separate schema tests
samsamai Jun 30, 2021
c820bb6
Fix relationship between Cake and lineitems, add Cake schema creation…
samsamai Jun 30, 2021
f60a1c3
Test bakery create
samsamai Jul 3, 2021
86bfcba
Cake has many lineitems
samsamai Jul 3, 2021
76d2cbb
Use f64 for money
samsamai Jul 3, 2021
335bdc5
Merge branch 'master' into ss/bakery
samsamai Jul 3, 2021
f61ed5a
Use DbErr instead of ExecErr
samsamai Jul 3, 2021
c5dec7e
Lineitem belongs to cake
samsamai Jul 3, 2021
23e0b3c
Fix create table for the new relationship between Cake and Lineitem
samsamai Jul 4, 2021
47e7914
Remove LineitemId from cake
samsamai Jul 4, 2021
c0177bf
Fix create order table foreign keys
samsamai Jul 4, 2021
a40785c
Test baker and bakery create
samsamai Jul 5, 2021
cf356ec
Test create customer
samsamai Jul 5, 2021
0eeb37b
Test create cake
samsamai Jul 5, 2021
527bc3d
Merge branch 'master' into ss/bakery
samsamai Jul 6, 2021
f05de48
Use Decimal for lineitem price
samsamai Jul 7, 2021
dbe950e
Merge branch 'master' into ss/bakery
tyt2y3 Jul 7, 2021
40dd5e7
Fix build error from rust_decimal
tyt2y3 Jul 7, 2021
3b67ff8
Support Decimal on sqlite
tyt2y3 Jul 7, 2021
1ecc848
Test create lineitem
samsamai Jul 8, 2021
ad4c1c8
Test create order
samsamai Jul 8, 2021
550d9a7
Cake price is decimal now
samsamai Jul 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions tests/bakery_chain/baker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use sea_orm::entity::prelude::*;

#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
pub struct Entity;

impl EntityName for Entity {
fn table_name(&self) -> &str {
"baker"
}
}

#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model {
pub id: i32,
pub name: String,
pub bakery_id: Option<i32>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
Id,
Name,
BakeryId,
}

#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
pub enum PrimaryKey {
Id,
}

impl PrimaryKeyTrait for PrimaryKey {
fn auto_increment() -> bool {
true
}
}

#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {
Bakery,
}

impl ColumnTrait for Column {
type EntityName = Entity;

fn def(&self) -> ColumnDef {
match self {
Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(None).def(),
Self::BakeryId => ColumnType::Integer.def(),
}
}
}

impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
match self {
Self::Bakery => Entity::belongs_to(super::bakery::Entity)
.from(Column::BakeryId)
.to(super::bakery::Column::Id)
.into(),
}
}
}

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

impl Related<super::cake::Entity> for Entity {
fn to() -> RelationDef {
super::cakes_bakers::Relation::Cake.def()
}

fn via() -> Option<RelationDef> {
Some(super::cakes_bakers::Relation::Baker.def().rev())
}
}

impl ActiveModelBehavior for ActiveModel {}
84 changes: 84 additions & 0 deletions tests/bakery_chain/bakery.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use sea_orm::entity::prelude::*;

#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
pub struct Entity;

impl EntityName for Entity {
fn table_name(&self) -> &str {
"bakery"
}
}

#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model {
pub id: i32,
pub name: String,
pub profit_margin: f64,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
Id,
Name,
ProfitMargin,
}

#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
pub enum PrimaryKey {
Id,
}

impl PrimaryKeyTrait for PrimaryKey {
fn auto_increment() -> bool {
true
}
}

#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {
Baker,
Order,
Cake,
}

impl ColumnTrait for Column {
type EntityName = Entity;

fn def(&self) -> ColumnDef {
match self {
Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(None).def(),
Self::ProfitMargin => ColumnType::Float.def(),
}
}
}

impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
match self {
Self::Baker => Entity::has_many(super::baker::Entity).into(),
Self::Order => Entity::has_many(super::order::Entity).into(),
Self::Cake => Entity::has_many(super::cake::Entity).into(),
}
}
}

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

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

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

impl ActiveModelBehavior for ActiveModel {}
1 change: 1 addition & 0 deletions tests/bakery_chain/bakery_chain_erd.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<mxfile host="Electron" modified="2021-06-30T11:15:47.471Z" agent="5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/14.6.13 Chrome/89.0.4389.128 Electron/12.0.7 Safari/537.36" etag="CL5AmeQl6dz-rdIIukNg" version="14.6.13" type="device"><diagram id="R2lEEEUBdFMjLlhIrx00" name="Page-1">7Zxtc5s4EIB/jT82w5sBf4yd5Nq75K6TdKbtp44Csq0zIB/Itd1ff6sgjI1EYoe3uxnNZDLWWgihZ1fsLmtG9ize/Zai9fKBhjgaWUa4G9k3I8uaOC7854J9LjB9w84li5SEQlYKnsgvLISGkG5IiLOTjozSiJH1qTCgSYIDdiJDaUq3p93mNDo96xotsCR4ClAkS7+SkC1zqT82SvlHTBbL4symIb6JUdFZCLIlCun2RIR37I4mTEzxM05jlOCEwTcPKF3hdDS+XTLGr/R6ZN3B35z3vlpQuogwWpPsKqAxiIMMutzNUUwivs5HA03FQHA6+3Zkz1JKWf4p3s1wxFkVGPI53dV8e1iHlI97xgGr6S77vnKC36eJP/m6/LBa7v/+IEb5iaKNWN/p9R+3j99BNvt4/elPsVJsXyw/gyUa2dMliyMQmPAxYyld4RmNaAqShCbQczonUVQRoYgsEmgGMF9YAHv6E6eMANhr8UVMwpCfZrpdEoaf1ijg59yCGoMspZskxPxSDD48LLtQTdsq2mKSxSXB6HhXu1bmgQBYCqYxZukeuogDLEcohLCSDxNXCLalztmekC2P9K3QQyR0aHEYu0QDHwSdS0iNZVQINGkvIcq2JI5QjuFoXfi6BUsShfdoTzd8uhlDwapoTZc0Jb+gPyrBorRYZcs96fHEjxRj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ouuKnnYK11f8TQnQgEOfyCW2/ANYvgLieXn8Rr0BaBVecReQbuy43xfmrGOlhpHS9Jv/WxVsYUysve6ipZcuXRZR0sXWLVbk2B8JVpSAe7O+5LvzYGqdEpDPR+qKlbqFaorJ0p1gUWLgFUxU7+A5ZTlPxuUMML2em9ui7IycOp3c5ZvvjTPTMPMbLN8/qAJv4+wKnjq15Dl5MesLqulHerGDrXy8YPSoe7sx2GunMfUDvUlJl3z8PM/41DLfHX1couAh3eu5YhJv2WhMdbBXWpf8aNOynD+PjXjC0ep+b6f7/DOtCv/rEQi2mb1EN4R9k0Q45+PiuCgVdbA8UYHJXDFNvVmNZFT8/aE3qqJ/IqTZk/eW0tUHcmpjtRxJVHxLoK+Vcy8QMUO1ZmHRu/Vmc65qunVuPdaNS9XzYF2v/+ZahavWnpbNb2BVbP63hPn3RWY1ZHGPddf+nKWq1vVfKeaDXxDL97u9vYNfeDyYKu613nvrWiXVbP6DtuW6tlNKVF0+sbiburZJ5cq/kuC//GFDk0aWECLWumdq5VezY9netswq+9Ntqu6dLZWVkdyztTKt/UGmuVbt/Pu5avS7dt/AQ==</diagram></mxfile>
Binary file added tests/bakery_chain/bakery_chain_erd.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
98 changes: 98 additions & 0 deletions tests/bakery_chain/cake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use sea_orm::entity::prelude::*;

#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
pub struct Entity;

impl EntityName for Entity {
fn table_name(&self) -> &str {
"cake"
}
}

#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model {
pub id: i32,
pub name: String,
pub price: f32,
tyt2y3 marked this conversation as resolved.
Show resolved Hide resolved
pub bakery_id: Option<i32>,
pub lineitem_id: Option<i32>,
tyt2y3 marked this conversation as resolved.
Show resolved Hide resolved
pub gluten_free: bool,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
Id,
Name,
Price,
BakeryId,
LineitemId,
GlutenFree,
}

#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
pub enum PrimaryKey {
Id,
}

impl PrimaryKeyTrait for PrimaryKey {
fn auto_increment() -> bool {
true
}
}

#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {
Bakery,
Lineitem,
}

impl ColumnTrait for Column {
type EntityName = Entity;

fn def(&self) -> ColumnDef {
match self {
Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(None).def(),
Self::Price => ColumnType::Money(Some((19, 4))).def(),
Self::BakeryId => ColumnType::Integer.def(),
Self::LineitemId => ColumnType::Integer.def(),
Self::GlutenFree => ColumnType::Boolean.def(),
}
}
}

impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
match self {
Self::Bakery => Entity::belongs_to(super::bakery::Entity)
.from(Column::BakeryId)
.to(super::bakery::Column::Id)
.into(),
Self::Lineitem => Entity::has_many(super::lineitem::Entity).into(),
}
}
}

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

impl Related<super::baker::Entity> for Entity {
fn to() -> RelationDef {
super::cakes_bakers::Relation::Baker.def()
}

fn via() -> Option<RelationDef> {
Some(super::cakes_bakers::Relation::Cake.def().rev())
}
}

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

impl ActiveModelBehavior for ActiveModel {}
68 changes: 68 additions & 0 deletions tests/bakery_chain/cakes_bakers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use sea_orm::entity::prelude::*;

#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
pub struct Entity;

impl EntityName for Entity {
fn table_name(&self) -> &str {
"cakes_bakers"
}
}

#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model {
pub cake_id: i32,
pub baker_id: i32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
CakeId,
BakerId,
}

#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
pub enum PrimaryKey {
CakeId,
BakerId,
}

impl PrimaryKeyTrait for PrimaryKey {
fn auto_increment() -> bool {
false
}
}

#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {
Cake,
Baker,
}

impl ColumnTrait for Column {
type EntityName = Entity;

fn def(&self) -> ColumnDef {
match self {
Self::CakeId => ColumnType::Integer.def(),
Self::BakerId => ColumnType::Integer.def(),
}
}
}

impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
match self {
Self::Cake => Entity::belongs_to(super::cake::Entity)
.from(Column::CakeId)
.to(super::cake::Column::Id)
.into(),
Self::Baker => Entity::belongs_to(super::baker::Entity)
.from(Column::BakerId)
.to(super::baker::Column::Id)
.into(),
}
}
}

impl ActiveModelBehavior for ActiveModel {}
Loading