,,
This commit is contained in:
parent
0c75d3b4c2
commit
3dab48f58b
1 changed files with 1141 additions and 1376 deletions
|
|
@ -9,7 +9,7 @@ use anyhow::{Context as _, Result};
|
|||
use log::info;
|
||||
use models::VaultId;
|
||||
use sha2::{Digest, Sha256};
|
||||
use sqlx::{ConnectOptions, sqlite::SqliteConnectOptions};
|
||||
use sqlx::{ConnectOptions, Executor, sqlite::SqliteConnectOptions};
|
||||
use sqlx::{Pool, Sqlite, sqlite::SqlitePoolOptions};
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::time::Instant;
|
||||
|
|
@ -115,7 +115,7 @@ impl Database {
|
|||
.await
|
||||
.with_context(|| format!("Cannot open database at `{}`", file_name.display()))?;
|
||||
|
||||
Self::run_migrations(&pool).await?;
|
||||
Self::initialize_schema(&pool).await?;
|
||||
|
||||
if let Some(directory) = file_name.parent() {
|
||||
#[cfg(unix)]
|
||||
|
|
@ -138,11 +138,21 @@ impl Database {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn run_migrations(pool: &Pool<Sqlite>) -> Result<()> {
|
||||
sqlx::migrate!("src/app_state/database/migrations")
|
||||
.run(pool)
|
||||
async fn initialize_schema(pool: &Pool<Sqlite>) -> Result<()> {
|
||||
let mut tx = pool.begin_with("BEGIN IMMEDIATE").await?;
|
||||
let initialized: bool = sqlx::query_scalar(
|
||||
"SELECT EXISTS(SELECT 1 FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%')",
|
||||
)
|
||||
.fetch_one(&mut *tx)
|
||||
.await?;
|
||||
if !initialized {
|
||||
tx.execute(include_str!("database/schema.sql"))
|
||||
.await
|
||||
.context("Cannot initialize vault database")?;
|
||||
}
|
||||
tx.commit()
|
||||
.await
|
||||
.context("Cannot check for pending migrations")
|
||||
.context("Cannot commit vault database initialization")
|
||||
}
|
||||
|
||||
async fn get_connection_pool(&self, vault: &VaultId) -> Result<Pool<Sqlite>> {
|
||||
|
|
|
|||
Loading…
Reference in a new issue