Create session database table

This commit is contained in:
Evie Viau-Chow-Stuart 2023-06-26 01:57:32 -04:00
parent aecaf0cca3
commit bdfe6bc507
Signed by: evie
GPG key ID: 928652CDFCEC8099
6 changed files with 79 additions and 1 deletions

View file

@ -2,6 +2,7 @@ pub use sea_orm_migration::prelude::*;
mod m20230619_221344_create_user;
mod m20230620_232650_create_keypair;
mod m20230625_211327_create_session;
pub struct Migrator;
@ -11,6 +12,7 @@ impl MigratorTrait for Migrator {
vec![
Box::new(m20230619_221344_create_user::Migration),
Box::new(m20230620_232650_create_keypair::Migration),
Box::new(m20230625_211327_create_session::Migration),
]
}
}

View file

@ -0,0 +1,51 @@
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(Session::Table)
.if_not_exists()
.col(
ColumnDef::new(Session::Id)
.string()
.not_null()
.primary_key(),
)
.col(ColumnDef::new(Session::Token).string().not_null())
.col(ColumnDef::new(Session::User).string().not_null())
.col(ColumnDef::new(Session::Issued).date_time().not_null())
.col(ColumnDef::new(Session::Expiry).date_time().not_null())
.col(ColumnDef::new(Session::Device).string().not_null())
.col(ColumnDef::new(Session::Ip).inet().not_null())
.col(ColumnDef::new(Session::LastUsage).date_time())
.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,
Token,
User,
Issued,
Expiry,
Device,
Ip,
LastUsage
}

View file

@ -3,4 +3,5 @@
pub mod prelude;
pub mod key_pair;
pub mod session;
pub mod user;

View file

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

View file

@ -0,0 +1,23 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
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)]
pub id: String,
pub token: String,
pub user: String,
pub issued: DateTime,
pub expiry: DateTime,
pub device: String,
#[sea_orm(column_type = "custom(\"inet\")")]
pub ip: String,
pub last_usage: Option<DateTime>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -1 +1 @@
pub mod entities;
pub mod entities;