You've already forked AstralRinth
forked from didirus/AstralRinth
Switch to Postgres (#39)
* WIP Switch to Postgres * feat(postgres): more work on porting to postgres, now compiles * feat(docker-compose): Changed the docker-compose.yml file to use postgres. * Update docker, documentation, gh actions... * Remove bson dependency * Remove bson import * feat: move mock filehost to trait rather than cargo feature * feat(postgres): transactions for mod creation, multipart refactor * fix: Add Cargo.lock so that sqlx functions * Update sqlx offline build data * fix: Use SQLX_OFFLINE to force sqlx into offline mode for CI * Default release channels * feat(postgres): refactor database models to fit postgres models * fix: Fix sqlx prepare, fix double allocation in indexing * Add dockerfile (#40) Co-authored-by: Charalampos Fanoulis <charalampos.fanoulis@gmail.com> Co-authored-by: Aeledfyr <aeledfyr@gmail.com> Co-authored-by: redblueflame <contact@redblueflame.com> Co-authored-by: Jai A <jai.a@tuta.io> Co-authored-by: Valentin Ricard <redblueflame1@gmail.Com> Co-authored-by: Charalampos Fanoulis <charalampos.fanoulis@gmail.com>
This commit is contained in:
@@ -1,48 +1,215 @@
|
||||
use crate::database::models::Item;
|
||||
use crate::database::Result;
|
||||
use bson::{Bson, Document};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use super::ids::*;
|
||||
use super::DatabaseError;
|
||||
|
||||
//TODO: Files should probably be moved to their own table
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Version {
|
||||
///The unqiue VersionId of this version
|
||||
pub version_id: i32,
|
||||
/// The ModId of the mod that this version belongs to
|
||||
pub mod_id: i32,
|
||||
pub struct VersionBuilder {
|
||||
pub version_id: VersionId,
|
||||
pub mod_id: ModId,
|
||||
pub name: String,
|
||||
pub number: String,
|
||||
pub version_number: String,
|
||||
pub changelog_url: Option<String>,
|
||||
pub date_published: String,
|
||||
pub downloads: i32,
|
||||
pub files: Vec<VersionFile>,
|
||||
pub dependencies: Vec<i32>,
|
||||
pub game_versions: Vec<String>,
|
||||
pub loaders: Vec<String>,
|
||||
pub version_type: String,
|
||||
pub files: Vec<VersionFileBuilder>,
|
||||
pub dependencies: Vec<VersionId>,
|
||||
pub game_versions: Vec<GameVersionId>,
|
||||
pub loaders: Vec<LoaderId>,
|
||||
pub release_channel: ChannelId,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct VersionFile {
|
||||
pub game_versions: Vec<String>,
|
||||
pub hashes: Vec<FileHash>,
|
||||
pub struct VersionFileBuilder {
|
||||
pub url: String,
|
||||
pub filename: String,
|
||||
pub hashes: Vec<HashBuilder>,
|
||||
}
|
||||
|
||||
/// A hash of a mod's file
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct FileHash {
|
||||
pub struct HashBuilder {
|
||||
pub algorithm: String,
|
||||
pub hash: String,
|
||||
pub hash: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Item for Version {
|
||||
fn get_collection() -> &'static str {
|
||||
"versions"
|
||||
}
|
||||
impl VersionBuilder {
|
||||
pub async fn insert(
|
||||
self,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
) -> Result<VersionId, DatabaseError> {
|
||||
let version = Version {
|
||||
id: self.version_id,
|
||||
mod_id: self.mod_id,
|
||||
name: self.name,
|
||||
version_number: self.version_number,
|
||||
changelog_url: self.changelog_url,
|
||||
date_published: chrono::Utc::now(),
|
||||
downloads: 0,
|
||||
release_channel: self.release_channel,
|
||||
};
|
||||
|
||||
fn from_doc(elem: Document) -> Result<Box<Version>> {
|
||||
let version: Version = bson::from_bson(Bson::from(elem))?;
|
||||
Ok(Box::from(version))
|
||||
version.insert(&mut *transaction).await?;
|
||||
|
||||
for file in self.files {
|
||||
let file_id = generate_file_id(&mut *transaction).await?;
|
||||
sqlx::query(
|
||||
"
|
||||
INSERT INTO files (id, version_id, url, filename)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
",
|
||||
)
|
||||
.bind(file_id)
|
||||
.bind(self.version_id)
|
||||
.bind(file.url)
|
||||
.bind(file.filename)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
|
||||
for hash in file.hashes {
|
||||
sqlx::query(
|
||||
"
|
||||
INSERT INTO hashes (file_id, algorithm, hash)
|
||||
VALUES ($1, $2, $3)
|
||||
",
|
||||
)
|
||||
.bind(file_id)
|
||||
.bind(hash.algorithm)
|
||||
.bind(hash.hash)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
for dependency in self.dependencies {
|
||||
sqlx::query(
|
||||
"
|
||||
INSERT INTO dependencies (dependent_id, dependency_id)
|
||||
VALUES ($1, $2)
|
||||
",
|
||||
)
|
||||
.bind(self.version_id)
|
||||
.bind(dependency)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
}
|
||||
|
||||
for loader in self.loaders {
|
||||
sqlx::query(
|
||||
"
|
||||
INSERT INTO dependencies (loader_id, version_id)
|
||||
VALUES ($1, $2)
|
||||
",
|
||||
)
|
||||
.bind(loader)
|
||||
.bind(self.version_id)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
}
|
||||
|
||||
for game_version in self.game_versions {
|
||||
sqlx::query(
|
||||
"
|
||||
INSERT INTO dependencies (game_version_id, joining_version_id)
|
||||
VALUES ($1, $2)
|
||||
",
|
||||
)
|
||||
.bind(game_version)
|
||||
.bind(self.version_id)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(self.version_id)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Version {
|
||||
pub id: VersionId,
|
||||
pub mod_id: ModId,
|
||||
pub name: String,
|
||||
pub version_number: String,
|
||||
pub changelog_url: Option<String>,
|
||||
pub date_published: chrono::DateTime<chrono::Utc>,
|
||||
pub downloads: i32,
|
||||
pub release_channel: ChannelId,
|
||||
}
|
||||
|
||||
impl Version {
|
||||
pub async fn insert(
|
||||
&self,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
) -> Result<(), sqlx::error::Error> {
|
||||
sqlx::query(
|
||||
"
|
||||
INSERT INTO versions (
|
||||
id, mod_id, name, version_number,
|
||||
changelog_url, date_published,
|
||||
downloads, release_channel
|
||||
)
|
||||
VALUES (
|
||||
$1, $2, $3, $4,
|
||||
$5, $6,
|
||||
$7, $8
|
||||
)
|
||||
",
|
||||
)
|
||||
.bind(self.id)
|
||||
.bind(self.mod_id)
|
||||
.bind(&self.name)
|
||||
.bind(&self.version_number)
|
||||
.bind(self.changelog_url.as_ref())
|
||||
.bind(self.date_published)
|
||||
.bind(self.downloads)
|
||||
.bind(self.release_channel)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_dependencies<'a, E>(&self, exec: E) -> Result<Vec<VersionId>, sqlx::Error>
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
{
|
||||
use futures::stream::TryStreamExt;
|
||||
|
||||
let vec = sqlx::query_as::<_, (VersionId,)>(
|
||||
"
|
||||
SELECT id FROM versions v
|
||||
INNER JOIN dependencies d ON d.dependency_id = v.id
|
||||
WHERE d.dependent_id = $1
|
||||
",
|
||||
)
|
||||
.bind(self.id)
|
||||
.fetch_many(exec)
|
||||
.try_filter_map(|e| async { Ok(e.right().map(|(v,)| v)) })
|
||||
.try_collect::<Vec<VersionId>>()
|
||||
.await?;
|
||||
|
||||
Ok(vec)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ReleaseChannel {
|
||||
pub id: ChannelId,
|
||||
pub channel: String,
|
||||
}
|
||||
pub struct Loader {
|
||||
pub id: LoaderId,
|
||||
pub loader: String,
|
||||
}
|
||||
pub struct GameVersion {
|
||||
pub id: GameVersionId,
|
||||
pub version: String,
|
||||
}
|
||||
|
||||
pub struct VersionFile {
|
||||
pub id: FileId,
|
||||
pub version_id: VersionId,
|
||||
pub url: String,
|
||||
pub filename: String,
|
||||
}
|
||||
|
||||
pub struct FileHash {
|
||||
pub file_id: FileId,
|
||||
pub algorithm: String,
|
||||
pub hash: Vec<u8>,
|
||||
}
|
||||
|
||||
pub struct Category {
|
||||
pub id: CategoryId,
|
||||
pub category: String,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user