Migrate claims

This commit is contained in:
Adrian Woźniak 2023-06-07 17:09:54 +02:00
parent 9a3d61aaf6
commit bfb164ae36
2 changed files with 213 additions and 102 deletions

View File

@ -15,3 +15,14 @@ macro_rules! ts_def_now_not_null {
.not_null()
};
}
#[macro_export]
macro_rules! auto_uuid_not_null {
($identifier: expr) => {
ColumnDef::new($identifier)
.uuid()
.not_null()
.default(SimpleExpr::Custom("uuid_generate_v4()".into()))
.primary_key()
};
}

View File

@ -1,10 +1,11 @@
use sea_orm_migration::manager;
use sea_orm_migration::prelude::*;
use sea_orm_migration::sea_orm::Statement;
use sea_orm_migration::sea_orm::Iterable;
use sea_query::expr::SimpleExpr;
use crate::sea_orm::DatabaseBackend;
use crate::ts_def_now_not_null;
use crate::types::{
ClaimItemReason, ClaimOrderFulfillmentStatus, ClaimOrderPaymentStatus, ClaimOrderType,
};
use crate::{auto_uuid_not_null, ts_def_now_not_null};
/// ```sql
/// CREATE TABLE claim_images
@ -73,105 +74,12 @@ pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(ClaimImage::ClaimImages)
.col(
ColumnDef::new(ClaimImage::Id)
.uuid()
.not_null()
.default(SimpleExpr::Custom("uuid_generate_v4()".into()))
.primary_key(),
)
.col(ColumnDef::new(ClaimImage::ClaimItemId).uuid().not_null())
.col(ColumnDef::new(ClaimImage::Url).string().not_null())
.col(ColumnDef::new(ClaimImage::CreatedAt).string())
.col(ColumnDef::new(ClaimImage::UpdatedAt).string())
.col(ColumnDef::new(ClaimImage::DeletedAt).string())
.col(ColumnDef::new(ClaimImage::Metadata).string())
.to_owned(),
)
.await?;
Self::create_claim_images(manager).await?;
Self::create_claim_items(manager).await?;
Self::create_claim_item_tags(manager).await?;
Self::create_claim_orders(manager).await?;
Self::create_claim_tags(manager).await?;
manager
.create_table(
Table::create()
.table(ClaimItem::ClaimItems)
.col(
ColumnDef::new(ClaimItem::Id)
.uuid()
.not_null()
.default(SimpleExpr::Custom("uuid_generate_v4()".into()))
.primary_key(),
)
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(ClaimItemTag::ClaimItemTags)
.col(ColumnDef::new(ClaimItemTag::ItemId).uuid().not_null())
.col(ColumnDef::new(ClaimItemTag::TagId).uuid().not_null())
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(ClaimOrder::ClaimOrders)
.col(
ColumnDef::new(ClaimOrder::Id)
.uuid()
.not_null()
.default(SimpleExpr::Custom("uuid_generate_v4()".into()))
.primary_key(),
)
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(ClaimTag::ClaimTags)
.col(ColumnDef::new(ClaimTag::Id).uuid())
.to_owned(),
)
.await?;
////
// manager
// .create_table(
// Table::create()
// .table(AnalyticsConfig::AnalyticsConfigs)
// .col(
// ColumnDef::new(AnalyticsConfig::Id)
// .uuid()
// .not_null()
// .default(SimpleExpr::Custom("uuid_generate_v4()".into()))
// .primary_key(),
// )
// .col(ts_def_now_not_null!(AnalyticsConfig::CreatedAt))
// .col(ts_def_now_not_null!(AnalyticsConfig::UpdatedAt))
// .col(ColumnDef::new(AnalyticsConfig::DeletedAt).timestamp())
// .col(ColumnDef::new(AnalyticsConfig::UserId).uuid().not_null())
// .col(
// ColumnDef::new(AnalyticsConfig::OptOut)
// .boolean()
// .default(false)
// .not_null(),
// )
// .col(
// ColumnDef::new(AnalyticsConfig::Anonymize)
// .boolean()
// .default(false)
// .not_null(),
// )
// .to_owned(),
// )
// .await?;
Ok(())
}
@ -195,6 +103,193 @@ impl MigrationTrait for Migration {
}
}
impl Migration {
/// ```sql
/// CREATE TABLE claim_item_tags
/// (
/// item_id uuid NOT NULL,
/// tag_id uuid NOT NULL
/// );
/// ```
async fn create_claim_item_tags(manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(ClaimItemTag::ClaimItemTags)
.col(ColumnDef::new(ClaimItemTag::ItemId).uuid().not_null())
.col(ColumnDef::new(ClaimItemTag::TagId).uuid().not_null())
.to_owned(),
)
.await
}
/// ```sql
/// CREATE TABLE claim_items
/// (
/// id uuid NOT NULL,
/// claim_order_id uuid NOT NULL,
/// item_id uuid NOT NULL,
/// variant_id uuid NOT NULL,
/// reason claim_item_reasons NOT NULL,
/// note character varying,
/// quantity integer NOT NULL,
/// created_at timestamp with time zone DEFAULT now() NOT NULL,
/// updated_at timestamp with time zone DEFAULT now() NOT NULL,
/// deleted_at timestamp with time zone,
/// metadata jsonb
/// );
/// ```
async fn create_claim_items(manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(ClaimItem::ClaimItems)
.col(auto_uuid_not_null!(ClaimItem::Id))
.col(ColumnDef::new(ClaimItem::ClaimOrderId).uuid().not_null())
.col(ColumnDef::new(ClaimItem::ItemId).uuid().not_null())
.col(ColumnDef::new(ClaimItem::VariantId).uuid().not_null())
.col(
ColumnDef::new(ClaimItem::Reason)
.enumeration(
ClaimItemReason::ClaimItemReasons,
ClaimItemReason::iter().skip(1),
)
.not_null(),
)
.col(ColumnDef::new(ClaimItem::Note).string())
.col(ColumnDef::new(ClaimItem::Quantity).integer().not_null())
.col(ts_def_now_not_null!(ClaimItem::CreatedAt))
.col(ts_def_now_not_null!(ClaimItem::UpdatedAt))
.col(ColumnDef::new(ClaimItem::DeletedAt).timestamp())
.col(ColumnDef::new(ClaimItem::Metadata).json_binary())
.to_owned(),
)
.await
}
/// ```sql
/// CREATE TABLE claim_images
/// (
/// id uuid NOT NULL,
/// claim_item_id uuid NOT NULL,
/// url character varying NOT NULL,
/// created_at timestamp with time zone DEFAULT now() NOT NULL,
/// updated_at timestamp with time zone DEFAULT now() NOT NULL,
/// deleted_at timestamp with time zone,
/// metadata jsonb
/// );
/// ```
async fn create_claim_images(manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(ClaimImage::ClaimImages)
.col(auto_uuid_not_null!(ClaimImage::Id))
.col(ColumnDef::new(ClaimImage::ClaimItemId).uuid().not_null())
.col(ColumnDef::new(ClaimImage::Url).string().not_null())
.col(ts_def_now_not_null!(ClaimImage::CreatedAt))
.col(ts_def_now_not_null!(ClaimImage::UpdatedAt))
.col(ColumnDef::new(ClaimImage::DeletedAt).string())
.col(ColumnDef::new(ClaimImage::Metadata).json_binary())
.to_owned(),
)
.await
}
/// ```sql
/// CREATE TABLE claim_orders
/// (
/// id uuid NOT NULL,
/// payment_status public.claim_order_payment_statuses DEFAULT 'na'::public.claim_order_payment_statuses NOT NULL,
/// fulfillment_status public.claim_order_fulfillment_statuses DEFAULT 'not_fulfilled'::public.claim_order_fulfillment_statuses NOT NULL,
/// claim_order_type public.claim_order_types NOT NULL,
/// order_id uuid NOT NULL,
/// shipping_address_id character varying,
/// refund_amount integer,
/// canceled_at timestamp with time zone,
/// created_at timestamp with time zone DEFAULT now() NOT NULL,
/// updated_at timestamp with time zone DEFAULT now() NOT NULL,
/// deleted_at timestamp with time zone,
/// metadata jsonb,
/// idempotency_key character varying,
/// no_notification boolean
/// );
/// ```
async fn create_claim_orders(manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(ClaimOrder::ClaimOrders)
.col(auto_uuid_not_null!(ClaimOrder::Id))
.col(
ColumnDef::new(ClaimOrder::PaymentStatus)
.enumeration(
ClaimOrderPaymentStatus::ClaimOrderPaymentStatuses,
ClaimOrderPaymentStatus::iter().skip(1),
)
.default(ClaimOrderPaymentStatus::NA)
.not_null(),
)
.col(
ColumnDef::new(ClaimOrder::FulfillmentStatus)
.enumeration(
ClaimOrderFulfillmentStatus::ClaimOrderFulfillmentStatuses,
ClaimOrderFulfillmentStatus::iter().skip(1),
)
.default(ClaimOrderFulfillmentStatus::NotFulfilled)
.not_null(),
)
.col(
ColumnDef::new(ClaimOrder::ClaimOrderType)
.enumeration(
ClaimOrderType::ClaimOrderTypes,
ClaimOrderType::iter().skip(1),
)
.not_null(),
)
.col(ColumnDef::new(ClaimOrder::OrderId).uuid().not_null())
.col(ColumnDef::new(ClaimOrder::ShippingAddressId).uuid())
.col(ColumnDef::new(ClaimOrder::RefundAmount).integer())
.col(ColumnDef::new(ClaimOrder::CanceledAt).timestamp())
.col(ts_def_now_not_null!(ClaimOrder::CreatedAt))
.col(ts_def_now_not_null!(ClaimOrder::UpdatedAt))
.col(ColumnDef::new(ClaimOrder::DeletedAt).timestamp())
.col(ColumnDef::new(ClaimOrder::Metadata).json_binary())
.col(ColumnDef::new(ClaimOrder::IdempotencyKey).uuid())
.col(ColumnDef::new(ClaimOrder::NoNotification).boolean())
.to_owned(),
)
.await
}
/// ```sql
/// CREATE TABLE claim_tags
/// (
/// id uuid NOT NULL,
/// value character varying NOT NULL,
/// created_at timestamp with time zone DEFAULT now() NOT NULL,
/// updated_at timestamp with time zone DEFAULT now() NOT NULL,
/// deleted_at timestamp with time zone,
/// metadata jsonb
/// );
/// ```
async fn create_claim_tags(manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(ClaimTag::ClaimTags)
.col(auto_uuid_not_null!(ClaimTag::Id))
.col(ColumnDef::new(ClaimTag::Value).string())
.col(ts_def_now_not_null!(ClaimTag::CreatedAt))
.col(ts_def_now_not_null!(ClaimTag::UpdatedAt))
.col(ColumnDef::new(ClaimTag::DeletedAt).timestamp())
.col(ColumnDef::new(ClaimTag::Metadata).json_binary())
.to_owned(),
)
.await
}
}
#[derive(Iden)]
pub enum ClaimImage {
ClaimImages,
@ -242,6 +337,11 @@ pub enum ClaimOrder {
RefundAmount,
CanceledAt,
CreatedAt,
UpdatedAt,
DeletedAt,
Metadata,
IdempotencyKey,
NoNotification,
}
#[derive(Iden)]