-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod migrations; | ||
pub mod user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod m20240204_000001_users; | ||
pub mod migrator; |
66 changes: 66 additions & 0 deletions
66
backend-rs/src/models/migrations/m20240204_000001_users.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use sea_orm_migration::prelude::*; | ||
|
||
pub struct Migration; | ||
|
||
impl MigrationName for Migration { | ||
fn name(&self) -> &str { | ||
"m_20240204_000001_users" | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl MigrationTrait for Migration { | ||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.create_table( | ||
Table::create() | ||
.table(User::Table) | ||
.col( | ||
ColumnDef::new(User::Id) | ||
.big_integer() | ||
.not_null() | ||
.primary_key(), | ||
) | ||
.col( | ||
ColumnDef::new(User::CreatedAt) | ||
.timestamp_with_time_zone() | ||
.not_null(), | ||
) | ||
.col( | ||
ColumnDef::new(User::UpdatedAt) | ||
.timestamp_with_time_zone() | ||
.not_null(), | ||
) | ||
.col(ColumnDef::new(User::Username).text().unique_key()) | ||
.col(ColumnDef::new(User::CanonicalUsername).text().unique_key()) | ||
.col(ColumnDef::new(User::EmailAddress).text().unique_key()) | ||
.col( | ||
ColumnDef::new(User::CanonicalEmailAddress) | ||
.text() | ||
.unique_key(), | ||
) | ||
.col(ColumnDef::new(User::Password).text()) | ||
.to_owned(), | ||
) | ||
.await | ||
} | ||
|
||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.drop_table(Table::drop().table(User::Table).to_owned()) | ||
.await | ||
} | ||
} | ||
|
||
#[derive(Iden)] | ||
pub enum User { | ||
Table, | ||
Id, | ||
CreatedAt, | ||
UpdatedAt, | ||
Username, | ||
EmailAddress, | ||
Password, | ||
CanonicalUsername, | ||
CanonicalEmailAddress, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
use crate::models::migrations::m20240204_000001_users; | ||
use sea_orm_migration::prelude::*; | ||
|
||
pub struct Migrator; | ||
|
||
#[async_trait::async_trait] | ||
impl MigratorTrait for Migrator { | ||
fn migrations() -> Vec<Box<dyn MigrationTrait>> { | ||
vec![] | ||
vec![Box::new(m20240204_000001_users::Migration)] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use crate::app_state::AppState; | ||
use chrono::Local; | ||
use sea_orm::entity::prelude::*; | ||
use sea_orm::ActiveValue; | ||
|
||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] | ||
#[sea_orm(table_name = "user")] | ||
pub struct Model { | ||
#[sea_orm(primary_key, auto_increment = false)] | ||
pub id: i64, | ||
pub created_at: DateTimeWithTimeZone, | ||
pub updated_at: DateTimeWithTimeZone, | ||
#[sea_orm(unique)] | ||
pub username: Option<String>, | ||
#[sea_orm(unique)] | ||
pub canonical_username: Option<String>, | ||
#[sea_orm(column_type = "Text", nullable, unique)] | ||
pub email_address: Option<String>, | ||
#[sea_orm(column_type = "Text", nullable, unique)] | ||
pub canonical_email_address: Option<String>, | ||
pub password: Option<String>, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation {} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |