mirror of
https://github.com/spacedriveapp/spacedrive.git
synced 2026-05-05 22:03:16 -04:00
43 lines
1.2 KiB
Rust
43 lines
1.2 KiB
Rust
use crate::prisma::{self, PrismaClient};
|
|
use prisma_client_rust::{migrations::*, NewClientError};
|
|
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)
|
|
}
|