Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions sqlx-core/src/config/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefaultMigrator>,
}

#[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),
Comment on lines +119 to +131

@abonander abonander Jul 9, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Yeah this should be under test.default-migrator to avoid users thinking this affects sqlx::migrate!()
  2. Why all this instead of a simple Option<String>?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding 2., I wanted to support parsing a dictionary of crate names to map to the migrator path. I didn't realize that sqlx.toml can already be set per crate, so this can all be simplified!

}

#[derive(Debug, Default)]
Expand Down
26 changes: 19 additions & 7 deletions sqlx-macros-core/src/test_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,27 @@ fn expand_advanced(args: AttributeArgs, input: syn::ItemFn) -> crate::Result<Tok
quote! { args.migrator(&#migrator); }
}
MigrationsOpt::InferredPath if !inputs.is_empty() => {
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) => {
Expand Down
Loading