Skip to main content

linkrs_storage/schema/
version_history.rs

1//! Version history tracking for schema migrations
2//!
3//! Maintains a complete history of schema versions, including change logs
4//! for each label.
5
6use super::change::{ChangeLog, PropertyChange, SchemaObjectType};
7use serde::{Deserialize, Serialize};
8
9/// Version history for a single label (vertex or edge type)
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct LabelVersionHistory {
12    /// Label ID
13    pub label_id: u32,
14    /// Label name
15    pub label_name: String,
16    /// Object type (vertex or edge)
17    pub object_type: SchemaObjectType,
18    /// Change log for this label
19    pub change_log: ChangeLog,
20}
21
22impl LabelVersionHistory {
23    /// Create a new version history
24    pub fn new(label_id: u32, label_name: String, object_type: SchemaObjectType) -> Self {
25        Self {
26            label_id,
27            label_name: label_name.clone(),
28            object_type,
29            change_log: ChangeLog::new(object_type, label_id, label_name),
30        }
31    }
32
33    /// Add a change to the history
34    pub fn add_change(&mut self, change: PropertyChange) {
35        self.change_log.add_change(change);
36    }
37
38    /// Get the latest version
39    pub fn latest_version(&self) -> u64 {
40        self.change_log.latest_version().unwrap_or(1)
41    }
42
43    /// Get all versions in order
44    pub fn get_versions(&self) -> Vec<u64> {
45        self.change_log.get_versions()
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_label_version_history_creation() {
55        let history = LabelVersionHistory::new(1, "User".to_string(), SchemaObjectType::Vertex);
56        assert_eq!(history.label_id, 1);
57        assert_eq!(history.label_name, "User");
58        assert_eq!(history.latest_version(), 1);
59    }
60}