Skip to content

Commit

Permalink
Traverse symlinks when resolving migrations (#2445)
Browse files Browse the repository at this point in the history
* Traverse symlinks when resolving migrations

When enumerating the source directory seeking migration files, `sqlx`
ignores entries that aren't files. This was previously reported as #614
and fixed in #985 but apparently regressed somewhere along the way. This
commit reintroduces the fix from #985 to the current implementation: use
`std::fs::metadata` instead of `std::fs::DirEntry::metadata`. The former
is documented to traverse symlinks; the latter does not.

* add migrations_symlink test
  • Loading branch information
tgeoghegan authored Jun 13, 2023
1 parent 238a95b commit 0c8fe72
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion sqlx-core/src/migrate/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ impl<'s> MigrationSource<'s> for &'s Path {
let mut migrations = Vec::new();

while let Some(entry) = s.next().await? {
if !entry.metadata.is_file() {
// std::fs::metadata traverses symlinks
if !std::fs::metadata(&entry.path)?.is_file() {
// not a file; ignore
continue;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/migrate/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ use std::path::Path;

static EMBEDDED_SIMPLE: Migrator = sqlx::migrate!("tests/migrate/migrations_simple");
static EMBEDDED_REVERSIBLE: Migrator = sqlx::migrate!("tests/migrate/migrations_reversible");
static EMBEDDED_SYMLINK: Migrator = sqlx::migrate!("tests/migrate/migrations_symlink");

#[sqlx_macros::test]
async fn same_output() -> anyhow::Result<()> {
let runtime_simple = Migrator::new(Path::new("tests/migrate/migrations_simple")).await?;
let runtime_reversible =
Migrator::new(Path::new("tests/migrate/migrations_reversible")).await?;
let runtime_symlink = Migrator::new(Path::new("tests/migrate/migrations_symlink")).await?;

assert_same(&EMBEDDED_SIMPLE, &runtime_simple);
assert_same(&EMBEDDED_REVERSIBLE, &runtime_reversible);
assert_same(&EMBEDDED_SYMLINK, &runtime_symlink);

Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions tests/migrate/migrations_symlink

0 comments on commit 0c8fe72

Please sign in to comment.