Files
spacedrive/core/src/util/db.rs
jake 22bc77a39e [ENG-319] Balloon hashing (#489)
* add key attribute to `Key` in `ListOfKeys`

* add balloon hashing function with untailored parameters

* add ser/de rules for blake3-balloon (and change argon2id's)

* fix benchmark

* use `to_bytes`, `from_bytes` and `from_reader`

* cleanup code

* add blake3-balloon options to the UI and fix library sync/automount enable bug

* cleanup some serialization code

* fix hashing algorithm deserialization

* clean up header serialization + more idiomatic master key decryption

* clippy

* add generic ser/de error to crypto crate

* fix `Display` and crypto cli

* move crypto cli to cli app

Co-authored-by: Brendan Allan <brendonovich@outlook.com>
2023-01-04 09:57:02 +00:00

69 lines
1.9 KiB
Rust

use crate::prisma::{self, PrismaClient};
use prisma_client_rust::QueryError;
use prisma_client_rust::{migrations::*, NewClientError};
use sd_crypto::keys::keymanager::StoredKey;
use thiserror::Error;
/// MigrationError represents an error that occurring while opening a initialising and running migrations on the database.
#[derive(Error, Debug)]
pub enum MigrationError {
#[error("An error occurred while initialising a new database connection: {0}")]
NewClient(#[from] Box<NewClientError>),
#[cfg(debug_assertions)]
#[error("An error occurred during migration: {0}")]
MigrateFailed(#[from] DbPushError),
#[cfg(not(debug_assertions))]
#[error("An error occurred during migration: {0}")]
MigrateFailed(#[from] MigrateDeployError),
}
/// load_and_migrate will load the database from the given path and migrate it to the latest version of the schema.
pub async fn load_and_migrate(db_url: &str) -> Result<PrismaClient, MigrationError> {
let client = prisma::new_client_with_url(db_url)
.await
.map_err(Box::new)?;
#[cfg(debug_assertions)]
{
let mut builder = client._db_push();
if std::env::var("SD_FORCE_RESET_DB")
.map(|v| v == "true")
.unwrap_or(false)
{
builder = builder.accept_data_loss().force_reset();
}
builder.await?;
}
#[cfg(not(debug_assertions))]
client._migrate_deploy().await?;
Ok(client)
}
/// This writes a `StoredKey` to prisma
/// If the key is marked as memory-only, it is skipped
pub async fn write_storedkey_to_db(db: &PrismaClient, key: &StoredKey) -> Result<(), QueryError> {
if !key.memory_only {
db.key()
.create(
key.uuid.to_string(),
key.algorithm.to_bytes().to_vec(),
key.hashing_algorithm.to_bytes().to_vec(),
key.content_salt.to_vec(),
key.master_key.to_vec(),
key.master_key_nonce.to_vec(),
key.key_nonce.to_vec(),
key.key.to_vec(),
key.salt.to_vec(),
vec![],
)
.exec()
.await?;
}
Ok(())
}