diff options
| author | tv <tv@krebsco.de> | 2026-11-29 15:02:23 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-30 14:59:15 +0000 |
| commit | a0f22c88d54d351b060c2d0d9f0a8cbf5f412ba2 (patch) | |
| tree | d65495f9ed222fb978bc09afcb3bd644a99aa7c5 /src/store.rs | |
| parent | 83f1d36bbaeb193ae1c663b5b1b7956661ecac8c (diff) | |
enstate: --migrate, renames both ways, kept entries
What a generation sees of a snapshot accepted under another schema:
same name and type, renamedFrom, a rename in the stored schema read
backwards, and entries kept, with their types, in the snapshot's kept/.
Plans start from what is seen, and store it under the running schema;
a generation change rewrites no snapshot. --migrate (and --dry-run)
says what each channel sees, refuses for required slots nothing fills,
puts the views in place and runs their reactions; --materialize and
--status go by what is seen, too. A view made from an unfilled
required slot isn't there. The rollback cases of migrations.md that
need no converters or steps are tests.
Diffstat (limited to 'src/store.rs')
| -rw-r--r-- | src/store.rs | 60 |
1 files changed, 58 insertions, 2 deletions
diff --git a/src/store.rs b/src/store.rs index b25495e..4ac5ef2 100644 --- a/src/store.rs +++ b/src/store.rs @@ -6,6 +6,7 @@ //! STATE/channels/CHANNEL/snapshots/ID/snapshot.json id, and the id it was built on //! STATE/channels/CHANNEL/snapshots/ID/schema.json the schema it was accepted under //! STATE/channels/CHANNEL/snapshots/ID/entries/SLOT +//! STATE/channels/CHANNEL/snapshots/ID/kept/SLOT what no slot wanted, types in snapshot.json //! RUN/views/VIEW -> .generations/VIEW.ID //! RUN/tmp/ scratch //! ``` @@ -24,6 +25,9 @@ use serde::{Deserialize, Serialize}; use crate::content::{self, Content}; use crate::scratch::Scratch; +/// Entries kept: by slot name, the type each had, and the entry. +pub type Kept = std::collections::BTreeMap<String, (String, Vec<u8>)>; + pub struct Store { pub state: PathBuf, pub run: PathBuf, @@ -34,6 +38,9 @@ pub struct Snapshot { pub id: String, pub base: Option<String>, pub content: Content, + /// Entries no slot of the schema it was accepted under wanted, by slot name, with + /// the types they had (docs/design.md, section 6). + pub kept: Kept, /// The schema it was accepted under, as it was. pub schema: Vec<u8>, } @@ -44,6 +51,9 @@ struct SnapshotFile { id: String, channel: String, base: Option<String>, + /// The types of the entries in `kept/`. + #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")] + kept: std::collections::BTreeMap<String, String>, } /// Held while the store is used; released when dropped. @@ -122,20 +132,33 @@ impl Store { } let schema = std::fs::read(dir.join("schema.json")) .with_context(|| format!("reading {}", dir.join("schema.json").display()))?; + let mut kept_entries = content::read_dir(&dir.join("kept"))?; + let mut kept = Kept::new(); + for (slot, media_type) in file.kept { + let bytes = kept_entries + .remove(&slot) + .with_context(|| format!("{}: kept {slot} is missing", dir.display()))?; + kept.insert(slot, (media_type, bytes)); + } + if let Some(slot) = kept_entries.keys().next() { + bail!("{}: kept {slot} has no type", dir.display()); + } Ok(Some(Snapshot { id: file.id, base: file.base, content: content::read_dir(&dir.join("entries"))?, + kept, schema, })) } - /// Makes `content` the current snapshot of `channel`, accepted under `schema`, - /// built on `base`; returns its id. The snapshot it replaces is removed. + /// Makes `content`, and `kept`, the current snapshot of `channel`, accepted under + /// `schema`, built on `base`; returns its id. The snapshot it replaces is removed. pub fn commit( &self, channel: &str, content: &Content, + kept: &Kept, base: Option<&str>, schema: &[u8], ) -> Result<String> { @@ -148,6 +171,10 @@ impl Store { id: id.clone(), channel: channel.to_owned(), base: base.map(str::to_owned), + kept: kept + .iter() + .map(|(slot, (media_type, _))| (slot.clone(), media_type.clone())) + .collect(), }; content::write_file( &new.join("snapshot.json"), @@ -156,6 +183,13 @@ impl Store { )?; content::write_file(&new.join("schema.json"), schema, 0o600)?; content::write_dir(content, &new.join("entries"), 0o600)?; + if !kept.is_empty() { + let kept: Content = kept + .iter() + .map(|(slot, (_, bytes))| (slot.clone(), bytes.clone())) + .collect(); + content::write_dir(&kept, &new.join("kept"), 0o600)?; + } std::fs::rename(&new, snapshots.join(&id))?; let old = std::fs::read_link(self.channel_dir(channel).join("current")).ok(); replace_link( @@ -168,6 +202,28 @@ impl Store { Ok(id) } + /// The views in place. + pub fn installed_views(&self) -> Result<Vec<String>> { + let views = self.run.join("views"); + let mut names = Vec::new(); + let entries = match std::fs::read_dir(&views) { + Ok(entries) => entries, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(names), + Err(error) => { + return Err(error).with_context(|| format!("reading {}", views.display())); + } + }; + for entry in entries { + let entry = entry?; + let name = entry.file_name().to_string_lossy().into_owned(); + if entry.file_type()?.is_symlink() && !name.ends_with(".new-link") { + names.push(name); + } + } + names.sort(); + Ok(names) + } + /// The directory the view `name` is in place at, if it is. pub fn installed_view(&self, name: &str) -> Option<PathBuf> { let link = self.run.join("views").join(name); |
