Skip to content

Commit

Permalink
feat: loco doctor now checks min SeaORM CLI version
Browse files Browse the repository at this point in the history
  • Loading branch information
jondot committed Oct 22, 2024
1 parent 6d59f68 commit 21afe22
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 52 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ hyper = "1.1"
mime = "0.3"
bytes = "1.1"
ipnetwork = "0.20.0"
semver = "1"

axum-test = { version = "16.1.0", optional = true }

Expand Down
57 changes: 19 additions & 38 deletions examples/demo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ pub async fn main<H: Hooks, M: MigratorTrait>() -> crate::Result<()> {
println!("Environment: {}", &environment);
} else {
let mut should_exit = false;
for (_, check) in doctor::run_all(&config).await {
for (_, check) in doctor::run_all(&config).await? {
if !should_exit && !check.valid() {
should_exit = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ where
///
/// Returns a [`AppResult`] if an error occurs during generate model entity.
pub async fn entities<M: MigratorTrait>(ctx: &AppContext) -> AppResult<String> {
doctor::check_seaorm_cli().to_result()?;
doctor::check_seaorm_cli()?.to_result()?;
doctor::check_db(&ctx.config.database).await.to_result()?;

let out = cmd!(
Expand Down
64 changes: 52 additions & 12 deletions src/doctor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::{collections::BTreeMap, process::Command};

use regex::Regex;
use semver::Version;

use crate::{
bgworker,
config::{self, Config, Database},
Expand Down Expand Up @@ -88,17 +91,19 @@ impl std::fmt::Display for Check {
}

/// Runs checks for all configured resources.
pub async fn run_all(config: &Config) -> BTreeMap<Resource, Check> {
/// # Errors
/// Error when one of the checks fail
pub async fn run_all(config: &Config) -> Result<BTreeMap<Resource, Check>> {
let mut checks = BTreeMap::from([
(Resource::SeaOrmCLI, check_seaorm_cli()),
(Resource::SeaOrmCLI, check_seaorm_cli()?),
(Resource::Database, check_db(&config.database).await),
]);

if config.workers.mode == config::WorkerMode::BackgroundQueue {
checks.insert(Resource::Redis, check_queue(config).await);
}

checks
Ok(checks)
}

/// Checks the database connection.
Expand Down Expand Up @@ -155,19 +160,54 @@ pub async fn check_queue(config: &Config) -> Check {
}
}

const MIN_SEAORMCLI_VER: &str = "1.1.0";
/// Checks the presence and version of `SeaORM` CLI.
#[must_use]
pub fn check_seaorm_cli() -> Check {
/// # Panics
/// On illegal regex
/// # Errors
/// Fails when cannot check version
pub fn check_seaorm_cli() -> Result<Check> {
match Command::new("sea-orm-cli").arg("--version").output() {
Ok(_) => Check {
status: CheckStatus::Ok,
message: SEAORM_INSTALLED.to_string(),
description: None,
},
Err(_) => Check {
Ok(out) => {
let input = String::from_utf8_lossy(&out.stdout);
// Extract the version from the input string
let re = Regex::new(r"(\d+\.\d+\.\d+)").unwrap();

let version_str = re
.captures(&input)
.and_then(|caps| caps.get(0))
.map(|m| m.as_str())
.ok_or("SeaORM CLI version not found")
.map_err(Box::from)?;

// Parse the extracted version using semver
let version = Version::parse(version_str).map_err(Box::from)?;

// Parse the minimum version for comparison
let min_version = Version::parse(MIN_SEAORMCLI_VER).map_err(Box::from)?;

// Compare the extracted version with the minimum version
if version >= min_version {
Ok(Check {
status: CheckStatus::Ok,
message: SEAORM_INSTALLED.to_string(),
description: None,
})
} else {
Ok(Check {
status: CheckStatus::NotOk,
message: format!(
"SeaORM CLI minimal version is `{min_version}` (you have `{version}`). \
Run `cargo install sea-orm-cli` to update."
),
description: Some(SEAORM_NOT_FIX.to_string()),
})
}
}
Err(_) => Ok(Check {
status: CheckStatus::NotOk,
message: SEAORM_NOT_INSTALLED.to_string(),
description: Some(SEAORM_NOT_FIX.to_string()),
},
}),
}
}

0 comments on commit 21afe22

Please sign in to comment.