Revert "sea-orm-cli migrate init: don't overwrite files by default (#1829)"

This seems to be a breaking change may be we should apply later
This commit is contained in:
Chris Tsang 2023-12-08 14:16:26 +00:00
parent a1396f3679
commit c9a2528546
2 changed files with 4 additions and 16 deletions

View File

@ -101,10 +101,7 @@ you should provide the directory of that submodule.",
#[derive(Subcommand, PartialEq, Eq, Debug)] #[derive(Subcommand, PartialEq, Eq, Debug)]
pub enum MigrateSubcommands { pub enum MigrateSubcommands {
#[command(about = "Initialize migration directory", display_order = 10)] #[command(about = "Initialize migration directory", display_order = 10)]
Init { Init,
#[arg(short, long, help = "Overwrite files if the folder already exists")]
force: bool,
},
#[command(about = "Generate a new, empty migration", display_order = 20)] #[command(about = "Generate a new, empty migration", display_order = 20)]
Generate { Generate {
#[arg(required = true, help = "Name of the new migration")] #[arg(required = true, help = "Name of the new migration")]

View File

@ -21,7 +21,7 @@ pub fn run_migrate_command(
verbose: bool, verbose: bool,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
match command { match command {
Some(MigrateSubcommands::Init { force }) => run_migrate_init(migration_dir, force)?, Some(MigrateSubcommands::Init) => run_migrate_init(migration_dir)?,
Some(MigrateSubcommands::Generate { Some(MigrateSubcommands::Generate {
migration_name, migration_name,
universal_time: _, universal_time: _,
@ -78,16 +78,11 @@ pub fn run_migrate_command(
Ok(()) Ok(())
} }
pub fn run_migrate_init(migration_dir: &str, force: bool) -> Result<(), Box<dyn Error>> { pub fn run_migrate_init(migration_dir: &str) -> Result<(), Box<dyn Error>> {
let migration_dir = match migration_dir.ends_with('/') { let migration_dir = match migration_dir.ends_with('/') {
true => migration_dir.to_string(), true => migration_dir.to_string(),
false => format!("{migration_dir}/"), false => format!("{migration_dir}/"),
}; };
if !force && Path::new(&migration_dir).is_dir() {
if fs::read_dir(&migration_dir)?.next().transpose()?.is_some() {
return Err(Box::new(MigrationCommandError::DirAlreadyExists));
}
}
println!("Initializing migration directory..."); println!("Initializing migration directory...");
macro_rules! write_file { macro_rules! write_file {
($filename: literal) => { ($filename: literal) => {
@ -257,17 +252,13 @@ fn update_migrator(migration_name: &str, migration_dir: &str) -> Result<(), Box<
#[derive(Debug)] #[derive(Debug)]
enum MigrationCommandError { enum MigrationCommandError {
DirAlreadyExists,
InvalidName(String), InvalidName(String),
} }
impl Display for MigrationCommandError { impl Display for MigrationCommandError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Self::DirAlreadyExists => { MigrationCommandError::InvalidName(name) => {
write!(f, "Migration directory already exists! Use `--force` flag if you want to overwrite it anyway.")
}
Self::InvalidName(name) => {
write!(f, "Invalid migration name: {name}") write!(f, "Invalid migration name: {name}")
} }
} }