Apply SurrealDB schemas after connecting

- Increase client ready timeout to 30s
- Apply all SurrealDB schemas during initialization and fail fast on errors
- Remove schema application from the connection helper
This commit is contained in:
Jacob Schmidt 2026-05-13 21:20:42 -05:00
parent 01dda0e060
commit 9be7f91e2f

View File

@ -13,7 +13,7 @@ use crate::schema;
pub type SurrealDb = Surreal<Client>;
const CLIENT_READY_TIMEOUT: Duration = Duration::from_secs(5);
const CLIENT_READY_TIMEOUT: Duration = Duration::from_secs(30);
const CLIENT_READY_POLL_INTERVAL: Duration = Duration::from_millis(25);
static SURREAL_DB: OnceLock<SurrealDb> = OnceLock::new();
@ -80,6 +80,18 @@ pub async fn initialize(config: SurrealConfig) {
}
};
log::log("surreal", "DEBUG", "Applying SurrealDB schemas");
if let Err(error) = schema::apply_all(&db).await {
log::log(
"surreal",
"ERROR",
&format!("Failed to apply SurrealDB schemas: {}", error),
);
set_failure_reason(error);
*SURREAL_CONNECTION_STATE.write().unwrap() = SurrealConnectionState::Failed;
return;
}
if SURREAL_DB.set(db).is_ok() {
log::log("surreal", "INFO", "Connected to SurrealDB server");
*SURREAL_CONNECTION_STATE.write().unwrap() = SurrealConnectionState::Connected;
@ -121,8 +133,6 @@ async fn connect(config: SurrealConfig) -> Result<SurrealDb, String> {
.await
.map_err(|error| error.to_string())?;
schema::apply_all(&db).await?;
Ok(db)
}