Skip to content

Commit

Permalink
Use create_new instead of atomic-file-write
Browse files Browse the repository at this point in the history
This provides the same functionality but without temporary files, platform-specific code, fragility of `O_TMPFILE` support, and an extra dependency.
  • Loading branch information
mattfbacon committed Nov 30, 2023
1 parent 929af41 commit 8c10dd6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 46 deletions.
37 changes: 2 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion sqlx-macros-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ tokio = { workspace = true, optional = true }

dotenvy = { workspace = true }

atomic-write-file = { version = "0.1" }
hex = { version = "0.4.3" }
heck = { version = "0.4", features = ["unicode"] }
either = "1.6.1"
Expand Down
22 changes: 12 additions & 10 deletions sqlx-macros-core/src/query/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,23 @@ where

pub(super) fn save_in(&self, dir: impl AsRef<Path>) -> crate::Result<()> {
let path = dir.as_ref().join(format!("query-{}.json", self.hash));
let mut file = atomic_write_file::AtomicWriteFile::open(&path)
.map_err(|err| format!("failed to open the temporary file: {err:?}"))?;

serde_json::to_writer_pretty(file.as_file_mut(), self)
.map_err(|err| format!("failed to serialize query data to file: {err:?}"))?;
std::fs::remove_file(&path).map_err(|err| format!("failed to delete {path:?}: {err:?}"))?;
let mut file = std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&path)
.map_err(|err| format!("failed to exclusively create {path:?}: {err:?}"))?;

let data = serde_json::to_string_pretty(self)
.map_err(|err| format!("failed to serialize query data: {err:?}"))?;
file.write_all(data.as_bytes())
.map_err(|err| format!("failed to write query data to file: {err:?}"))?;

// Ensure there is a newline at the end of the JSON file to avoid
// accidental modification by IDE and make github diff tool happier.
file.as_file_mut()
.write_all(b"\n")
file.write_all(b"\n")
.map_err(|err| format!("failed to append a newline to file: {err:?}"))?;

file.commit()
.map_err(|err| format!("failed to commit the query data to {path:?}: {err:?}"))?;

Ok(())
}
}
Expand Down

0 comments on commit 8c10dd6

Please sign in to comment.