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 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(
m.create_2col_idx(
CartGiftCard::CartGiftCards,
CartGiftCard::CartId,
CartGiftCard::GiftCardId,
))
.primary()
.if_not_exists()
.to_owned(),
)
.await?;

View File

@ -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(),
)

View File

@ -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,28 +63,52 @@ 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),
)
m.create_constraint(LineItems, Check::greater(Quantity, 0.iden()))
.await?;
m.create_constraint(
LineItem::LineItems,
Check::less_eq(LineItem::FulfilledQuantity, LineItem::Quantity),
)
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(())
}
}

View File

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

View File

@ -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;