From f74fbde3796bd560699ed0eb05a712e7f15bce67 Mon Sep 17 00:00:00 2001 From: Sphereso Date: Fri, 5 Jul 2024 15:14:36 +0200 Subject: [PATCH] backend: user migration --- crates/migration/src/lib.rs | 6 +-- .../src/m20220101_000001_create_table.rs | 47 ------------------- .../src/m20240705_100914_create_user.rs | 45 ++++++++++++++++++ 3 files changed, 48 insertions(+), 50 deletions(-) delete mode 100644 crates/migration/src/m20220101_000001_create_table.rs create mode 100644 crates/migration/src/m20240705_100914_create_user.rs diff --git a/crates/migration/src/lib.rs b/crates/migration/src/lib.rs index 2c605af..04bbb7d 100644 --- a/crates/migration/src/lib.rs +++ b/crates/migration/src/lib.rs @@ -1,12 +1,12 @@ pub use sea_orm_migration::prelude::*; -mod m20220101_000001_create_table; - pub struct Migrator; +mod m20240705_100914_create_user; + #[async_trait::async_trait] impl MigratorTrait for Migrator { fn migrations() -> Vec> { - vec![Box::new(m20220101_000001_create_table::Migration)] + vec![Box::new(m20240705_100914_create_user::Migration)] } } diff --git a/crates/migration/src/m20220101_000001_create_table.rs b/crates/migration/src/m20220101_000001_create_table.rs deleted file mode 100644 index c8f2a5d..0000000 --- a/crates/migration/src/m20220101_000001_create_table.rs +++ /dev/null @@ -1,47 +0,0 @@ -use sea_orm_migration::prelude::*; - -#[derive(DeriveMigrationName)] -pub struct Migration; - -#[async_trait::async_trait] -impl MigrationTrait for Migration { - async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { - // Replace the sample below with your own migration scripts - todo!(); - - manager - .create_table( - Table::create() - .table(Post::Table) - .if_not_exists() - .col( - ColumnDef::new(Post::Id) - .integer() - .not_null() - .auto_increment() - .primary_key(), - ) - .col(ColumnDef::new(Post::Title).string().not_null()) - .col(ColumnDef::new(Post::Text).string().not_null()) - .to_owned(), - ) - .await - } - - async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { - // Replace the sample below with your own migration scripts - todo!(); - - manager - .drop_table(Table::drop().table(Post::Table).to_owned()) - .await - } -} - -#[derive(DeriveIden)] -enum Post { - Table, - Id, - Title, - Text, -} diff --git a/crates/migration/src/m20240705_100914_create_user.rs b/crates/migration/src/m20240705_100914_create_user.rs new file mode 100644 index 0000000..a8c940f --- /dev/null +++ b/crates/migration/src/m20240705_100914_create_user.rs @@ -0,0 +1,45 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(User::Table) + .if_not_exists() + .col( + ColumnDef::new(User::Id) + .uuid() + .extra("DEFAULT gen_random_uuid()") + .primary_key() + .not_null(), + ) + .col(ColumnDef::new(User::Name).string().not_null()) + .col(ColumnDef::new(User::Email).string().not_null().unique_key()) + .col(ColumnDef::new(User::Hash).string().not_null()) + .col(ColumnDef::new(User::Salt).string().not_null()) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(User::Table).to_owned()) + .await + } +} + +#[derive(DeriveIden)] +enum User { + Table, + Id, + Name, + Email, + Hash, + Salt, +}