mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-28 00:00:38 +00:00
* feat(rust): add litellm-db and litellm-db-testing workspace scaffolding Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(db-testing): apply the real Prisma migrations in a test and drop the sort mutation Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Yujong Lee <yujong@berri.ai> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
50 lines
1.5 KiB
Rust
50 lines
1.5 KiB
Rust
use std::{collections::BTreeSet, fs, path::Path};
|
|
|
|
use sqlx::{
|
|
AssertSqlSafe, SqlSafeStr,
|
|
migrate::{Migration, MigrationType},
|
|
};
|
|
|
|
use crate::Error;
|
|
|
|
pub const PRISMA_MIGRATIONS_DIR: &str = concat!(
|
|
env!("CARGO_MANIFEST_DIR"),
|
|
"/../../../litellm-proxy-extras/litellm_proxy_extras/migrations"
|
|
);
|
|
|
|
const MIGRATION_FILENAME: &str = "migration.sql";
|
|
|
|
pub fn prisma_migrations(directory: &Path) -> Result<Vec<Migration>, Error> {
|
|
migration_names(directory)?
|
|
.into_iter()
|
|
.zip(1..)
|
|
.map(|(name, version)| {
|
|
let path = directory.join(&name).join(MIGRATION_FILENAME);
|
|
let sql = fs::read_to_string(&path).map_err(|source| Error::Read { path, source })?;
|
|
Ok(Migration::new(
|
|
version,
|
|
name.into(),
|
|
MigrationType::Simple,
|
|
AssertSqlSafe(sql).into_sql_str(),
|
|
true,
|
|
))
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn migration_names(directory: &Path) -> Result<BTreeSet<String>, Error> {
|
|
let read_error = |source| Error::Read {
|
|
path: directory.to_owned(),
|
|
source,
|
|
};
|
|
|
|
fs::read_dir(directory)
|
|
.map_err(read_error)?
|
|
.map(|entry| {
|
|
let entry = entry.map_err(read_error)?;
|
|
let is_dir = entry.file_type().map_err(read_error)?.is_dir();
|
|
Ok(is_dir.then(|| entry.file_name().to_string_lossy().into_owned()))
|
|
})
|
|
.filter_map(Result::transpose)
|
|
.collect()
|
|
}
|