Skip to content

Commit

Permalink
fix: add new db migration
Browse files Browse the repository at this point in the history
  • Loading branch information
sargon64 committed Jul 27, 2023
1 parent 9373e0c commit 2837539
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 11 deletions.
2 changes: 2 additions & 0 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub use sea_orm_migration::prelude::*;

mod m20220101_000001_create_table;
mod m20230727_015400_load_default_data;
mod m20230727_063415_create_mod_beat_saber_versions;

pub struct Migrator;

Expand All @@ -11,6 +12,7 @@ impl MigratorTrait for Migrator {
vec![
Box::new(m20220101_000001_create_table::Migration),
Box::new(m20230727_015400_load_default_data::Migration),
Box::new(m20230727_063415_create_mod_beat_saber_versions::Migration),
]
}
}
87 changes: 87 additions & 0 deletions migration/src/m20230727_063415_create_mod_beat_saber_versions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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(ModBeatSaberVersions::Table)
.if_not_exists()
.col(
ColumnDef::new(ModBeatSaberVersions::ModId)
.uuid()
.not_null(),
)
.col(
ColumnDef::new(ModBeatSaberVersions::BeatSaberVersionId)
.uuid()
.not_null(),
)
.foreign_key(
ForeignKey::create()
.name("fk_mod_beat_saber_versions_mods_mod_id")
.from(
ModBeatSaberVersions::Table,
ModBeatSaberVersions::ModId,
)
.to(Mods::Table, Mods::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk_mod_beat_saber_versions_beat_saber_versions_beat_saber_version_id")
.from(
ModBeatSaberVersions::Table,
ModBeatSaberVersions::BeatSaberVersionId,
)
.to(BeatSaberVersions::Table, BeatSaberVersions::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(ModBeatSaberVersions::Table).to_owned())
.await
}
}

/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum ModBeatSaberVersions {
Table,
ModId,
BeatSaberVersionId,
}

#[derive(Iden)]
enum Mods {
Table,
Id,
Slug,

Check warning on line 70 in migration/src/m20230727_063415_create_mod_beat_saber_versions.rs

View workflow job for this annotation

GitHub Actions / Build and Publish

multiple variants are never constructed

Check warning on line 70 in migration/src/m20230727_063415_create_mod_beat_saber_versions.rs

View workflow job for this annotation

GitHub Actions / Build and Publish

multiple variants are never constructed
Name,
Description,
Icon,
Cover,
Author,
Category,
Stats,
CreatedAt,
UpdatedAt,
}

#[derive(Iden)]
enum BeatSaberVersions {
Table,
Id,
Ver,

Check warning on line 86 in migration/src/m20230727_063415_create_mod_beat_saber_versions.rs

View workflow job for this annotation

GitHub Actions / Build and Publish

variant `Ver` is never constructed

Check warning on line 86 in migration/src/m20230727_063415_create_mod_beat_saber_versions.rs

View workflow job for this annotation

GitHub Actions / Build and Publish

variant `Ver` is never constructed
}
2 changes: 1 addition & 1 deletion src/mods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub struct ModCategory {
pub desc: Option<String>,
}

pub async fn find_all(db: &Database, limit: i32, offset: i32) -> FieldResult<Vec<Mod>> {
pub async fn find_all(db: &Database, limit: i32, offset: i32, version: Option<String>) -> FieldResult<Vec<Mod>> {

Check warning on line 97 in src/mods.rs

View workflow job for this annotation

GitHub Actions / Build and Publish

unused variable: `version`

Check warning on line 97 in src/mods.rs

View workflow job for this annotation

GitHub Actions / Build and Publish

unused variable: `version`
let limit = limit as u64;
let offset = offset as u64;

Expand Down
25 changes: 18 additions & 7 deletions src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use juniper::{graphql_value, EmptyMutation, EmptySubscription, FieldResult, RootNode};
use juniper::{
graphql_value, EmptyMutation, EmptySubscription, FieldResult, GraphQLEnum, RootNode,
};

#[derive(GraphQLEnum)]
enum Episode {
Expand All @@ -7,7 +9,6 @@ enum Episode {
Jedi,
}

use juniper::{GraphQLEnum};
use uuid::Uuid;

use crate::auth::Authorization;
Expand All @@ -22,30 +23,40 @@ impl QueryRoot {
async fn user_by_id(db: &Database, id: Uuid, auth: Option<String>) -> FieldResult<User> {
users::find_by_id(db, id, Authorization::parse(auth)).await
}

async fn users(
db: &Database,
limit: Option<i32>,
offset: Option<i32>,
auth: Option<String>,
_version: Option<String>,
) -> FieldResult<Vec<User>> {
if limit > Some(10) {
return Err(juniper::FieldError::new(
"Limit must be less than 10",
graphql_value!({ "limit": "Limit must be less than 10" }),
));
}
users::find_all(db, limit.unwrap_or(10), offset.unwrap_or(0), Authorization::parse(auth)).await
users::find_all(
db,
limit.unwrap_or(10),
offset.unwrap_or(0),
Authorization::parse(auth),
)
.await
}
async fn mods(db: &Database, limit: Option<i32>, offset: Option<i32>) -> FieldResult<Vec<Mod>> {
async fn mods(
db: &Database,
limit: Option<i32>,
offset: Option<i32>,
version: Option<String>,
) -> FieldResult<Vec<Mod>> {
if limit > Some(10) {
return Err(juniper::FieldError::new(
"Limit must be less than 10",
graphql_value!({ "limit": "Limit must be less than 10" }),
));
}
mods::find_all(db, limit.unwrap_or(10), offset.unwrap_or(0)).await
mods::find_all(db, limit.unwrap_or(10), offset.unwrap_or(0), version).await
}
async fn mod_by_id(db: &Database, id: Uuid) -> FieldResult<Mod> {
mods::find_by_id(db, id).await
Expand Down
4 changes: 1 addition & 3 deletions src/versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use serde::{Serialize, Deserialize};
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
use uuid::Uuid;

use crate::{
Database,
};
use crate::Database;

#[derive(GraphQLObject, Debug, Deserialize, Serialize)]
pub struct GVersion {
Expand Down

0 comments on commit 2837539

Please sign in to comment.