2024-07-24 11:03:19 -07:00
|
|
|
use crate::state::DirectoryInfo;
|
App redesign (#2946)
* Start of app redesign
* format
* continue progress
* Content page nearly done
* Fix recursion issues with content page
* Fix update all alignment
* Discover page progress
* Settings progress
* Removed unlocked-size hack that breaks web
* Revamp project page, refactor web project page to share code with app, fixed loading bar, misc UI/UX enhancements, update ko-fi logo, update arrow icons, fix web issues caused by floating-vue migration, fix tooltip issues, update web tooltips, clean up web hydration issues
* Ads + run prettier
* Begin auth refactor, move common messages to ui lib, add i18n extraction to all apps, begin Library refactor
* fix ads not hiding when plus log in
* rev lockfile changes/conflicts
* Fix sign in page
* Add generated
* (mostly) Data driven search
* Fix search mobile issue
* profile fixes
* Project versions page, fix typescript on UI lib and misc fixes
* Remove unused gallery component
* Fix linkfunction err
* Search filter controls at top, localization for locked filters
* Fix provided filter names
* Fix navigating from instance browse to main browse
* Friends frontend (#2995)
* Friends system frontend
* (almost) finish frontend
* finish friends, fix lint
* Fix lint
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
* Refresh macOS app icon
* Update web search UI more
* Fix link opens
* Fix frontend build
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
Co-authored-by: Jai A <jaiagr+gpg@pm.me>
Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
2024-12-11 19:54:18 -08:00
|
|
|
use sqlx::sqlite::{
|
|
|
|
|
SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions,
|
|
|
|
|
};
|
2024-07-24 11:03:19 -07:00
|
|
|
use sqlx::{Pool, Sqlite};
|
App redesign (#2946)
* Start of app redesign
* format
* continue progress
* Content page nearly done
* Fix recursion issues with content page
* Fix update all alignment
* Discover page progress
* Settings progress
* Removed unlocked-size hack that breaks web
* Revamp project page, refactor web project page to share code with app, fixed loading bar, misc UI/UX enhancements, update ko-fi logo, update arrow icons, fix web issues caused by floating-vue migration, fix tooltip issues, update web tooltips, clean up web hydration issues
* Ads + run prettier
* Begin auth refactor, move common messages to ui lib, add i18n extraction to all apps, begin Library refactor
* fix ads not hiding when plus log in
* rev lockfile changes/conflicts
* Fix sign in page
* Add generated
* (mostly) Data driven search
* Fix search mobile issue
* profile fixes
* Project versions page, fix typescript on UI lib and misc fixes
* Remove unused gallery component
* Fix linkfunction err
* Search filter controls at top, localization for locked filters
* Fix provided filter names
* Fix navigating from instance browse to main browse
* Friends frontend (#2995)
* Friends system frontend
* (almost) finish frontend
* finish friends, fix lint
* Fix lint
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
* Refresh macOS app icon
* Update web search UI more
* Fix link opens
* Fix frontend build
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
Co-authored-by: Jai A <jaiagr+gpg@pm.me>
Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
2024-12-11 19:54:18 -08:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
use std::time::Duration;
|
2024-07-24 11:03:19 -07:00
|
|
|
|
2026-05-15 18:22:51 +02:00
|
|
|
static MIGRATOR: sqlx::migrate::Migrator = sqlx::migrate!();
|
|
|
|
|
const HISTORICAL_INIT_MIGRATION_VERSION: i64 = 20240711194701;
|
|
|
|
|
|
2026-02-02 08:10:23 -05:00
|
|
|
pub(crate) async fn connect(
|
|
|
|
|
app_identifier: &str,
|
|
|
|
|
) -> crate::Result<Pool<Sqlite>> {
|
|
|
|
|
let settings_dir = DirectoryInfo::initial_settings_dir_path(app_identifier)
|
|
|
|
|
.ok_or(crate::ErrorKind::FSError(
|
2024-07-24 11:03:19 -07:00
|
|
|
"Could not find valid config dir".to_string(),
|
2026-02-02 08:10:23 -05:00
|
|
|
))?;
|
2024-07-24 11:03:19 -07:00
|
|
|
|
|
|
|
|
if !settings_dir.exists() {
|
|
|
|
|
crate::util::io::create_dir_all(&settings_dir).await?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let uri = format!("sqlite:{}", settings_dir.join("app.db").display());
|
|
|
|
|
|
App redesign (#2946)
* Start of app redesign
* format
* continue progress
* Content page nearly done
* Fix recursion issues with content page
* Fix update all alignment
* Discover page progress
* Settings progress
* Removed unlocked-size hack that breaks web
* Revamp project page, refactor web project page to share code with app, fixed loading bar, misc UI/UX enhancements, update ko-fi logo, update arrow icons, fix web issues caused by floating-vue migration, fix tooltip issues, update web tooltips, clean up web hydration issues
* Ads + run prettier
* Begin auth refactor, move common messages to ui lib, add i18n extraction to all apps, begin Library refactor
* fix ads not hiding when plus log in
* rev lockfile changes/conflicts
* Fix sign in page
* Add generated
* (mostly) Data driven search
* Fix search mobile issue
* profile fixes
* Project versions page, fix typescript on UI lib and misc fixes
* Remove unused gallery component
* Fix linkfunction err
* Search filter controls at top, localization for locked filters
* Fix provided filter names
* Fix navigating from instance browse to main browse
* Friends frontend (#2995)
* Friends system frontend
* (almost) finish frontend
* finish friends, fix lint
* Fix lint
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
* Refresh macOS app icon
* Update web search UI more
* Fix link opens
* Fix frontend build
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
Co-authored-by: Jai A <jaiagr+gpg@pm.me>
Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
2024-12-11 19:54:18 -08:00
|
|
|
let conn_options = SqliteConnectOptions::from_str(&uri)?
|
|
|
|
|
.busy_timeout(Duration::from_secs(30))
|
|
|
|
|
.journal_mode(SqliteJournalMode::Wal)
|
2025-07-02 21:32:15 +01:00
|
|
|
.optimize_on_close(true, None)
|
|
|
|
|
.create_if_missing(true);
|
App redesign (#2946)
* Start of app redesign
* format
* continue progress
* Content page nearly done
* Fix recursion issues with content page
* Fix update all alignment
* Discover page progress
* Settings progress
* Removed unlocked-size hack that breaks web
* Revamp project page, refactor web project page to share code with app, fixed loading bar, misc UI/UX enhancements, update ko-fi logo, update arrow icons, fix web issues caused by floating-vue migration, fix tooltip issues, update web tooltips, clean up web hydration issues
* Ads + run prettier
* Begin auth refactor, move common messages to ui lib, add i18n extraction to all apps, begin Library refactor
* fix ads not hiding when plus log in
* rev lockfile changes/conflicts
* Fix sign in page
* Add generated
* (mostly) Data driven search
* Fix search mobile issue
* profile fixes
* Project versions page, fix typescript on UI lib and misc fixes
* Remove unused gallery component
* Fix linkfunction err
* Search filter controls at top, localization for locked filters
* Fix provided filter names
* Fix navigating from instance browse to main browse
* Friends frontend (#2995)
* Friends system frontend
* (almost) finish frontend
* finish friends, fix lint
* Fix lint
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
* Refresh macOS app icon
* Update web search UI more
* Fix link opens
* Fix frontend build
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
Co-authored-by: Jai A <jaiagr+gpg@pm.me>
Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
2024-12-11 19:54:18 -08:00
|
|
|
|
2024-07-24 11:03:19 -07:00
|
|
|
let pool = SqlitePoolOptions::new()
|
|
|
|
|
.max_connections(100)
|
App redesign (#2946)
* Start of app redesign
* format
* continue progress
* Content page nearly done
* Fix recursion issues with content page
* Fix update all alignment
* Discover page progress
* Settings progress
* Removed unlocked-size hack that breaks web
* Revamp project page, refactor web project page to share code with app, fixed loading bar, misc UI/UX enhancements, update ko-fi logo, update arrow icons, fix web issues caused by floating-vue migration, fix tooltip issues, update web tooltips, clean up web hydration issues
* Ads + run prettier
* Begin auth refactor, move common messages to ui lib, add i18n extraction to all apps, begin Library refactor
* fix ads not hiding when plus log in
* rev lockfile changes/conflicts
* Fix sign in page
* Add generated
* (mostly) Data driven search
* Fix search mobile issue
* profile fixes
* Project versions page, fix typescript on UI lib and misc fixes
* Remove unused gallery component
* Fix linkfunction err
* Search filter controls at top, localization for locked filters
* Fix provided filter names
* Fix navigating from instance browse to main browse
* Friends frontend (#2995)
* Friends system frontend
* (almost) finish frontend
* finish friends, fix lint
* Fix lint
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
* Refresh macOS app icon
* Update web search UI more
* Fix link opens
* Fix frontend build
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
Co-authored-by: Jai A <jaiagr+gpg@pm.me>
Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
2024-12-11 19:54:18 -08:00
|
|
|
.connect_with(conn_options)
|
2024-07-24 11:03:19 -07:00
|
|
|
.await?;
|
|
|
|
|
|
2026-05-15 18:22:51 +02:00
|
|
|
reconcile_historical_migration_checksums(&pool).await?;
|
|
|
|
|
MIGRATOR.run(&pool).await?;
|
2024-07-24 11:03:19 -07:00
|
|
|
|
2025-07-02 21:32:15 +01:00
|
|
|
if let Err(err) = stale_data_cleanup(&pool).await {
|
|
|
|
|
tracing::warn!(
|
|
|
|
|
"Failed to clean up stale data from state database: {err}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 11:03:19 -07:00
|
|
|
Ok(pool)
|
|
|
|
|
}
|
2025-07-02 21:32:15 +01:00
|
|
|
|
2026-05-15 18:22:51 +02:00
|
|
|
async fn reconcile_historical_migration_checksums(
|
|
|
|
|
pool: &Pool<Sqlite>,
|
|
|
|
|
) -> crate::Result<()> {
|
|
|
|
|
let Some(init_migration) = MIGRATOR.iter().find(|migration| {
|
|
|
|
|
migration.version == HISTORICAL_INIT_MIGRATION_VERSION
|
|
|
|
|
})
|
|
|
|
|
else {
|
|
|
|
|
return Ok(());
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let has_migration_table: i64 = sqlx::query_scalar(
|
|
|
|
|
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = '_sqlx_migrations'",
|
|
|
|
|
)
|
|
|
|
|
.fetch_one(pool)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
if has_migration_table == 0 {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let stored_checksum: Option<Vec<u8>> = sqlx::query_scalar(
|
|
|
|
|
"SELECT checksum FROM _sqlx_migrations WHERE version = ? AND success = TRUE",
|
|
|
|
|
)
|
|
|
|
|
.bind(HISTORICAL_INIT_MIGRATION_VERSION)
|
|
|
|
|
.fetch_optional(pool)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
if let Some(stored_checksum) = stored_checksum {
|
2026-05-15 18:57:07 +02:00
|
|
|
let bundled_checksum: &[u8] = init_migration.checksum.as_ref();
|
2026-05-15 18:22:51 +02:00
|
|
|
|
2026-05-15 18:57:07 +02:00
|
|
|
if stored_checksum.as_slice() != bundled_checksum {
|
2026-05-15 18:22:51 +02:00
|
|
|
tracing::warn!(
|
|
|
|
|
"Reconciling checksum for historical Modrinth init migration {}",
|
|
|
|
|
HISTORICAL_INIT_MIGRATION_VERSION
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
sqlx::query(
|
|
|
|
|
"UPDATE _sqlx_migrations SET checksum = ? WHERE version = ? AND success = TRUE",
|
|
|
|
|
)
|
|
|
|
|
.bind(bundled_checksum)
|
|
|
|
|
.bind(HISTORICAL_INIT_MIGRATION_VERSION)
|
|
|
|
|
.execute(pool)
|
|
|
|
|
.await?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-02 21:32:15 +01:00
|
|
|
/// Cleans up data from the database that is no longer referenced, but must be
|
|
|
|
|
/// kept around for a little while to allow users to recover from accidental
|
|
|
|
|
/// deletions.
|
|
|
|
|
async fn stale_data_cleanup(pool: &Pool<Sqlite>) -> crate::Result<()> {
|
|
|
|
|
let mut tx = pool.begin().await?;
|
|
|
|
|
|
|
|
|
|
sqlx::query!(
|
|
|
|
|
"DELETE FROM default_minecraft_capes WHERE minecraft_user_uuid NOT IN (SELECT uuid FROM minecraft_users)"
|
|
|
|
|
)
|
|
|
|
|
.execute(&mut *tx)
|
|
|
|
|
.await?;
|
|
|
|
|
sqlx::query!(
|
|
|
|
|
"DELETE FROM custom_minecraft_skins WHERE minecraft_user_uuid NOT IN (SELECT uuid FROM minecraft_users)"
|
|
|
|
|
)
|
|
|
|
|
.execute(&mut *tx)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
tx.commit().await?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|