diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 953d082..e167053 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -1,6 +1,7 @@ pub use sea_orm_migration::prelude::*; mod m20230603_102634_types; +mod m20230603_120814_addresses; pub mod types; pub struct Migrator; @@ -8,6 +9,9 @@ pub struct Migrator; #[async_trait::async_trait] impl MigratorTrait for Migrator { fn migrations() -> Vec> { - vec![Box::new(m20230603_102634_types::Migration)] + vec![ + Box::new(m20230603_102634_types::Migration), + Box::new(m20230603_120814_addresses::Migration), + ] } } diff --git a/migration/src/m20230603_102634_types.rs b/migration/src/m20230603_102634_types.rs index 6fad665..40f8e7b 100644 --- a/migration/src/m20230603_102634_types.rs +++ b/migration/src/m20230603_102634_types.rs @@ -1,4 +1,4 @@ -use sea_orm::{EnumIter, Iterable}; +use sea_orm::Iterable; use sea_orm_migration::prelude::*; use crate::extension::postgres::Type; diff --git a/migration/src/m20230603_120814_addresses.rs b/migration/src/m20230603_120814_addresses.rs new file mode 100644 index 0000000..520e373 --- /dev/null +++ b/migration/src/m20230603_120814_addresses.rs @@ -0,0 +1,86 @@ +use sea_orm_migration::prelude::*; +use sea_orm_migration::sea_orm::Statement; +use sea_query::expr::SimpleExpr; + +use crate::sea_orm::DatabaseBackend; + +#[derive(DeriveMigrationName)] +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 + */ + + manager + .get_connection() + .execute(Statement::from_string( + DatabaseBackend::Postgres, + "CREATE EXTENSION \"uuid-ossp\";".to_string(), + )) + .await?; + manager + .create_table( + Table::create() + .table(Address::Addresses) + .if_not_exists() + .col( + ColumnDef::new(Address::Id) + .uuid() + .not_null() + .default(SimpleExpr::FunctionCall(Func::cust(Expr::cust( + "uuid_generate_v4", + )))) + .primary_key(), + ) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(Address::Addresses).to_owned()) + .await + } +} + +/// Learn more at https://docs.rs/sea-query#iden +#[derive(Iden)] +enum Address { + Addresses, + Id, + CustomerId, + Company, + FirstName, + LastName, + Address1, + Address2, + City, + CountryCode, + Province, + PostalCode, + Phone, + CreatedAt, + UpdatedAt, + DeletedAt, + Metadata, +} diff --git a/migration/src/types.rs b/migration/src/types.rs index 815e386..a4a2e9f 100644 --- a/migration/src/types.rs +++ b/migration/src/types.rs @@ -1,4 +1,4 @@ -use sea_orm::{EnumIter, Iterable}; +use sea_orm::EnumIter; use sea_orm_migration::prelude::*; #[derive(Iden, EnumIter)]