Add additional tables. Cleanup

This commit is contained in:
eraden 2023-06-11 21:20:25 +02:00
parent da2c1b9b19
commit 4ca694b8a2
5 changed files with 99 additions and 47 deletions

View File

@ -1,6 +1,6 @@
use sea_orm_migration::prelude::*; use sea_orm_migration::prelude::*;
use crate::{to_pk2_name, CreateIndexExt, DropTable, IntoColumnDef}; use crate::{CreateIndexExt, DropTable, IntoColumnDef};
/// ```sql /// ```sql
/// CREATE TABLE cart_discounts /// CREATE TABLE cart_discounts
@ -79,19 +79,10 @@ impl Migration {
) )
.await?; .await?;
m.create_index( m.create_2col_idx(
IndexCreateStatement::new() CartGiftCard::CartGiftCards,
.table(CartGiftCard::CartGiftCards) CartGiftCard::CartId,
.col(CartGiftCard::CartId) CartGiftCard::GiftCardId,
.col(CartGiftCard::GiftCardId)
.name(&to_pk2_name(
CartGiftCard::CartGiftCards,
CartGiftCard::CartId,
CartGiftCard::GiftCardId,
))
.primary()
.if_not_exists()
.to_owned(),
) )
.await?; .await?;

View File

@ -493,12 +493,14 @@ impl Migration {
Table::create() Table::create()
.table(PaymentCollectionSession::PaymentCollectionSessions) .table(PaymentCollectionSession::PaymentCollectionSessions)
.col( .col(
ColumnDef::new(PaymentCollectionSession::PaymentCollectionId) PaymentCollectionSession::PaymentCollectionId
.col()
.uuid() .uuid()
.not_null(), .not_null(),
) )
.col( .col(
ColumnDef::new(PaymentCollectionSession::PaymentSessionId) PaymentCollectionSession::PaymentSessionId
.col()
.uuid() .uuid()
.not_null(), .not_null(),
) )

View File

@ -2,8 +2,7 @@ use sea_orm_migration::prelude::*;
use crate::constraint::Check; use crate::constraint::Check;
use crate::{ use crate::{
auto_uuid_not_null, create_type, drop_type, to_fk_name, to_pk2_name, ts_def_now_not_null, auto_uuid_not_null, ts_def_now_not_null, AsIden, CreateConstraint, DropTable, IntoColumnDef,
AsIden, CreateConstraint,
}; };
#[derive(DeriveMigrationName)] #[derive(DeriveMigrationName)]
@ -12,10 +11,19 @@ pub struct Migration;
#[async_trait::async_trait] #[async_trait::async_trait]
impl MigrationTrait for Migration { impl MigrationTrait for Migration {
async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> { 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(()) Ok(())
} }
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> { 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(()) Ok(())
} }
} }
@ -55,29 +63,53 @@ impl Migration {
/// ); /// );
/// ``` /// ```
async fn create_line_items(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { async fn create_line_items(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> {
m.create_constraint( use LineItem::*;
LineItem::LineItems,
Check::less_eq(LineItem::ShippedQuantity, LineItem::FulfilledQuantity), 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?; .await?;
m.create_constraint( m.create_constraint(
LineItem::LineItems, LineItems,
Check::greater(LineItem::Quantity, 0.iden()), Check::less_eq(ShippedQuantity, FulfilledQuantity),
) )
.await?; .await?;
m.create_constraint( m.create_constraint(LineItems, Check::greater(Quantity, 0.iden()))
LineItem::LineItems, .await?;
Check::less_eq(LineItem::ReturnedQuantity, LineItem::Quantity),
)
.await?;
m.create_constraint( m.create_constraint(LineItems, Check::less_eq(ReturnedQuantity, Quantity))
LineItem::LineItems, .await?;
Check::less_eq(LineItem::FulfilledQuantity, LineItem::Quantity),
) m.create_constraint(LineItems, Check::less_eq(FulfilledQuantity, Quantity))
.await?; .await?;
Ok(()) Ok(())
} }
@ -94,6 +126,20 @@ impl Migration {
/// ); /// );
/// ``` /// ```
async fn create_line_item_adjustments(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { 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(()) Ok(())
} }
@ -111,6 +157,23 @@ impl Migration {
/// ); /// );
/// ``` /// ```
async fn create_line_item_tax_lines(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { 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(()) Ok(())
} }
} }

View File

@ -251,20 +251,20 @@ impl Display for Constraint {
} }
} }
#[async_trait(?Send)] #[async_trait]
pub trait CreateConstraint { pub trait CreateConstraint {
async fn create_constraint<T: Iden, C: Into<Constraint>>( async fn create_constraint<T, C>(&self, table: T, c: C) -> Result<(), DbErr>
&self, where
table: T, T: Iden + Send,
c: C, C: Into<Constraint> + Send;
) -> Result<(), DbErr>;
} }
#[async_trait(?Send)] #[async_trait]
impl CreateConstraint for SchemaManager<'_> { impl CreateConstraint for SchemaManager<'_> {
async fn create_constraint<T: Iden, C>(&self, table: T, c: C) -> Result<(), DbErr> async fn create_constraint<T, C>(&self, table: T, c: C) -> Result<(), DbErr>
where where
C: Into<Constraint>, T: Iden + Send,
C: Into<Constraint> + Send,
{ {
let c = c.into(); let c = c.into();
self.get_connection() self.get_connection()

View File

@ -1,11 +1,7 @@
use sea_orm_migration::prelude::*; use sea_orm_migration::prelude::*;
use crate::constraint::Check; use crate::constraint::Check;
use crate::sea_orm::Iterable; use crate::{auto_uuid_not_null, ts_def_now_not_null, AsIden, CreateConstraint, IntoColumnDef};
use crate::{
auto_uuid_not_null, create_type, drop_type, ts_def_now_not_null, AsIden, CreateConstraint,
DropTable, IntoColumnDef,
};
#[derive(DeriveMigrationName)] #[derive(DeriveMigrationName)]
pub struct Migration; pub struct Migration;