Add Identity and Notification

This commit is contained in:
eraden 2023-06-17 06:28:24 +02:00
parent 8383104d53
commit 8dae1384af
9 changed files with 291 additions and 4 deletions

View File

@ -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,
}

View File

@ -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<Box<dyn MigrationTrait>> {
vec![Box::new(m20230603_120814_identity::Migration)]
}
}

View File

@ -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::*;

View File

@ -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<M>(cli: &mut Cli, schema: PostgreSQLSchema, migrator: M)

View File

@ -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,
}

View File

@ -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<Box<dyn MigrationTrait>> {
vec![Box::new(m20230603_120814_notifications::Migration)]
}
}

View File

@ -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",
}
}
}

View File

@ -1,5 +1,3 @@
------ PENDING
CREATE TABLE customers
(
id uuid NOT NULL,

View File

@ -1,5 +1,3 @@
---- PENDING
CREATE TABLE notifications
(
id uuid NOT NULL,