From a60246c95e3ba18a89b78341f43b875b7ddb6b71 Mon Sep 17 00:00:00 2001 From: eraden Date: Mon, 19 Jun 2023 17:09:53 +0200 Subject: [PATCH] Add most tables --- .../src/checkouts/m20230603_120800_carts.rs | 159 +++++++ migration/src/checkouts/mod.rs | 2 + migration/src/lib.rs | 9 +- migration/src/main.rs | 3 +- .../marketing/m20230603_120810_marketing.rs | 114 +++++ .../m20230603_120814_discounts.rs | 0 .../m20230603_120815_gift_carts.rs | 1 + migration/src/{discounts => marketing}/mod.rs | 7 +- .../src/oauth2/m20230603_120810_oauth2.rs | 152 +++++++ migration/src/oauth2/mod.rs | 12 + migration/src/public/mod.rs | 2 - migration/src/schema_list.rs | 6 +- migration/src/sea_ext/mod.rs | 62 ++- .../m20230603_120810_geolocation.rs} | 8 +- .../src/stores/m20230603_120810_store_ext.rs | 244 +++++++++++ migration/src/stores/mod.rs | 16 + migrations/20230603073510_init.sql | 405 ------------------ migrations/20230603073520_customers.sql | 31 -- migrations/20230603073520_identity.sql | 76 ++++ migrations/20230603073530_shippings.sql | 67 ++- migrations/20230603073532_checkouts.sql | 40 ++ migrations/20230603073532_marketing.sql | 150 +++++++ migrations/20230603073534_open_api.sql | 26 ++ migrations/20230603073535_stores.sql | 85 ++++ ...oducts.sql => 20230609171700_products.sql} | 0 25 files changed, 1221 insertions(+), 456 deletions(-) create mode 100644 migration/src/checkouts/m20230603_120800_carts.rs create mode 100644 migration/src/marketing/m20230603_120810_marketing.rs rename migration/src/{discounts => marketing}/m20230603_120814_discounts.rs (100%) rename migration/src/{discounts => marketing}/m20230603_120815_gift_carts.rs (99%) rename migration/src/{discounts => marketing}/mod.rs (67%) create mode 100644 migration/src/oauth2/m20230603_120810_oauth2.rs create mode 100644 migration/src/oauth2/mod.rs rename migration/src/{public/m20230603_120816_geolocation.rs => stores/m20230603_120810_geolocation.rs} (96%) create mode 100644 migration/src/stores/m20230603_120810_store_ext.rs create mode 100644 migration/src/stores/mod.rs delete mode 100644 migrations/20230603073520_customers.sql create mode 100644 migrations/20230603073520_identity.sql create mode 100644 migrations/20230603073532_checkouts.sql create mode 100644 migrations/20230603073532_marketing.sql create mode 100644 migrations/20230603073534_open_api.sql create mode 100644 migrations/20230603073535_stores.sql rename migrations/{202306091717_products.sql => 20230609171700_products.sql} (100%) diff --git a/migration/src/checkouts/m20230603_120800_carts.rs b/migration/src/checkouts/m20230603_120800_carts.rs new file mode 100644 index 0000000..4233682 --- /dev/null +++ b/migration/src/checkouts/m20230603_120800_carts.rs @@ -0,0 +1,159 @@ +use sea_orm_migration::prelude::*; + +use crate::sea_orm::Iterable; +use crate::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> { + create_type!(m, CartType); + + self.create_carts(m).await?; + self.create_cart_discounts(m).await?; + self.create_cart_gift_cards(m).await?; + + Ok(()) + } + + async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> { + self.drop_table(m, Cart::Carts).await?; + self.drop_table(m, CartDiscount::CartDiscounts).await?; + self.drop_table(m, CartGiftCard::CartGiftCards).await?; + + drop_type!(m, CartType); + + Ok(()) + } +} + +impl Migration { + /// ```sql + /// CREATE TABLE carts + /// ( + /// id uuid NOT NULL, + /// email character varying, + /// billing_address_id uuid, + /// shipping_address_id uuid, + /// region_id uuid NOT NULL, + /// customer_id uuid, + /// payment_id uuid, + /// type cart_types DEFAULT 'default'::cart_types NOT NULL, + /// completed_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, + /// context jsonb, + /// payment_authorized_at timestamp with time zone, + /// sales_channel_id uuid + /// ); + /// ``` + async fn create_carts(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { + use Cart::*; + + m.build_table( + Carts, + &mut [ + Id.col().uuid().not_null().primary_key().default_gen_uuid(), + Email.col().string(), + BillingAddressId.col().uuid(), + ShippingAddressId.col().uuid(), + RegionId.col().uuid().not_null(), + CustomerId.col().uuid(), + PaymentId.col().uuid(), + CartType + .col() + .not_null() + .default(crate::types::CartType::Default.to_string()) + .enumeration( + crate::types::CartType::CartTypes, + crate::types::CartType::iter().skip(1), + ), + CompletedAt.col().timestamp(), + CreatedAt.col().timestamp().not_null().default_now(), + UpdatedAt.col().timestamp().not_null().default_now(), + DeletedAt.col().timestamp(), + Metadata.col().json_binary(), + IdempotencyKey.col().uuid(), + Context.col().json_binary(), + PaymentAuthorizedAt.col().timestamp(), + SalesChannelId.col().uuid(), + ], + ) + .await?; + + Ok(()) + } + + /// ```sql + /// CREATE TABLE cart_discounts + /// ( + /// cart_id uuid NOT NULL, + /// discount_id uuid NOT NULL + /// ); + /// ``` + async fn create_cart_discounts(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { + use CartDiscount::*; + + m.create_bridge_table(CartDiscounts, CartId, DiscountId) + .await?; + + Ok(()) + } + + /// ```sql + /// CREATE TABLE cart_gift_cards + /// ( + /// cart_id uuid NOT NULL, + /// gift_card_id uuid NOT NULL + /// ); + /// ``` + async fn create_cart_gift_cards(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { + use CartGiftCard::*; + + m.create_bridge_table(CartGiftCards, CartId, GiftCardId) + .await?; + + Ok(()) + } +} + +#[derive(Iden, Debug, Clone, Copy)] +pub enum Cart { + Carts, + Id, + Email, + BillingAddressId, + ShippingAddressId, + RegionId, + CustomerId, + PaymentId, + CartType, + CompletedAt, + CreatedAt, + UpdatedAt, + DeletedAt, + Metadata, + IdempotencyKey, + Context, + PaymentAuthorizedAt, + SalesChannelId, +} + +#[derive(Iden, Debug, Clone, Copy)] +pub enum CartDiscount { + CartDiscounts, + CartId, + DiscountId, +} + +#[derive(Iden, Debug, Clone, Copy)] +pub enum CartGiftCard { + CartGiftCards, + CartId, + GiftCardId, +} diff --git a/migration/src/checkouts/mod.rs b/migration/src/checkouts/mod.rs index 9895c3c..0c872c1 100644 --- a/migration/src/checkouts/mod.rs +++ b/migration/src/checkouts/mod.rs @@ -1,3 +1,4 @@ +mod m20230603_120800_carts; mod m20230603_120814_checkouts; mod m20230603_120815_items; @@ -9,6 +10,7 @@ pub struct CheckoutsMigrator; impl MigratorTrait for CheckoutsMigrator { fn migrations() -> Vec> { vec![ + Box::new(m20230603_120800_carts::Migration), Box::new(m20230603_120814_checkouts::Migration), Box::new(m20230603_120815_items::Migration), ] diff --git a/migration/src/lib.rs b/migration/src/lib.rs index ae09c66..e5f508b 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -4,8 +4,6 @@ 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; @@ -24,6 +22,11 @@ pub mod notifications; pub use notifications::*; pub mod identity; pub use identity::*; +pub mod stores; +pub use stores::*; +pub mod marketing; +pub use marketing::*; +pub mod oauth2; +pub use oauth2::*; pub mod types; - pub use types::*; diff --git a/migration/src/main.rs b/migration/src/main.rs index c4dbb94..f27321b 100644 --- a/migration/src/main.rs +++ b/migration/src/main.rs @@ -36,12 +36,13 @@ async fn main() { migrate_schema!(cli, Public, PublicMigrator); migrate_schema!(cli, Jobs, JobsMigrator); migrate_schema!(cli, Carts, CartsMigrator); - migrate_schema!(cli, Discounts, DiscountsMigrator); + migrate_schema!(cli, Marketing, MarketingMigrator); migrate_schema!(cli, Checkouts, CheckoutsMigrator); migrate_schema!(cli, Shipping, ShippingMigrator); migrate_schema!(cli, Stocks, StocksMigrator); migrate_schema!(cli, Identity, IdentityMigrator); migrate_schema!(cli, Notifications, NotificationsMigrator); + migrate_schema!(cli, Oauth2, OAuth2Migrator); } pub async fn run_cli(cli: &mut Cli, schema: PostgreSQLSchema, migrator: M) diff --git a/migration/src/marketing/m20230603_120810_marketing.rs b/migration/src/marketing/m20230603_120810_marketing.rs new file mode 100644 index 0000000..0ff9494 --- /dev/null +++ b/migration/src/marketing/m20230603_120810_marketing.rs @@ -0,0 +1,114 @@ +use sea_orm_migration::prelude::*; + +use crate::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> { + self.create_sales_channels(m).await?; + self.create_sales_channel_locations(m).await?; + + Ok(()) + } + + async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> { + self.drop_table(m, SalesChannel::SalesChannels).await?; + self.drop_table(m, SalesChannelLocation::SalesChannelLocations) + .await?; + + Ok(()) + } +} + +impl Migration { + /// ```sql + /// CREATE TABLE sales_channels + /// ( + /// 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, + /// name character varying NOT NULL, + /// description character varying, + /// is_disabled boolean DEFAULT false NOT NULL, + /// metadata jsonb + /// ); + /// ``` + async fn create_sales_channels(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { + use SalesChannel::*; + + m.build_table( + SalesChannels, + &mut [ + Id.col().uuid().default_gen_uuid().not_null().primary_key(), + CreatedAt.col().timestamp().default_now().not_null(), + UpdatedAt.col().timestamp().default_now().not_null(), + DeletedAt.col().timestamp(), + Name.col().string().not_null(), + Description.col().string(), + IsDisabled.col().boolean().default(false).not_null(), + Metadata.col().json_binary(), + ], + ) + .await?; + + Ok(()) + } + + /// ```sql + /// CREATE TABLE sales_channel_locations + /// ( + /// id uuid NOT NULL, + /// created_at timestamp with time zone DEFAULT now() NOT NULL, + /// updated_at timestamp with time zone DEFAULT now() NOT NULL, + /// sales_channel_id text NOT NULL, + /// location_id text NOT NULL, + /// deleted_at timestamp with time zone + /// ); + /// ``` + async fn create_sales_channel_locations(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { + use SalesChannelLocation::*; + + m.build_table( + SalesChannelLocations, + &mut [ + Id.col().uuid().default_gen_uuid().not_null().primary_key(), + CreatedAt.col().timestamp().default_now().not_null(), + UpdatedAt.col().timestamp().default_now().not_null(), + SalesChannelId.col().uuid().not_null(), + LocationId.col().uuid().not_null(), + DeletedAt.col().timestamp(), + ], + ) + .await?; + + Ok(()) + } +} + +#[derive(Iden, Debug, Clone, Copy)] +pub enum SalesChannel { + SalesChannels, + Id, + CreatedAt, + UpdatedAt, + DeletedAt, + Name, + Description, + IsDisabled, + Metadata, +} + +#[derive(Iden, Debug, Clone, Copy)] +pub enum SalesChannelLocation { + SalesChannelLocations, + Id, + CreatedAt, + UpdatedAt, + SalesChannelId, + LocationId, + DeletedAt, +} diff --git a/migration/src/discounts/m20230603_120814_discounts.rs b/migration/src/marketing/m20230603_120814_discounts.rs similarity index 100% rename from migration/src/discounts/m20230603_120814_discounts.rs rename to migration/src/marketing/m20230603_120814_discounts.rs diff --git a/migration/src/discounts/m20230603_120815_gift_carts.rs b/migration/src/marketing/m20230603_120815_gift_carts.rs similarity index 99% rename from migration/src/discounts/m20230603_120815_gift_carts.rs rename to migration/src/marketing/m20230603_120815_gift_carts.rs index 72985bc..b99025b 100644 --- a/migration/src/discounts/m20230603_120815_gift_carts.rs +++ b/migration/src/marketing/m20230603_120815_gift_carts.rs @@ -10,6 +10,7 @@ impl MigrationTrait for Migration { async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> { Self::create_gift_carts(m).await?; Self::gift_card_transactions(m).await?; + Ok(()) } diff --git a/migration/src/discounts/mod.rs b/migration/src/marketing/mod.rs similarity index 67% rename from migration/src/discounts/mod.rs rename to migration/src/marketing/mod.rs index a479996..31ddce4 100644 --- a/migration/src/discounts/mod.rs +++ b/migration/src/marketing/mod.rs @@ -1,14 +1,17 @@ +mod m20230603_120810_marketing; mod m20230603_120814_discounts; mod m20230603_120815_gift_carts; use sea_orm_migration::{MigrationTrait, MigratorTrait}; -pub struct DiscountsMigrator; +pub struct MarketingMigrator; +/// TODO #[async_trait::async_trait] -impl MigratorTrait for DiscountsMigrator { +impl MigratorTrait for MarketingMigrator { fn migrations() -> Vec> { vec![ + Box::new(m20230603_120810_marketing::Migration), Box::new(m20230603_120814_discounts::Migration), Box::new(m20230603_120815_gift_carts::Migration), ] diff --git a/migration/src/oauth2/m20230603_120810_oauth2.rs b/migration/src/oauth2/m20230603_120810_oauth2.rs new file mode 100644 index 0000000..8495046 --- /dev/null +++ b/migration/src/oauth2/m20230603_120810_oauth2.rs @@ -0,0 +1,152 @@ +use sea_orm_migration::prelude::*; + +use crate::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> { + self.create_oauth(m).await?; + self.create_publishable_api_keys(m).await?; + self.create_publishable_api_key_sales_channels(m).await?; + + Ok(()) + } + + async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> { + self.drop_table(m, Oauth2::Oauth).await?; + self.drop_table(m, PublishableApiKey::PublishableApiKeys) + .await?; + self.drop_table( + m, + PublishableApiKeySalesChannel::PublishableApiKeySalesChannels, + ) + .await?; + + Ok(()) + } +} + +impl Migration { + /// ```sql + /// CREATE TABLE oauth + /// ( + /// id uuid NOT NULL, + /// display_name character varying NOT NULL, + /// application_name character varying NOT NULL, + /// install_url character varying, + /// uninstall_url character varying, + /// data jsonb + /// ); + /// ``` + async fn create_oauth(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { + use Oauth2::*; + + m.build_table( + Oauth, + &mut [ + Id.col().uuid().not_null().primary_key().default_gen_uuid(), + DisplayName.col().string().not_null(), + ApplicationName.col().string().not_null(), + InstallUrl.col().string(), + UninstallUrl.col().string(), + Data.col().json_binary(), + ], + ) + .await?; + + Ok(()) + } + + /// ```sql + /// CREATE TABLE publishable_api_keys + /// ( + /// id uuid NOT NULL, + /// created_at timestamp with time zone DEFAULT now() NOT NULL, + /// updated_at timestamp with time zone DEFAULT now() NOT NULL, + /// created_by character varying, + /// revoked_by character varying, + /// revoked_at timestamp with time zone, + /// title character varying NOT NULL + /// ); + /// ``` + async fn create_publishable_api_keys(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { + use PublishableApiKey::*; + + m.build_table( + PublishableApiKeys, + &mut [ + Id.col() + .uuid() + .primary_key() + .uuid() + .not_null() + .default_gen_uuid(), + CreatedAt.col().timestamp().not_null().default_now(), + UpdatedAt.col().timestamp().not_null().default_now(), + CreatedBy.col().uuid(), + RevokedBy.col().uuid(), + RevokedAt.col().timestamp(), + Title.col().string().not_null(), + ], + ) + .await?; + + Ok(()) + } + + /// ```sql + /// CREATE TABLE publishable_api_key_sales_channels + /// ( + /// sales_channel_id uuid NOT NULL, + /// publishable_key_id uuid NOT NULL + /// ); + /// ``` + async fn create_publishable_api_key_sales_channels( + &self, + m: &SchemaManager<'_>, + ) -> Result<(), DbErr> { + use PublishableApiKeySalesChannel::*; + + m.create_bridge_table( + PublishableApiKeySalesChannels, + SalesChannelId, + PublishableKeyId, + ) + .await?; + + Ok(()) + } +} + +#[derive(Iden, Copy, Clone, Debug)] +pub enum Oauth2 { + Oauth, + Id, + DisplayName, + ApplicationName, + InstallUrl, + UninstallUrl, + Data, +} + +#[derive(Iden, Copy, Clone, Debug)] +pub enum PublishableApiKey { + PublishableApiKeys, + Id, + CreatedAt, + UpdatedAt, + CreatedBy, + RevokedBy, + RevokedAt, + Title, +} + +#[derive(Iden, Copy, Clone, Debug)] +pub enum PublishableApiKeySalesChannel { + PublishableApiKeySalesChannels, + SalesChannelId, + PublishableKeyId, +} diff --git a/migration/src/oauth2/mod.rs b/migration/src/oauth2/mod.rs new file mode 100644 index 0000000..69ddbc8 --- /dev/null +++ b/migration/src/oauth2/mod.rs @@ -0,0 +1,12 @@ +mod m20230603_120810_oauth2; + +use sea_orm_migration::{MigrationTrait, MigratorTrait}; + +pub struct OAuth2Migrator; + +#[async_trait::async_trait] +impl MigratorTrait for OAuth2Migrator { + fn migrations() -> Vec> { + vec![Box::new(m20230603_120810_oauth2::Migration)] + } +} diff --git a/migration/src/public/mod.rs b/migration/src/public/mod.rs index e910713..536f4e1 100644 --- a/migration/src/public/mod.rs +++ b/migration/src/public/mod.rs @@ -3,7 +3,6 @@ use sea_orm_migration::{MigrationTrait, MigratorTrait}; mod m20230603_102634_types; mod m20230603_120814_addresses; mod m20230603_120815_claims; -mod m20230603_120816_geolocation; pub struct PublicMigrator; @@ -14,7 +13,6 @@ impl MigratorTrait for PublicMigrator { Box::new(m20230603_102634_types::Migration), Box::new(m20230603_120814_addresses::Migration), Box::new(m20230603_120815_claims::Migration), - Box::new(m20230603_120816_geolocation::Migration), ] } } diff --git a/migration/src/schema_list.rs b/migration/src/schema_list.rs index b11faa1..122f775 100644 --- a/migration/src/schema_list.rs +++ b/migration/src/schema_list.rs @@ -7,12 +7,13 @@ pub enum PostgreSQLSchema { Public, Jobs, Carts, - Discounts, Checkouts, Shipping, Stocks, Identity, Notifications, + Oauth2, + Marketing, } impl PostgreSQLSchema { @@ -21,12 +22,13 @@ impl PostgreSQLSchema { PostgreSQLSchema::Public => "public", PostgreSQLSchema::Jobs => "jobs", PostgreSQLSchema::Carts => "carts", - PostgreSQLSchema::Discounts => "discounts", PostgreSQLSchema::Checkouts => "checkouts", PostgreSQLSchema::Shipping => "shipping", PostgreSQLSchema::Stocks => "stocks", PostgreSQLSchema::Identity => "identity", PostgreSQLSchema::Notifications => "notifications", + PostgreSQLSchema::Oauth2 => "oauth2", + PostgreSQLSchema::Marketing => "marketing", } } } diff --git a/migration/src/sea_ext/mod.rs b/migration/src/sea_ext/mod.rs index 29f9be5..1370381 100644 --- a/migration/src/sea_ext/mod.rs +++ b/migration/src/sea_ext/mod.rs @@ -110,16 +110,54 @@ impl CreateBridgeTable for SchemaManager<'_> { } } +pub trait DefaultNow { + fn default_now(&mut self) -> &mut Self; +} + +impl DefaultNow for ColumnDef { + fn default_now(&mut self) -> &mut Self { + self.default(SimpleExpr::Custom("now()".into())) + } +} + +pub trait DefaultGenUuid { + fn default_gen_uuid(&mut self) -> &mut Self; +} + +impl DefaultGenUuid for ColumnDef { + fn default_gen_uuid(&mut self) -> &mut Self { + self.default(SimpleExpr::Custom("public.uuid_generate_v4()".into())) + } +} + #[async_trait] pub trait CreateTableExt { - async fn build_table(&self, table: Enum, fields: Vec) -> Result<(), DbErr> + async fn build_table<'c, Enum>( + &self, + table: Enum, + fields: &'c mut [&'c mut ColumnDef], + ) -> Result<(), DbErr> where Enum: IntoIden + Copy + 'static + Sync + Send; + + async fn build_table_with<'c, Enum, F>( + &self, + table: Enum, + fields: &'c mut [&'c mut ColumnDef], + f: F, + ) -> Result<(), DbErr> + where + Enum: IntoIden + Copy + 'static + Sync + Send, + F: FnOnce(&mut TableCreateStatement) + Send; } #[async_trait] impl CreateTableExt for SchemaManager<'_> { - async fn build_table(&self, table: Enum, mut fields: Vec) -> Result<(), DbErr> + async fn build_table<'c, Enum>( + &self, + table: Enum, + fields: &'c mut [&'c mut ColumnDef], + ) -> Result<(), DbErr> where Enum: IntoIden + Copy + 'static + Sync + Send, { @@ -131,6 +169,26 @@ impl CreateTableExt for SchemaManager<'_> { self.create_table(t).await?; Ok(()) } + + async fn build_table_with<'c, Enum, F>( + &self, + table: Enum, + fields: &'c mut [&'c mut ColumnDef], + f: F, + ) -> Result<(), DbErr> + where + Enum: IntoIden + Copy + 'static + Sync + Send, + F: FnOnce(&mut TableCreateStatement) + Send, + { + let mut t = Table::create(); + t.table(table); + fields.iter_mut().for_each(|col| { + t.col(col); + }); + f(&mut t); + self.create_table(t).await?; + Ok(()) + } } pub fn to_pk2_name(t1: T1, c1: C1, c2: C2) -> String { diff --git a/migration/src/public/m20230603_120816_geolocation.rs b/migration/src/stores/m20230603_120810_geolocation.rs similarity index 96% rename from migration/src/public/m20230603_120816_geolocation.rs rename to migration/src/stores/m20230603_120810_geolocation.rs index cceddc7..56c4806 100644 --- a/migration/src/public/m20230603_120816_geolocation.rs +++ b/migration/src/stores/m20230603_120810_geolocation.rs @@ -8,10 +8,10 @@ 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?; + async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> { + Self::create_countries(m).await?; + Self::create_currencies(m).await?; + Self::create_regions(m).await?; Ok(()) } diff --git a/migration/src/stores/m20230603_120810_store_ext.rs b/migration/src/stores/m20230603_120810_store_ext.rs new file mode 100644 index 0000000..0971056 --- /dev/null +++ b/migration/src/stores/m20230603_120810_store_ext.rs @@ -0,0 +1,244 @@ +use sea_orm_migration::prelude::*; + +use crate::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> { + self.create_stores(m).await?; + self.create_store_currencies(m).await?; + self.create_tax_providers(m).await?; + self.create_tax_rates(m).await?; + self.create_region_fulfillment_providers(m).await?; + self.create_region_payment_providers(m).await?; + + Ok(()) + } + + async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> { + self.drop_table(m, Store::Stores).await?; + self.drop_table(m, StoreCurrency::StoreCurrencies).await?; + self.drop_table(m, TaxProvider::TaxProviders).await?; + self.drop_table(m, TaxRate::TaxRates).await?; + self.drop_table(m, RegionFulfillmentProvider::RegionFulfillmentProviders) + .await?; + self.drop_table(m, RegionPaymentProvider::RegionPaymentProviders) + .await?; + Ok(()) + } +} + +impl Migration { + /// ```sql + /// CREATE TABLE stores + /// ( + /// id uuid NOT NULL, + /// name character varying DEFAULT 'Bazaar'::character varying NOT NULL, + /// default_currency_code character varying DEFAULT 'pln'::character varying NOT NULL, + /// swap_link_template character varying, + /// created_at timestamp with time zone DEFAULT now() NOT NULL, + /// updated_at timestamp with time zone DEFAULT now() NOT NULL, + /// metadata jsonb, + /// payment_link_template character varying, + /// invite_link_template character varying, + /// default_sales_channel_id uuid, + /// default_location_id uuid + /// ); + /// ``` + async fn create_stores(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { + use Store::*; + + m.build_table( + Stores, + &mut [ + Id.col().uuid().not_null().primary_key().default_gen_uuid(), + Name.col().string().default("Bazzar".to_string()).not_null(), + DefaultCurrencyCode.col().string().default("pln").not_null(), + SwapLinkTemplate.col().string(), + CreatedAt.col().timestamp().not_null().default_now(), + UpdatedAt.col().timestamp().not_null().default_now(), + Metadata.col().json_binary(), + PaymentLinkTemplate.col().string(), + InviteLinkTemplate.col().string(), + DefaultSalesChannelId.col().uuid(), + DefaultLocationId.col().uuid(), + ], + ) + .await?; + + Ok(()) + } + + /// ```sql + /// CREATE TABLE store_currencies + /// ( + /// store_id uuid NOT NULL, + /// currency_code character varying NOT NULL + /// ); + /// ``` + async fn create_store_currencies(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { + use StoreCurrency::*; + + m.create_table( + Table::create() + .table(StoreCurrencies) + .col(StoreId.col().uuid().not_null()) + .col(CurrencyCode.col().string().not_null()) + .primary_key(&mut m.bridge_primary_key(StoreCurrencies, StoreId, CurrencyCode)) + .to_owned(), + ) + .await?; + + Ok(()) + } + + /// ```sql + /// CREATE TABLE tax_providers + /// ( + /// id uuid NOT NULL, + /// is_installed boolean DEFAULT true NOT NULL + /// ); + /// ``` + async fn create_tax_providers(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { + use TaxProvider::*; + + m.build_table( + TaxProviders, + &mut [ + Id.col().uuid().not_null().default_gen_uuid().primary_key(), + IsInstalled.col().boolean().default(true).not_null(), + ], + ) + .await?; + + Ok(()) + } + + /// ```sql + /// CREATE TABLE tax_rates + /// ( + /// id uuid NOT NULL, + /// rate real, + /// code character varying, + /// name character varying NOT NULL, + /// region_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_tax_rates(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { + use TaxRate::*; + + m.build_table( + TaxRates, + &mut [ + Id.col().uuid().primary_key().default_gen_uuid(), + Rate.col().double(), + Code.col().string(), + Name.col().string().not_null(), + RegionId.col().uuid().not_null(), + CreatedAt.col().timestamp().not_null().default_now(), + UpdatedAt.col().timestamp().not_null().default_now(), + Metadata.col().json_binary(), + ], + ) + .await?; + Ok(()) + } + + /// ```sql + /// CREATE TABLE region_fulfillment_providers + /// ( + /// region_id uuid NOT NULL, + /// provider_id uuid NOT NULL + /// ); + /// ``` + async fn create_region_fulfillment_providers( + &self, + m: &SchemaManager<'_>, + ) -> Result<(), DbErr> { + use RegionFulfillmentProvider::*; + + m.create_bridge_table(RegionFulfillmentProviders, RegionId, ProviderId) + .await?; + + Ok(()) + } + + /// ```sql + /// CREATE TABLE region_payment_providers + /// ( + /// region_id uuid NOT NULL, + /// provider_id uuid NOT NULL + /// ); + /// ``` + async fn create_region_payment_providers(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { + use RegionPaymentProvider::*; + + m.create_bridge_table(RegionPaymentProviders, RegionId, ProviderId) + .await?; + + Ok(()) + } +} + +#[derive(Iden, Clone, Copy)] +pub enum Store { + Stores, + Id, + Name, + DefaultCurrencyCode, + SwapLinkTemplate, + CreatedAt, + UpdatedAt, + Metadata, + PaymentLinkTemplate, + InviteLinkTemplate, + DefaultSalesChannelId, + DefaultLocationId, +} + +#[derive(Iden, Clone, Copy)] +pub enum StoreCurrency { + StoreCurrencies, + StoreId, + CurrencyCode, +} + +#[derive(Iden, Clone, Copy)] +pub enum TaxProvider { + TaxProviders, + Id, + IsInstalled, +} + +#[derive(Iden, Clone, Copy)] +pub enum TaxRate { + TaxRates, + Id, + Rate, + Code, + Name, + RegionId, + CreatedAt, + UpdatedAt, + Metadata, +} + +#[derive(Iden, Clone, Copy)] +pub enum RegionFulfillmentProvider { + RegionFulfillmentProviders, + RegionId, + ProviderId, +} + +#[derive(Iden, Clone, Copy)] +pub enum RegionPaymentProvider { + RegionPaymentProviders, + RegionId, + ProviderId, +} diff --git a/migration/src/stores/mod.rs b/migration/src/stores/mod.rs new file mode 100644 index 0000000..7b7f2f6 --- /dev/null +++ b/migration/src/stores/mod.rs @@ -0,0 +1,16 @@ +mod m20230603_120810_geolocation; +mod m20230603_120810_store_ext; + +use sea_orm_migration::{MigrationTrait, MigratorTrait}; + +pub struct StoresMigrator; + +#[async_trait::async_trait] +impl MigratorTrait for StoresMigrator { + fn migrations() -> Vec> { + vec![ + Box::new(m20230603_120810_geolocation::Migration), + Box::new(m20230603_120810_store_ext::Migration), + ] + } +} diff --git a/migrations/20230603073510_init.sql b/migrations/20230603073510_init.sql index a6d5b14..c27e866 100644 --- a/migrations/20230603073510_init.sql +++ b/migrations/20230603073510_init.sql @@ -13,14 +13,6 @@ CREATE TYPE payment_collection_types AS ENUM ( 'order_edit' ); -CREATE TYPE cart_types AS ENUM ( - 'default', - 'swap', - 'draft_order', - 'payment_link', - 'claim' - ); - CREATE TYPE claim_item_reasons AS ENUM ( 'missing_item', 'wrong_item', @@ -198,32 +190,6 @@ CREATE TYPE swap_payment_statuses AS ENUM ( 'requires_action' ); -CREATE TYPE user_roles AS ENUM ( - 'admin', - 'member', - 'developer' - ); - -CREATE TABLE addresses -( - id uuid NOT NULL, - customer_id uuid, - company character varying, - first_name character varying, - last_name character varying, - address_1 character varying, - address_2 character varying, - city character varying, - country_code character varying, - province character varying, - postal_code character varying, - phone character varying, - 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 analytics_configs ( id uuid NOT NULL, @@ -254,39 +220,6 @@ CREATE TABLE batch_jobs deleted_at timestamp with time zone ); -CREATE TABLE carts -( - id uuid NOT NULL, - email character varying, - billing_address_id uuid, - shipping_address_id uuid, - region_id uuid NOT NULL, - customer_id uuid, - payment_id uuid, - type cart_types DEFAULT 'default'::cart_types NOT NULL, - completed_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, - context jsonb, - payment_authorized_at timestamp with time zone, - sales_channel_id uuid -); - -CREATE TABLE cart_discounts -( - cart_id uuid NOT NULL, - discount_id uuid NOT NULL -); - -CREATE TABLE cart_gift_cards -( - cart_id uuid NOT NULL, - gift_card_id uuid NOT NULL -); - CREATE TABLE claim_images ( id uuid NOT NULL, @@ -347,143 +280,6 @@ CREATE TABLE claim_tags metadata jsonb ); - -CREATE TABLE regions -( - id uuid NOT NULL, - name character varying NOT NULL, - currency_code character varying NOT NULL, - tax_rate real NOT NULL, - tax_code character varying, - 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, - gift_cards_taxable boolean DEFAULT true NOT NULL, - automatic_taxes boolean DEFAULT true NOT NULL, - tax_provider_id uuid -); - -CREATE TABLE countries -( - id sequence NOT NULL, - iso_2 character varying NOT NULL, - iso_3 character varying NOT NULL, - num_code integer NOT NULL, - name character varying NOT NULL, - display_name character varying NOT NULL, - region_id uuid -); - -CREATE TABLE currencies -( - code character varying NOT NULL, - symbol character varying NOT NULL, - symbol_native character varying NOT NULL, - name character varying NOT NULL -); - -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 -); - -CREATE TABLE discount_conditions -( - id uuid NOT NULL, - type discount_condition_types NOT NULL, - operator 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 -); - -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 -); - -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 -); - -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 -); - -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 -); - -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 -); - -CREATE TABLE discount_regions -( - discount_id uuid NOT NULL, - region_id uuid NOT NULL -); - -CREATE TABLE discount_rules -( - id uuid NOT NULL, - description character varying, - type discount_rule_types NOT NULL, - value integer NOT NULL, - allocation 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 -); - -CREATE TABLE discount_rule_products -( - discount_rule_id uuid NOT NULL, - product_id uuid NOT NULL -); - CREATE TABLE orders ( id uuid NOT NULL, @@ -624,34 +420,6 @@ CREATE TABLE payment_sessions is_initiated boolean DEFAULT false NOT NULL ); -CREATE TABLE gift_cards -( - id uuid NOT NULL, - code character varying NOT NULL, - value integer NOT NULL, - balance integer NOT NULL, - region_id uuid NOT NULL, - order_id uuid, - is_disabled boolean DEFAULT false 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, - tax_rate real -); - -CREATE TABLE gift_card_transactions -( - id uuid NOT NULL, - gift_card_id uuid NOT NULL, - order_id uuid NOT NULL, - amount integer NOT NULL, - created_at timestamp with time zone DEFAULT now() NOT NULL, - is_taxable boolean, - tax_rate real -); - CREATE TABLE line_items ( id uuid NOT NULL, @@ -800,125 +568,6 @@ CREATE TABLE notes metadata jsonb ); -CREATE TABLE oauth -( - id uuid NOT NULL, - display_name character varying NOT NULL, - application_name character varying NOT NULL, - install_url character varying, - uninstall_url character varying, - data jsonb -); - -CREATE TABLE publishable_api_keys -( - id uuid NOT NULL, - created_at timestamp with time zone DEFAULT now() NOT NULL, - updated_at timestamp with time zone DEFAULT now() NOT NULL, - created_by character varying, - revoked_by character varying, - revoked_at timestamp with time zone, - title character varying NOT NULL -); - -CREATE TABLE publishable_api_key_sales_channels -( - sales_channel_id uuid NOT NULL, - publishable_key_id uuid NOT NULL -); - -CREATE TABLE refunds -( - id uuid NOT NULL, - order_id uuid, - amount integer NOT NULL, - note character varying, - reason refund_reasons NOT NULL, - 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, - payment_id uuid -); - -CREATE TABLE region_fulfillment_providers -( - region_id uuid NOT NULL, - provider_id uuid NOT NULL -); - -CREATE TABLE region_payment_providers -( - region_id uuid NOT NULL, - provider_id uuid NOT NULL -); - -CREATE TABLE returns -( - id uuid NOT NULL, - status return_statuses DEFAULT 'requested'::return_statuses NOT NULL, - swap_id uuid, - order_id uuid, - shipping_data jsonb, - refund_amount integer NOT NULL, - received_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, - claim_order_id uuid, - no_notification boolean, - location_id uuid -); - -CREATE TABLE return_items -( - return_id uuid NOT NULL, - item_id uuid NOT NULL, - quantity integer NOT NULL, - is_requested boolean DEFAULT true NOT NULL, - requested_quantity integer, - received_quantity integer, - metadata jsonb, - reason_id uuid, - note character varying -); - -CREATE TABLE return_reasons -( - id uuid NOT NULL, - value character varying NOT NULL, - label character varying NOT NULL, - description character varying, - 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, - parent_return_reason_id uuid -); - -CREATE TABLE sales_channels -( - 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, - name character varying NOT NULL, - description character varying, - is_disabled boolean DEFAULT false NOT NULL, - metadata jsonb -); - -CREATE TABLE sales_channel_locations -( - id uuid NOT NULL, - created_at timestamp with time zone DEFAULT now() NOT NULL, - updated_at timestamp with time zone DEFAULT now() NOT NULL, - sales_channel_id text NOT NULL, - location_id text NOT NULL, - deleted_at timestamp with time zone -); - CREATE TABLE staged_jobs ( id uuid NOT NULL, @@ -927,27 +576,6 @@ CREATE TABLE staged_jobs options jsonb DEFAULT '{}'::jsonb NOT NULL ); -CREATE TABLE stores -( - id uuid NOT NULL, - name character varying DEFAULT 'Bazaar'::character varying NOT NULL, - default_currency_code character varying DEFAULT 'pln'::character varying NOT NULL, - swap_link_template character varying, - created_at timestamp with time zone DEFAULT now() NOT NULL, - updated_at timestamp with time zone DEFAULT now() NOT NULL, - metadata jsonb, - payment_link_template character varying, - invite_link_template character varying, - default_sales_channel_id uuid, - default_location_id uuid -); - -CREATE TABLE store_currencies -( - store_id uuid NOT NULL, - currency_code character varying NOT NULL -); - CREATE TABLE swaps ( id uuid NOT NULL, @@ -967,36 +595,3 @@ CREATE TABLE swaps canceled_at timestamp with time zone, allow_backorder boolean DEFAULT false NOT NULL ); - -CREATE TABLE tax_providers -( - id uuid NOT NULL, - is_installed boolean DEFAULT true NOT NULL -); - -CREATE TABLE tax_rates -( - id uuid NOT NULL, - rate real, - code character varying, - name character varying NOT NULL, - region_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 -); - -CREATE TABLE users -( - id uuid NOT NULL, - email character varying NOT NULL, - first_name character varying, - last_name character varying, - password_hash character varying, - api_token character varying, - 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, - role user_roles DEFAULT 'member'::user_roles -); diff --git a/migrations/20230603073520_customers.sql b/migrations/20230603073520_customers.sql deleted file mode 100644 index 3332953..0000000 --- a/migrations/20230603073520_customers.sql +++ /dev/null @@ -1,31 +0,0 @@ -CREATE TABLE 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 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 customer_group_customers -( - customer_group_id uuid NOT NULL, - customer_id uuid NOT NULL -); diff --git a/migrations/20230603073520_identity.sql b/migrations/20230603073520_identity.sql new file mode 100644 index 0000000..e480d05 --- /dev/null +++ b/migrations/20230603073520_identity.sql @@ -0,0 +1,76 @@ +CREATE TABLE 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 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 customer_group_customers +( + customer_group_id uuid NOT NULL, + customer_id uuid NOT NULL +); + +------------------------------------------ +------------------------------------------ +------------------------------------------ + +CREATE TYPE user_roles AS ENUM ( + 'admin', + 'member', + 'developer' + ); + +CREATE TABLE users +( + id uuid NOT NULL, + email character varying NOT NULL, + first_name character varying, + last_name character varying, + password_hash character varying, + api_token character varying, + 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, + role user_roles DEFAULT 'member'::user_roles +); + +CREATE TABLE addresses +( + id uuid NOT NULL, + customer_id uuid, + company character varying, + first_name character varying, + last_name character varying, + address_1 character varying, + address_2 character varying, + city character varying, + country_code character varying, + province character varying, + postal_code character varying, + phone character varying, + 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 +); diff --git a/migrations/20230603073530_shippings.sql b/migrations/20230603073530_shippings.sql index 224ca3c..a825532 100644 --- a/migrations/20230603073530_shippings.sql +++ b/migrations/20230603073530_shippings.sql @@ -1,7 +1,3 @@ ---- DONE ---- DONE ---- DONE - CREATE TABLE tracking_links ( id uuid NOT NULL, @@ -103,3 +99,66 @@ CREATE TABLE shipping_tax_rates updated_at timestamp with time zone DEFAULT now() NOT NULL, metadata jsonb ); + +----------------------------------------- +----------------------------------------- +----------------------------------------- +----------------------------------------- + +CREATE TABLE refunds +( + id uuid NOT NULL, + order_id uuid, + amount integer NOT NULL, + note character varying, + reason refund_reasons NOT NULL, + 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, + payment_id uuid +); + +CREATE TABLE returns +( + id uuid NOT NULL, + status return_statuses DEFAULT 'requested'::return_statuses NOT NULL, + swap_id uuid, + order_id uuid, + shipping_data jsonb, + refund_amount integer NOT NULL, + received_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, + claim_order_id uuid, + no_notification boolean, + location_id uuid +); + +CREATE TABLE return_items +( + return_id uuid NOT NULL, + item_id uuid NOT NULL, + quantity integer NOT NULL, + is_requested boolean DEFAULT true NOT NULL, + requested_quantity integer, + received_quantity integer, + metadata jsonb, + reason_id uuid, + note character varying +); + +CREATE TABLE return_reasons +( + id uuid NOT NULL, + value character varying NOT NULL, + label character varying NOT NULL, + description character varying, + 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, + parent_return_reason_id uuid +); diff --git a/migrations/20230603073532_checkouts.sql b/migrations/20230603073532_checkouts.sql new file mode 100644 index 0000000..069a76f --- /dev/null +++ b/migrations/20230603073532_checkouts.sql @@ -0,0 +1,40 @@ +CREATE TYPE cart_types AS ENUM ( + 'default', + 'swap', + 'draft_order', + 'payment_link', + 'claim' + ); + +CREATE TABLE carts +( + id uuid NOT NULL, + email character varying, + billing_address_id uuid, + shipping_address_id uuid, + region_id uuid NOT NULL, + customer_id uuid, + payment_id uuid, + type cart_types DEFAULT 'default'::cart_types NOT NULL, + completed_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, + context jsonb, + payment_authorized_at timestamp with time zone, + sales_channel_id uuid +); + +CREATE TABLE cart_discounts +( + cart_id uuid NOT NULL, + discount_id uuid NOT NULL +); + +CREATE TABLE cart_gift_cards +( + cart_id uuid NOT NULL, + gift_card_id uuid NOT NULL +); diff --git a/migrations/20230603073532_marketing.sql b/migrations/20230603073532_marketing.sql new file mode 100644 index 0000000..5693de4 --- /dev/null +++ b/migrations/20230603073532_marketing.sql @@ -0,0 +1,150 @@ +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 +); + +CREATE TABLE discount_conditions +( + id uuid NOT NULL, + type discount_condition_types NOT NULL, + operator 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 +); + +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 +); + +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 +); + +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 +); + +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 +); + +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 +); + +CREATE TABLE discount_regions +( + discount_id uuid NOT NULL, + region_id uuid NOT NULL +); + +CREATE TABLE discount_rules +( + id uuid NOT NULL, + description character varying, + type discount_rule_types NOT NULL, + value integer NOT NULL, + allocation 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 +); + +CREATE TABLE discount_rule_products +( + discount_rule_id uuid NOT NULL, + product_id uuid NOT NULL +); + +CREATE TABLE gift_cards +( + id uuid NOT NULL, + code character varying NOT NULL, + value integer NOT NULL, + balance integer NOT NULL, + region_id uuid NOT NULL, + order_id uuid, + is_disabled boolean DEFAULT false 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, + tax_rate real +); + +CREATE TABLE gift_card_transactions +( + id uuid NOT NULL, + gift_card_id uuid NOT NULL, + order_id uuid NOT NULL, + amount integer NOT NULL, + created_at timestamp with time zone DEFAULT now() NOT NULL, + is_taxable boolean, + tax_rate real +); + +CREATE TABLE sales_channels +( + 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, + name character varying NOT NULL, + description character varying, + is_disabled boolean DEFAULT false NOT NULL, + metadata jsonb +); + +CREATE TABLE sales_channel_locations +( + id uuid NOT NULL, + created_at timestamp with time zone DEFAULT now() NOT NULL, + updated_at timestamp with time zone DEFAULT now() NOT NULL, + sales_channel_id text NOT NULL, + location_id text NOT NULL, + deleted_at timestamp with time zone +); diff --git a/migrations/20230603073534_open_api.sql b/migrations/20230603073534_open_api.sql new file mode 100644 index 0000000..a2eaed6 --- /dev/null +++ b/migrations/20230603073534_open_api.sql @@ -0,0 +1,26 @@ +CREATE TABLE oauth +( + id uuid NOT NULL, + display_name character varying NOT NULL, + application_name character varying NOT NULL, + install_url character varying, + uninstall_url character varying, + data jsonb +); + +CREATE TABLE publishable_api_keys +( + id uuid NOT NULL, + created_at timestamp with time zone DEFAULT now() NOT NULL, + updated_at timestamp with time zone DEFAULT now() NOT NULL, + created_by character varying, + revoked_by character varying, + revoked_at timestamp with time zone, + title character varying NOT NULL +); + +CREATE TABLE publishable_api_key_sales_channels +( + sales_channel_id uuid NOT NULL, + publishable_key_id uuid NOT NULL +); diff --git a/migrations/20230603073535_stores.sql b/migrations/20230603073535_stores.sql new file mode 100644 index 0000000..5e52041 --- /dev/null +++ b/migrations/20230603073535_stores.sql @@ -0,0 +1,85 @@ +CREATE TABLE stores +( + id uuid NOT NULL, + name character varying DEFAULT 'Bazaar'::character varying NOT NULL, + default_currency_code character varying DEFAULT 'pln'::character varying NOT NULL, + swap_link_template character varying, + created_at timestamp with time zone DEFAULT now() NOT NULL, + updated_at timestamp with time zone DEFAULT now() NOT NULL, + metadata jsonb, + payment_link_template character varying, + invite_link_template character varying, + default_sales_channel_id uuid, + default_location_id uuid +); + +CREATE TABLE store_currencies +( + store_id uuid NOT NULL, + currency_code character varying NOT NULL +); + +CREATE TABLE tax_providers +( + id uuid NOT NULL, + is_installed boolean DEFAULT true NOT NULL +); + +CREATE TABLE tax_rates +( + id uuid NOT NULL, + rate real, + code character varying, + name character varying NOT NULL, + region_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 +); + +CREATE TABLE region_fulfillment_providers +( + region_id uuid NOT NULL, + provider_id uuid NOT NULL +); + +CREATE TABLE region_payment_providers +( + region_id uuid NOT NULL, + provider_id uuid NOT NULL +); + +CREATE TABLE regions +( + id uuid NOT NULL, + name character varying NOT NULL, + currency_code character varying NOT NULL, + tax_rate real NOT NULL, + tax_code character varying, + 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, + gift_cards_taxable boolean DEFAULT true NOT NULL, + automatic_taxes boolean DEFAULT true NOT NULL, + tax_provider_id uuid +); + +CREATE TABLE countries +( + id sequence NOT NULL, + iso_2 character varying NOT NULL, + iso_3 character varying NOT NULL, + num_code integer NOT NULL, + name character varying NOT NULL, + display_name character varying NOT NULL, + region_id uuid +); + +CREATE TABLE currencies +( + code character varying NOT NULL, + symbol character varying NOT NULL, + symbol_native character varying NOT NULL, + name character varying NOT NULL +); diff --git a/migrations/202306091717_products.sql b/migrations/20230609171700_products.sql similarity index 100% rename from migrations/202306091717_products.sql rename to migrations/20230609171700_products.sql