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_102634_types;
mod m20230603_120814_addresses; mod m20230603_120814_addresses;
pub mod schema_list;
pub mod types; pub mod types;
pub struct Migrator; pub struct Migrator;

View File

@ -1,7 +1,8 @@
use sea_orm_migration::prelude::*; 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 sea_query::expr::SimpleExpr;
use crate::schema_list::PostgreSQLSchema;
use crate::sea_orm::DatabaseBackend; use crate::sea_orm::DatabaseBackend;
#[derive(DeriveMigrationName)] #[derive(DeriveMigrationName)]
@ -10,47 +11,188 @@ pub struct Migration;
#[async_trait::async_trait] #[async_trait::async_trait]
impl MigrationTrait for Migration { impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
/* for schema in PostgreSQLSchema::iter().skip(1) {
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
*/
manager manager
.get_connection() .get_connection()
.execute(Statement::from_string( .execute(Statement::from_string(
DatabaseBackend::Postgres, DatabaseBackend::Postgres,
"CREATE EXTENSION \"uuid-ossp\";".to_string(), format!("CREATE SCHEMA {}", schema.as_str()),
)) ))
.await?; .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 manager
.create_table( .create_table(
Table::create() 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) .table(Address::Addresses)
.if_not_exists() .if_not_exists()
.col( .col(
ColumnDef::new(Address::Id) ColumnDef::new(Address::Id)
.uuid() .uuid()
.not_null() .not_null()
.default(SimpleExpr::FunctionCall(Func::cust(Expr::cust( .default(SimpleExpr::Custom("uuid_generate_v4()".into()))
"uuid_generate_v4",
))))
.primary_key(), .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(), .to_owned(),
) )
.await .await
@ -59,11 +201,58 @@ impl MigrationTrait for Migration {
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager manager
.drop_table(Table::drop().table(Address::Addresses).to_owned()) .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)] #[derive(Iden)]
enum Address { enum Address {
Addresses, 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' 'developer'
); );
---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################
CREATE TABLE public.addresses CREATE TABLE public.addresses
( (
id character varying NOT NULL, id character varying NOT NULL,
@ -236,14 +224,6 @@ CREATE TABLE public.addresses
metadata jsonb metadata jsonb
); );
ALTER TABLE public.addresses
OWNER TO postgres;
--
-- Name: analytics_config; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.analytics_configs CREATE TABLE public.analytics_configs
( (
id character varying NOT NULL, id character varying NOT NULL,
@ -255,14 +235,6 @@ CREATE TABLE public.analytics_configs
anonymize boolean DEFAULT false NOT NULL 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 CREATE TABLE public.batch_jobs
( (
id character varying NOT NULL, id character varying NOT NULL,
@ -282,13 +254,14 @@ CREATE TABLE public.batch_jobs
deleted_at timestamp with time zone 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 CREATE TABLE public.carts
( (