From 5d1af39cfc43d82aa5392995e4fa634a3099e60a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Tue, 30 Jun 2026 11:04:40 +0200 Subject: [PATCH] Add support for setting a default migrator --- sqlx-core/src/config/migrate.rs | 13 +++++++++++++ sqlx-macros-core/src/test_attr.rs | 26 +++++++++++++++++++------- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/sqlx-core/src/config/migrate.rs b/sqlx-core/src/config/migrate.rs index 0dd6cc2257..a22d0e71fe 100644 --- a/sqlx-core/src/config/migrate.rs +++ b/sqlx-core/src/config/migrate.rs @@ -116,6 +116,19 @@ pub struct Config { /// Specify default options for new migrations created with `sqlx migrate add`. pub defaults: MigrationDefaults, + /// Default migrator used for tests. + pub default_migrator: Option, +} + +#[derive(Debug)] +#[cfg_attr( + feature = "sqlx-toml", + derive(serde::Deserialize), + serde(rename_all = "kebab-case", deny_unknown_fields, untagged) +)] +pub enum DefaultMigrator { + /// A single path to a migrator, e.g. `crate::foo::MIGRATOR`. + Path(String), } #[derive(Debug, Default)] diff --git a/sqlx-macros-core/src/test_attr.rs b/sqlx-macros-core/src/test_attr.rs index 046ff5c2fb..30188d0212 100644 --- a/sqlx-macros-core/src/test_attr.rs +++ b/sqlx-macros-core/src/test_attr.rs @@ -149,15 +149,27 @@ fn expand_advanced(args: AttributeArgs, input: syn::ItemFn) -> crate::Result { - let path = crate::migrate::default_path(&config); + if let Some(migrator) = config.migrate.default_migrator { + match migrator { + sqlx_core::config::migrate::DefaultMigrator::Path(path) => { + let path: syn::Path = syn::parse_str(&path).unwrap_or_else(|e| { + panic!("Cannot parse default migrator {path} as a Rust path: {e:?}") + }); + quote! { args.migrator(&#path); } + } + } + } else { + let path = crate::migrate::default_path(&config); - let resolved_path = crate::common::resolve_path(path, proc_macro2::Span::call_site())?; + let resolved_path = + crate::common::resolve_path(path, proc_macro2::Span::call_site())?; - if resolved_path.is_dir() { - let migrator = crate::migrate::expand_with_path(&config, &resolved_path)?; - quote! { args.migrator(&#migrator); } - } else { - quote! {} + if resolved_path.is_dir() { + let migrator = crate::migrate::expand_with_path(&config, &resolved_path)?; + quote! { args.migrator(&#migrator); } + } else { + quote! {} + } } } MigrationsOpt::ExplicitMigrator(path) => {