add 'db migrate' command option to specify target version

This commit is contained in:
Benjamin Lee 2024-09-14 01:18:11 -07:00
parent 6446822bf2
commit b93d4f471c
No known key found for this signature in database
GPG key ID: FB9624E2885D55A4
3 changed files with 164 additions and 25 deletions

View file

@ -3,7 +3,7 @@
//! CLI argument structs are defined in this module. Execution logic for each
//! command goes in a submodule.
use std::path::PathBuf;
use std::{path::PathBuf, str::FromStr};
use clap::{Parser, Subcommand};
@ -78,6 +78,23 @@ pub(crate) struct MigrateDbArgs {
#[clap(flatten)]
config: ConfigArg,
/// Target server implementation to migrate database to.
///
/// If migrating to the current version of grapevine, specify the version
/// as 'grapevine'.
///
/// If migrating to a released version of conduit, specified the version
/// of conduit as `conduit-{version}` (example: `conduit-0.8.0`). If
/// migrating to an unreleased conduit build, instead specify the raw
/// database version as `conduit-db-{version}` (example: `conduit-db-13`).
/// The raw database version can be found by looking at the
/// `latest_database_version` variable in `src/database/mod.rs`.
///
/// The server implementation used for the current database will be
/// detected automatically, and does not need to be specified.
#[clap(long)]
pub(crate) to: DbMigrationTarget,
/// Path to read database from.
#[clap(long = "in", short)]
pub(crate) in_path: PathBuf,
@ -87,6 +104,78 @@ pub(crate) struct MigrateDbArgs {
pub(crate) out_path: PathBuf,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum DbMigrationTarget {
/// The latest grapevine db version
///
/// Example:
///
/// ```
/// assert_eq!("grapevine".parse(), Ok(DbMigrationTarget::Grapevine))
/// ```
Grapevine,
/// A conduit-compatible db version.
///
/// This may either be specified as a released version number or directly
/// as a database version. The raw database version must be used when
/// migrating to a conduit deployment built from an unreleased commit
/// on the `next` branch.
Conduit(ConduitDbVersion),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum ConduitDbVersion {
/// A conduit release version number
///
/// Example:
///
/// ```
/// assert_eq!(
/// "conduit-0.8.0".parse(),
/// Ok(DbMigrationTarget::Conduit(ConduitDbVersion::Release("0.8.0")))
/// );
/// ```
Release(String),
/// A raw database version
///
/// This corresponds directly to a
/// [`crate::service::globals::DbVersion::Conduit`] version.
///
/// Example:
///
/// ```
/// assert_eq!(
/// "conduit-db-13".parse(),
/// Ok(DbMigrationTarget::Conduit(ConduitDbVersion::Db(13)))
/// );
/// ```
Db(u64),
}
#[derive(thiserror::Error, Debug)]
#[error("invalid db migration target version")]
pub(crate) struct DbMigrationTargetParseError;
impl FromStr for DbMigrationTarget {
type Err = DbMigrationTargetParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s == "grapevine" {
Ok(DbMigrationTarget::Grapevine)
} else if let Some(version) = s.strip_prefix("conduit-db-") {
let version =
version.parse().map_err(|_| DbMigrationTargetParseError)?;
Ok(DbMigrationTarget::Conduit(ConduitDbVersion::Db(version)))
} else if let Some(version) = s.strip_prefix("conduit-") {
Ok(DbMigrationTarget::Conduit(ConduitDbVersion::Release(
version.to_owned(),
)))
} else {
Err(DbMigrationTargetParseError)
}
}
}
impl Args {
pub(crate) async fn run(self) -> Result<(), error::Main> {
match self.command {