Skip to content

Commit

Permalink
Added check to make sure migration name is valid (#1155)
Browse files Browse the repository at this point in the history
Co-authored-by: Forest Anderson <forestkzanderson@gmail.com>
  • Loading branch information
billy1624 and AngelOnFira committed Oct 26, 2022
1 parent 9f2eb3d commit 32061e3
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions sea-orm-cli/src/commands/migrate.rs
Expand Up @@ -2,6 +2,7 @@ use chrono::{Local, Utc};
use regex::Regex;
use std::{
error::Error,
fmt::Display,
fs,
io::Write,
path::{Path, PathBuf},
Expand Down Expand Up @@ -117,6 +118,14 @@ pub fn run_migrate_generate(
migration_name: &str,
universal_time: bool,
) -> Result<(), Box<dyn Error>> {
// 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
Expand Down Expand Up @@ -227,6 +236,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::*;
Expand Down

0 comments on commit 32061e3

Please sign in to comment.