on main help sign in
Fix migration files not sorted naturally

Closes #15

commit5a6aff12900b335cc0dce35be582b83a20691824 authorfabianlindfors <fabian@flapplabs.se> dateTue 26 Jul 2022, 16:00 (4 years ago) parentce7a0b3
2 files changed+14−5
Cargo.toml+2−1 src/main.rs+12−4
Cargo.toml+2−1
@@ -22,4 +22,5 @@ toml = "0.5"
2222version = "3.0.0"
2323colored = "2"
2424rand = "0.8"
25−dotenv = "0.15.0"
25+dotenv = "0.15.0"
26+lexical-sort = "0.3.1"
src/main.rs+12−4
@@ -4,7 +4,7 @@ use std::{
44 path::Path,
55};
66
7−use anyhow::{Context};
7+use anyhow::Context;
88use clap::{Args, Parser};
99use reshape::{
1010 migrations::{Action, Migration},
@@ -196,7 +196,13 @@ fn find_migrations(opts: &FindMigrationsOptions) -> anyhow::Result<Vec<Migration
196196 }
197197
198198 // Sort all files by their file names (without extension)
199− file_paths.sort_unstable_by_key(|path| path.as_path().file_stem().unwrap().to_os_string());
199+ // The files are sorted naturally, e.g. "1_test_migration" < "10_test_migration"
200+ file_paths.sort_unstable_by(|path1, path2| {
201+ let file1 = path1.as_path().file_stem().unwrap().to_str().unwrap();
202+ let file2 = path2.as_path().file_stem().unwrap().to_str().unwrap();
203+
204+ lexical_sort::natural_cmp(file1, file2)
205+ });
200206
201207 file_paths
202208 .iter()
@@ -212,8 +218,10 @@ fn find_migrations(opts: &FindMigrationsOptions) -> anyhow::Result<Vec<Migration
212218 .map(|result| {
213219 result.and_then(|(path, data)| {
214220 let extension = path.extension().and_then(|ext| ext.to_str()).unwrap();
215− let file_migration = decode_migration_file(&data, extension)
216− .with_context(|| format!("failed to parse migration file {}", path.display()))?;
221+ let file_migration =
222+ decode_migration_file(&data, extension).with_context(|| {
223+ format!("failed to parse migration file {}", path.display())
224+ })?;
217225
218226 let file_name = path.file_stem().and_then(|name| name.to_str()).unwrap();
219227 Ok(Migration {