Skip to content

Commit

Permalink
Add User table
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksbgbg committed Feb 5, 2024
1 parent 80e507e commit 10896d3
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
1 change: 1 addition & 0 deletions backend-rs/src/models.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod migrations;
pub mod user;
1 change: 1 addition & 0 deletions backend-rs/src/models/migrations.rs
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 backend-rs/src/models/migrations/m20240204_000001_users.rs
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,
}
3 changes: 2 additions & 1 deletion backend-rs/src/models/migrations/migrator.rs
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)]
}
}
27 changes: 27 additions & 0 deletions backend-rs/src/models/user.rs
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 {}

0 comments on commit 10896d3

Please sign in to comment.