1
Fork 0

Move sql initialization in context creation

This commit is contained in:
prescientmoon 2024-08-22 22:25:17 +02:00
parent fee7fe77f8
commit ec5a8c8a6e
Signed by: prescientmoon
SSH key fingerprint: SHA256:UUF9JT2s8Xfyv76b8ZuVL7XrmimH4o49p4b+iexbVH4
2 changed files with 32 additions and 36 deletions

View file

@ -1,5 +1,9 @@
use include_dir::{include_dir, Dir};
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use rusqlite_migration::Migrations;
use std::fs;
use std::sync::LazyLock;
use crate::{
arcaea::{chart::SongCache, jacket::JacketCache},
@ -30,10 +34,35 @@ pub struct UserContext {
impl UserContext {
#[inline]
pub async fn new(db: DbConnection) -> Result<Self, Error> {
pub async fn new() -> Result<Self, Error> {
timed!("create_context", {
fs::create_dir_all(get_data_dir())?;
// {{{ Connect to database
let db = timed!("create_sqlite_pool", {
Pool::new(
SqliteConnectionManager::file(&format!(
"{}/db.sqlite",
get_data_dir().to_str().unwrap()
))
.with_init(|conn| {
static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations");
static MIGRATIONS: LazyLock<Migrations> = LazyLock::new(|| {
Migrations::from_directory(&MIGRATIONS_DIR)
.expect("Could not load migrations")
});
MIGRATIONS
.to_latest(conn)
.expect("Could not run migrations");
Ok(())
}),
)
.expect("Could not open sqlite database.")
});
// }}}
let mut song_cache = timed!("make_song_cache", { SongCache::new(&db)? });
let jacket_cache = timed!("make_jacket_cache", { JacketCache::new(&mut song_cache)? });
let ui_measurements = timed!("read_ui_measurements", { UIMeasurements::read()? });
@ -51,8 +80,6 @@ impl UserContext {
.with_borrow_mut(|font| CharMeasurements::from_text(font, WHITELIST, Some(700)))?;
// }}}
println!("Created user context");
Ok(Self {
db,
song_cache,

View file

@ -20,18 +20,9 @@ mod transform;
mod user;
use arcaea::play::generate_missing_scores;
use assets::get_data_dir;
use context::{Error, UserContext};
use include_dir::{include_dir, Dir};
use poise::serenity_prelude::{self as serenity};
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use rusqlite_migration::Migrations;
use std::{
env::var,
sync::{Arc, LazyLock},
time::Duration,
};
use std::{env::var, sync::Arc, time::Duration};
// {{{ Error handler
async fn on_error(error: poise::FrameworkError<'_, UserContext, Error>) {
@ -47,28 +38,6 @@ async fn on_error(error: poise::FrameworkError<'_, UserContext, Error>) {
#[tokio::main]
async fn main() {
let pool = Pool::new(
SqliteConnectionManager::file(&format!("{}/db.sqlite", get_data_dir().to_str().unwrap()))
.with_init(|conn| {
static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations");
static MIGRATIONS: LazyLock<Migrations> = LazyLock::new(|| {
timed!("create_migration_structure", {
Migrations::from_directory(&MIGRATIONS_DIR)
.expect("Could not load migrations")
})
});
timed!("run_migrations", {
MIGRATIONS
.to_latest(conn)
.expect("Could not run migrations");
});
Ok(())
}),
)
.expect("Could not open sqlite database.");
// {{{ Poise options
let options = poise::FrameworkOptions {
commands: vec![
@ -110,7 +79,7 @@ async fn main() {
Box::pin(async move {
println!("Logged in as {}", _ready.user.name);
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
let ctx = UserContext::new(pool).await?;
let ctx = UserContext::new().await?;
if var("SHIMMERING_REGEN_SCORES").unwrap_or_default() == "1" {
timed!("generate_missing_scores", {