Traverse symlinks when resolving migrations (#2445)

* 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
This commit is contained in:
Tim Geoghegan 2023-06-13 11:21:09 -07:00 committed by GitHub
parent 238a95b0f3
commit 0c8fe729ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 1 deletions

View file

@ -24,7 +24,8 @@ impl<'s> MigrationSource<'s> for &'s Path {
let mut migrations = Vec::new(); let mut migrations = Vec::new();
while let Some(entry) = s.next().await? { 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 // not a file; ignore
continue; continue;
} }

View file

@ -3,15 +3,18 @@ use std::path::Path;
static EMBEDDED_SIMPLE: Migrator = sqlx::migrate!("tests/migrate/migrations_simple"); static EMBEDDED_SIMPLE: Migrator = sqlx::migrate!("tests/migrate/migrations_simple");
static EMBEDDED_REVERSIBLE: Migrator = sqlx::migrate!("tests/migrate/migrations_reversible"); static EMBEDDED_REVERSIBLE: Migrator = sqlx::migrate!("tests/migrate/migrations_reversible");
static EMBEDDED_SYMLINK: Migrator = sqlx::migrate!("tests/migrate/migrations_symlink");
#[sqlx_macros::test] #[sqlx_macros::test]
async fn same_output() -> anyhow::Result<()> { async fn same_output() -> anyhow::Result<()> {
let runtime_simple = Migrator::new(Path::new("tests/migrate/migrations_simple")).await?; let runtime_simple = Migrator::new(Path::new("tests/migrate/migrations_simple")).await?;
let runtime_reversible = let runtime_reversible =
Migrator::new(Path::new("tests/migrate/migrations_reversible")).await?; 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_SIMPLE, &runtime_simple);
assert_same(&EMBEDDED_REVERSIBLE, &runtime_reversible); assert_same(&EMBEDDED_REVERSIBLE, &runtime_reversible);
assert_same(&EMBEDDED_SYMLINK, &runtime_symlink);
Ok(()) Ok(())
} }

View file

@ -0,0 +1 @@
migrations_simple