sdk

Radroots SDK and bindings
git clone https://radroots.dev/git/sdk.git
Log | Files | Refs | README

snapshot.rs (2958B)


      1 use radroots_replica_db::ReplicaDbExportManifestRs;
      2 
      3 pub const EXPORT_MANIFEST_FIELD: &str = "manifest_rs";
      4 pub const EXPORT_DB_BYTES_FIELD: &str = "db_bytes";
      5 
      6 #[derive(Debug, Clone, PartialEq, Eq)]
      7 pub struct ExportManifestSummary {
      8     pub export_version: String,
      9     pub replica_db_version: String,
     10     pub backup_format_version: String,
     11     pub schema_hash: String,
     12     pub schema_table_count: usize,
     13     pub migration_count: usize,
     14     pub table_count_count: usize,
     15 }
     16 
     17 pub fn synced_export_error(pending_count: usize, expected_count: usize) -> Option<String> {
     18     if pending_count == 0 {
     19         None
     20     } else {
     21         Some(format!(
     22             "replica db export requires synced state (pending {pending_count}/{expected_count})"
     23         ))
     24     }
     25 }
     26 
     27 pub fn export_manifest_summary(manifest: &ReplicaDbExportManifestRs) -> ExportManifestSummary {
     28     ExportManifestSummary {
     29         export_version: manifest.export_version.clone(),
     30         replica_db_version: manifest.replica_db_version.clone(),
     31         backup_format_version: manifest.backup_format_version.clone(),
     32         schema_hash: manifest.schema_hash.clone(),
     33         schema_table_count: manifest.schema.len(),
     34         migration_count: manifest.migrations.len(),
     35         table_count_count: manifest.table_counts.len(),
     36     }
     37 }
     38 
     39 #[cfg(test)]
     40 mod tests {
     41     use super::{
     42         EXPORT_DB_BYTES_FIELD, EXPORT_MANIFEST_FIELD, export_manifest_summary, synced_export_error,
     43     };
     44 
     45     fn manifest() -> radroots_replica_db::ReplicaDbExportManifestRs {
     46         radroots_replica_db::ReplicaDbExportManifestRs {
     47             export_version: "1".to_owned(),
     48             replica_db_version: "0.1.0".to_owned(),
     49             backup_format_version: "1".to_owned(),
     50             schema_hash: "schema-hash".to_owned(),
     51             schema: Vec::new(),
     52             migrations: Vec::new(),
     53             table_counts: Vec::new(),
     54         }
     55     }
     56 
     57     #[test]
     58     fn export_snapshot_field_names_are_stable() {
     59         assert_eq!(EXPORT_MANIFEST_FIELD, "manifest_rs");
     60         assert_eq!(EXPORT_DB_BYTES_FIELD, "db_bytes");
     61     }
     62 
     63     #[test]
     64     fn synced_export_error_allows_empty_pending_queue() {
     65         assert_eq!(synced_export_error(0, 4), None);
     66     }
     67 
     68     #[test]
     69     fn synced_export_error_reports_pending_and_expected_counts() {
     70         assert_eq!(
     71             synced_export_error(2, 4).expect("error"),
     72             "replica db export requires synced state (pending 2/4)"
     73         );
     74     }
     75 
     76     #[test]
     77     fn export_manifest_summary_preserves_versions_and_counts() {
     78         let summary = export_manifest_summary(&manifest());
     79         assert_eq!(summary.export_version, "1");
     80         assert_eq!(summary.replica_db_version, "0.1.0");
     81         assert_eq!(summary.backup_format_version, "1");
     82         assert_eq!(summary.schema_hash, "schema-hash");
     83         assert_eq!(summary.schema_table_count, 0);
     84         assert_eq!(summary.migration_count, 0);
     85         assert_eq!(summary.table_count_count, 0);
     86     }
     87 }