Skip to content

Commit f12a3e9

Browse files
committed
chore: filesystem layout helpers
1 parent 7f99fbb commit f12a3e9

3 files changed

Lines changed: 38 additions & 8 deletions

File tree

crates/dry_run_cli/src/main.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -966,14 +966,8 @@ fn write_snapshot_export(
966966
key: &SnapshotKey,
967967
snap: &dry_run_core::SchemaSnapshot,
968968
) -> anyhow::Result<PathBuf> {
969-
let path = out_root
970-
.join(&key.project_id.0)
971-
.join(&key.database_id.0)
972-
.join(format!(
973-
"{}-{}.json.zst",
974-
snap.timestamp.format("%Y%m%dT%H%M%SZ"),
975-
snap.content_hash,
976-
));
969+
let path =
970+
dry_run_core::history::snapshot_path(out_root, key, snap.timestamp, &snap.content_hash);
977971
if let Some(parent) = path.parent() {
978972
std::fs::create_dir_all(parent)?;
979973
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use crate::history::SnapshotKey;
2+
use chrono::{DateTime, NaiveDateTime, TimeZone, Utc};
3+
use std::path::{Path, PathBuf};
4+
5+
pub const SNAPSHOT_EXTENSION: &str = "json.zst";
6+
7+
const TS_FORMAT: &str = "%Y%m%dT%H%M%SZ";
8+
9+
#[must_use]
10+
pub fn snapshot_path(
11+
root: &Path,
12+
key: &SnapshotKey,
13+
timestamp: DateTime<Utc>,
14+
content_hash: &str,
15+
) -> PathBuf {
16+
root.join(&key.project_id.0)
17+
.join(&key.database_id.0)
18+
.join(format!(
19+
"{}-{}.{}",
20+
timestamp.format(TS_FORMAT),
21+
content_hash,
22+
SNAPSHOT_EXTENSION,
23+
))
24+
}
25+
26+
#[must_use]
27+
pub fn parse_snapshot_filename(name: &str) -> Option<(DateTime<Utc>, String)> {
28+
let stem = name.strip_suffix(&format!(".{SNAPSHOT_EXTENSION}"))?;
29+
let (ts_str, hash) = stem.split_once('-')?;
30+
let naive = NaiveDateTime::parse_from_str(ts_str, TS_FORMAT).ok()?;
31+
let ts = Utc.from_utc_datetime(&naive);
32+
33+
Some((ts, hash.to_string()))
34+
}

crates/dry_run_core/src/history/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
pub mod filesystem_layout;
12
mod snapshot_store;
23
mod store;
34

5+
pub use filesystem_layout::{SNAPSHOT_EXTENSION, parse_snapshot_filename, snapshot_path};
46
pub use snapshot_store::{
57
DatabaseId, ProjectId, PutOutcome, SnapshotKey, SnapshotRef, SnapshotStore, TimeRange,
68
};

0 commit comments

Comments
 (0)