Move sql initialization in context creation
This commit is contained in:
		
					parent
					
						
							
								fee7fe77f8
							
						
					
				
			
			
				commit
				
					
						ec5a8c8a6e
					
				
			
		
					 2 changed files with 32 additions and 36 deletions
				
			
		|  | @ -1,5 +1,9 @@ | ||||||
|  | use include_dir::{include_dir, Dir}; | ||||||
|  | use r2d2::Pool; | ||||||
| use r2d2_sqlite::SqliteConnectionManager; | use r2d2_sqlite::SqliteConnectionManager; | ||||||
|  | use rusqlite_migration::Migrations; | ||||||
| use std::fs; | use std::fs; | ||||||
|  | use std::sync::LazyLock; | ||||||
| 
 | 
 | ||||||
| use crate::{ | use crate::{ | ||||||
| 	arcaea::{chart::SongCache, jacket::JacketCache}, | 	arcaea::{chart::SongCache, jacket::JacketCache}, | ||||||
|  | @ -30,10 +34,35 @@ pub struct UserContext { | ||||||
| 
 | 
 | ||||||
| impl UserContext { | impl UserContext { | ||||||
| 	#[inline] | 	#[inline] | ||||||
| 	pub async fn new(db: DbConnection) -> Result<Self, Error> { | 	pub async fn new() -> Result<Self, Error> { | ||||||
| 		timed!("create_context", { | 		timed!("create_context", { | ||||||
| 			fs::create_dir_all(get_data_dir())?; | 			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 mut song_cache = timed!("make_song_cache", { SongCache::new(&db)? }); | ||||||
| 			let jacket_cache = timed!("make_jacket_cache", { JacketCache::new(&mut song_cache)? }); | 			let jacket_cache = timed!("make_jacket_cache", { JacketCache::new(&mut song_cache)? }); | ||||||
| 			let ui_measurements = timed!("read_ui_measurements", { UIMeasurements::read()? }); | 			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)))?; | 				.with_borrow_mut(|font| CharMeasurements::from_text(font, WHITELIST, Some(700)))?; | ||||||
| 			// }}}
 | 			// }}}
 | ||||||
| 
 | 
 | ||||||
| 			println!("Created user context"); |  | ||||||
| 
 |  | ||||||
| 			Ok(Self { | 			Ok(Self { | ||||||
| 				db, | 				db, | ||||||
| 				song_cache, | 				song_cache, | ||||||
|  |  | ||||||
							
								
								
									
										35
									
								
								src/main.rs
									
										
									
									
									
								
							
							
						
						
									
										35
									
								
								src/main.rs
									
										
									
									
									
								
							|  | @ -20,18 +20,9 @@ mod transform; | ||||||
| mod user; | mod user; | ||||||
| 
 | 
 | ||||||
| use arcaea::play::generate_missing_scores; | use arcaea::play::generate_missing_scores; | ||||||
| use assets::get_data_dir; |  | ||||||
| use context::{Error, UserContext}; | use context::{Error, UserContext}; | ||||||
| use include_dir::{include_dir, Dir}; |  | ||||||
| use poise::serenity_prelude::{self as serenity}; | use poise::serenity_prelude::{self as serenity}; | ||||||
| use r2d2::Pool; | use std::{env::var, sync::Arc, time::Duration}; | ||||||
| use r2d2_sqlite::SqliteConnectionManager; |  | ||||||
| use rusqlite_migration::Migrations; |  | ||||||
| use std::{ |  | ||||||
| 	env::var, |  | ||||||
| 	sync::{Arc, LazyLock}, |  | ||||||
| 	time::Duration, |  | ||||||
| }; |  | ||||||
| 
 | 
 | ||||||
| // {{{ Error handler
 | // {{{ Error handler
 | ||||||
| async fn on_error(error: poise::FrameworkError<'_, UserContext, Error>) { | async fn on_error(error: poise::FrameworkError<'_, UserContext, Error>) { | ||||||
|  | @ -47,28 +38,6 @@ async fn on_error(error: poise::FrameworkError<'_, UserContext, Error>) { | ||||||
| 
 | 
 | ||||||
| #[tokio::main] | #[tokio::main] | ||||||
| async fn 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
 | 	// {{{ Poise options
 | ||||||
| 	let options = poise::FrameworkOptions { | 	let options = poise::FrameworkOptions { | ||||||
| 		commands: vec![ | 		commands: vec![ | ||||||
|  | @ -110,7 +79,7 @@ async fn main() { | ||||||
| 			Box::pin(async move { | 			Box::pin(async move { | ||||||
| 				println!("Logged in as {}", _ready.user.name); | 				println!("Logged in as {}", _ready.user.name); | ||||||
| 				poise::builtins::register_globally(ctx, &framework.options().commands).await?; | 				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" { | 				if var("SHIMMERING_REGEN_SCORES").unwrap_or_default() == "1" { | ||||||
| 					timed!("generate_missing_scores", { | 					timed!("generate_missing_scores", { | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue