Skip to content

Commit

Permalink
add --no-update / fix surrealdb version panic
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Sep 19, 2024
1 parent d54e9ec commit 411d485
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 24 deletions.
4 changes: 1 addition & 3 deletions crates/unavi-app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ use surrealdb::{engine::local::Db, Surreal};
use unavi_world::UserActor;

#[cfg(not(target_family = "wasm"))]
pub mod dirs;
pub mod native;
mod unavi_system;
#[cfg(not(target_family = "wasm"))]
pub mod update;

pub struct StartOptions {
pub debug_physics: bool,
Expand Down
36 changes: 16 additions & 20 deletions crates/unavi-app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ struct Args {
#[arg(long, default_value_t, value_enum)]
log_level: LogLevel,

/// Disables automatic updates.
#[arg(long)]
no_update: bool,

#[arg(long, default_value = "filesystem")]
storage: Storage,

Expand All @@ -83,36 +87,28 @@ pub enum Storage {

#[cfg(not(target_family = "wasm"))]
fn main() {
use surrealdb::engine::local::{Mem, SurrealKV};
use unavi_app::{dirs::get_project_dirs, update::check_for_updates};

if let Err(e) = check_for_updates() {
panic!("Failed to update: {}", e);
};
use surrealdb::engine::local::Mem;
use unavi_app::native::{db::get_filesystem_db, update::check_for_updates};

let args = Args::parse();

if !args.no_update {
if let Err(e) = check_for_updates() {
panic!("Failed to update: {}", e);
};
}

let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("Failed to build tokio runtime");

rt.block_on(async {
let db = match args.storage {
Storage::Filesystem => {
let dirs = get_project_dirs();
let db_path = dirs.data_dir();

std::fs::create_dir_all(db_path).expect("Failed to create database dir.");

Surreal::new::<SurrealKV>(db_path)
.await
.expect("Failed to create SurrealDB.")
}
Storage::Memory => Surreal::new::<Mem>(())
.await
.expect("Failed to create SurrealDB."),
};
Storage::Filesystem => get_filesystem_db().await,
Storage::Memory => Surreal::new::<Mem>(()).await,
}
.expect("Failed to create SurrealDB");

let opts = args_to_options(args);
unavi_app::start(db, opts).await;
Expand Down
31 changes: 31 additions & 0 deletions crates/unavi-app/src/native/db.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::{future::Future, pin::Pin};

use surrealdb::{
engine::local::{Db, SurrealKV},
Error, Surreal,
};

use super::dirs::get_project_dirs;

pub fn get_filesystem_db() -> Pin<Box<dyn Future<Output = Result<Surreal<Db>, Error>>>> {
Box::pin(async {
let dirs = get_project_dirs();
let db_path = dirs.data_dir();

std::fs::create_dir_all(db_path).expect("Failed to create database dir.");

let db = Surreal::new::<SurrealKV>(db_path).await;

if let Err(e) = &db {
if e.to_string().contains("given version is older") {
// Breaking SurrealDB update.
// Not sure how to properly migrate to a new version, so
// delete all data and start fresh.
std::fs::remove_dir_all(db_path).expect("Failed to remove database dir.");
return get_filesystem_db().await;
}
}

db
})
}
File renamed without changes.
3 changes: 3 additions & 0 deletions crates/unavi-app/src/native/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod db;
mod dirs;
pub mod update;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use self_update::{backends::github::ReleaseList, cargo_crate_version};
use tempfile::tempdir_in;
use zip::ZipArchive;

use crate::dirs::get_project_dirs;
use crate::native::dirs::get_project_dirs;

pub fn check_for_updates() -> Result<()> {
println!("Checking for updates...");
Expand Down

0 comments on commit 411d485

Please sign in to comment.