Fix migration files not sorted naturally

Closes #15
This commit is contained in:
fabianlindfors 2022-07-26 16:00:08 +02:00
parent ce7a0b38ee
commit 5a6aff1290
2 changed files with 14 additions and 5 deletions

View File

@ -23,3 +23,4 @@ version = "3.0.0"
colored = "2" colored = "2"
rand = "0.8" rand = "0.8"
dotenv = "0.15.0" dotenv = "0.15.0"
lexical-sort = "0.3.1"

View File

@ -4,7 +4,7 @@ use std::{
path::Path, path::Path,
}; };
use anyhow::{Context}; use anyhow::Context;
use clap::{Args, Parser}; use clap::{Args, Parser};
use reshape::{ use reshape::{
migrations::{Action, Migration}, migrations::{Action, Migration},
@ -196,7 +196,13 @@ fn find_migrations(opts: &FindMigrationsOptions) -> anyhow::Result<Vec<Migration
} }
// Sort all files by their file names (without extension) // Sort all files by their file names (without extension)
file_paths.sort_unstable_by_key(|path| path.as_path().file_stem().unwrap().to_os_string()); // The files are sorted naturally, e.g. "1_test_migration" < "10_test_migration"
file_paths.sort_unstable_by(|path1, path2| {
let file1 = path1.as_path().file_stem().unwrap().to_str().unwrap();
let file2 = path2.as_path().file_stem().unwrap().to_str().unwrap();
lexical_sort::natural_cmp(file1, file2)
});
file_paths file_paths
.iter() .iter()
@ -212,8 +218,10 @@ fn find_migrations(opts: &FindMigrationsOptions) -> anyhow::Result<Vec<Migration
.map(|result| { .map(|result| {
result.and_then(|(path, data)| { result.and_then(|(path, data)| {
let extension = path.extension().and_then(|ext| ext.to_str()).unwrap(); let extension = path.extension().and_then(|ext| ext.to_str()).unwrap();
let file_migration = decode_migration_file(&data, extension) let file_migration =
.with_context(|| format!("failed to parse migration file {}", path.display()))?; decode_migration_file(&data, extension).with_context(|| {
format!("failed to parse migration file {}", path.display())
})?;
let file_name = path.file_stem().and_then(|name| name.to_str()).unwrap(); let file_name = path.file_stem().and_then(|name| name.to_str()).unwrap();
Ok(Migration { Ok(Migration {