mirror of
https://github.com/aljazceru/cdk.git
synced 2026-02-09 15:16:00 +01:00
Add consistent ordering of sql migrations (#1047)
Also sort the prefix and not only the filenames
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use std::cmp::Ordering;
|
||||
use std::fs::{self, File};
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
@@ -7,10 +8,45 @@ fn main() {
|
||||
let root = Path::new("src");
|
||||
|
||||
for migration_path in find_migrations_dirs(root) {
|
||||
// Step 3: Output file path (e.g., `src/db/migrations.rs`)
|
||||
let parent = migration_path.parent().unwrap();
|
||||
let skip_path = parent.to_str().unwrap_or_default().len();
|
||||
let dest_path = parent.join("migrations.rs");
|
||||
let mut out_file = File::create(&dest_path).expect("Failed to create migrations.rs");
|
||||
|
||||
let skip_name = migration_path.to_str().unwrap_or_default().len();
|
||||
|
||||
// Step 2: Collect all files inside the migrations dir
|
||||
let mut files = Vec::new();
|
||||
visit_dirs(&migration_path, &mut files).expect("Failed to read migrations directory");
|
||||
files.sort_by(|path_a, path_b| {
|
||||
let parts_a = path_a.to_str().unwrap().replace("\\", "/")[skip_name + 1..]
|
||||
.split("/")
|
||||
.map(|x| x.to_owned())
|
||||
.collect::<Vec<_>>();
|
||||
let parts_b = path_b.to_str().unwrap().replace("\\", "/")[skip_name + 1..]
|
||||
.split("/")
|
||||
.map(|x| x.to_owned())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let prefix_a = if parts_a.len() == 2 {
|
||||
parts_a.first().map(|x| x.to_owned()).unwrap_or_default()
|
||||
} else {
|
||||
"".to_owned()
|
||||
};
|
||||
|
||||
let prefix_b = if parts_a.len() == 2 {
|
||||
parts_b.first().map(|x| x.to_owned()).unwrap_or_default()
|
||||
} else {
|
||||
"".to_owned()
|
||||
};
|
||||
|
||||
let prefix_cmp = prefix_a.cmp(&prefix_b);
|
||||
|
||||
if prefix_cmp != Ordering::Equal {
|
||||
return prefix_cmp;
|
||||
}
|
||||
|
||||
let path_a = path_a.file_name().unwrap().to_str().unwrap();
|
||||
let path_b = path_b.file_name().unwrap().to_str().unwrap();
|
||||
|
||||
@@ -32,14 +68,6 @@ fn main() {
|
||||
}
|
||||
});
|
||||
|
||||
// Step 3: Output file path (e.g., `src/db/migrations.rs`)
|
||||
let parent = migration_path.parent().unwrap();
|
||||
let skip_path = parent.to_str().unwrap_or_default().len();
|
||||
let dest_path = parent.join("migrations.rs");
|
||||
let mut out_file = File::create(&dest_path).expect("Failed to create migrations.rs");
|
||||
|
||||
let skip_name = migration_path.to_str().unwrap_or_default().len();
|
||||
|
||||
writeln!(out_file, "/// @generated").unwrap();
|
||||
writeln!(out_file, "/// Auto-generated by build.rs").unwrap();
|
||||
writeln!(
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
/// Auto-generated by build.rs
|
||||
pub static MIGRATIONS: &[(&str, &str, &str)] = &[
|
||||
("postgres", "1_init.sql", include_str!(r#"./migrations/postgres/1_init.sql"#)),
|
||||
("postgres", "20250822104351_rename_blind_message_y_to_b.sql", include_str!(r#"./migrations/postgres/20250822104351_rename_blind_message_y_to_b.sql"#)),
|
||||
("sqlite", "1_fix_sqlx_migration.sql", include_str!(r#"./migrations/sqlite/1_fix_sqlx_migration.sql"#)),
|
||||
("sqlite", "20250109143347_init.sql", include_str!(r#"./migrations/sqlite/20250109143347_init.sql"#)),
|
||||
("postgres", "20250822104351_rename_blind_message_y_to_b.sql", include_str!(r#"./migrations/postgres/20250822104351_rename_blind_message_y_to_b.sql"#)),
|
||||
("sqlite", "20250822104351_rename_blind_message_y_to_b.sql", include_str!(r#"./migrations/sqlite/20250822104351_rename_blind_message_y_to_b.sql"#)),
|
||||
];
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
/// Auto-generated by build.rs
|
||||
pub static MIGRATIONS: &[(&str, &str, &str)] = &[
|
||||
("postgres", "1_initial.sql", include_str!(r#"./migrations/postgres/1_initial.sql"#)),
|
||||
("sqlite", "1_fix_sqlx_migration.sql", include_str!(r#"./migrations/sqlite/1_fix_sqlx_migration.sql"#)),
|
||||
("postgres", "2_remove_request_lookup_kind_constraints.sql", include_str!(r#"./migrations/postgres/2_remove_request_lookup_kind_constraints.sql"#)),
|
||||
("postgres", "20250901090000_add_kv_store.sql", include_str!(r#"./migrations/postgres/20250901090000_add_kv_store.sql"#)),
|
||||
("postgres", "20250903200000_add_signatory_amounts.sql", include_str!(r#"./migrations/postgres/20250903200000_add_signatory_amounts.sql"#)),
|
||||
("sqlite", "1_fix_sqlx_migration.sql", include_str!(r#"./migrations/sqlite/1_fix_sqlx_migration.sql"#)),
|
||||
("sqlite", "20240612124932_init.sql", include_str!(r#"./migrations/sqlite/20240612124932_init.sql"#)),
|
||||
("sqlite", "20240618195700_quote_state.sql", include_str!(r#"./migrations/sqlite/20240618195700_quote_state.sql"#)),
|
||||
("sqlite", "20240626092101_nut04_state.sql", include_str!(r#"./migrations/sqlite/20240626092101_nut04_state.sql"#)),
|
||||
@@ -27,8 +29,6 @@ pub static MIGRATIONS: &[(&str, &str, &str)] = &[
|
||||
("sqlite", "20250706101057_bolt12.sql", include_str!(r#"./migrations/sqlite/20250706101057_bolt12.sql"#)),
|
||||
("sqlite", "20250812132015_drop_melt_request.sql", include_str!(r#"./migrations/sqlite/20250812132015_drop_melt_request.sql"#)),
|
||||
("sqlite", "20250819200000_remove_request_lookup_kind_constraints.sql", include_str!(r#"./migrations/sqlite/20250819200000_remove_request_lookup_kind_constraints.sql"#)),
|
||||
("postgres", "20250901090000_add_kv_store.sql", include_str!(r#"./migrations/postgres/20250901090000_add_kv_store.sql"#)),
|
||||
("sqlite", "20250901090000_add_kv_store.sql", include_str!(r#"./migrations/sqlite/20250901090000_add_kv_store.sql"#)),
|
||||
("postgres", "20250903200000_add_signatory_amounts.sql", include_str!(r#"./migrations/postgres/20250903200000_add_signatory_amounts.sql"#)),
|
||||
("sqlite", "20250903200000_add_signatory_amounts.sql", include_str!(r#"./migrations/sqlite/20250903200000_add_signatory_amounts.sql"#)),
|
||||
];
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
/// Auto-generated by build.rs
|
||||
pub static MIGRATIONS: &[(&str, &str, &str)] = &[
|
||||
("postgres", "1_initial.sql", include_str!(r#"./migrations/postgres/1_initial.sql"#)),
|
||||
("postgres", "20250831215438_melt_quote_method.sql", include_str!(r#"./migrations/postgres/20250831215438_melt_quote_method.sql"#)),
|
||||
("postgres", "20250906200000_add_transaction_quote_id.sql", include_str!(r#"./migrations/postgres/20250906200000_add_transaction_quote_id.sql"#)),
|
||||
("sqlite", "1_fix_sqlx_migration.sql", include_str!(r#"./migrations/sqlite/1_fix_sqlx_migration.sql"#)),
|
||||
("sqlite", "20240612132920_init.sql", include_str!(r#"./migrations/sqlite/20240612132920_init.sql"#)),
|
||||
("sqlite", "20240618200350_quote_state.sql", include_str!(r#"./migrations/sqlite/20240618200350_quote_state.sql"#)),
|
||||
@@ -22,8 +24,6 @@ pub static MIGRATIONS: &[(&str, &str, &str)] = &[
|
||||
("sqlite", "20250707093445_bolt12.sql", include_str!(r#"./migrations/sqlite/20250707093445_bolt12.sql"#)),
|
||||
("sqlite", "20250729111701_keyset_v2_u32.sql", include_str!(r#"./migrations/sqlite/20250729111701_keyset_v2_u32.sql"#)),
|
||||
("sqlite", "20250812084621_keyset_plus_one.sql", include_str!(r#"./migrations/sqlite/20250812084621_keyset_plus_one.sql"#)),
|
||||
("postgres", "20250831215438_melt_quote_method.sql", include_str!(r#"./migrations/postgres/20250831215438_melt_quote_method.sql"#)),
|
||||
("sqlite", "20250831215438_melt_quote_method.sql", include_str!(r#"./migrations/sqlite/20250831215438_melt_quote_method.sql"#)),
|
||||
("postgres", "20250906200000_add_transaction_quote_id.sql", include_str!(r#"./migrations/postgres/20250906200000_add_transaction_quote_id.sql"#)),
|
||||
("sqlite", "20250906200000_add_transaction_quote_id.sql", include_str!(r#"./migrations/sqlite/20250906200000_add_transaction_quote_id.sql"#)),
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user