diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 2d8fcfc..9f4c38e 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -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), ] } } diff --git a/migration/src/m20230625_211327_create_session.rs b/migration/src/m20230625_211327_create_session.rs new file mode 100644 index 0000000..529c4ae --- /dev/null +++ b/migration/src/m20230625_211327_create_session.rs @@ -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 +} diff --git a/slop-common/src/db/entities/mod.rs b/slop-common/src/db/entities/mod.rs index 5e2a40f..0336908 100644 --- a/slop-common/src/db/entities/mod.rs +++ b/slop-common/src/db/entities/mod.rs @@ -3,4 +3,5 @@ pub mod prelude; pub mod key_pair; +pub mod session; pub mod user; diff --git a/slop-common/src/db/entities/prelude.rs b/slop-common/src/db/entities/prelude.rs index 2a3df84..f9aa167 100644 --- a/slop-common/src/db/entities/prelude.rs +++ b/slop-common/src/db/entities/prelude.rs @@ -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; diff --git a/slop-common/src/db/entities/session.rs b/slop-common/src/db/entities/session.rs new file mode 100644 index 0000000..6c9d0eb --- /dev/null +++ b/slop-common/src/db/entities/session.rs @@ -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, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/slop-common/src/db/mod.rs b/slop-common/src/db/mod.rs index 72a608a..0b8f0b5 100644 --- a/slop-common/src/db/mod.rs +++ b/slop-common/src/db/mod.rs @@ -1 +1 @@ -pub mod entities; \ No newline at end of file +pub mod entities;