Skip to content

Commit

Permalink
feat(rust): hide the migrate-database command
Browse files Browse the repository at this point in the history
  • Loading branch information
etorreborre committed Jan 20, 2025
1 parent 618a491 commit 8523dd6
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use crate::docs;
use crate::util::async_cmd;
use crate::CommandGlobalOpts;
use clap::Args;
use miette::IntoDiagnostic;
use ockam_core::errcode::{Kind, Origin};
use ockam_core::Error;
use miette::miette;
use ockam_node::database::node_migration_set::NodeMigrationSet;
use ockam_node::database::{DatabaseConfiguration, MigrationSet, SqlxDatabase};
use ockam_node::Context;
Expand All @@ -14,6 +12,7 @@ const AFTER_LONG_HELP: &str = include_str!("./static/after_long_help.txt");

#[derive(Clone, Debug, Args)]
#[command(
hide = true,
long_about = docs::about(LONG_ABOUT),
after_long_help = docs::after_help(AFTER_LONG_HELP)
)]
Expand Down Expand Up @@ -54,7 +53,7 @@ impl MigrateDatabaseCommand {

Ok(())
},
None => Err(Error::new(Origin::Core, Kind::NotFound, "There is no postgres database configuration, or it is incomplete. Please run ockam environment to check the database environment variables".to_string())).into_diagnostic()?,
None => Err(miette!("There is no postgres database configuration, or it is incomplete. Please run ockam environment to check the database environment variables")),
}
}
}
3 changes: 2 additions & 1 deletion implementations/rust/ockam/ockam_command/src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use crate::lease::LeaseCommand;
use crate::manpages::ManpagesCommand;
use crate::markdown::MarkdownCommand;
use crate::message::MessageCommand;
use crate::migrate_database::MigrateDatabaseCommand;
use crate::node::{NodeCommand, NodeSubcommand};
use crate::policy::PolicyCommand;
use crate::project::ProjectCommand;
Expand Down Expand Up @@ -152,7 +153,7 @@ impl OckamSubcommand {
OckamSubcommand::Lease(c) => c.run(opts),
OckamSubcommand::Authority(c) => c.run(opts),
OckamSubcommand::Markdown(c) => c.run(),
OckamSubcommand::MigrateDatabase(c) => c.run(),
OckamSubcommand::MigrateDatabase(c) => c.run(opts),
OckamSubcommand::Worker(c) => c.run(opts),
OckamSubcommand::Service(c) => c.run(opts),
OckamSubcommand::Message(c) => c.run(opts),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub enum MigrationResult {
/// The migration was successful
MigrationSuccess,
/// The migration had a failure
SomeMigrationError(MigrationFailure),
MigrationFailure(MigrationFailure),
}

impl MigrationResult {
Expand All @@ -23,7 +23,7 @@ impl MigrationResult {
actual_checksum: String,
expected_checksum: String,
) -> Self {
Self::SomeMigrationError(MigrationFailure::IncorrectChecksum(
Self::MigrationFailure(MigrationFailure::IncorrectChecksum(
description,
sql,
actual_checksum,
Expand All @@ -33,12 +33,12 @@ impl MigrationResult {

/// Create a failure when a down migration was attempted
pub fn down_migration() -> Self {
Self::SomeMigrationError(MigrationFailure::DownMigration)
Self::MigrationFailure(MigrationFailure::DownMigration)
}

/// Create a failure when a migration failed for a given version
pub fn dirty_version() -> Self {
Self::SomeMigrationError(MigrationFailure::DirtyVersion)
Self::MigrationFailure(MigrationFailure::DirtyVersion)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,6 @@ impl Display for MigrationStatus {
}

impl MigrationStatus {
/// Create a new MigrationStatus::UpToDate
pub fn create_up_to_date(current_version: Version) -> Self {
MigrationStatus::UpToDate(current_version)
}

/// Create a new MigrationStatus::Todo
pub fn create_to_do(current_version: Option<Version>, next_version: Version) -> Self {
MigrationStatus::Todo(current_version, next_version)
}

/// Create a new MigrationStatus::Failed
pub fn create_failed(version: Version, failure: MigrationFailure) -> Self {
MigrationStatus::Failed(version, failure)
}

/// Return true if the database is up to date
pub fn up_to_date(&self) -> bool {
matches!(self, MigrationStatus::UpToDate(_))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Migrator {

let version = connection.dirty_version().await.into_core()?;
if let Some(version) = version {
return Ok(MigrationStatus::create_failed(
return Ok(MigrationStatus::Failed(
Version(version),
MigrationFailure::DirtyVersion,
));
Expand Down Expand Up @@ -185,7 +185,7 @@ impl Migrator {
};
last_migrated_version = migration.version();
}
Ok(MigrationStatus::create_up_to_date(last_migrated_version))
Ok(MigrationStatus::UpToDate(last_migrated_version))
}
Mode::ApplyMigrations => {
let mut last_migrated_version = Version::MIN;
Expand All @@ -200,8 +200,8 @@ impl Migrator {
.await?
{
MigrationSuccess => (),
MigrationResult::SomeMigrationError(failure) => {
return Ok(MigrationStatus::create_failed(
MigrationResult::MigrationFailure(failure) => {
return Ok(MigrationStatus::Failed(
Version(sql_migration.version),
failure,
))
Expand All @@ -213,8 +213,8 @@ impl Migrator {
.await?
{
MigrationSuccess => (),
MigrationResult::SomeMigrationError(failure) => {
return Ok(MigrationStatus::create_failed(
MigrationResult::MigrationFailure(failure) => {
return Ok(MigrationStatus::Failed(
rust_migration.version(),
failure,
))
Expand All @@ -224,7 +224,7 @@ impl Migrator {
}
last_migrated_version = migration.version();
}
Ok(MigrationStatus::create_up_to_date(last_migrated_version))
Ok(MigrationStatus::UpToDate(last_migrated_version))
}
}
}
Expand Down Expand Up @@ -281,7 +281,7 @@ impl Migrator {

if !self.needs_migration(&mut connection, up_to).await? {
debug!("No database migrations was required");
return Ok(MigrationStatus::create_up_to_date(up_to));
return Ok(MigrationStatus::UpToDate(up_to));
}

let is_sqlite = connection.backend_name() == "SQLite";
Expand Down

0 comments on commit 8523dd6

Please sign in to comment.