Skip to content

Commit

Permalink
[server] Db objects are not recreated upon server restart #1093 (#1095)
Browse files Browse the repository at this point in the history
* Update misc_routes.rs

* Update misc_routes.rs

* Update db_pool.rs
  • Loading branch information
michaelvlach authored May 14, 2024
1 parent 375079f commit ece7e81
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
29 changes: 28 additions & 1 deletion agdb_server/src/db_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,34 @@ impl DbPool {
pool: RwLock::new(HashMap::new()),
}));

if !db_exists {
if db_exists {
let dbs: Vec<Database> = db_pool
.0
.server_db
.get()
.await
.exec(
&QueryBuilder::select()
.ids(
QueryBuilder::search()
.from("dbs")
.where_()
.distance(CountComparison::Equal(2))
.query(),
)
.query(),
)?
.try_into()?;

for db in dbs {
let (owner, db_name) = db.name.split_once('/').ok_or(ErrorCode::DbInvalid)?;
let db_path = Path::new(&config.data_dir).join(db_name);
std::fs::create_dir_all(db_audit_dir(owner, config))?;
let server_db =
ServerDb::new(&format!("{}:{}", db.db_type, db_path.to_string_lossy()))?;
db_pool.0.pool.write().await.insert(db.name, server_db);
}
} else {
let admin_password = Password::create(&config.admin, &config.admin);

db_pool.0.server_db.get_mut().await.transaction_mut(|t| {
Expand Down
60 changes: 60 additions & 0 deletions agdb_server/tests/routes/misc_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,63 @@ async fn config_reuse() -> anyhow::Result<()> {
.spawn()?;
Ok(())
}

#[tokio::test]
async fn db_list_after_shutdown() -> anyhow::Result<()> {
let mut server = TestServerImpl::new().await?;
let mut client = AgdbApi::new(ReqwestClient::new(), &server.address);

{
client.user_login(ADMIN, ADMIN).await?;
client.admin_user_add("userx", "userxpassword").await?;
client.user_logout().await?;
client.user_login("userx", "userxpassword").await?;
client
.db_add("userx", "mydb", agdb_api::DbType::Mapped)
.await?;
client.user_logout().await?;
client.user_login(ADMIN, ADMIN).await?;
client.admin_shutdown().await?;
assert!(server.process.wait()?.success());
}

server.process = Command::cargo_bin("agdb_server")?
.current_dir(&server.dir)
.spawn()?;
client.user_login("userx", "userxpassword").await?;
let dbs = client.db_list().await?.1;
assert_eq!(dbs.len(), 1);

Ok(())
}

#[tokio::test]
async fn db_list_after_shutdown_corrupted_data() -> anyhow::Result<()> {
let mut server = TestServerImpl::new().await?;
let mut client = AgdbApi::new(ReqwestClient::new(), &server.address);

{
client.user_login(ADMIN, ADMIN).await?;
client.admin_user_add("userx", "userxpassword").await?;
client.user_logout().await?;
client.user_login("userx", "userxpassword").await?;
client
.db_add("userx", "mydb", agdb_api::DbType::Mapped)
.await?;
client.user_logout().await?;
client.user_login(ADMIN, ADMIN).await?;
client.admin_shutdown().await?;
assert!(server.process.wait()?.success());
}

std::fs::remove_dir_all(&server.data_dir)?;

server.process = Command::cargo_bin("agdb_server")?
.current_dir(&server.dir)
.spawn()?;
client.user_login("userx", "userxpassword").await?;
let dbs = client.db_list().await?.1;
assert_eq!(dbs.len(), 1);

Ok(())
}

0 comments on commit ece7e81

Please sign in to comment.