From c0e8d9da34a8a8bcf9984cef680e4fd111db1e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Wo=C5=BAniak?= Date: Thu, 8 Jun 2023 15:48:02 +0200 Subject: [PATCH] Start checkout db --- migration/src/carts/m20230603_102634_types.rs | 2 - .../checkouts/m20230603_120814_checkouts.rs | 435 ++++++++++++++ migration/src/checkouts/mod.rs | 12 + .../discounts/m20230603_120814_discounts.rs | 544 ++++++++++++++++++ migration/src/discounts/mod.rs | 12 + migration/src/lib.rs | 39 +- migration/src/main.rs | 39 +- .../src/public/m20230603_102630_schema.rs | 37 -- .../src/public/m20230603_102634_types.rs | 10 - .../public/m20230603_120816_geolocation.rs | 133 ++++- migration/src/public/mod.rs | 3 - migration/src/schema_list.rs | 2 + migration/src/types.rs | 14 +- migrations/20230603073510_init.sql | 392 ++++++------- 14 files changed, 1391 insertions(+), 283 deletions(-) create mode 100644 migration/src/checkouts/m20230603_120814_checkouts.rs create mode 100644 migration/src/checkouts/mod.rs create mode 100644 migration/src/discounts/m20230603_120814_discounts.rs create mode 100644 migration/src/discounts/mod.rs delete mode 100644 migration/src/public/m20230603_102630_schema.rs diff --git a/migration/src/carts/m20230603_102634_types.rs b/migration/src/carts/m20230603_102634_types.rs index e0d092d..6a48f87 100644 --- a/migration/src/carts/m20230603_102634_types.rs +++ b/migration/src/carts/m20230603_102634_types.rs @@ -1,8 +1,6 @@ use sea_orm::Iterable; use sea_orm_migration::prelude::*; -use crate::extension::postgres::Type; -use crate::types::*; use crate::{create_type, drop_type}; #[derive(DeriveMigrationName)] diff --git a/migration/src/checkouts/m20230603_120814_checkouts.rs b/migration/src/checkouts/m20230603_120814_checkouts.rs new file mode 100644 index 0000000..1188280 --- /dev/null +++ b/migration/src/checkouts/m20230603_120814_checkouts.rs @@ -0,0 +1,435 @@ +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}; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> { + Self::create_orders(m).await?; + Self::create_order_discounts(m).await?; + Self::create_order_edits(m).await?; + Self::create_order_gift_cards(m).await?; + Self::create_order_item_changes(m).await?; + Self::create_payments(m).await?; + Self::create_payment_collections(m).await?; + Self::create_payment_collection_payments(m).await?; + Self::create_payment_collection_sessions(m).await?; + Self::create_payment_providers(m).await?; + Self::create_payment_sessions(m).await?; + Ok(()) + } + + async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> { + drop_type!(m, crate::types::OrderStatus); + drop_type!(m, crate::types::OrderFulfillmentStatus); + drop_type!(m, crate::types::OrderPaymentStatus); + Ok(()) + } +} + +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); + Ok(()) + } + /// ```sql + /// CREATE TABLE orders + /// ( + /// id uuid NOT NULL, + /// status order_statuses DEFAULT 'pending'::order_statuses NOT NULL, + /// fulfillment_status order_fulfillment_statuses DEFAULT 'not_fulfilled'::order_fulfillment_statuses NOT NULL, + /// payment_status order_payment_statuses DEFAULT 'not_paid'::order_payment_statuses NOT NULL, + /// display_id integer NOT NULL, + /// cart_id uuid, + /// customer_id uuid NOT NULL, + /// email character varying NOT NULL, + /// billing_address_id uuid, + /// shipping_address_id uuid, + /// region_id uuid NOT NULL, + /// currency_code character varying NOT NULL, + /// tax_rate real, + /// 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, + /// metadata jsonb, + /// idempotency_key character varying, + /// draft_order_id uuid, + /// no_notification boolean, + /// external_id uuid, + /// sales_channel_id uuid + /// } + /// ``` + async fn create_orders(m: &SchemaManager<'_>) -> Result<(), DbErr> { + m.create_table( + Table::create() + .table(Order::Orders) + .col(auto_uuid_not_null!(Order::Id)) + .col( + ColumnDef::new(Order::Status) + .enumeration( + crate::types::OrderStatus::OrderStatuses, + crate::types::OrderStatus::iter().skip(1), + ) + .default(crate::types::OrderStatus::Pending) + .not_null(), + ) + .col( + ColumnDef::new(Order::FulfillmentStatus) + .enumeration( + crate::types::OrderFulfillmentStatus::OrderFulfillmentStatuses, + crate::types::OrderFulfillmentStatus::iter().take(1), + ) + .not_null(), + ) + .col( + ColumnDef::new(Order::PaymentStatus) + .enumeration( + crate::types::OrderPaymentStatus::OrderPaymentStatuses, + crate::types::OrderPaymentStatus::iter().take(1), + ) + .not_null(), + ) + .col(ColumnDef::new(Order::DisplayId).uuid()) + .col(ColumnDef::new(Order::CartId).uuid()) + .col(ColumnDef::new(Order::CustomerId).uuid()) + .col(ColumnDef::new(Order::Email).uuid()) + .col(ColumnDef::new(Order::BillingAddressId).uuid()) + .col(ColumnDef::new(Order::ShippingAddressId).uuid()) + .col(ColumnDef::new(Order::RegionId).uuid()) + .col(ColumnDef::new(Order::CurrencyCode).uuid()) + .col(ColumnDef::new(Order::TaxRate).uuid()) + .col(ColumnDef::new(Order::CanceledAt).uuid()) + .col(ColumnDef::new(Order::CreatedAt).uuid()) + .col(ColumnDef::new(Order::UpdatedAt).uuid()) + .col(ColumnDef::new(Order::Metadata).uuid()) + .col(ColumnDef::new(Order::IdempotencyKey).uuid()) + .col(ColumnDef::new(Order::DraftOrderId).uuid()) + .col(ColumnDef::new(Order::NoNotification).uuid()) + .col(ColumnDef::new(Order::ExternalId).uuid()) + .col(ColumnDef::new(Order::SalesChannelId).uuid()) + .to_owned(), + ) + .await?; + + Ok(()) + } + + /// ```sql + /// CREATE TABLE order_discounts + /// ( + /// order_id uuid NOT NULL, + /// discount_id uuid NOT NULL + /// } + async fn create_order_discounts(m: &SchemaManager<'_>) -> Result<(), DbErr> { + Ok(()) + } + + /// ```sql + /// CREATE TABLE order_edits + /// ( + /// id uuid NOT NULL, + /// created_at timestamp with time zone DEFAULT now() NOT NULL, + /// updated_at timestamp with time zone DEFAULT now() NOT NULL, + /// order_id uuid NOT NULL, + /// internal_note character varying, + /// created_by character varying NOT NULL, + /// requested_by character varying, + /// requested_at timestamp with time zone, + /// confirmed_by character varying, + /// confirmed_at timestamp with time zone, + /// declined_by character varying, + /// declined_reason character varying, + /// declined_at timestamp with time zone, + /// canceled_by character varying, + /// canceled_at timestamp with time zone, + /// payment_collection_id uuid + /// } + /// ``` + async fn create_order_edits(m: &SchemaManager<'_>) -> Result<(), DbErr> { + Ok(()) + } + + /// ```sql + /// CREATE TABLE order_gift_cards + /// ( + /// order_id uuid NOT NULL, + /// gift_card_id uuid NOT NULL + /// } + /// ``` + async fn create_order_gift_cards(m: &SchemaManager<'_>) -> Result<(), DbErr> { + Ok(()) + } + + /// ```sql + /// CREATE TABLE order_item_changes + /// ( + /// id uuid 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, + /// type order_item_change_types NOT NULL, + /// order_edit_id uuid NOT NULL, + /// original_line_item_id uuid, + /// line_item_id uuid + /// } + /// ``` + async fn create_order_item_changes(m: &SchemaManager<'_>) -> Result<(), DbErr> { + Ok(()) + } + + /// ```sql + /// CREATE TABLE payments + /// ( + /// id uuid NOT NULL, + /// swap_id uuid, + /// cart_id uuid, + /// order_id uuid, + /// amount integer NOT NULL, + /// currency_code character varying NOT NULL, + /// amount_refunded integer DEFAULT 0 NOT NULL, + /// provider_id uuid NOT NULL, + /// data jsonb NOT NULL, + /// captured_at timestamp with time zone, + /// 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, + /// metadata jsonb, + /// idempotency_key character varying + /// } + /// ``` + async fn create_payments(m: &SchemaManager<'_>) -> Result<(), DbErr> { + Ok(()) + } + + /// ```sql + /// CREATE TABLE payment_collections + /// ( + /// id uuid 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, + /// type payment_collection_types NOT NULL, + /// status payment_collection_statuses NOT NULL, + /// description text, + /// amount integer NOT NULL, + /// authorized_amount integer, + /// region_id uuid NOT NULL, + /// currency_code character varying NOT NULL, + /// metadata jsonb, + /// created_by character varying NOT NULL + /// } + /// ``` + async fn create_payment_collections(m: &SchemaManager<'_>) -> Result<(), DbErr> { + Ok(()) + } + + /// ```sql + /// CREATE TABLE payment_collection_payments + /// ( + /// payment_collection_id uuid NOT NULL, + /// payment_id uuid NOT NULL + /// } + /// ``` + async fn create_payment_collection_payments(m: &SchemaManager<'_>) -> Result<(), DbErr> { + Ok(()) + } + + /// ```sql + /// CREATE TABLE payment_collection_sessions + /// ( + /// payment_collection_id uuid NOT NULL, + /// payment_session_id uuid NOT NULL + /// } + /// ```` + async fn create_payment_collection_sessions(m: &SchemaManager<'_>) -> Result<(), DbErr> { + Ok(()) + } + /// ```sql + /// CREATE TABLE payment_providers + /// ( + /// id uuid NOT NULL, + /// is_installed boolean DEFAULT true NOT NULL + /// } + /// ``` + async fn create_payment_providers(m: &SchemaManager<'_>) -> Result<(), DbErr> { + Ok(()) + } + + /// ```sql + /// CREATE TABLE payment_sessions + /// ( + /// id uuid NOT NULL, + /// cart_id uuid, + /// provider_id uuid NOT NULL, + /// is_selected boolean, + /// status payment_session_statuses NOT NULL, + /// data jsonb NOT NULL, + /// created_at timestamp with time zone DEFAULT now() NOT NULL, + /// updated_at timestamp with time zone DEFAULT now() NOT NULL, + /// idempotency_key character varying, + /// payment_authorized_at timestamp with time zone, + /// amount integer, + /// is_initiated boolean DEFAULT false NOT NULL + /// } + /// ``` + async fn create_payment_sessions(m: &SchemaManager<'_>) -> Result<(), DbErr> { + Ok(()) + } +} + +#[derive(Inde)] +pub enum Order { + Orders, + Id, + Status, + FulfillmentStatus, + PaymentStatus, + DisplayId, + CartId, + CustomerId, + Email, + BillingAddressId, + ShippingAddressId, + RegionId, + CurrencyCode, + TaxRate, + CanceledAt, + CreatedAt, + UpdatedAt, + Metadata, + IdempotencyKey, + DraftOrderId, + NoNotification, + ExternalId, + SalesChannelId, +} + +#[derive(Inde)] +pub enum OrderDiscount { + OrderDiscounts, + OrderId, + DiscountId, +} + +#[derive(Inde)] +pub enum OrderEdit { + OrderEdits, + Id, + CreatedAt, + UpdatedAt, + OrderId, + InternalNote, + CreatedBy, + RequestedBy, + RequestedAt, + ConfirmedBy, + ConfirmedAt, + DeclinedBy, + DeclinedReason, + DeclinedAt, + CanceledBy, + CanceledAt, + PaymentCollectionId, +} + +#[derive(Inde)] +pub enum OrderGiftCard { + OrderGiftCards, + OrderId, + GiftCardId, +} + +#[derive(Inde)] +pub enum OrderItemChange { + OrderItemChanges, + Id, + CreatedAt, + UpdatedAt, + DeletedAt, + Type, + OrderEditId, + OriginalLineItemId, + LineItemId, +} + +#[derive(Inde)] +pub enum Payment { + Payments, + Id, + SwapId, + CartId, + OrderId, + Amount, + CurrencyCode, + AmountRefunded, + ProviderId, + Data, + CapturedAt, + CanceledAt, + CreatedAt, + UpdatedAt, + Metadata, + IdempotencyKey, +} + +#[derive(Inde)] +pub enum PaymentCollection { + PaymentCollections, + Id, + CreatedAt, + UpdatedAt, + DeletedAt, + Type, + Status, + Description, + Amount, + AuthorizedAmount, + RegionId, + CurrencyCode, + Metadata, + CreatedBy, +} + +#[derive(Inde)] +pub enum PaymentCollectionPayment { + PaymentCollectionPayments, + PaymentCollectionId, + PaymentId, +} + +#[derive(Inde)] +pub enum PaymentCollectionSession { + PaymentCollectionSessions, + PaymentCollectionId, + PaymentSessionId, +} + +#[derive(Inde)] +pub enum PaymentProvider { + PaymentProviders, + Id, + IsInstalled, +} + +#[derive(Inde)] +pub enum PaymentSession { + PaymentSessions, + Id, + CartId, + ProviderId, + IsSelected, + Status, + Data, + CreatedAt, + UpdatedAt, + IdempotencyKey, + PaymentAuthorizedAt, + Amount, + IsInitiated, +} diff --git a/migration/src/checkouts/mod.rs b/migration/src/checkouts/mod.rs new file mode 100644 index 0000000..ac09d7b --- /dev/null +++ b/migration/src/checkouts/mod.rs @@ -0,0 +1,12 @@ +mod m20230603_120814_checkouts; + +use sea_orm_migration::{MigrationTrait, MigratorTrait}; + +pub struct CheckoutsMigrator; + +#[async_trait::async_trait] +impl MigratorTrait for CheckoutsMigrator { + fn migrations() -> Vec> { + vec![Box::new(m20230603_120814_checkouts::Migration)] + } +} diff --git a/migration/src/discounts/m20230603_120814_discounts.rs b/migration/src/discounts/m20230603_120814_discounts.rs new file mode 100644 index 0000000..8c3e710 --- /dev/null +++ b/migration/src/discounts/m20230603_120814_discounts.rs @@ -0,0 +1,544 @@ +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}; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> { + create_type!(m, DiscountConditionType); + create_type!(m, DiscountConditionOperator); + create_type!(m, DiscountRuleType); + create_type!(m, DiscountRuleAllocation); + + Self::create_discounts(m).await?; + Self::create_discount_conditions(m).await?; + Self::create_discount_condition_customer_groups(m).await?; + Self::create_discount_condition_products(m).await?; + Self::create_discount_condition_product_collections(m).await?; + Self::create_discount_condition_product_tags(m).await?; + Self::create_discount_condition_product_types(m).await?; + Self::create_discount_regions(m).await?; + Self::create_discount_rules(m).await?; + Self::create_discount_rule_products(m).await?; + Ok(()) + } + + async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> { + self.drop_table(m, Discount::Discounts).await?; + self.drop_table(m, DiscountCondition::DiscountConditions) + .await?; + self.drop_table( + m, + DiscountConditionCustomerGroup::DiscountConditionCustomerGroups, + ) + .await?; + self.drop_table(m, DiscountConditionProduct::DiscountConditionProducts) + .await?; + self.drop_table( + m, + DiscountConditionProductCollection::DiscountConditionProductCollections, + ) + .await?; + self.drop_table(m, DiscountConditionProductTag::DiscountConditionProductTags) + .await?; + self.drop_table( + m, + DiscountConditionProductType::DiscountConditionProductTypes, + ) + .await?; + self.drop_table(m, DiscountRegion::DiscountRegions).await?; + self.drop_table(m, DiscountRule::DiscountRules).await?; + self.drop_table(m, DiscountRuleProduct::DiscountRuleProducts) + .await?; + + drop_type!(m, DiscountConditionType); + drop_type!(m, DiscountConditionOperator); + drop_type!(m, DiscountRuleType); + drop_type!(m, DiscountRuleAllocation); + + Ok(()) + } +} + +impl Migration { + /// ```sql + /// CREATE TABLE discounts + /// ( + /// id uuid NOT NULL, + /// code character varying NOT NULL, + /// is_dynamic boolean NOT NULL, + /// rule_id uuid, + /// is_disabled boolean NOT NULL, + /// parent_discount_id uuid, + /// starts_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL, + /// ends_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, + /// usage_limit integer, + /// usage_count integer DEFAULT 0 NOT NULL, + /// valid_duration character varying + /// ); + /// ``` + async fn create_discounts(m: &SchemaManager<'_>) -> Result<(), DbErr> { + 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()) + .col( + ColumnDef::new(Discount::StartsAt) + .timestamp() + .default(SimpleExpr::Custom("CURRENT_TIMESTAMP".into())) + .not_null(), + ) + .col(ColumnDef::new(Discount::EndsAt).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()) + .to_owned(), + ) + .await?; + Ok(()) + } + + /// ```sql + /// CREATE TABLE discount_conditions + /// ( + /// id uuid NOT NULL, + /// type public.discount_condition_types NOT NULL, + /// operator public.discount_condition_operators NOT NULL, + /// discount_rule_id uuid 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_discount_conditions(m: &SchemaManager<'_>) -> Result<(), DbErr> { + 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( + 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()) + .to_owned(), + ) + .await?; + Ok(()) + } + + /// ```sql + /// CREATE TABLE discount_condition_customer_groups + /// ( + /// customer_group_id uuid NOT NULL, + /// condition_id uuid NOT NULL, + /// created_at timestamp with time zone DEFAULT now() NOT NULL, + /// updated_at timestamp with time zone DEFAULT now() NOT NULL, + /// metadata jsonb + /// ); + /// ``` + async fn create_discount_condition_customer_groups(m: &SchemaManager<'_>) -> Result<(), DbErr> { + 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()) + .to_owned(), + ) + .await?; + Ok(()) + } + + /// ```sql + /// CREATE TABLE discount_condition_products + /// ( + /// product_id uuid NOT NULL, + /// condition_id uuid NOT NULL, + /// created_at timestamp with time zone DEFAULT now() NOT NULL, + /// updated_at timestamp with time zone DEFAULT now() NOT NULL, + /// metadata jsonb + /// ); + /// ``` + async fn create_discount_condition_products(m: &SchemaManager<'_>) -> Result<(), DbErr> { + 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()) + .to_owned(), + ) + .await?; + Ok(()) + } + + /// ```sql + /// CREATE TABLE discount_condition_product_collections + /// ( + /// product_collection_id uuid NOT NULL, + /// condition_id uuid NOT NULL, + /// created_at timestamp with time zone DEFAULT now() NOT NULL, + /// updated_at timestamp with time zone DEFAULT now() NOT NULL, + /// metadata jsonb + /// ); + /// ``` + async fn create_discount_condition_product_collections( + m: &SchemaManager<'_>, + ) -> Result<(), DbErr> { + m.create_table( + Table::create() + .table(DiscountConditionProductCollection::DiscountConditionProductCollections) + .col( + ColumnDef::new(DiscountConditionProductCollection::ProductCollectionId) + .uuid() + .not_null(), + ) + .col( + ColumnDef::new(DiscountConditionProductCollection::ConditionId) + .uuid() + .not_null(), + ) + .col(ts_def_now_not_null!( + DiscountConditionProductCollection::CreatedAt + )) + .col(ts_def_now_not_null!( + DiscountConditionProductCollection::UpdatedAt + )) + .col(ColumnDef::new(DiscountConditionProductCollection::Metadata).json_binary()) + .to_owned(), + ) + .await?; + Ok(()) + } + + /// ```sql + /// CREATE TABLE discount_condition_product_tags + /// ( + /// product_tag_id uuid NOT NULL, + /// condition_id uuid NOT NULL, + /// created_at timestamp with time zone DEFAULT now() NOT NULL, + /// updated_at timestamp with time zone DEFAULT now() NOT NULL, + /// metadata jsonb + /// ); + /// ``` + async fn create_discount_condition_product_tags(m: &SchemaManager<'_>) -> Result<(), DbErr> { + m.create_table( + Table::create() + .table(DiscountConditionProductTag::DiscountConditionProductTags) + .col( + ColumnDef::new(DiscountConditionProductTag::ProductTagId) + .uuid() + .not_null(), + ) + .col( + ColumnDef::new(DiscountConditionProductTag::ConditionId) + .uuid() + .not_null(), + ) + .col(ts_def_now_not_null!(DiscountConditionProductTag::CreatedAt)) + .col(ts_def_now_not_null!(DiscountConditionProductTag::UpdatedAt)) + .col(ColumnDef::new(DiscountConditionProductTag::Metadata).json_binary()) + .to_owned(), + ) + .await?; + Ok(()) + } + + /// ```sql + /// CREATE TABLE discount_condition_product_types + /// ( + /// product_type_id uuid NOT NULL, + /// condition_id uuid NOT NULL, + /// created_at timestamp with time zone DEFAULT now() NOT NULL, + /// updated_at timestamp with time zone DEFAULT now() NOT NULL, + /// metadata jsonb + /// ); + /// ``` + async fn create_discount_condition_product_types(m: &SchemaManager<'_>) -> Result<(), DbErr> { + m.create_table( + Table::create() + .table(DiscountConditionProductType::DiscountConditionProductTypes) + .col( + ColumnDef::new(DiscountConditionProductType::ProductTypeId) + .uuid() + .not_null(), + ) + .col( + ColumnDef::new(DiscountConditionProductType::ConditionId) + .uuid() + .not_null(), + ) + .col(ts_def_now_not_null!( + DiscountConditionProductType::CreatedAt + )) + .col(ts_def_now_not_null!( + DiscountConditionProductType::UpdatedAt + )) + .col(ColumnDef::new(DiscountConditionProductType::Metadata).json_binary()) + .to_owned(), + ) + .await?; + Ok(()) + } + + /// ```sql + /// CREATE TABLE discount_regions + /// ( + /// discount_id uuid NOT NULL, + /// region_id uuid NOT NULL + /// ); + /// ``` + async fn create_discount_regions(m: &SchemaManager<'_>) -> Result<(), DbErr> { + m.create_table( + Table::create() + .table(DiscountRegion::DiscountRegions) + .col(ColumnDef::new(DiscountRegion::DiscountId).uuid().not_null()) + .col(ColumnDef::new(DiscountRegion::RegionId).uuid().not_null()) + .to_owned(), + ) + .await?; + Ok(()) + } + + /// ```sql + /// CREATE TABLE discount_rules + /// ( + /// id uuid NOT NULL, + /// description character varying, + /// type public.discount_rule_types NOT NULL, + /// value integer NOT NULL, + /// allocation public.discount_rule_allocations, + /// 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_discount_rules(m: &SchemaManager<'_>) -> Result<(), DbErr> { + m.create_table( + Table::create() + .table(DiscountRule::DiscountRules) + .col(auto_uuid_not_null!(DiscountRule::Id)) + .col(ColumnDef::new(DiscountRule::Description).string()) + .col( + ColumnDef::new(DiscountRule::DiscountRuleType) + .enumeration( + crate::types::DiscountRuleType::DiscountRuleTypes, + crate::types::DiscountRuleType::iter().skip(1), + ) + .not_null(), + ) + .col(ColumnDef::new(DiscountRule::Value).integer().not_null()) + .col(ColumnDef::new(DiscountRule::Allocation).enumeration( + crate::types::DiscountRuleAllocation::DiscountRuleAllocations, + crate::types::DiscountRuleAllocation::iter().skip(1), + )) + .col(ts_def_now_not_null!(DiscountRule::CreatedAt)) + .col(ts_def_now_not_null!(DiscountRule::UpdatedAt)) + .col(ColumnDef::new(DiscountRule::DeletedAt).timestamp()) + .col(ColumnDef::new(DiscountRule::Metadata).json_binary()) + .to_owned(), + ) + .await?; + Ok(()) + } + + /// ```sql + /// CREATE TABLE discount_rule_products + /// ( + /// discount_rule_id uuid NOT NULL, + /// product_id uuid NOT NULL + /// ); + /// ``` + async fn create_discount_rule_products(m: &SchemaManager<'_>) -> Result<(), DbErr> { + m.create_table( + Table::create() + .table(DiscountRuleProduct::DiscountRuleProducts) + .col( + ColumnDef::new(DiscountRuleProduct::DiscountRuleId) + .uuid() + .not_null(), + ) + .col( + ColumnDef::new(DiscountRuleProduct::ProductId) + .uuid() + .not_null(), + ) + .to_owned(), + ) + .await?; + Ok(()) + } +} + +#[derive(Iden)] +pub enum Discount { + Discounts, + Id, + Code, + IsDynamic, + RuleId, + IsDisabled, + ParentDiscountId, + StartsAt, + EndsAt, + CreatedAt, + UpdatedAt, + DeletedAt, + Metadata, + UsageLimit, + UsageCount, + ValidDuration, +} + +#[derive(Iden)] +pub enum DiscountCondition { + DiscountConditions, + Id, + DiscountConditionType, + Operator, + DiscountRuleId, + CreatedAt, + UpdatedAt, + DeletedAt, + Metadata, +} + +#[derive(Iden)] +pub enum DiscountConditionCustomerGroup { + DiscountConditionCustomerGroups, + CustomerGroupId, + ConditionId, + CreatedAt, + UpdatedAt, + Metadata, +} + +#[derive(Iden)] +pub enum DiscountConditionProduct { + DiscountConditionProducts, + ProductId, + ConditionId, + CreatedAt, + UpdatedAt, + Metadata, +} + +#[derive(Iden)] +pub enum DiscountConditionProductCollection { + DiscountConditionProductCollections, + ProductCollectionId, + ConditionId, + CreatedAt, + UpdatedAt, + Metadata, +} + +#[derive(Iden)] +pub enum DiscountConditionProductTag { + DiscountConditionProductTags, + ProductTagId, + ConditionId, + CreatedAt, + UpdatedAt, + Metadata, +} + +#[derive(Iden)] +pub enum DiscountConditionProductType { + DiscountConditionProductTypes, + ProductTypeId, + ConditionId, + CreatedAt, + UpdatedAt, + Metadata, +} + +#[derive(Iden)] +pub enum DiscountRegion { + DiscountRegions, + DiscountId, + RegionId, +} + +#[derive(Iden)] +pub enum DiscountRule { + DiscountRules, + Id, + Description, + DiscountRuleType, + Value, + Allocation, + CreatedAt, + UpdatedAt, + DeletedAt, + Metadata, +} + +#[derive(Iden)] +pub enum DiscountRuleProduct { + DiscountRuleProducts, + DiscountRuleId, + ProductId, +} diff --git a/migration/src/discounts/mod.rs b/migration/src/discounts/mod.rs new file mode 100644 index 0000000..06f29cb --- /dev/null +++ b/migration/src/discounts/mod.rs @@ -0,0 +1,12 @@ +mod m20230603_120814_discounts; + +use sea_orm_migration::{MigrationTrait, MigratorTrait}; + +pub struct DiscountsMigrator; + +#[async_trait::async_trait] +impl MigratorTrait for DiscountsMigrator { + fn migrations() -> Vec> { + vec![Box::new(m20230603_120814_discounts::Migration)] + } +} diff --git a/migration/src/lib.rs b/migration/src/lib.rs index b03a3e6..edc5462 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -1,10 +1,19 @@ pub use sea_orm_migration::prelude::*; pub mod carts; +pub use carts::*; +pub mod discounts; +pub use discounts::*; pub mod jobs; +pub use jobs::*; pub mod public; +pub use public::*; pub mod schema_list; +pub use schema_list::*; +pub mod checkouts; +pub use checkouts::*; pub mod types; +pub use types::*; #[macro_export] macro_rules! ts_def_now_not_null { @@ -22,7 +31,35 @@ macro_rules! auto_uuid_not_null { ColumnDef::new($identifier) .uuid() .not_null() - .default(SimpleExpr::Custom("uuid_generate_v4()".into())) + .default(SimpleExpr::Custom("public.uuid_generate_v4()".into())) .primary_key() }; } + +// #[macro_export] +// macro_rules! col { +// ($name: expr $ty: expr) => { +// ColumnDef::new($name).$ty() +// }; +// ($name: expr auto uuid) => { +// $crate::auto_uuid_not_null!($name) +// }; +// ($name: expr $ty: expr default $value: expr) => { +// $crate::col!($name $ty).default($value) +// }; +// } + +#[async_trait::async_trait] +pub trait DropTable { + async fn drop_table( + &self, + manager: &SchemaManager<'_>, + identifier: I, + ) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(identifier).to_owned()) + .await + } +} + +impl DropTable for T {} diff --git a/migration/src/main.rs b/migration/src/main.rs index f98653f..f5be3ba 100644 --- a/migration/src/main.rs +++ b/migration/src/main.rs @@ -1,3 +1,5 @@ +#![feature(concat_idents)] + use std::error::Error; use std::fmt::Display; use std::process::exit; @@ -13,6 +15,17 @@ use tracing_subscriber::EnvFilter; // const MIGRATION_DIR: &str = "./"; +macro_rules! migrate_schema { + ($cli: expr, $schema_name: ident, $migrator_name: ident) => {{ + run_cli( + &mut $cli, + PostgreSQLSchema::$schema_name, + migration::$migrator_name, + ) + .await; + }}; +} + #[async_std::main] async fn main() { dotenv().ok(); @@ -20,24 +33,10 @@ async fn main() { init_logger(cli.verbose); - run_cli( - &mut cli, - PostgreSQLSchema::Public, - migration::public::PublicMigrator, - ) - .await; - run_cli( - &mut cli, - PostgreSQLSchema::Jobs, - migration::jobs::JobsMigrator, - ) - .await; - run_cli( - &mut cli, - PostgreSQLSchema::Carts, - migration::carts::CartsMigrator, - ) - .await; + migrate_schema!(cli, Public, PublicMigrator); + migrate_schema!(cli, Jobs, JobsMigrator); + migrate_schema!(cli, Carts, CartsMigrator); + migrate_schema!(cli, Discounts, DiscountsMigrator); } pub async fn run_cli(cli: &mut Cli, schema: PostgreSQLSchema, migrator: M) @@ -57,6 +56,10 @@ where .await .expect("Fail to acquire database connection"); + db.execute_unprepared(&format!("CREATE SCHEMA {}", schema)) + .await + .ok(); + db.execute_unprepared(&format!("SET search_path = '{}'", schema)) .await .unwrap(); diff --git a/migration/src/public/m20230603_102630_schema.rs b/migration/src/public/m20230603_102630_schema.rs deleted file mode 100644 index f51d3b4..0000000 --- a/migration/src/public/m20230603_102630_schema.rs +++ /dev/null @@ -1,37 +0,0 @@ -use sea_orm::Iterable; -use sea_orm_migration::prelude::*; -use sea_orm_migration::sea_orm::{DatabaseBackend, Statement}; - -use crate::schema_list::PostgreSQLSchema; - -#[derive(DeriveMigrationName)] -pub struct Migration; - -#[async_trait::async_trait] -impl MigrationTrait for Migration { - async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { - for schema in PostgreSQLSchema::iter().skip(1) { - manager - .get_connection() - .execute(Statement::from_string( - DatabaseBackend::Postgres, - format!("CREATE SCHEMA {}", schema.as_str()), - )) - .await?; - } - Ok(()) - } - - async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { - for schema in PostgreSQLSchema::iter().skip(1) { - manager - .get_connection() - .execute(Statement::from_string( - DatabaseBackend::Postgres, - format!("DROP SCHEMA {}", schema.as_str()), - )) - .await?; - } - Ok(()) - } -} diff --git a/migration/src/public/m20230603_102634_types.rs b/migration/src/public/m20230603_102634_types.rs index 1e1a6a3..9bf6a91 100644 --- a/migration/src/public/m20230603_102634_types.rs +++ b/migration/src/public/m20230603_102634_types.rs @@ -1,8 +1,6 @@ use sea_orm::Iterable; use sea_orm_migration::prelude::*; -use crate::extension::postgres::Type; -use crate::types::*; use crate::{create_type, drop_type}; #[derive(DeriveMigrationName)] @@ -18,10 +16,6 @@ impl MigrationTrait for Migration { create_type!(manager, ClaimOrderFulfillmentStatus); create_type!(manager, ClaimOrderPaymentStatus); create_type!(manager, ClaimOrderType); - create_type!(manager, DiscountConditionOperator); - create_type!(manager, DiscountConditionType); - create_type!(manager, DiscountRuleAllocation); - create_type!(manager, DiscountRuleType); create_type!(manager, DraftOrderStatus); create_type!(manager, InviteRole); create_type!(manager, OrderFulfillmentStatus); @@ -50,10 +44,6 @@ impl MigrationTrait for Migration { drop_type!(manager, ClaimOrderFulfillmentStatus); drop_type!(manager, ClaimOrderPaymentStatus); drop_type!(manager, ClaimOrderType); - drop_type!(manager, DiscountConditionOperator); - drop_type!(manager, DiscountConditionType); - drop_type!(manager, DiscountRuleAllocation); - drop_type!(manager, DiscountRuleType); drop_type!(manager, DraftOrderStatus); drop_type!(manager, InviteRole); drop_type!(manager, OrderFulfillmentStatus); diff --git a/migration/src/public/m20230603_120816_geolocation.rs b/migration/src/public/m20230603_120816_geolocation.rs index 383ad04..6eef285 100644 --- a/migration/src/public/m20230603_120816_geolocation.rs +++ b/migration/src/public/m20230603_120816_geolocation.rs @@ -1,22 +1,27 @@ use sea_orm_migration::prelude::*; -use sea_orm_migration::sea_orm::Iterable; use sea_query::expr::SimpleExpr; -use crate::types::*; use crate::{auto_uuid_not_null, ts_def_now_not_null}; -/// ```sql -/// ``` #[derive(DeriveMigrationName)] pub struct Migration; #[async_trait::async_trait] impl MigrationTrait for Migration { async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + Self::create_countries(manager).await?; + Self::create_currencies(manager).await?; + Self::create_regions(manager).await?; Ok(()) } - async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> { + m.drop_table(Table::drop().table(Country::Countries).to_owned()) + .await?; + m.drop_table(Table::drop().table(Currency::Currencies).to_owned()) + .await?; + m.drop_table(Table::drop().table(Currency::Currencies).to_owned()) + .await?; Ok(()) } } @@ -34,8 +39,35 @@ impl Migration { /// region_id character varying /// ); /// ``` - async fn create_countries(manager: &SchemaManager<'_>) -> Result<(), DbErr> { - todo!() + async fn create_countries(m: &SchemaManager<'_>) -> Result<(), DbErr> { + m.create_table( + Table::create() + .table(Country::Countries) + .col(auto_uuid_not_null!(Country::Id)) + .col(ColumnDef::new(Country::Iso2).char_len(2).not_null()) + .col(ColumnDef::new(Country::Iso3).char_len(3).not_null()) + .col(ColumnDef::new(Country::NumCode).integer().not_null()) + .col(ColumnDef::new(Country::Name).string().not_null()) + .col(ColumnDef::new(Country::DisplayName).string().not_null()) + .col(ColumnDef::new(Country::RegionId).uuid()) + .to_owned(), + ) + .await?; + m.create_index( + IndexCreateStatement::new() + .table(Country::Countries) + .col(Country::Iso2) + .to_owned(), + ) + .await?; + m.create_index( + IndexCreateStatement::new() + .table(Country::Countries) + .col(Country::Iso3) + .to_owned(), + ) + .await?; + Ok(()) } /// ```sql @@ -47,8 +79,25 @@ impl Migration { /// name character varying NOT NULL /// ); /// ``` - async fn create_currencies(manager: &SchemaManager<'_>) -> Result<(), DbErr> { - todo!() + async fn create_currencies(m: &SchemaManager<'_>) -> Result<(), DbErr> { + m.create_table( + Table::create() + .table(Currency::Currencies) + .col(ColumnDef::new(Currency::Code).string().not_null()) + .col(ColumnDef::new(Currency::Symbol).string().not_null()) + .col(ColumnDef::new(Currency::SymbolNative).string().not_null()) + .col(ColumnDef::new(Currency::Name).string().not_null()) + .to_owned(), + ) + .await?; + m.create_index( + IndexCreateStatement::new() + .table(Currency::Currencies) + .col(Currency::Code) + .to_owned(), + ) + .await?; + Ok(()) } /// CREATE TABLE public.regions @@ -66,19 +115,73 @@ impl Migration { /// automatic_taxes boolean DEFAULT true NOT NULL, /// tax_provider_id uuid /// ); - async fn create_regions(manager: &SchemaManager<'_>) -> Result<(), DbErr> { - todo!() + async fn create_regions(m: &SchemaManager<'_>) -> Result<(), DbErr> { + m.create_table( + Table::create() + .table(Region::Regions) + .col(auto_uuid_not_null!(Region::Id)) + .col(ColumnDef::new(Region::Name).string().not_null()) + .col(ColumnDef::new(Region::CurrencyCode).string().not_null()) + .col(ColumnDef::new(Region::TaxRate).double().not_null()) + .col(ColumnDef::new(Region::TaxCode).string().not_null()) + .col(ts_def_now_not_null!(Region::CreatedAt)) + .col(ts_def_now_not_null!(Region::UpdatedAt)) + .col(ColumnDef::new(Region::DeletedAt).timestamp()) + .col(ColumnDef::new(Region::Metadata).json_binary()) + .col( + ColumnDef::new(Region::GiftCardsTaxable) + .boolean() + .default(true) + .not_null(), + ) + .col( + ColumnDef::new(Region::AutomaticTaxes) + .boolean() + .default(true) + .not_null(), + ) + .col(ColumnDef::new(Region::TaxProviderId).uuid().not_null()) + .to_owned(), + ) + .await?; + Ok(()) } } #[derive(Iden)] -pub enum ClaimImage { - ClaimImages, +pub enum Country { + Countries, Id, - ClaimItemId, - Url, + Iso2, + Iso3, + NumCode, + Name, + DisplayName, + RegionId, +} + +#[derive(Iden)] +pub enum Currency { + Currencies, + Code, + Symbol, + SymbolNative, + Name, +} + +#[derive(Iden)] +pub enum Region { + Regions, + Id, + Name, + CurrencyCode, + TaxRate, + TaxCode, CreatedAt, UpdatedAt, DeletedAt, Metadata, + GiftCardsTaxable, + AutomaticTaxes, + TaxProviderId, } diff --git a/migration/src/public/mod.rs b/migration/src/public/mod.rs index bcaeec9..e910713 100644 --- a/migration/src/public/mod.rs +++ b/migration/src/public/mod.rs @@ -1,6 +1,5 @@ use sea_orm_migration::{MigrationTrait, MigratorTrait}; -mod m20230603_102630_schema; mod m20230603_102634_types; mod m20230603_120814_addresses; mod m20230603_120815_claims; @@ -11,9 +10,7 @@ pub struct PublicMigrator; #[async_trait::async_trait] impl MigratorTrait for PublicMigrator { fn migrations() -> Vec> { - eprintln!("PublicMigrator"); vec![ - Box::new(m20230603_102630_schema::Migration), Box::new(m20230603_102634_types::Migration), Box::new(m20230603_120814_addresses::Migration), Box::new(m20230603_120815_claims::Migration), diff --git a/migration/src/schema_list.rs b/migration/src/schema_list.rs index 4a7506e..d75f1ee 100644 --- a/migration/src/schema_list.rs +++ b/migration/src/schema_list.rs @@ -7,6 +7,7 @@ pub enum PostgreSQLSchema { Public, Jobs, Carts, + Discounts, } impl PostgreSQLSchema { @@ -15,6 +16,7 @@ impl PostgreSQLSchema { PostgreSQLSchema::Public => "public", PostgreSQLSchema::Jobs => "jobs", PostgreSQLSchema::Carts => "carts", + PostgreSQLSchema::Discounts => "discounts", } } } diff --git a/migration/src/types.rs b/migration/src/types.rs index 2f721f2..1e99630 100644 --- a/migration/src/types.rs +++ b/migration/src/types.rs @@ -3,7 +3,10 @@ use sea_orm_migration::prelude::*; #[macro_export] macro_rules! create_type { - ($manager: ident, $ty: ident) => { + ($manager: ident, $ty: ident) => {{ + use crate::extension::postgres::Type; + use crate::types::*; + $manager .create_type( Type::create() @@ -12,11 +15,14 @@ macro_rules! create_type { .to_owned(), ) .await?; - }; + }}; } #[macro_export] macro_rules! drop_type { - ($manager: ident, $ty: ident) => { + ($manager: ident, $ty: ident) => {{ + use crate::extension::postgres::Type; + use crate::types::*; + $manager .drop_type( Type::drop() @@ -26,7 +32,7 @@ macro_rules! drop_type { .to_owned(), ) .await?; - }; + }}; } #[derive(Iden, EnumIter)] diff --git a/migrations/20230603073510_init.sql b/migrations/20230603073510_init.sql index 1994a5a..a546a44 100644 --- a/migrations/20230603073510_init.sql +++ b/migrations/20230603073510_init.sql @@ -347,15 +347,6 @@ CREATE TABLE public.claim_tags metadata jsonb ); ----- ########################################################### ----- ########################################################### ----- ########################################################### ----- ########################################################### ----- ########################################################### ----- ########################################################### ----- ########################################################### ----- ########################################################### - CREATE TABLE public.regions ( @@ -392,50 +383,6 @@ CREATE TABLE public.currencies name character varying NOT NULL ); -CREATE TABLE public.custom_shipping_options -( - id uuid NOT NULL, - price integer NOT NULL, - shipping_option_id uuid NOT NULL, - cart_id uuid, - 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 -); - -CREATE TABLE public.customers -( - id uuid NOT NULL, - email character varying NOT NULL, - first_name character varying, - last_name character varying, - billing_address_id uuid, - password_hash character varying, - phone character varying, - has_account boolean DEFAULT false 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 -); - -CREATE TABLE public.customer_groups -( - id uuid NOT NULL, - name 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 -); - -CREATE TABLE public.customer_group_customers -( - customer_group_id uuid NOT NULL, - customer_id uuid NOT NULL -); - CREATE TABLE public.discounts ( id uuid NOT NULL, @@ -537,6 +484,205 @@ CREATE TABLE public.discount_rule_products product_id uuid NOT NULL ); +---- ########################################################### +---- ########################################################### +---- ########################################################### +---- ########################################################### +---- ########################################################### +---- ########################################################### +---- ########################################################### +---- ########################################################### + + +CREATE TABLE public.orders +( + id uuid NOT NULL, + status public.order_statuses DEFAULT 'pending'::public.order_statuses NOT NULL, + fulfillment_status public.order_fulfillment_statuses DEFAULT 'not_fulfilled'::public.order_fulfillment_statuses NOT NULL, + payment_status public.order_payment_statuses DEFAULT 'not_paid'::public.order_payment_statuses NOT NULL, + display_id integer NOT NULL, + cart_id uuid, + customer_id uuid NOT NULL, + email character varying NOT NULL, + billing_address_id uuid, + shipping_address_id uuid, + region_id uuid NOT NULL, + currency_code character varying NOT NULL, + tax_rate real, + 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, + metadata jsonb, + idempotency_key character varying, + draft_order_id uuid, + no_notification boolean, + external_id uuid, + sales_channel_id uuid +); + +CREATE TABLE public.order_discounts +( + order_id uuid NOT NULL, + discount_id uuid NOT NULL +); + +CREATE TABLE public.order_edits +( + id uuid NOT NULL, + created_at timestamp with time zone DEFAULT now() NOT NULL, + updated_at timestamp with time zone DEFAULT now() NOT NULL, + order_id uuid NOT NULL, + internal_note character varying, + created_by character varying NOT NULL, + requested_by character varying, + requested_at timestamp with time zone, + confirmed_by character varying, + confirmed_at timestamp with time zone, + declined_by character varying, + declined_reason character varying, + declined_at timestamp with time zone, + canceled_by character varying, + canceled_at timestamp with time zone, + payment_collection_id uuid +); + +CREATE TABLE public.order_gift_cards +( + order_id uuid NOT NULL, + gift_card_id uuid NOT NULL +); + +CREATE TABLE public.order_item_changes +( + id uuid 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, + type public.order_item_change_types NOT NULL, + order_edit_id uuid NOT NULL, + original_line_item_id uuid, + line_item_id uuid +); + +CREATE TABLE public.payments +( + id uuid NOT NULL, + swap_id uuid, + cart_id uuid, + order_id uuid, + amount integer NOT NULL, + currency_code character varying NOT NULL, + amount_refunded integer DEFAULT 0 NOT NULL, + provider_id uuid NOT NULL, + data jsonb NOT NULL, + captured_at timestamp with time zone, + 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, + metadata jsonb, + idempotency_key character varying +); + +CREATE TABLE public.payment_collections +( + id uuid 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, + type public.payment_collection_types NOT NULL, + status public.payment_collection_statuses NOT NULL, + description text, + amount integer NOT NULL, + authorized_amount integer, + region_id uuid NOT NULL, + currency_code character varying NOT NULL, + metadata jsonb, + created_by character varying NOT NULL +); + +CREATE TABLE public.payment_collection_payments +( + payment_collection_id uuid NOT NULL, + payment_id uuid NOT NULL +); + +CREATE TABLE public.payment_collection_sessions +( + payment_collection_id uuid NOT NULL, + payment_session_id uuid NOT NULL +); + +CREATE TABLE public.payment_providers +( + id uuid NOT NULL, + is_installed boolean DEFAULT true NOT NULL +); + +CREATE TABLE public.payment_sessions +( + id uuid NOT NULL, + cart_id uuid, + provider_id uuid NOT NULL, + is_selected boolean, + status public.payment_session_statuses NOT NULL, + data jsonb NOT NULL, + created_at timestamp with time zone DEFAULT now() NOT NULL, + updated_at timestamp with time zone DEFAULT now() NOT NULL, + idempotency_key character varying, + payment_authorized_at timestamp with time zone, + amount integer, + is_initiated boolean DEFAULT false NOT NULL +); + +--------- +--------- +--------- +--------- + +CREATE TABLE public.customers +( + id uuid NOT NULL, + email character varying NOT NULL, + first_name character varying, + last_name character varying, + billing_address_id uuid, + password_hash character varying, + phone character varying, + has_account boolean DEFAULT false 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 +); + +CREATE TABLE public.customer_groups +( + id uuid NOT NULL, + name 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 +); + +CREATE TABLE public.customer_group_customers +( + customer_group_id uuid NOT NULL, + customer_id uuid NOT NULL +); + +CREATE TABLE public.custom_shipping_options +( + id uuid NOT NULL, + price integer NOT NULL, + shipping_option_id uuid NOT NULL, + cart_id uuid, + 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 +); + CREATE TABLE public.draft_orders ( id uuid NOT NULL, @@ -771,146 +917,6 @@ CREATE TABLE public.oauth data jsonb ); -CREATE TABLE public.orders -( - id uuid NOT NULL, - status public.order_statuses DEFAULT 'pending'::public.order_statuses NOT NULL, - fulfillment_status public.order_fulfillment_statuses DEFAULT 'not_fulfilled'::public.order_fulfillment_statuses NOT NULL, - payment_status public.order_payment_statuses DEFAULT 'not_paid'::public.order_payment_statuses NOT NULL, - display_id integer NOT NULL, - cart_id uuid, - customer_id uuid NOT NULL, - email character varying NOT NULL, - billing_address_id uuid, - shipping_address_id uuid, - region_id uuid NOT NULL, - currency_code character varying NOT NULL, - tax_rate real, - 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, - metadata jsonb, - idempotency_key character varying, - draft_order_id uuid, - no_notification boolean, - external_id uuid, - sales_channel_id uuid -); - -CREATE TABLE public.order_discounts -( - order_id uuid NOT NULL, - discount_id uuid NOT NULL -); - -CREATE TABLE public.order_edits -( - id uuid NOT NULL, - created_at timestamp with time zone DEFAULT now() NOT NULL, - updated_at timestamp with time zone DEFAULT now() NOT NULL, - order_id uuid NOT NULL, - internal_note character varying, - created_by character varying NOT NULL, - requested_by character varying, - requested_at timestamp with time zone, - confirmed_by character varying, - confirmed_at timestamp with time zone, - declined_by character varying, - declined_reason character varying, - declined_at timestamp with time zone, - canceled_by character varying, - canceled_at timestamp with time zone, - payment_collection_id uuid -); - -CREATE TABLE public.order_gift_cards -( - order_id uuid NOT NULL, - gift_card_id uuid NOT NULL -); - -CREATE TABLE public.order_item_changes -( - id uuid 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, - type public.order_item_change_types NOT NULL, - order_edit_id uuid NOT NULL, - original_line_item_id uuid, - line_item_id uuid -); - -CREATE TABLE public.payments -( - id uuid NOT NULL, - swap_id uuid, - cart_id uuid, - order_id uuid, - amount integer NOT NULL, - currency_code character varying NOT NULL, - amount_refunded integer DEFAULT 0 NOT NULL, - provider_id uuid NOT NULL, - data jsonb NOT NULL, - captured_at timestamp with time zone, - 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, - metadata jsonb, - idempotency_key character varying -); - -CREATE TABLE public.payment_collections -( - id uuid 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, - type public.payment_collection_types NOT NULL, - status public.payment_collection_statuses NOT NULL, - description text, - amount integer NOT NULL, - authorized_amount integer, - region_id uuid NOT NULL, - currency_code character varying NOT NULL, - metadata jsonb, - created_by character varying NOT NULL -); - -CREATE TABLE public.payment_collection_payments -( - payment_collection_id uuid NOT NULL, - payment_id uuid NOT NULL -); - -CREATE TABLE public.payment_collection_sessions -( - payment_collection_id uuid NOT NULL, - payment_session_id uuid NOT NULL -); - -CREATE TABLE public.payment_providers -( - id uuid NOT NULL, - is_installed boolean DEFAULT true NOT NULL -); - -CREATE TABLE public.payment_sessions -( - id uuid NOT NULL, - cart_id uuid, - provider_id uuid NOT NULL, - is_selected boolean, - status public.payment_session_statuses NOT NULL, - data jsonb NOT NULL, - created_at timestamp with time zone DEFAULT now() NOT NULL, - updated_at timestamp with time zone DEFAULT now() NOT NULL, - idempotency_key character varying, - payment_authorized_at timestamp with time zone, - amount integer, - is_initiated boolean DEFAULT false NOT NULL -); - CREATE TABLE public.price_lists ( id uuid NOT NULL,