From f95e61e402b6dd28715fcfb5a0266425288f8841 Mon Sep 17 00:00:00 2001 From: asonix Date: Wed, 16 Aug 2023 16:32:19 -0500 Subject: [PATCH] Expose repo migrations --- src/config/commandline.rs | 80 +++++++++++++++++++++++++++++++++++++-- src/config/defaults.rs | 18 +++++++++ src/lib.rs | 6 +++ src/repo.rs | 2 +- 4 files changed, 101 insertions(+), 5 deletions(-) diff --git a/src/config/commandline.rs b/src/config/commandline.rs index 7017060..d08a52c 100644 --- a/src/config/commandline.rs +++ b/src/config/commandline.rs @@ -346,6 +346,35 @@ impl Args { } } } + Command::MigrateRepo(MigrateRepo { repo }) => { + let server = Server::default(); + let client = Client::default(); + let media = Media::default(); + let metrics = Metrics::default(); + + match repo { + MigrateRepoFrom::Sled(MigrateSledRepo { from, to }) => match to { + MigrateRepoTo::Sled(MigrateSledInner { to }) => Output { + config_format: ConfigFormat { + server, + client, + old_repo, + tracing, + metrics, + media, + repo: None, + store: None, + }, + operation: Operation::MigrateRepo { + from: from.into(), + to: to.into(), + }, + config_file, + save_to, + }, + }, + } + } } } } @@ -366,6 +395,10 @@ pub(crate) enum Operation { from: crate::config::primitives::Store, to: crate::config::primitives::Store, }, + MigrateRepo { + from: crate::config::file::Repo, + to: crate::config::file::Repo, + }, } #[derive(Debug, Default, serde::Serialize)] @@ -750,6 +783,9 @@ enum Command { /// Migrates from one provided media store to another MigrateStore(MigrateStore), + + /// Migrates from one provided repo to another + MigrateRepo(MigrateRepo), } #[derive(Debug, Parser)] @@ -1000,6 +1036,12 @@ struct MigrateStore { store: MigrateStoreFrom, } +#[derive(Debug, Parser)] +struct MigrateRepo { + #[command(subcommand)] + repo: MigrateRepoFrom, +} + /// Configure the pict-rs storage migration #[derive(Debug, Subcommand)] // allow large enum variant - this is an instantiated-once config @@ -1012,6 +1054,12 @@ enum MigrateStoreFrom { ObjectStorage(MigrateObjectStorage), } +/// Configure the pict-rs repo migration +#[derive(Debug, Subcommand)] +enum MigrateRepoFrom { + Sled(MigrateSledRepo), +} + /// Configure the destination storage for pict-rs storage migration #[derive(Debug, Subcommand)] // allow large enum variant - this is an instantiated-once config @@ -1024,6 +1072,13 @@ enum MigrateStoreTo { ObjectStorage(MigrateObjectStorageInner), } +/// Configure the destination repo for pict-rs repo migration +#[derive(Debug, Subcommand)] +enum MigrateRepoTo { + /// Migrate to the provided sled storage + Sled(MigrateSledInner), +} + /// Migrate pict-rs' storage from the provided filesystem storage #[derive(Debug, Parser)] struct MigrateFilesystem { @@ -1034,6 +1089,16 @@ struct MigrateFilesystem { to: MigrateStoreTo, } +/// Migrate pict-rs' repo from the provided sled repo +#[derive(Debug, Parser)] +struct MigrateSledRepo { + #[command(flatten)] + from: Sled, + + #[command(subcommand)] + to: MigrateRepoTo, +} + /// Migrate pict-rs' storage to the provided filesystem storage #[derive(Debug, Parser)] struct MigrateFilesystemInner { @@ -1044,6 +1109,13 @@ struct MigrateFilesystemInner { repo: Option, } +/// Migrate pict-rs' repo to the provided sled repo +#[derive(Debug, Parser)] +struct MigrateSledInner { + #[command(flatten)] + to: Sled, +} + /// Migrate pict-rs' storage from the provided object storage #[derive(Debug, Parser)] struct MigrateObjectStorage { @@ -1166,20 +1238,20 @@ struct ObjectStorage { /// Configuration for the sled-backed data repository #[derive(Debug, Parser, serde::Serialize)] #[serde(rename_all = "snake_case")] -struct Sled { +pub(super) struct Sled { /// The path to store the sled database #[arg(short, long)] #[serde(skip_serializing_if = "Option::is_none")] - path: Option, + pub(super) path: Option, /// The cache capacity, in bytes, allowed to sled for in-memory operations #[arg(short, long)] #[serde(skip_serializing_if = "Option::is_none")] - cache_capacity: Option, + pub(super) cache_capacity: Option, #[arg(short, long)] #[serde(skip_serializing_if = "Option::is_none")] - export_path: Option, + pub(super) export_path: Option, } #[derive(Debug, Parser, serde::Serialize)] diff --git a/src/config/defaults.rs b/src/config/defaults.rs index a2dd2b7..9642e0c 100644 --- a/src/config/defaults.rs +++ b/src/config/defaults.rs @@ -353,3 +353,21 @@ impl From for crate::config::primitives: crate::config::primitives::Store::Filesystem(value.into()) } } + +impl From for crate::config::file::Sled { + fn from(value: crate::config::commandline::Sled) -> Self { + let defaults = SledDefaults::default(); + + crate::config::file::Sled { + path: value.path.unwrap_or(defaults.path), + cache_capacity: value.cache_capacity.unwrap_or(defaults.cache_capacity), + export_path: defaults.export_path, + } + } +} + +impl From for crate::config::file::Repo { + fn from(value: crate::config::commandline::Sled) -> Self { + crate::config::file::Repo::Sled(value.into()) + } +} diff --git a/src/lib.rs b/src/lib.rs index 49035e4..aabf9e5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1916,6 +1916,12 @@ impl PictRsConfiguration { return Ok(()); } + Operation::MigrateRepo { from, to } => { + let from = Repo::open(from)?.to_arc(); + let to = Repo::open(to)?.to_arc(); + + repo::migrate_repo(from, to).await?; + } } if config.server.read_only { diff --git a/src/repo.rs b/src/repo.rs index d5fa821..3c602ba 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -13,7 +13,7 @@ mod migrate; pub(crate) mod sled; pub(crate) use hash::Hash; -pub(crate) use migrate::migrate_04; +pub(crate) use migrate::{migrate_04, migrate_repo}; pub(crate) type ArcRepo = Arc;