Add some abstraction
This commit is contained in:
parent
87fb672497
commit
6f0fc918df
@ -84,6 +84,71 @@ impl CreateIndexExt for SchemaManager<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait CreateBridgeTable: CreateIndexExt {
|
||||
async fn create_bridge_table<T, C1, C2>(
|
||||
&self,
|
||||
table: T,
|
||||
col1: C1,
|
||||
col2: C2,
|
||||
) -> Result<(), DbErr>
|
||||
where
|
||||
T: IntoIden + Copy + 'static + Sync + Send,
|
||||
C1: IntoIden + Copy + 'static + Sync + Send,
|
||||
C2: IntoIden + Copy + 'static + Sync + Send;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl CreateBridgeTable for SchemaManager<'_> {
|
||||
async fn create_bridge_table<T, C1, C2>(
|
||||
&self,
|
||||
table: T,
|
||||
col1: C1,
|
||||
col2: C2,
|
||||
) -> Result<(), DbErr>
|
||||
where
|
||||
T: IntoIden + Copy + 'static + Sync + Send,
|
||||
C1: IntoIden + Copy + 'static + Sync + Send,
|
||||
C2: IntoIden + Copy + 'static + Sync + Send,
|
||||
{
|
||||
self.create_table(
|
||||
Table::create()
|
||||
.table(table)
|
||||
.col(col1.col().uuid().not_null())
|
||||
.col(col2.col().uuid().not_null())
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
self.create_2col_idx(table, col1, col2).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait CreateTableExt {
|
||||
async fn build_table<Enum>(&self, table: Enum, fields: Vec<ColumnDef>) -> Result<(), DbErr>
|
||||
where
|
||||
Enum: IntoIden + Copy + 'static + Sync + Send;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl CreateTableExt for SchemaManager<'_> {
|
||||
async fn build_table<Enum>(&self, table: Enum, mut fields: Vec<ColumnDef>) -> Result<(), DbErr>
|
||||
where
|
||||
Enum: IntoIden + Copy + 'static + Sync + Send,
|
||||
{
|
||||
let mut t = Table::create();
|
||||
t.table(table);
|
||||
fields.iter_mut().for_each(|col| {
|
||||
t.col(col);
|
||||
});
|
||||
self.create_table(t).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_pk2_name<T1: IntoIden, C1: IntoIden, C2: IntoIden>(t1: T1, c1: C1, c2: C2) -> String {
|
||||
let t1 = to_snake(t1.into_iden().to_string());
|
||||
let c1 = to_snake(c1.into_iden().to_string());
|
||||
|
@ -3,8 +3,8 @@ use sea_orm_migration::prelude::*;
|
||||
use crate::constraint::Check;
|
||||
use crate::sea_orm::Iterable;
|
||||
use crate::{
|
||||
auto_uuid_not_null, ts_def_now_not_null, AsIden, CreateConstraint, CreateIndexExt, DropTable,
|
||||
IntoColumnDef,
|
||||
auto_uuid_not_null, ts_def_now_not_null, AsIden, CreateBridgeTable, CreateConstraint,
|
||||
CreateIndexExt, DropTable, IntoColumnDef,
|
||||
};
|
||||
|
||||
#[derive(DeriveMigrationName)]
|
||||
@ -13,10 +13,55 @@ pub struct Migration;
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
|
||||
self.create_price_lists(m).await?;
|
||||
self.create_price_list_customer_groups(m).await?;
|
||||
self.create_products(m).await?;
|
||||
self.create_product_categories(m).await?;
|
||||
self.create_product_category_products(m).await?;
|
||||
self.create_product_collections(m).await?;
|
||||
self.create_product_images(m).await?;
|
||||
self.create_product_options(m).await?;
|
||||
self.create_product_option_values(m).await?;
|
||||
self.create_product_sales_channels(m).await?;
|
||||
self.create_product_tags(m).await?;
|
||||
self.create_product_to_tags(m).await?;
|
||||
self.create_product_tax_rates(m).await?;
|
||||
self.create_product_types(m).await?;
|
||||
self.create_product_type_tax_rates(m).await?;
|
||||
self.create_product_variants(m).await?;
|
||||
self.create_product_variant_inventory_items(m).await?;
|
||||
self.create_money_amounts(m).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
|
||||
self.drop_table(m, PriceList::PriceLists).await?;
|
||||
self.drop_table(m, PriceListCustomerGroup::PriceListCustomerGroups)
|
||||
.await?;
|
||||
self.drop_table(m, Product::Products).await?;
|
||||
self.drop_table(m, ProductCategory::ProductCategories)
|
||||
.await?;
|
||||
self.drop_table(m, ProductCategoryProduct::ProductCategoryProducts)
|
||||
.await?;
|
||||
self.drop_table(m, ProductCollection::ProductCollections)
|
||||
.await?;
|
||||
self.drop_table(m, ProductImage::ProductImages).await?;
|
||||
self.drop_table(m, ProductOption::ProductOptions).await?;
|
||||
self.drop_table(m, ProductOptionValue::ProductOptionValues)
|
||||
.await?;
|
||||
self.drop_table(m, ProductSalesChannel::ProductSalesChannels)
|
||||
.await?;
|
||||
self.drop_table(m, ProductTag::ProductTags).await?;
|
||||
self.drop_table(m, ProductToTag::ProductToTags).await?;
|
||||
self.drop_table(m, ProductTaxRate::ProductTaxRates).await?;
|
||||
self.drop_table(m, ProductType::ProductTypes).await?;
|
||||
self.drop_table(m, ProductTypeTaxRate::ProductTypeTaxRates)
|
||||
.await?;
|
||||
self.drop_table(m, ProductVariant::ProductVariants).await?;
|
||||
self.drop_table(m, ProductVariantInventoryItem::ProductVariantInventoryItems)
|
||||
.await?;
|
||||
self.drop_table(m, MoneyAmount::MoneyAmounts).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@ -40,6 +85,41 @@ impl Migration {
|
||||
async fn create_price_lists(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> {
|
||||
use PriceList::*;
|
||||
|
||||
m.create_table(
|
||||
Table::create()
|
||||
.table(PriceLists)
|
||||
.col(auto_uuid_not_null!(Id))
|
||||
.col(Name.col().string().not_null())
|
||||
.col(Description.col().string().not_null())
|
||||
.col(
|
||||
PriceListType
|
||||
.col()
|
||||
.enumeration(
|
||||
crate::types::PriceListType::PriceListTypes,
|
||||
crate::types::PriceListType::iter().skip(1),
|
||||
)
|
||||
.default(crate::types::PriceListType::Sale.to_string())
|
||||
.not_null(),
|
||||
)
|
||||
.col(
|
||||
Status
|
||||
.col()
|
||||
.enumeration(
|
||||
crate::types::PriceListStatus::PriceListStatuses,
|
||||
crate::types::PriceListStatus::iter().skip(1),
|
||||
)
|
||||
.default(crate::types::PriceListStatus::Draft.to_string())
|
||||
.not_null(),
|
||||
)
|
||||
.col(StartsAt.col().timestamp())
|
||||
.col(EndsAt.col().timestamp())
|
||||
.col(ts_def_now_not_null!(CreatedAt))
|
||||
.col(ts_def_now_not_null!(UpdatedAt))
|
||||
.col(DeletedAt.col().timestamp())
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
/// ```sql
|
||||
@ -52,6 +132,9 @@ impl Migration {
|
||||
async fn create_price_list_customer_groups(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> {
|
||||
use PriceListCustomerGroup::*;
|
||||
|
||||
m.create_bridge_table(PriceListCustomerGroups, PriceListId, CustomerGroupId)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
/// ```sql
|
||||
@ -109,6 +192,9 @@ impl Migration {
|
||||
async fn create_product_categories(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> {
|
||||
use ProductCategory::*;
|
||||
|
||||
m.create_constraint(ProductCategories, Check::greater_eq(Rank, 0.iden()), None)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
/// ```sql
|
||||
@ -121,6 +207,9 @@ impl Migration {
|
||||
async fn create_product_category_products(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> {
|
||||
use ProductCategoryProduct::*;
|
||||
|
||||
m.create_bridge_table(ProductCategoryProducts, ProductCategoryId, ProductId)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
/// ```sql
|
||||
@ -150,6 +239,9 @@ impl Migration {
|
||||
async fn create_product_images(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> {
|
||||
use ProductImage::*;
|
||||
|
||||
m.create_bridge_table(ProductImages, ProductId, ImageId)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
/// ```sql
|
||||
@ -197,6 +289,9 @@ impl Migration {
|
||||
async fn create_product_sales_channels(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> {
|
||||
use ProductSalesChannel::*;
|
||||
|
||||
m.create_bridge_table(ProductSalesChannels, ProductId, SalesChannelId)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
/// ```sql
|
||||
@ -225,6 +320,9 @@ impl Migration {
|
||||
async fn create_product_to_tags(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> {
|
||||
use ProductToTag::*;
|
||||
|
||||
m.create_bridge_table(ProductToTags, ProductId, ProductTagId)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
/// ```sql
|
||||
@ -302,7 +400,7 @@ impl Migration {
|
||||
/// );
|
||||
/// ```
|
||||
async fn create_product_variants(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> {
|
||||
use ::*;
|
||||
use ProductVariant::*;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -319,11 +417,11 @@ impl Migration {
|
||||
/// );
|
||||
/// ```
|
||||
async fn create_product_variant_inventory_items(
|
||||
use ::*;
|
||||
|
||||
&self,
|
||||
m: &SchemaManager<'_>,
|
||||
) -> Result<(), DbErr> {
|
||||
use ProductVariantInventoryItem::*;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
/// ```sql
|
||||
@ -343,7 +441,7 @@ impl Migration {
|
||||
/// );
|
||||
/// ```
|
||||
async fn create_money_amounts(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> {
|
||||
use ::*;
|
||||
use MoneyAmount::*;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user