mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 09:47:50 +03:00
feat(database)!: switch from diesel to sea-orm
This commit is contained in:
11
database/Cargo.toml
Normal file
11
database/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "database"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
migration = { path = "migration" }
|
||||
entity = { path = "entity" }
|
||||
sea-orm = { version = "2.0.0-rc.6", features = ["sqlx-postgres", "runtime-tokio"] }
|
||||
|
||||
paste = "1.0.15"
|
||||
1
database/entity/.gitignore
vendored
Normal file
1
database/entity/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
9
database/entity/Cargo.toml
Normal file
9
database/entity/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "entity"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
sea-orm = "2.0.0-rc.6"
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
utoipa = "5.4.0"
|
||||
6
database/entity/src/lib.rs
Normal file
6
database/entity/src/lib.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.12
|
||||
|
||||
pub mod prelude;
|
||||
|
||||
pub mod sea_orm_active_enums;
|
||||
pub mod user;
|
||||
3
database/entity/src/prelude.rs
Normal file
3
database/entity/src/prelude.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.12
|
||||
|
||||
pub use super::user::Entity as User;
|
||||
25
database/entity/src/sea_orm_active_enums.rs
Normal file
25
database/entity/src/sea_orm_active_enums.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.12
|
||||
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(
|
||||
Debug,
|
||||
Clone,
|
||||
PartialEq,
|
||||
Eq,
|
||||
EnumIter,
|
||||
DeriveActiveEnum,
|
||||
:: serde :: Serialize,
|
||||
:: serde :: Deserialize,
|
||||
:: utoipa :: ToSchema,
|
||||
)]
|
||||
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "user_role")]
|
||||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
||||
pub enum UserRole {
|
||||
#[sea_orm(string_value = "student")]
|
||||
Student,
|
||||
#[sea_orm(string_value = "teacher")]
|
||||
Teacher,
|
||||
#[sea_orm(string_value = "admin")]
|
||||
Admin,
|
||||
}
|
||||
25
database/entity/src/user.rs
Normal file
25
database/entity/src/user.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.12
|
||||
|
||||
use super::sea_orm_active_enums::UserRole;
|
||||
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)]
|
||||
pub id: String,
|
||||
#[sea_orm(unique)]
|
||||
pub username: String,
|
||||
pub password: Option<String>,
|
||||
pub vk_id: Option<i32>,
|
||||
pub group: Option<String>,
|
||||
pub role: UserRole,
|
||||
pub android_version: Option<String>,
|
||||
#[sea_orm(unique)]
|
||||
pub telegram_id: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
1
database/migration/.gitignore
vendored
Normal file
1
database/migration/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
22
database/migration/Cargo.toml
Normal file
22
database/migration/Cargo.toml
Normal file
@@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "migration"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
[lib]
|
||||
name = "migration"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
async-std = { version = "1", features = ["attributes", "tokio1"] }
|
||||
|
||||
[dependencies.sea-orm-migration]
|
||||
version = "2.0.0-rc.6"
|
||||
features = [
|
||||
# Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI.
|
||||
# View the list of supported features at https://www.sea-ql.org/SeaORM/docs/install-and-config/database-and-async-runtime.
|
||||
# e.g.
|
||||
"runtime-tokio", # `ASYNC_RUNTIME` feature
|
||||
"sqlx-postgres", # `DATABASE_DRIVER` feature
|
||||
]
|
||||
16
database/migration/src/lib.rs
Normal file
16
database/migration/src/lib.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
pub use sea_orm_migration::prelude::MigratorTrait;
|
||||
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
mod m20250904_024854_init;
|
||||
|
||||
pub struct Migrator;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigratorTrait for Migrator {
|
||||
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
|
||||
vec![
|
||||
Box::new(m20250904_024854_init::Migration),
|
||||
]
|
||||
}
|
||||
}
|
||||
70
database/migration/src/m20250904_024854_init.rs
Normal file
70
database/migration/src/m20250904_024854_init.rs
Normal file
@@ -0,0 +1,70 @@
|
||||
use sea_orm_migration::prelude::extension::postgres::Type;
|
||||
use sea_orm_migration::sea_orm::{EnumIter, Iterable};
|
||||
use sea_orm_migration::{prelude::*, schema::*};
|
||||
|
||||
#[derive(DeriveMigrationName)]
|
||||
pub struct Migration;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.create_type(
|
||||
Type::create()
|
||||
.as_enum(UserRole)
|
||||
.values(UserRoleVariants::iter())
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(User::Table)
|
||||
.if_not_exists()
|
||||
.col(string_uniq(User::Id).primary_key().not_null())
|
||||
.col(string_uniq(User::Username).not_null())
|
||||
.col(string_null(User::Password))
|
||||
.col(integer_null(User::VkId))
|
||||
.col(string_null(User::Group))
|
||||
.col(enumeration(User::Role, UserRole, UserRoleVariants::iter()))
|
||||
.col(string_null(User::AndroidVersion))
|
||||
.col(big_integer_null(User::TelegramId).unique_key())
|
||||
.to_owned(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(User::Table).to_owned())
|
||||
.await?;
|
||||
|
||||
manager
|
||||
.drop_type(Type::drop().name(UserRole).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
struct UserRole;
|
||||
|
||||
#[derive(DeriveIden, EnumIter)]
|
||||
enum UserRoleVariants {
|
||||
Student,
|
||||
Teacher,
|
||||
Admin,
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum User {
|
||||
Table,
|
||||
Id,
|
||||
Username,
|
||||
Password,
|
||||
VkId,
|
||||
Group,
|
||||
Role,
|
||||
AndroidVersion,
|
||||
TelegramId,
|
||||
}
|
||||
6
database/migration/src/main.rs
Normal file
6
database/migration/src/main.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[async_std::main]
|
||||
async fn main() {
|
||||
cli::run_cli(migration::Migrator).await;
|
||||
}
|
||||
10
database/src/lib.rs
Normal file
10
database/src/lib.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
pub mod query;
|
||||
|
||||
pub use migration;
|
||||
pub use sea_orm;
|
||||
|
||||
pub mod entity {
|
||||
pub use entity::*;
|
||||
|
||||
pub use entity::user::{ActiveModel as ActiveUser, Model as User, Entity as UserEntity, Column as UserColumn};
|
||||
}
|
||||
62
database/src/query.rs
Normal file
62
database/src/query.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use paste::paste;
|
||||
use sea_orm::ColumnTrait;
|
||||
use sea_orm::EntityTrait;
|
||||
use sea_orm::QueryFilter;
|
||||
|
||||
pub struct Query;
|
||||
|
||||
macro_rules! ref_type {
|
||||
(String) => {
|
||||
&String
|
||||
};
|
||||
(str) => {
|
||||
&str
|
||||
};
|
||||
($other:ty) => {
|
||||
$other
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! define_is_exists {
|
||||
($entity: ident, $by: ident, $by_type: ident, $by_column: ident) => {
|
||||
paste! {
|
||||
pub async fn [<is_ $entity _exists_by_ $by>](
|
||||
db: &::sea_orm::DbConn,
|
||||
$by: ref_type!($by_type)
|
||||
) -> Result<bool, ::sea_orm::DbErr> {
|
||||
::entity::$entity::Entity::find()
|
||||
.filter(::entity::$entity::Column::$by_column.eq($by))
|
||||
.one(db)
|
||||
.await
|
||||
.map(|x| x.is_some())
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! define_find_by {
|
||||
($entity: ident, $by: ident, $by_type: ident, $by_column: ident) => {
|
||||
paste! {
|
||||
pub async fn [<find_ $entity _by_ $by>](
|
||||
db: &::sea_orm::DbConn,
|
||||
$by: ref_type!($by_type)
|
||||
) -> Result<Option<::entity::$entity::Model>, ::sea_orm::DbErr> {
|
||||
::entity::$entity::Entity::find()
|
||||
.filter(::entity::$entity::Column::$by_column.eq($by))
|
||||
.one(db)
|
||||
.await
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl Query {
|
||||
define_find_by!(user, id, str, Id);
|
||||
define_find_by!(user, telegram_id, i64, TelegramId);
|
||||
define_find_by!(user, vk_id, i32, VkId);
|
||||
define_find_by!(user, username, str, Username);
|
||||
|
||||
define_is_exists!(user, id, str, Id);
|
||||
define_is_exists!(user, username, str, Username);
|
||||
define_is_exists!(user, telegram_id, i64, TelegramId);
|
||||
}
|
||||
Reference in New Issue
Block a user