diff --git a/sea-orm-cli/src/commands/migrate.rs b/sea-orm-cli/src/commands/migrate.rs index 1ab5f33ad..4c4b8b848 100644 --- a/sea-orm-cli/src/commands/migrate.rs +++ b/sea-orm-cli/src/commands/migrate.rs @@ -2,6 +2,7 @@ use chrono::{Local, Utc}; use regex::Regex; use std::{ error::Error, + fmt::Display, fs, io::Write, path::{Path, PathBuf}, @@ -115,6 +116,14 @@ pub fn run_migrate_generate( migration_name: &str, universal_time: bool, ) -> Result<(), Box> { + // Make sure the migration name doesn't contain any characters that + // are invalid module names in Rust. + if migration_name.contains('-') { + return Err(Box::new(MigrationCommandError::InvalidName( + "Hyphen `-` cannot be used in migration name".to_string(), + ))); + } + println!("Generating new migration..."); // build new migration filename @@ -225,6 +234,23 @@ fn update_migrator(migration_name: &str, migration_dir: &str) -> Result<(), Box< Ok(()) } +#[derive(Debug)] +enum MigrationCommandError { + InvalidName(String), +} + +impl Display for MigrationCommandError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + MigrationCommandError::InvalidName(name) => { + write!(f, "Invalid migration name: {}", name) + } + } + } +} + +impl Error for MigrationCommandError {} + #[cfg(test)] mod tests { use super::*;