Add additional tables

This commit is contained in:
Adrian Woźniak 2023-06-08 17:18:38 +02:00
parent c0e8d9da34
commit 592f157225
5 changed files with 160 additions and 6 deletions

View File

@ -1,7 +1,9 @@
use sea_orm_migration::prelude::*;
use crate::sea_orm::Iterable;
use crate::{auto_uuid_not_null, create_type, drop_type, ts_def_now_not_null, DropTable};
use crate::{
auto_uuid_not_null, create_type, drop_type, ts_def_now_not_null, DropTable, IntoColumnDef,
};
#[derive(DeriveMigrationName)]
pub struct Migration;
@ -24,9 +26,25 @@ impl MigrationTrait for Migration {
}
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
self.drop_table(m, PaymentCollectionSession::PaymentCollectionSessions)
.await?;
self.drop_table(m, PaymentCollectionPayment::PaymentCollectionPayments)
.await?;
self.drop_table(m, OrderItemChange::OrderItemChanges)
.await?;
self.drop_table(m, PaymentProvider::PaymentProviders)
.await?;
self.drop_table(m, OrderGiftCard::OrderGiftCards).await?;
self.drop_table(m, OrderEdit::OrderEdits).await?;
self.drop_table(m, OrderDiscount::OrderDiscounts).await?;
self.drop_table(m, Order::Orders).await?;
drop_type!(m, crate::types::OrderStatus);
drop_type!(m, crate::types::OrderFulfillmentStatus);
drop_type!(m, crate::types::OrderPaymentStatus);
drop_type!(m, crate::types::OrderItemChangeType);
drop_type!(m, crate::types::PaymentCollectionType);
drop_type!(m, crate::types::OrderFulfillmentStatus);
Ok(())
}
}
@ -34,8 +52,10 @@ impl MigrationTrait for Migration {
impl Migration {
async fn create_types(m: &SchemaManager<'_>) -> Result<(), DbErr> {
create_type!(m, crate::types::OrderStatus);
create_type!(m, crate::types::OrderFulfillmentStatus);
create_type!(m, crate::types::OrderPaymentStatus);
create_type!(m, crate::types::OrderItemChangeType);
create_type!(m, crate::types::PaymentCollectionType);
create_type!(m, crate::types::OrderFulfillmentStatus);
Ok(())
}
/// ```sql
@ -127,6 +147,14 @@ impl Migration {
/// discount_id uuid NOT NULL
/// }
async fn create_order_discounts(m: &SchemaManager<'_>) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(OrderDiscount::OrderDiscounts)
.col(ColumnDef::new(OrderDiscount::OrderId).uuid().not_null())
.col(ColumnDef::new(OrderDiscount::DiscountId).uuid().not_null())
.to_owned(),
)
.await?;
Ok(())
}
@ -152,6 +180,28 @@ impl Migration {
/// }
/// ```
async fn create_order_edits(m: &SchemaManager<'_>) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(OrderEdit::OrderEdits)
.col(auto_uuid_not_null!(OrderEdit::Id))
.col(ts_def_now_not_null!(OrderEdit::CreatedAt))
.col(ts_def_now_not_null!(OrderEdit::UpdatedAt))
.col(ColumnDef::new(OrderEdit::OrderId).uuid().not_null())
.col(ColumnDef::new(OrderEdit::InternalNote).string())
.col(ColumnDef::new(OrderEdit::CreatedBy).uuid().not_null())
.col(ColumnDef::new(OrderEdit::RequestedBy).uuid())
.col(ColumnDef::new(OrderEdit::RequestedAt).timestamp())
.col(ColumnDef::new(OrderEdit::ConfirmedBy).uuid())
.col(ColumnDef::new(OrderEdit::ConfirmedAt).timestamp())
.col(ColumnDef::new(OrderEdit::DeclinedBy).uuid())
.col(ColumnDef::new(OrderEdit::DeclinedReason).string())
.col(ColumnDef::new(OrderEdit::DeclinedAt).timestamp())
.col(ColumnDef::new(OrderEdit::CanceledBy).uuid())
.col(ColumnDef::new(OrderEdit::CanceledAt).timestamp())
.col(ColumnDef::new(OrderEdit::PaymentCollectionId).uuid())
.to_owned(),
)
.await?;
Ok(())
}
@ -163,6 +213,14 @@ impl Migration {
/// }
/// ```
async fn create_order_gift_cards(m: &SchemaManager<'_>) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(OrderGiftCard::OrderGiftCards)
.col(ColumnDef::new(OrderGiftCard::OrderId).uuid().not_null())
.col(ColumnDef::new(OrderGiftCard::GiftCardId).uuid().not_null())
.to_owned(),
)
.await?;
Ok(())
}
@ -180,6 +238,31 @@ impl Migration {
/// }
/// ```
async fn create_order_item_changes(m: &SchemaManager<'_>) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(OrderItemChange::OrderItemChanges)
.col(auto_uuid_not_null!(OrderItemChange::Id))
.col(ts_def_now_not_null!(OrderItemChange::CreatedAt))
.col(ts_def_now_not_null!(OrderItemChange::UpdatedAt))
.col(ColumnDef::new(OrderItemChange::DeletedAt).timestamp())
.col(
ColumnDef::new(OrderItemChange::OrderItemChangeType)
.enumeration(
crate::types::OrderItemChangeType::OrderItemChangeTypes,
crate::types::OrderItemChangeType::iter().skip(1),
)
.not_null(),
)
.col(
ColumnDef::new(OrderItemChange::OrderEditId)
.uuid()
.not_null(),
)
.col(ColumnDef::new(OrderItemChange::OriginalLineItemId).uuid())
.col(ColumnDef::new(OrderItemChange::LineItemId).uuid())
.to_owned(),
)
.await?;
Ok(())
}
@ -226,6 +309,22 @@ impl Migration {
/// }
/// ```
async fn create_payment_collections(m: &SchemaManager<'_>) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(PaymentCollection::PaymentCollections)
.col(auto_uuid_not_null!(PaymentCollection::Id))
.col(ts_def_now_not_null!(PaymentCollection::CreatedAt))
.col(ts_def_now_not_null!(PaymentCollection::UpdatedAt))
.col(PaymentCollection::DeletedAt.col().timestamp())
.col(PaymentCollection::Type.col().enumeration(crate::types::PaymentCollectionType::PaymentCollectionTypes, crate::types::PaymentCollectionType::iter().skip(1)))
////////
//////// TODO
//////// TODO
//////// TODO
////////
.to_owned(),
)
.await?;
Ok(())
}
@ -237,6 +336,22 @@ impl Migration {
/// }
/// ```
async fn create_payment_collection_payments(m: &SchemaManager<'_>) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(PaymentCollectionPayment::PaymentCollectionPayments)
.col(
ColumnDef::new(PaymentCollectionPayment::PaymentCollectionId)
.uuid()
.not_null(),
)
.col(
ColumnDef::new(PaymentCollectionPayment::PaymentId)
.uuid()
.not_null(),
)
.to_owned(),
)
.await?;
Ok(())
}
@ -248,6 +363,22 @@ impl Migration {
/// }
/// ````
async fn create_payment_collection_sessions(m: &SchemaManager<'_>) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(PaymentCollectionSession::PaymentCollectionSessions)
.col(
ColumnDef::new(PaymentCollectionSession::PaymentCollectionId)
.uuid()
.not_null(),
)
.col(
ColumnDef::new(PaymentCollectionSession::PaymentSessionId)
.uuid()
.not_null(),
)
.to_owned(),
)
.await?;
Ok(())
}
/// ```sql
@ -258,6 +389,20 @@ impl Migration {
/// }
/// ```
async fn create_payment_providers(m: &SchemaManager<'_>) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(PaymentProvider::PaymentProviders)
.col(auto_uuid_not_null!(PaymentProvider::Id))
.col(
PaymentProvider::IsInstalled
.col()
.boolean()
.default(true)
.not_null(),
)
.to_owned(),
)
.await?;
Ok(())
}
@ -352,7 +497,7 @@ pub enum OrderItemChange {
CreatedAt,
UpdatedAt,
DeletedAt,
Type,
OrderItemChangeType,
OrderEditId,
OriginalLineItemId,
LineItemId,

View File

@ -63,3 +63,11 @@ pub trait DropTable {
}
impl<T: MigrationTrait> DropTable for T {}
pub trait IntoColumnDef: IntoIden {
fn col(self) -> ColumnDef {
ColumnDef::new(self)
}
}
impl<T: IntoIden> IntoColumnDef for T {}

View File

@ -37,6 +37,7 @@ async fn main() {
migrate_schema!(cli, Jobs, JobsMigrator);
migrate_schema!(cli, Carts, CartsMigrator);
migrate_schema!(cli, Discounts, DiscountsMigrator);
migrate_schema!(cli, Checkouts, CheckoutsMigrator);
}
pub async fn run_cli<M>(cli: &mut Cli, schema: PostgreSQLSchema, migrator: M)

View File

@ -10,7 +10,6 @@ pub struct Migration;
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
create_type!(manager, PaymentCollectionStatus);
create_type!(manager, PaymentCollectionType);
create_type!(manager, CartType);
create_type!(manager, ClaimItemReason);
create_type!(manager, ClaimOrderFulfillmentStatus);
@ -38,7 +37,6 @@ impl MigrationTrait for Migration {
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
drop_type!(manager, PaymentCollectionStatus);
drop_type!(manager, PaymentCollectionType);
drop_type!(manager, CartType);
drop_type!(manager, ClaimItemReason);
drop_type!(manager, ClaimOrderFulfillmentStatus);

View File

@ -8,6 +8,7 @@ pub enum PostgreSQLSchema {
Jobs,
Carts,
Discounts,
Checkouts,
}
impl PostgreSQLSchema {
@ -17,6 +18,7 @@ impl PostgreSQLSchema {
PostgreSQLSchema::Jobs => "jobs",
PostgreSQLSchema::Carts => "carts",
PostgreSQLSchema::Discounts => "discounts",
PostgreSQLSchema::Checkouts => "checkouts",
}
}
}