From 4ca694b8a24678e31c8f6dbd97157ecf7bc4c641 Mon Sep 17 00:00:00 2001 From: eraden Date: Sun, 11 Jun 2023 21:20:25 +0200 Subject: [PATCH] Add additional tables. Cleanup --- .../carts/m20230603_120815_cart_discounts.rs | 19 +--- .../checkouts/m20230603_120814_checkouts.rs | 6 +- .../src/checkouts/m20230603_120815_items.rs | 97 +++++++++++++++---- migration/src/sea_ext/mod.rs | 18 ++-- .../src/shipping/m20230603_120810_shipping.rs | 6 +- 5 files changed, 99 insertions(+), 47 deletions(-) diff --git a/migration/src/carts/m20230603_120815_cart_discounts.rs b/migration/src/carts/m20230603_120815_cart_discounts.rs index ddfefc1..d091d46 100644 --- a/migration/src/carts/m20230603_120815_cart_discounts.rs +++ b/migration/src/carts/m20230603_120815_cart_discounts.rs @@ -1,6 +1,6 @@ use sea_orm_migration::prelude::*; -use crate::{to_pk2_name, CreateIndexExt, DropTable, IntoColumnDef}; +use crate::{CreateIndexExt, DropTable, IntoColumnDef}; /// ```sql /// CREATE TABLE cart_discounts @@ -79,19 +79,10 @@ impl Migration { ) .await?; - m.create_index( - IndexCreateStatement::new() - .table(CartGiftCard::CartGiftCards) - .col(CartGiftCard::CartId) - .col(CartGiftCard::GiftCardId) - .name(&to_pk2_name( - CartGiftCard::CartGiftCards, - CartGiftCard::CartId, - CartGiftCard::GiftCardId, - )) - .primary() - .if_not_exists() - .to_owned(), + m.create_2col_idx( + CartGiftCard::CartGiftCards, + CartGiftCard::CartId, + CartGiftCard::GiftCardId, ) .await?; diff --git a/migration/src/checkouts/m20230603_120814_checkouts.rs b/migration/src/checkouts/m20230603_120814_checkouts.rs index dcb4a06..69424c6 100644 --- a/migration/src/checkouts/m20230603_120814_checkouts.rs +++ b/migration/src/checkouts/m20230603_120814_checkouts.rs @@ -493,12 +493,14 @@ impl Migration { Table::create() .table(PaymentCollectionSession::PaymentCollectionSessions) .col( - ColumnDef::new(PaymentCollectionSession::PaymentCollectionId) + PaymentCollectionSession::PaymentCollectionId + .col() .uuid() .not_null(), ) .col( - ColumnDef::new(PaymentCollectionSession::PaymentSessionId) + PaymentCollectionSession::PaymentSessionId + .col() .uuid() .not_null(), ) diff --git a/migration/src/checkouts/m20230603_120815_items.rs b/migration/src/checkouts/m20230603_120815_items.rs index 4426696..1ad0871 100644 --- a/migration/src/checkouts/m20230603_120815_items.rs +++ b/migration/src/checkouts/m20230603_120815_items.rs @@ -2,8 +2,7 @@ use sea_orm_migration::prelude::*; use crate::constraint::Check; use crate::{ - auto_uuid_not_null, create_type, drop_type, to_fk_name, to_pk2_name, ts_def_now_not_null, - AsIden, CreateConstraint, + auto_uuid_not_null, ts_def_now_not_null, AsIden, CreateConstraint, DropTable, IntoColumnDef, }; #[derive(DeriveMigrationName)] @@ -12,10 +11,19 @@ pub struct Migration; #[async_trait::async_trait] impl MigrationTrait for Migration { async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> { + self.create_line_items(m).await?; + self.create_line_item_adjustments(m).await?; + self.create_line_item_tax_lines(m).await?; Ok(()) } async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> { + self.drop_table(m, LineItem::LineItems).await?; + self.drop_table(m, LineItemAdjustment::LineItemAdjustments) + .await?; + self.drop_table(m, LineItemTaxLine::LineItemTaxLines) + .await?; + Ok(()) } } @@ -55,29 +63,53 @@ impl Migration { /// ); /// ``` async fn create_line_items(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { - m.create_constraint( - LineItem::LineItems, - Check::less_eq(LineItem::ShippedQuantity, LineItem::FulfilledQuantity), + use LineItem::*; + + m.create_table( + Table::create() + .table(LineItems) + .col(auto_uuid_not_null!(LineItem::Id)) + .col(CartId.col().uuid()) + .col(OrderId.col().uuid()) + .col(SwapId.col().uuid()) + .col(Title.col().string().not_null()) + .col(Description.col().string().not_null()) + .col(Thumbnail.col().string().not_null()) + .col(IsGiftcard.col().boolean().default(false).not_null()) + .col(ShouldMerge.col().boolean().default(true).not_null()) + .col(AllowDiscounts.col().boolean().default(true).not_null()) + .col(HasShipping.col().boolean()) + .col(UnitPrice.col().integer().not_null()) + .col(VariantId.col().uuid()) + .col(Quantity.col().integer().not_null()) + .col(FulfilledQuantity.col().integer()) + .col(ReturnedQuantity.col().integer()) + .col(ShippedQuantity.col().integer()) + .col(ts_def_now_not_null!(LineItem::CreatedAt)) + .col(ts_def_now_not_null!(LineItem::UpdatedAt)) + .col(Metadata.col().json_binary()) + .col(ClaimOrderId.col().uuid()) + .col(IsReturn.col().boolean().default(false).not_null()) + .col(OriginalItemId.col().uuid()) + .col(OrderEditId.col().uuid()) + .to_owned(), ) .await?; m.create_constraint( - LineItem::LineItems, - Check::greater(LineItem::Quantity, 0.iden()), + LineItems, + Check::less_eq(ShippedQuantity, FulfilledQuantity), ) .await?; - m.create_constraint( - LineItem::LineItems, - Check::less_eq(LineItem::ReturnedQuantity, LineItem::Quantity), - ) - .await?; + m.create_constraint(LineItems, Check::greater(Quantity, 0.iden())) + .await?; - m.create_constraint( - LineItem::LineItems, - Check::less_eq(LineItem::FulfilledQuantity, LineItem::Quantity), - ) - .await?; + m.create_constraint(LineItems, Check::less_eq(ReturnedQuantity, Quantity)) + .await?; + + m.create_constraint(LineItems, Check::less_eq(FulfilledQuantity, Quantity)) + .await?; Ok(()) } @@ -94,6 +126,20 @@ impl Migration { /// ); /// ``` async fn create_line_item_adjustments(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { + use LineItemAdjustment::*; + + m.create_table( + Table::create() + .table(LineItemAdjustments) + .col(auto_uuid_not_null!(Id)) + .col(ItemId.col().uuid().not_null()) + .col(Description.col().string().not_null()) + .col(DiscountId.col().uuid()) + .col(Amount.col().integer().not_null()) + .col(Metadata.col().json_binary()) + .to_owned(), + ) + .await?; Ok(()) } @@ -111,6 +157,23 @@ impl Migration { /// ); /// ``` async fn create_line_item_tax_lines(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { + use LineItemTaxLine::*; + + m.create_table( + Table::create() + .table(LineItemTaxLines) + .col(auto_uuid_not_null!(Id)) + .col(Rate.col().double().not_null()) + .col(Name.col().string().not_null()) + .col(Code.col().string()) + .col(ts_def_now_not_null!(CreatedAt)) + .col(ts_def_now_not_null!(UpdatedAt)) + .col(Metadata.col().json_binary()) + .col(ItemId.col().uuid().not_null()) + .to_owned(), + ) + .await?; + Ok(()) } } diff --git a/migration/src/sea_ext/mod.rs b/migration/src/sea_ext/mod.rs index 66a5f8a..16639aa 100644 --- a/migration/src/sea_ext/mod.rs +++ b/migration/src/sea_ext/mod.rs @@ -251,20 +251,20 @@ impl Display for Constraint { } } -#[async_trait(?Send)] +#[async_trait] pub trait CreateConstraint { - async fn create_constraint>( - &self, - table: T, - c: C, - ) -> Result<(), DbErr>; + async fn create_constraint(&self, table: T, c: C) -> Result<(), DbErr> + where + T: Iden + Send, + C: Into + Send; } -#[async_trait(?Send)] +#[async_trait] impl CreateConstraint for SchemaManager<'_> { - async fn create_constraint(&self, table: T, c: C) -> Result<(), DbErr> + async fn create_constraint(&self, table: T, c: C) -> Result<(), DbErr> where - C: Into, + T: Iden + Send, + C: Into + Send, { let c = c.into(); self.get_connection() diff --git a/migration/src/shipping/m20230603_120810_shipping.rs b/migration/src/shipping/m20230603_120810_shipping.rs index adeb3c2..b67d38a 100644 --- a/migration/src/shipping/m20230603_120810_shipping.rs +++ b/migration/src/shipping/m20230603_120810_shipping.rs @@ -1,11 +1,7 @@ use sea_orm_migration::prelude::*; use crate::constraint::Check; -use crate::sea_orm::Iterable; -use crate::{ - auto_uuid_not_null, create_type, drop_type, ts_def_now_not_null, AsIden, CreateConstraint, - DropTable, IntoColumnDef, -}; +use crate::{auto_uuid_not_null, ts_def_now_not_null, AsIden, CreateConstraint, IntoColumnDef}; #[derive(DeriveMigrationName)] pub struct Migration;