Add schema list

This commit is contained in:
Adrian Woźniak 2023-06-05 15:18:12 +02:00
parent f95d6dd010
commit 7c9baa2a3f
4 changed files with 245 additions and 64 deletions

View File

@ -2,6 +2,7 @@ pub use sea_orm_migration::prelude::*;
mod m20230603_102634_types;
mod m20230603_120814_addresses;
pub mod schema_list;
pub mod types;
pub struct Migrator;

View File

@ -1,7 +1,8 @@
use sea_orm_migration::prelude::*;
use sea_orm_migration::sea_orm::Statement;
use sea_orm_migration::sea_orm::{Iterable, Statement};
use sea_query::expr::SimpleExpr;
use crate::schema_list::PostgreSQLSchema;
use crate::sea_orm::DatabaseBackend;
#[derive(DeriveMigrationName)]
@ -10,47 +11,188 @@ pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
/*
id character varying NOT NULL,
customer_id character varying,
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
*/
for schema in PostgreSQLSchema::iter().skip(1) {
manager
.get_connection()
.execute(Statement::from_string(
DatabaseBackend::Postgres,
"CREATE EXTENSION \"uuid-ossp\";".to_string(),
format!("CREATE SCHEMA {}", schema.as_str()),
))
.await?;
}
for schema in PostgreSQLSchema::iter() {
let conn = manager.get_connection();
conn.execute(Statement::from_string(
DatabaseBackend::Postgres,
format!("SET search_path = '{}'", schema.as_str()),
))
.await?;
conn.execute(Statement::from_string(
DatabaseBackend::Postgres,
"CREATE EXTENSION \"uuid-ossp\"".to_string(),
))
.await?;
}
manager
.create_table(
Table::create()
// id character varying NOT NULL,
// type text NOT NULL,
// created_by character varying,
// context jsonb,
// result jsonb,
// dry_run boolean DEFAULT false NOT NULL,
// created_at timestamp with time zone DEFAULT now() NOT NULL,
// pre_processed_at timestamp with time zone,
// confirmed_at timestamp with time zone,
// processing_at timestamp with time zone,
// completed_at timestamp with time zone,
// failed_at timestamp with time zone,
// canceled_at timestamp with time zone,
// updated_at timestamp with time zone DEFAULT now() NOT NULL,
// deleted_at timestamp with time zone
.table(BatchJob::BatchJobs)
.col(
ColumnDef::new(BatchJob::Id)
.uuid()
.not_null()
.default(SimpleExpr::Custom("uuid_generate_v4()".into()))
.primary_key(),
)
.col(ColumnDef::new(BatchJob::BatchType).string().not_null())
.col(ColumnDef::new(BatchJob::CreatedBy).uuid())
.col(ColumnDef::new(BatchJob::Context).json_binary())
.col(ColumnDef::new(BatchJob::Result).json_binary())
.col(
ColumnDef::new(BatchJob::DryRun)
.boolean()
.default(false)
.not_null(),
)
.col(
ColumnDef::new(BatchJob::CreatedAt)
.timestamp()
.default(SimpleExpr::Custom("now()".into()))
.not_null(),
)
.col(ColumnDef::new(BatchJob::PreProcessedAt).timestamp())
.col(ColumnDef::new(BatchJob::ConfirmedAt).timestamp())
.col(ColumnDef::new(BatchJob::ProcessingAt).timestamp())
.col(ColumnDef::new(BatchJob::CompletedAt).timestamp())
.col(ColumnDef::new(BatchJob::FailedAt).timestamp())
.col(ColumnDef::new(BatchJob::CanceledAt).timestamp())
.col(
ColumnDef::new(BatchJob::UpdatedAt)
.timestamp()
.default(SimpleExpr::Custom("now()".into()))
.not_null(),
)
.col(ColumnDef::new(BatchJob::DeletedAt).timestamp())
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
// id 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,
// user_id character varying NOT NULL,
// opt_out boolean DEFAULT false NOT NULL,
// anonymize boolean DEFAULT false NOT NULL
.table(AnalyticsConfig::AnalyticsConfigs)
.col(
ColumnDef::new(AnalyticsConfig::Id)
.uuid()
.not_null()
.default(SimpleExpr::Custom("uuid_generate_v4()".into()))
.primary_key(),
)
.col(
ColumnDef::new(AnalyticsConfig::CreatedAt)
.timestamp()
.default(SimpleExpr::Custom("now()".into()))
.not_null(),
)
.col(
ColumnDef::new(AnalyticsConfig::UpdatedAt)
.timestamp()
.default(SimpleExpr::Custom("now()".into()))
.not_null(),
)
.col(ColumnDef::new(AnalyticsConfig::DeletedAt).timestamp())
.col(ColumnDef::new(AnalyticsConfig::UserId).uuid().not_null())
.col(
ColumnDef::new(AnalyticsConfig::OptOut)
.boolean()
.default(false)
.not_null(),
)
.col(
ColumnDef::new(AnalyticsConfig::Anonymize)
.boolean()
.default(false)
.not_null(),
)
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
// id character varying NOT NULL,
// customer_id character varying,
// 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
.table(Address::Addresses)
.if_not_exists()
.col(
ColumnDef::new(Address::Id)
.uuid()
.not_null()
.default(SimpleExpr::FunctionCall(Func::cust(Expr::cust(
"uuid_generate_v4",
))))
.default(SimpleExpr::Custom("uuid_generate_v4()".into()))
.primary_key(),
)
.col(ColumnDef::new(Address::CustomerId).uuid())
.col(ColumnDef::new(Address::Company).string())
.col(ColumnDef::new(Address::FirstName).string())
.col(ColumnDef::new(Address::LastName).string())
.col(ColumnDef::new(Address::Address1).string())
.col(ColumnDef::new(Address::Address2).string())
.col(ColumnDef::new(Address::City).string())
.col(ColumnDef::new(Address::CountryCode).string())
.col(ColumnDef::new(Address::Province).string())
.col(ColumnDef::new(Address::PostalCode).string())
.col(ColumnDef::new(Address::Phone).string())
.col(
ColumnDef::new(Address::CreatedAt)
.timestamp()
.default(SimpleExpr::Custom("now()".into()))
.not_null(),
)
.col(
ColumnDef::new(Address::UpdatedAt)
.timestamp()
.default(SimpleExpr::Custom("now()".into()))
.not_null(),
)
.col(ColumnDef::new(Address::DeletedAt).timestamp())
.col(ColumnDef::new(Address::Metadata).json_binary())
.to_owned(),
)
.await
@ -59,11 +201,58 @@ impl MigrationTrait for Migration {
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Address::Addresses).to_owned())
.await
.await?;
manager
.drop_table(
Table::drop()
.table(AnalyticsConfig::AnalyticsConfigs)
.to_owned(),
)
.await?;
manager
.get_connection()
.execute(Statement::from_string(
DatabaseBackend::Postgres,
"DROP SCHEMA jobs".to_string(),
))
.await?;
Ok(())
}
}
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum BatchJob {
BatchJobs,
Id,
BatchType,
CreatedBy,
Context,
Result,
DryRun,
CreatedAt,
PreProcessedAt,
ConfirmedAt,
ProcessingAt,
CompletedAt,
FailedAt,
CanceledAt,
UpdatedAt,
DeletedAt,
}
#[derive(Iden)]
enum AnalyticsConfig {
AnalyticsConfigs,
Id,
CreatedAt,
UpdatedAt,
DeletedAt,
UserId,
OptOut,
Anonymize,
}
#[derive(Iden)]
enum Address {
Addresses,

View File

@ -0,0 +1,18 @@
use sea_orm::EnumIter;
use sea_orm_migration::prelude::*;
// set_schema_search_path
#[derive(Iden, EnumIter)]
pub enum PostgreSQLSchema {
Default,
Jobs,
}
impl PostgreSQLSchema {
pub fn as_str(&self) -> &'static str {
match self {
PostgreSQLSchema::Default => "",
PostgreSQLSchema::Jobs => "jobs",
}
}
}

View File

@ -204,18 +204,6 @@ CREATE TYPE public.user_roles AS ENUM (
'developer'
);
---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################
CREATE TABLE public.addresses
(
id character varying NOT NULL,
@ -236,14 +224,6 @@ CREATE TABLE public.addresses
metadata jsonb
);
ALTER TABLE public.addresses
OWNER TO postgres;
--
-- Name: analytics_config; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.analytics_configs
(
id character varying NOT NULL,
@ -255,14 +235,6 @@ CREATE TABLE public.analytics_configs
anonymize boolean DEFAULT false NOT NULL
);
ALTER TABLE public.analytics_configs
OWNER TO postgres;
--
-- Name: batch_job; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.batch_jobs
(
id character varying NOT NULL,
@ -282,13 +254,14 @@ CREATE TABLE public.batch_jobs
deleted_at timestamp with time zone
);
ALTER TABLE public.batch_jobs
OWNER TO postgres;
--
-- Name: cart; Type: TABLE; Schema: public; Owner: postgres
--
---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################
CREATE TABLE public.carts
(