Fix some parts of migration

This commit is contained in:
eraden 2023-06-13 22:00:47 +02:00
parent 6eb9857066
commit 2974a180f5
10 changed files with 149 additions and 278 deletions

View File

@ -1,6 +1,6 @@
use sea_orm_migration::prelude::*;
use crate::{CreateIndexExt, DropTable, IntoColumnDef};
use crate::{CreateBridgeTable, DropTable};
/// ```sql
/// CREATE TABLE cart_discounts
@ -41,22 +41,10 @@ impl Migration {
/// );
/// ```
async fn create_cart_discounts(m: &SchemaManager<'_>) -> Result<(), DbErr> {
m.create_table(
Table::create()
.if_not_exists()
.table(CartDiscount::CartDiscounts)
.col(CartDiscount::CartId.col().uuid().not_null())
.col(CartDiscount::DiscountId.col().uuid().not_null())
.to_owned(),
)
.await?;
use CartDiscount::*;
m.create_2_col_primary(
CartDiscount::CartDiscounts,
CartDiscount::CartId,
CartDiscount::DiscountId,
)
.await?;
m.create_bridge_table(CartDiscounts, CartId, DiscountId)
.await?;
Ok(())
}
@ -69,22 +57,10 @@ impl Migration {
/// );
/// ```
async fn create_cart_gift_cards(m: &SchemaManager<'_>) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(CartGiftCard::CartGiftCards)
.if_not_exists()
.col(CartGiftCard::CartId.col().uuid().not_null())
.col(CartGiftCard::GiftCardId.col().uuid().not_null())
.to_owned(),
)
.await?;
use CartGiftCard::*;
m.create_2_col_primary(
CartGiftCard::CartGiftCards,
CartGiftCard::CartId,
CartGiftCard::GiftCardId,
)
.await?;
m.create_bridge_table(CartGiftCards, CartId, GiftCardId)
.await?;
Ok(())
}

View File

@ -3,7 +3,7 @@ use sea_orm_migration::prelude::*;
use crate::sea_orm::Iterable;
use crate::{
auto_uuid_not_null, create_type, drop_type, to_fk_name, to_pk2_name, ts_def_now_not_null,
CreateIndexExt, DropTable, IntoColumnDef,
CreateBridgeTable, DropTable, IntoColumnDef,
};
#[derive(DeriveMigrationName)]
@ -184,22 +184,10 @@ impl Migration {
/// discount_id uuid NOT NULL
/// }
async fn create_order_discounts(m: &SchemaManager<'_>) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(OrderDiscount::OrderDiscounts)
.if_not_exists()
.col(OrderDiscount::OrderId.col().uuid().not_null())
.col(OrderDiscount::DiscountId.col().uuid().not_null())
.to_owned(),
)
.await?;
use OrderDiscount::*;
m.create_2_col_primary(
OrderDiscount::OrderDiscounts,
OrderDiscount::OrderId,
OrderDiscount::DiscountId,
)
.await?;
m.create_bridge_table(OrderDiscounts, OrderId, DiscountId)
.await?;
Ok(())
}
@ -260,22 +248,10 @@ impl Migration {
/// }
/// ```
async fn create_order_gift_cards(m: &SchemaManager<'_>) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(OrderGiftCard::OrderGiftCards)
.if_not_exists()
.col(OrderGiftCard::OrderId.col().uuid().not_null())
.col(OrderGiftCard::GiftCardId.col().uuid().not_null())
.to_owned(),
)
.await?;
use OrderGiftCard::*;
m.create_2_col_primary(
OrderGiftCard::OrderGiftCards,
OrderGiftCard::OrderId,
OrderGiftCard::GiftCardId,
)
.await?;
m.create_bridge_table(OrderGiftCards, OrderId, GiftCardId)
.await?;
Ok(())
}
@ -294,28 +270,27 @@ impl Migration {
/// }
/// ```
async fn create_order_item_changes(m: &SchemaManager<'_>) -> Result<(), DbErr> {
use OrderItemChange::*;
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())
.table(OrderItemChanges)
.col(auto_uuid_not_null!(Id))
.col(ts_def_now_not_null!(CreatedAt))
.col(ts_def_now_not_null!(UpdatedAt))
.col(DeletedAt.col().timestamp())
.col(
ColumnDef::new(OrderItemChange::OrderItemChangeType)
OrderItemChangeType
.col()
.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())
.col(OrderEditId.col().uuid().not_null())
.col(OriginalLineItemId.col().uuid())
.col(LineItemId.col().uuid())
.to_owned(),
)
.await?;
@ -399,19 +374,21 @@ impl Migration {
/// }
/// ```
async fn create_payment_collections(m: &SchemaManager<'_>) -> Result<(), DbErr> {
use PaymentCollection::*;
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::PaymentCollectionType.col().enumeration(
.table(PaymentCollections)
.col(auto_uuid_not_null!(Id))
.col(ts_def_now_not_null!(CreatedAt))
.col(ts_def_now_not_null!(UpdatedAt))
.col(DeletedAt.col().timestamp())
.col(PaymentCollectionType.col().enumeration(
crate::types::PaymentCollectionType::PaymentCollectionTypes,
crate::types::PaymentCollectionType::iter().skip(1),
))
.col(
PaymentCollection::Status
Status
.col()
.enumeration(
crate::types::PaymentCollectionStatus::PaymentCollectionStatuses,
@ -419,13 +396,13 @@ impl Migration {
)
.not_null(),
)
.col(PaymentCollection::Description.col().string())
.col(PaymentCollection::Amount.col().integer().not_null())
.col(PaymentCollection::AuthorizedAmount.col().integer())
.col(PaymentCollection::RegionId.col().uuid().not_null())
.col(PaymentCollection::CurrencyCode.col().string().not_null())
.col(PaymentCollection::Metadata.col().json_binary())
.col(PaymentCollection::CreatedBy.col().uuid().not_null())
.col(Description.col().string())
.col(Amount.col().integer().not_null())
.col(AuthorizedAmount.col().integer())
.col(RegionId.col().uuid().not_null())
.col(CurrencyCode.col().string().not_null())
.col(Metadata.col().json_binary())
.col(CreatedBy.col().uuid().not_null())
.to_owned(),
)
.await?;
@ -440,37 +417,18 @@ impl Migration {
/// }
/// ```
async fn create_payment_collection_payments(m: &SchemaManager<'_>) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(PaymentCollectionPayment::PaymentCollectionPayments)
.col(
PaymentCollectionPayment::PaymentCollectionId
.col()
.uuid()
.not_null(),
)
.col(PaymentCollectionPayment::PaymentId.col().uuid().not_null())
.to_owned(),
)
.await?;
use PaymentCollectionPayment::*;
m.create_2_col_primary(
PaymentCollectionPayment::PaymentCollectionPayments,
PaymentCollectionPayment::PaymentCollectionId,
PaymentCollectionPayment::PaymentId,
)
.await?;
m.create_bridge_table(PaymentCollectionPayments, PaymentCollectionId, PaymentId)
.await?;
m.create_foreign_key(
ForeignKeyCreateStatement::new()
.from(
PaymentCollectionPayment::PaymentCollectionPayments,
PaymentCollectionPayment::PaymentCollectionId,
)
.from(PaymentCollectionPayments, PaymentCollectionId)
.to(PaymentCollection::PaymentCollections, PaymentCollection::Id)
.name(&to_fk_name(
PaymentCollectionPayment::PaymentCollectionPayments,
PaymentCollectionPayment::PaymentCollectionId,
PaymentCollectionPayments,
PaymentCollectionId,
PaymentCollection::PaymentCollections,
PaymentCollection::Id,
))
@ -489,29 +447,12 @@ impl Migration {
/// }
/// ````
async fn create_payment_collection_sessions(m: &SchemaManager<'_>) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(PaymentCollectionSession::PaymentCollectionSessions)
.col(
PaymentCollectionSession::PaymentCollectionId
.col()
.uuid()
.not_null(),
)
.col(
PaymentCollectionSession::PaymentSessionId
.col()
.uuid()
.not_null(),
)
.to_owned(),
)
.await?;
use PaymentCollectionSession::*;
m.create_2_col_primary(
PaymentCollectionSession::PaymentCollectionSessions,
PaymentCollectionSession::PaymentCollectionId,
PaymentCollectionSession::PaymentSessionId,
m.create_bridge_table(
PaymentCollectionSessions,
PaymentCollectionId,
PaymentSessionId,
)
.await?;
Ok(())
@ -538,6 +479,7 @@ impl Migration {
.to_owned(),
)
.await?;
Ok(())
}

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;
@ -86,37 +88,36 @@ impl Migration {
/// );
/// ```
async fn create_discounts(m: &SchemaManager<'_>) -> Result<(), DbErr> {
use Discount::*;
m.create_table(
Table::create()
.table(Discount::Discounts)
.col(auto_uuid_not_null!(Discount::Id))
.col(ColumnDef::new(Discount::Code).string().not_null())
.col(ColumnDef::new(Discount::IsDynamic).boolean().not_null())
.col(ColumnDef::new(Discount::RuleId).uuid())
.col(ColumnDef::new(Discount::IsDisabled).boolean().not_null())
.col(ColumnDef::new(Discount::ParentDiscountId).uuid())
.table(Discounts)
.col(auto_uuid_not_null!(Id))
.col(Code.col().string().not_null())
.col(IsDynamic.col().boolean().not_null())
.col(RuleId.col().uuid())
.col(IsDisabled.col().boolean().not_null())
.col(ParentDiscountId.col().uuid())
.col(
ColumnDef::new(Discount::StartsAt)
StartsAt
.col()
.timestamp()
.default(SimpleExpr::Custom("CURRENT_TIMESTAMP".into()))
.not_null(),
)
.col(ColumnDef::new(Discount::EndsAt).timestamp())
.col(EndsAt.col().timestamp())
.col(ts_def_now_not_null!(Discount::CreatedAt))
.col(ts_def_now_not_null!(Discount::UpdatedAt))
.col(ColumnDef::new(Discount::DeletedAt).timestamp())
.col(ColumnDef::new(Discount::Metadata).json_binary())
.col(ColumnDef::new(Discount::UsageLimit).integer())
.col(
ColumnDef::new(Discount::UsageCount)
.integer()
.default(0)
.not_null(),
)
.col(ColumnDef::new(Discount::ValidDuration).string())
.col(DeletedAt.col().timestamp())
.col(Metadata.col().json_binary())
.col(UsageLimit.col().integer())
.col(UsageCount.col().integer().default(0).not_null())
.col(ValidDuration.col().string())
.to_owned(),
)
.await?;
Ok(())
}
@ -134,29 +135,25 @@ impl Migration {
/// );
/// ```
async fn create_discount_conditions(m: &SchemaManager<'_>) -> Result<(), DbErr> {
use DiscountCondition::*;
m.create_table(
Table::create()
.table(DiscountCondition::DiscountConditions)
.col(auto_uuid_not_null!(DiscountCondition::Id))
.col(
ColumnDef::new(DiscountCondition::DiscountConditionType).enumeration(
crate::types::DiscountConditionType::DiscountConditionTypes,
crate::types::DiscountConditionType::iter().skip(1),
),
)
.col(ColumnDef::new(DiscountCondition::Operator).enumeration(
.table(DiscountConditions)
.col(auto_uuid_not_null!(Id))
.col(DiscountConditionType.col().enumeration(
crate::types::DiscountConditionType::DiscountConditionTypes,
crate::types::DiscountConditionType::iter().skip(1),
))
.col(Operator.col().enumeration(
crate::types::DiscountConditionOperator::DiscountConditionOperators,
crate::types::DiscountConditionOperator::iter().skip(1),
))
.col(
ColumnDef::new(DiscountCondition::DiscountRuleId)
.uuid()
.not_null(),
)
.col(ts_def_now_not_null!(DiscountCondition::CreatedAt))
.col(ts_def_now_not_null!(DiscountCondition::UpdatedAt))
.col(ColumnDef::new(DiscountCondition::DeletedAt).timestamp())
.col(ColumnDef::new(DiscountCondition::Metadata).json_binary())
.col(DiscountRuleId.col().uuid().not_null())
.col(ts_def_now_not_null!(CreatedAt))
.col(ts_def_now_not_null!(UpdatedAt))
.col(DeletedAt.col().timestamp())
.col(Metadata.col().json_binary())
.to_owned(),
)
.await?;
@ -174,26 +171,16 @@ impl Migration {
/// );
/// ```
async fn create_discount_condition_customer_groups(m: &SchemaManager<'_>) -> Result<(), DbErr> {
use DiscountConditionCustomerGroup::*;
m.create_table(
Table::create()
.table(DiscountConditionCustomerGroup::DiscountConditionCustomerGroups)
.col(
ColumnDef::new(DiscountConditionCustomerGroup::CustomerGroupId)
.uuid()
.not_null(),
)
.col(
ColumnDef::new(DiscountConditionCustomerGroup::ConditionId)
.uuid()
.not_null(),
)
.col(ts_def_now_not_null!(
DiscountConditionCustomerGroup::CreatedAt
))
.col(ts_def_now_not_null!(
DiscountConditionCustomerGroup::UpdatedAt
))
.col(ColumnDef::new(DiscountConditionCustomerGroup::Metadata).json_binary())
.table(DiscountConditionCustomerGroups)
.col(CustomerGroupId.col().uuid().not_null())
.col(ConditionId.col().uuid().not_null())
.col(ts_def_now_not_null!(CreatedAt))
.col(ts_def_now_not_null!(UpdatedAt))
.col(Metadata.col().json_binary())
.to_owned(),
)
.await?;
@ -211,22 +198,16 @@ impl Migration {
/// );
/// ```
async fn create_discount_condition_products(m: &SchemaManager<'_>) -> Result<(), DbErr> {
use DiscountConditionProduct::*;
m.create_table(
Table::create()
.table(DiscountConditionProduct::DiscountConditionProducts)
.col(
ColumnDef::new(DiscountConditionProduct::ProductId)
.uuid()
.not_null(),
)
.col(
ColumnDef::new(DiscountConditionProduct::ConditionId)
.uuid()
.not_null(),
)
.col(ts_def_now_not_null!(DiscountConditionProduct::CreatedAt))
.col(ts_def_now_not_null!(DiscountConditionProduct::UpdatedAt))
.col(ColumnDef::new(DiscountConditionProduct::Metadata).json_binary())
.table(DiscountConditionProducts)
.col(ProductId.col().uuid().not_null())
.col(ConditionId.col().uuid().not_null())
.col(ts_def_now_not_null!(CreatedAt))
.col(ts_def_now_not_null!(UpdatedAt))
.col(Metadata.col().json_binary())
.to_owned(),
)
.await?;

View File

@ -39,6 +39,7 @@ async fn main() {
migrate_schema!(cli, Discounts, DiscountsMigrator);
migrate_schema!(cli, Checkouts, CheckoutsMigrator);
migrate_schema!(cli, Shipping, ShippingMigrator);
migrate_schema!(cli, Stocks, StocksMigrator);
}
pub async fn run_cli<M>(cli: &mut Cli, schema: PostgreSQLSchema, migrator: M)

View File

@ -77,24 +77,25 @@ impl Migration {
/// );
/// ```
async fn create_currencies(m: &SchemaManager<'_>) -> Result<(), DbErr> {
use Currency::*;
m.create_table(
Table::create()
.table(Currency::Currencies)
.col(Currency::Code.col().string().not_null())
.col(Currency::Symbol.col().string().not_null())
.col(Currency::SymbolNative.col().string().not_null())
.col(Currency::Name.col().string().not_null())
.to_owned(),
)
.await?;
m.create_index(
IndexCreateStatement::new()
.table(Currency::Currencies)
.col(Currency::Code)
.primary()
.table(Currencies)
.col(Code.col().string().not_null())
.col(Symbol.col().string().not_null())
.col(SymbolNative.col().string().not_null())
.col(Name.col().string().not_null())
.primary_key(
IndexCreateStatement::new()
.table(Currencies)
.col(Code)
.primary(),
)
.to_owned(),
)
.await?;
Ok(())
}

View File

@ -10,6 +10,7 @@ pub enum PostgreSQLSchema {
Discounts,
Checkouts,
Shipping,
Stocks,
}
impl PostgreSQLSchema {
@ -21,6 +22,7 @@ impl PostgreSQLSchema {
PostgreSQLSchema::Discounts => "discounts",
PostgreSQLSchema::Checkouts => "checkouts",
PostgreSQLSchema::Shipping => "shipping",
PostgreSQLSchema::Stocks => "stocks",
}
}
}

View File

@ -54,48 +54,7 @@ pub trait IntoColumnDef: IntoIden {
impl<T: IntoIden> IntoColumnDef for T {}
#[async_trait]
pub trait CreateIndexExt {
async fn create_2_col_primary<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 CreateIndexExt for SchemaManager<'_> {
async fn create_2_col_primary<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_index(
IndexCreateStatement::new()
.table(table)
.col(col1)
.col(col2)
.name(&to_pk2_name(table, col1, col2))
.primary()
.if_not_exists()
.to_owned(),
)
.await
}
}
#[async_trait]
pub trait CreateBridgeTable: CreateIndexExt {
pub trait CreateBridgeTable {
async fn create_bridge_table<T, C1, C2>(
&self,
table: T,
@ -106,6 +65,22 @@ pub trait CreateBridgeTable: CreateIndexExt {
T: IntoIden + Copy + 'static + Sync + Send,
C1: IntoIden + Copy + 'static + Sync + Send,
C2: IntoIden + Copy + 'static + Sync + Send;
fn bridge_primary_key<T, C1, C2>(&self, table: T, col1: C1, col2: C2) -> IndexCreateStatement
where
T: IntoIden + Copy + 'static + Sync + Send,
C1: IntoIden + Copy + 'static + Sync + Send,
C2: IntoIden + Copy + 'static + Sync + Send,
{
IndexCreateStatement::new()
.table(table)
.col(col1)
.col(col2)
.name(&to_pk2_name(table, col1, col2))
.primary()
.if_not_exists()
.to_owned()
}
}
#[async_trait]
@ -126,12 +101,11 @@ impl CreateBridgeTable for SchemaManager<'_> {
.table(table)
.col(col1.col().uuid().not_null())
.col(col2.col().uuid().not_null())
.primary_key(&mut self.bridge_primary_key(table, col1, col2))
.to_owned(),
)
.await?;
self.create_2_col_primary(table, col1, col2).await?;
Ok(())
}
}
@ -321,7 +295,7 @@ impl From<Check> for Constraint {
impl Display for Constraint {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Constraint::Check(check) => f.write_fmt(format_args!("CHECK {check}")),
Constraint::Check(check) => f.write_fmt(format_args!("CHECK ({check})")),
}
}
}

View File

@ -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,
DropTable, IntoColumnDef,
};
#[derive(DeriveMigrationName)]
@ -351,13 +351,11 @@ impl Migration {
.col(ts_def_now_not_null!(CreatedAt))
.col(ts_def_now_not_null!(UpdatedAt))
.col(Metadata.col().json_binary())
.primary_key(&mut m.bridge_primary_key(ShippingTaxRates, ShippingOptionId, RateId))
.to_owned(),
)
.await?;
m.create_2_col_primary(ShippingTaxRates, ShippingOptionId, RateId)
.await?;
Ok(())
}
}

View File

@ -4,7 +4,7 @@ use crate::constraint::Check;
use crate::sea_orm::Iterable;
use crate::{
auto_uuid_not_null, ts_def_now_not_null, AsIden, CreateBridgeTable, CreateConstraint,
CreateIndexExt, DropTable, IntoColumnDef,
DropTable, IntoColumnDef,
};
#[derive(DeriveMigrationName)]
@ -460,13 +460,11 @@ impl Migration {
.col(ts_def_now_not_null!(CreatedAt))
.col(ts_def_now_not_null!(UpdatedAt))
.col(Metadata.col().json_binary())
.primary_key(&mut m.bridge_primary_key(ProductTaxRates, ProductId, RateId))
.to_owned(),
)
.await?;
m.create_2_col_primary(ProductTaxRates, ProductId, RateId)
.await?;
Ok(())
}
/// ```sql
@ -519,13 +517,11 @@ impl Migration {
.col(ts_def_now_not_null!(CreatedAt))
.col(ts_def_now_not_null!(UpdatedAt))
.col(Metadata.col().json_binary())
.primary_key(&mut m.bridge_primary_key(ProductTypeTaxRates, ProductTypeId, RateId))
.to_owned(),
)
.await?;
m.create_2_col_primary(ProductTypeTaxRates, ProductTypeId, RateId)
.await?;
Ok(())
}
/// ```sql

View File

@ -2,10 +2,10 @@ mod m20230603_120810_products;
use sea_orm_migration::{MigrationTrait, MigratorTrait};
pub struct ShocksMigrator;
pub struct StocksMigrator;
#[async_trait::async_trait]
impl MigratorTrait for ShocksMigrator {
impl MigratorTrait for StocksMigrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20230603_120810_products::Migration)]
}