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:
parent
01dda0e060
commit
9be7f91e2f
@ -13,7 +13,7 @@ use crate::schema;
|
|||||||
|
|
||||||
pub type SurrealDb = Surreal<Client>;
|
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);
|
const CLIENT_READY_POLL_INTERVAL: Duration = Duration::from_millis(25);
|
||||||
|
|
||||||
static SURREAL_DB: OnceLock<SurrealDb> = OnceLock::new();
|
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() {
|
if SURREAL_DB.set(db).is_ok() {
|
||||||
log::log("surreal", "INFO", "Connected to SurrealDB server");
|
log::log("surreal", "INFO", "Connected to SurrealDB server");
|
||||||
*SURREAL_CONNECTION_STATE.write().unwrap() = SurrealConnectionState::Connected;
|
*SURREAL_CONNECTION_STATE.write().unwrap() = SurrealConnectionState::Connected;
|
||||||
@ -121,8 +133,6 @@ async fn connect(config: SurrealConfig) -> Result<SurrealDb, String> {
|
|||||||
.await
|
.await
|
||||||
.map_err(|error| error.to_string())?;
|
.map_err(|error| error.to_string())?;
|
||||||
|
|
||||||
schema::apply_all(&db).await?;
|
|
||||||
|
|
||||||
Ok(db)
|
Ok(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user