You've already forked AstralRinth
forked from didirus/AstralRinth
Upgrade to Actix V2, bump SQLX version, code cleanup, intergrate ratelimiter (#288)
* Upgrade to Actix V2, bump SQLX version, code cleanup, intergrate ratelimiter * Add pack file path validation * Fix compilation error caused by incorrect merge
This commit is contained in:
@@ -141,6 +141,7 @@ impl Category {
|
||||
SELECT c.id id, c.category category, c.icon icon, pt.name project_type
|
||||
FROM categories c
|
||||
INNER JOIN project_types pt ON c.project_type = pt.id
|
||||
ORDER BY c.id
|
||||
"
|
||||
)
|
||||
.fetch_many(exec)
|
||||
@@ -162,8 +163,6 @@ impl Category {
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
{
|
||||
use sqlx::Done;
|
||||
|
||||
let result = sqlx::query!(
|
||||
"
|
||||
DELETE FROM categories
|
||||
@@ -227,7 +226,7 @@ impl<'a> CategoryBuilder<'a> {
|
||||
"
|
||||
INSERT INTO categories (category, project_type, icon)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (category, project_type, icon) DO NOTHING
|
||||
ON CONFLICT (category) DO NOTHING
|
||||
RETURNING id
|
||||
",
|
||||
self.name,
|
||||
@@ -336,8 +335,6 @@ impl Loader {
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
{
|
||||
use sqlx::Done;
|
||||
|
||||
let result = sqlx::query!(
|
||||
"
|
||||
DELETE FROM loaders
|
||||
@@ -398,7 +395,7 @@ impl<'a> LoaderBuilder<'a> {
|
||||
"
|
||||
INSERT INTO loaders (loader, icon)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT (loader, icon) DO NOTHING
|
||||
ON CONFLICT (loader) DO NOTHING
|
||||
RETURNING id
|
||||
",
|
||||
self.name,
|
||||
@@ -597,8 +594,6 @@ impl GameVersion {
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
{
|
||||
use sqlx::Done;
|
||||
|
||||
let result = sqlx::query!(
|
||||
"
|
||||
DELETE FROM game_versions
|
||||
@@ -761,8 +756,6 @@ impl License {
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
{
|
||||
use sqlx::Done;
|
||||
|
||||
let result = sqlx::query!(
|
||||
"
|
||||
DELETE FROM licenses
|
||||
@@ -909,8 +902,6 @@ impl DonationPlatform {
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
{
|
||||
use sqlx::Done;
|
||||
|
||||
let result = sqlx::query!(
|
||||
"
|
||||
DELETE FROM donation_platforms
|
||||
@@ -1046,8 +1037,6 @@ impl ReportType {
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
{
|
||||
use sqlx::Done;
|
||||
|
||||
let result = sqlx::query!(
|
||||
"
|
||||
DELETE FROM report_types
|
||||
@@ -1199,8 +1188,6 @@ impl ProjectType {
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
{
|
||||
use sqlx::Done;
|
||||
|
||||
let result = sqlx::query!(
|
||||
"
|
||||
DELETE FROM project_types
|
||||
|
||||
@@ -100,20 +100,21 @@ impl Report {
|
||||
FROM reports r
|
||||
INNER JOIN report_types rt ON rt.id = r.report_type_id
|
||||
WHERE r.id = ANY($1)
|
||||
ORDER BY r.created DESC
|
||||
",
|
||||
&report_ids_parsed
|
||||
)
|
||||
.fetch_many(exec)
|
||||
.try_filter_map(|e| async {
|
||||
Ok(e.right().map(|row| QueryReport {
|
||||
id: ReportId(row.id),
|
||||
report_type: row.name,
|
||||
project_id: row.mod_id.map(ProjectId),
|
||||
version_id: row.version_id.map(VersionId),
|
||||
user_id: row.user_id.map(UserId),
|
||||
body: row.body,
|
||||
reporter: UserId(row.reporter),
|
||||
created: row.created,
|
||||
Ok(e.right().map(|x| QueryReport {
|
||||
id: ReportId(x.id),
|
||||
report_type: x.name,
|
||||
project_id: x.mod_id.map(ProjectId),
|
||||
version_id: x.version_id.map(VersionId),
|
||||
user_id: x.user_id.map(UserId),
|
||||
body: x.body,
|
||||
reporter: UserId(x.reporter),
|
||||
created: x.created,
|
||||
}))
|
||||
})
|
||||
.try_collect::<Vec<QueryReport>>()
|
||||
|
||||
@@ -403,7 +403,6 @@ impl TeamMember {
|
||||
where
|
||||
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
||||
{
|
||||
use sqlx::Done;
|
||||
let result = sqlx::query!(
|
||||
"
|
||||
DELETE FROM team_members
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
use log::info;
|
||||
use sqlx::migrate::{Migrate, MigrateDatabase, Migrator};
|
||||
use sqlx::migrate::MigrateDatabase;
|
||||
use sqlx::postgres::{PgPool, PgPoolOptions};
|
||||
use sqlx::{Connection, PgConnection, Postgres};
|
||||
use std::path::Path;
|
||||
|
||||
const MIGRATION_FOLDER: &str = "migrations";
|
||||
|
||||
pub async fn connect() -> Result<PgPool, sqlx::Error> {
|
||||
info!("Initializing database connection");
|
||||
@@ -34,31 +31,14 @@ pub async fn check_for_migrations() -> Result<(), sqlx::Error> {
|
||||
info!("Creating database...");
|
||||
Postgres::create_database(uri).await?;
|
||||
}
|
||||
|
||||
info!("Applying migrations...");
|
||||
run_migrations(uri).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn run_migrations(uri: &str) -> Result<(), sqlx::Error> {
|
||||
let migrator = Migrator::new(Path::new(MIGRATION_FOLDER)).await?;
|
||||
let mut conn: PgConnection = PgConnection::connect(uri).await?;
|
||||
|
||||
conn.ensure_migrations_table().await?;
|
||||
|
||||
let (version, dirty) = conn.version().await?.unwrap_or((0, false));
|
||||
|
||||
if dirty {
|
||||
panic!("The database is dirty! Please check your database status.");
|
||||
}
|
||||
|
||||
for migration in migrator.iter() {
|
||||
if migration.version > version {
|
||||
let _elapsed = conn.apply(migration).await?;
|
||||
} else {
|
||||
conn.validate(migration).await?;
|
||||
}
|
||||
}
|
||||
sqlx::migrate!()
|
||||
.run(&mut conn)
|
||||
.await
|
||||
.expect("Error while running database migrations!");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user