Add session table

This commit is contained in:
Evie Viau-Chow-Stuart 2023-03-22 03:57:52 -07:00
parent 494719409d
commit acccf035b8
Signed by: evie
GPG key ID: 928652CDFCEC8099
8 changed files with 134 additions and 2 deletions

View file

@ -1,12 +1,16 @@
pub use sea_orm_migration::prelude::*;
mod m20220101_000001_create_user;
mod m20230322_103045_create_session;
pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20220101_000001_create_user::Migration)]
vec![
Box::new(m20220101_000001_create_user::Migration),
Box::new(m20230322_103045_create_session::Migration),
]
}
}

View file

@ -36,7 +36,7 @@ impl MigrationTrait for Migration {
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum User {
pub enum User {
Table,
Id,
Username,

View file

@ -0,0 +1,50 @@
use sea_orm_migration::prelude::*;
use crate::m20220101_000001_create_user::User;
#[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(Session::Table)
.if_not_exists()
.col(
ColumnDef::new(Session::Id)
.text()
.not_null()
.primary_key(),
)
.col(ColumnDef::new(Session::User).text().not_null())
.col(ColumnDef::new(Session::Token).text().not_null())
.col(ColumnDef::new(Session::Expiry).date_time().not_null())
.foreign_key(
ForeignKey::create()
.name("fk-session-user_id")
.from(Session::Table, Session::User)
.to(User::Table, User::Id)
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Session::Table).to_owned())
.await
}
}
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum Session {
Table,
Id,
User,
Token,
Expiry
}

6
src/entities/mod.rs Normal file
View file

@ -0,0 +1,6 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.1
pub mod prelude;
pub mod session;
pub mod user;

4
src/entities/prelude.rs Normal file
View file

@ -0,0 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.1
pub use super::session::Entity as Session;
pub use super::user::Entity as User;

35
src/entities/session.rs Normal file
View file

@ -0,0 +1,35 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.1
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "session")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false, column_type = "Text")]
pub id: String,
#[sea_orm(column_type = "Text")]
pub user: String,
#[sea_orm(column_type = "Text")]
pub token: String,
pub expiry: DateTime,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::user::Entity",
from = "Column::User",
to = "super::user::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
)]
User,
}
impl Related<super::user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

32
src/entities/user.rs Normal file
View file

@ -0,0 +1,32 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.1
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "user")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false, column_type = "Text")]
pub id: String,
#[sea_orm(column_type = "Text")]
pub username: String,
#[sea_orm(column_type = "Text")]
pub email: String,
#[sea_orm(column_type = "Text")]
pub password_hash: String,
pub bio: String,
pub flags: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::session::Entity")]
Session,
}
impl Related<super::session::Entity> for Entity {
fn to() -> RelationDef {
Relation::Session.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -1,6 +1,7 @@
mod options;
mod api;
mod frontend;
mod entities;
use std::net::SocketAddr;
use axum::Router;