Added check to make sure migration name is valid (#1155)
Co-authored-by: Forest Anderson <forestkzanderson@gmail.com>
This commit is contained in:
parent
9f2eb3d46c
commit
32061e30b5
@ -2,6 +2,7 @@ use chrono::{Local, Utc};
|
|||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::{
|
use std::{
|
||||||
error::Error,
|
error::Error,
|
||||||
|
fmt::Display,
|
||||||
fs,
|
fs,
|
||||||
io::Write,
|
io::Write,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
@ -117,6 +118,14 @@ pub fn run_migrate_generate(
|
|||||||
migration_name: &str,
|
migration_name: &str,
|
||||||
universal_time: bool,
|
universal_time: bool,
|
||||||
) -> Result<(), Box<dyn Error>> {
|
) -> 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...");
|
println!("Generating new migration...");
|
||||||
|
|
||||||
// build new migration filename
|
// build new migration filename
|
||||||
@ -227,6 +236,23 @@ fn update_migrator(migration_name: &str, migration_dir: &str) -> Result<(), Box<
|
|||||||
Ok(())
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user