From 8dae1384af423485b22b3713c9da874c43569656 Mon Sep 17 00:00:00 2001 From: eraden Date: Sat, 17 Jun 2023 06:28:24 +0200 Subject: [PATCH] Add Identity and Notification --- .../src/identity/m20230603_120814_identity.rs | 150 ++++++++++++++++++ migration/src/identity/mod.rs | 12 ++ migration/src/lib.rs | 2 + migration/src/main.rs | 2 + .../m20230603_120814_notifications.rs | 109 +++++++++++++ migration/src/notifications/mod.rs | 12 ++ migration/src/schema_list.rs | 4 + migrations/20230603073520_customers.sql | 2 - migrations/20230603073531_notifications.sql | 2 - 9 files changed, 291 insertions(+), 4 deletions(-) create mode 100644 migration/src/identity/m20230603_120814_identity.rs create mode 100644 migration/src/identity/mod.rs create mode 100644 migration/src/notifications/m20230603_120814_notifications.rs diff --git a/migration/src/identity/m20230603_120814_identity.rs b/migration/src/identity/m20230603_120814_identity.rs new file mode 100644 index 0000000..7303c46 --- /dev/null +++ b/migration/src/identity/m20230603_120814_identity.rs @@ -0,0 +1,150 @@ +use sea_orm_migration::prelude::*; +use sea_query::expr::SimpleExpr; + +use crate::{auto_uuid_not_null, ts_def_now_not_null, CreateBridgeTable, DropTable, IntoColumnDef}; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> { + self.create_customers(m).await?; + self.create_customer_groups(m).await?; + self.create_customer_group_customers(m).await?; + Ok(()) + } + + async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> { + self.drop_table(m, Customer::Customers).await?; + self.drop_table(m, CustomerGroup::CustomerGroups).await?; + self.drop_table(m, CustomerGroupCustomer::CustomerGroupCustomers) + .await?; + Ok(()) + } +} + +impl Migration { + /// ```sql + /// 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 + /// ); + /// ``` + async fn create_customers(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { + use Customer::*; + + m.create_table( + Table::create() + .table(Customers) + .col(auto_uuid_not_null!(Id)) + .col(Email.col().string().not_null()) + .col(FirstName.col().string()) + .col(LastName.col().string()) + .col(BillingAddressId.col().uuid()) + .col(PasswordHash.col().string()) + .col(Phone.col().string()) + .col(HasAccount.col().boolean().default(false).not_null()) + .col(ts_def_now_not_null!(CreatedAt)) + .col(ts_def_now_not_null!(UpdatedAt)) + .col(DeletedAt.col().timestamp()) + .col(Metadata.col().json_binary()) + .to_owned(), + ) + .await?; + + Ok(()) + } + + /// ```sql + /// 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 + /// ); + /// ``` + async fn create_customer_groups(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { + use CustomerGroup::*; + + m.create_table( + Table::create() + .table(CustomerGroups) + .col(auto_uuid_not_null!(Id)) + .col(Name.col().string().not_null()) + .col(ts_def_now_not_null!(CreatedAt)) + .col(ts_def_now_not_null!(UpdatedAt)) + .col(DeletedAt.col().timestamp()) + .col(Metadata.col().json_binary()) + .to_owned(), + ) + .await?; + + Ok(()) + } + + /// ```sql + /// CREATE TABLE customer_group_customers + /// ( + /// customer_group_id uuid NOT NULL, + /// customer_id uuid NOT NULL + /// ); + /// ``` + async fn create_customer_group_customers(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { + use CustomerGroupCustomer::*; + + m.create_bridge_table(CustomerGroupCustomers, CustomerGroupId, CustomerId) + .await?; + + Ok(()) + } +} + +#[derive(Iden, Copy, Clone)] +pub enum Customer { + Customers, + Id, + Email, + FirstName, + LastName, + BillingAddressId, + PasswordHash, + Phone, + HasAccount, + CreatedAt, + UpdatedAt, + DeletedAt, + Metadata, +} + +#[derive(Iden, Copy, Clone)] +pub enum CustomerGroup { + CustomerGroups, + Id, + Name, + CreatedAt, + UpdatedAt, + DeletedAt, + Metadata, +} + +#[derive(Iden, Copy, Clone)] +pub enum CustomerGroupCustomer { + CustomerGroupCustomers, + CustomerGroupId, + CustomerId, +} diff --git a/migration/src/identity/mod.rs b/migration/src/identity/mod.rs new file mode 100644 index 0000000..9694ca4 --- /dev/null +++ b/migration/src/identity/mod.rs @@ -0,0 +1,12 @@ +use sea_orm_migration::{MigrationTrait, MigratorTrait}; + +mod m20230603_120814_identity; + +pub struct IdentityMigrator; + +#[async_trait::async_trait] +impl MigratorTrait for IdentityMigrator { + fn migrations() -> Vec> { + vec![Box::new(m20230603_120814_identity::Migration)] + } +} diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 77cab86..ae09c66 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -22,6 +22,8 @@ pub mod stocks; pub use stocks::*; pub mod notifications; pub use notifications::*; +pub mod identity; +pub use identity::*; pub mod types; pub use types::*; diff --git a/migration/src/main.rs b/migration/src/main.rs index 4c0438e..c4dbb94 100644 --- a/migration/src/main.rs +++ b/migration/src/main.rs @@ -40,6 +40,8 @@ async fn main() { 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); } pub async fn run_cli(cli: &mut Cli, schema: PostgreSQLSchema, migrator: M) diff --git a/migration/src/notifications/m20230603_120814_notifications.rs b/migration/src/notifications/m20230603_120814_notifications.rs new file mode 100644 index 0000000..94de5cc --- /dev/null +++ b/migration/src/notifications/m20230603_120814_notifications.rs @@ -0,0 +1,109 @@ +use sea_orm_migration::prelude::*; +use sea_query::expr::SimpleExpr; + +use crate::{auto_uuid_not_null, ts_def_now_not_null, DropTable, IntoColumnDef}; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> { + self.create_notifications(m).await?; + self.create_notification_providers(m).await?; + Ok(()) + } + + async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> { + self.drop_table(m, Notification::Notifications).await?; + self.drop_table(m, NotificationProvider::NotificationProviders) + .await?; + Ok(()) + } +} + +impl Migration { + /// ```sql + /// CREATE TABLE notifications + /// ( + /// id uuid NOT NULL, + /// event_name character varying, + /// resource_type character varying NOT NULL, + /// resource_id uuid NOT NULL, + /// customer_id uuid, + /// "to" character varying NOT NULL, + /// data jsonb NOT NULL, + /// parent_id uuid, + /// provider_id uuid, + /// created_at timestamp with time zone DEFAULT now() NOT NULL, + /// updated_at timestamp with time zone DEFAULT now() NOT NULL + /// ); + /// ``` + async fn create_notifications(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { + use Notification::*; + + m.create_table( + Table::create() + .table(Notifications) + .col(auto_uuid_not_null!(Id)) + .col(EventName.col().string().not_null()) + .col(ResourceType.col().string().not_null()) + .col(ResourceId.col().uuid().not_null()) + .col(CustomerId.col().uuid()) + .col(To.col().string().not_null()) + .col(Data.col().json_binary().not_null()) + .col(ParentId.col().uuid()) + .col(ProviderId.col().uuid()) + .col(ts_def_now_not_null!(CreatedAt)) + .col(ts_def_now_not_null!(UpdatedAt)) + .to_owned(), + ) + .await?; + + Ok(()) + } + /// ```sql + /// CREATE TABLE notification_providers + /// ( + /// id uuid NOT NULL, + /// is_installed boolean DEFAULT true NOT NULL + /// ); + /// ``` + async fn create_notification_providers(&self, m: &SchemaManager<'_>) -> Result<(), DbErr> { + use NotificationProvider::*; + + m.create_table( + Table::create() + .table(NotificationProviders) + .col(auto_uuid_not_null!(Id)) + .col(IsInstalled.col().boolean().default(true).not_null()) + .to_owned(), + ) + .await?; + + Ok(()) + } +} + +#[derive(Iden)] +pub enum Notification { + Notifications, + Id, + EventName, + ResourceType, + ResourceId, + CustomerId, + To, + Data, + ParentId, + ProviderId, + CreatedAt, + UpdatedAt, +} + +#[derive(Iden)] +pub enum NotificationProvider { + NotificationProviders, + Id, + IsInstalled, +} diff --git a/migration/src/notifications/mod.rs b/migration/src/notifications/mod.rs index e69de29..eefc92b 100644 --- a/migration/src/notifications/mod.rs +++ b/migration/src/notifications/mod.rs @@ -0,0 +1,12 @@ +use sea_orm_migration::{MigrationTrait, MigratorTrait}; + +mod m20230603_120814_notifications; + +pub struct NotificationsMigrator; + +#[async_trait::async_trait] +impl MigratorTrait for NotificationsMigrator { + fn migrations() -> Vec> { + vec![Box::new(m20230603_120814_notifications::Migration)] + } +} diff --git a/migration/src/schema_list.rs b/migration/src/schema_list.rs index 68516c3..b11faa1 100644 --- a/migration/src/schema_list.rs +++ b/migration/src/schema_list.rs @@ -11,6 +11,8 @@ pub enum PostgreSQLSchema { Checkouts, Shipping, Stocks, + Identity, + Notifications, } impl PostgreSQLSchema { @@ -23,6 +25,8 @@ impl PostgreSQLSchema { PostgreSQLSchema::Checkouts => "checkouts", PostgreSQLSchema::Shipping => "shipping", PostgreSQLSchema::Stocks => "stocks", + PostgreSQLSchema::Identity => "identity", + PostgreSQLSchema::Notifications => "notifications", } } } diff --git a/migrations/20230603073520_customers.sql b/migrations/20230603073520_customers.sql index a702ff0..3332953 100644 --- a/migrations/20230603073520_customers.sql +++ b/migrations/20230603073520_customers.sql @@ -1,5 +1,3 @@ ------- PENDING - CREATE TABLE customers ( id uuid NOT NULL, diff --git a/migrations/20230603073531_notifications.sql b/migrations/20230603073531_notifications.sql index f1230b3..e434c31 100644 --- a/migrations/20230603073531_notifications.sql +++ b/migrations/20230603073531_notifications.sql @@ -1,5 +1,3 @@ ----- PENDING - CREATE TABLE notifications ( id uuid NOT NULL,