Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch invalid migration name when generating a new migration #1155

Merged
merged 2 commits into from
Oct 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions sea-orm-cli/src/commands/migrate.rs
Original file line number Diff line number Diff line change
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 @@ -115,6 +116,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 @@ -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::*;
Expand Down