rust_ef/migration/
snapshot.rs1use super::dialect::MigrationDialect;
10use super::engine::MigrationEngine;
11use super::types::ModelSnapshot;
12use crate::error::{EFError, EFResult};
13use crate::metadata_cache::MetadataCache;
14use crate::model_builder::ModelBuilder;
15use std::path::Path;
16
17pub fn build_registered_snapshot(
24 context_key: Option<&str>,
25 dialect: MigrationDialect,
26) -> EFResult<ModelSnapshot> {
27 let built = MetadataCache::build(context_key);
28 let builder = ModelBuilder::from_built(&built);
29 let metas = builder.build();
30 if metas.is_empty() {
31 return Err(EFError::configuration(
32 "No entity types registered. Derive EntityType on the application entities \
33 (and apply #[entity(T)] configurations) before building a snapshot.",
34 ));
35 }
36 Ok(MigrationEngine::new(dialect).create_snapshot("__snapshot__", &metas))
37}
38
39pub fn write_snapshot(path: impl AsRef<Path>, snapshot: &ModelSnapshot) -> EFResult<()> {
41 let json = super::snapshot_io::snapshot_to_json(snapshot);
42 if let Some(parent) = path.as_ref().parent() {
43 if !parent.as_os_str().is_empty() {
44 std::fs::create_dir_all(parent).map_err(|e| {
45 EFError::migration(format!("Cannot create {}: {e}", parent.display()))
46 })?;
47 }
48 }
49 std::fs::write(path.as_ref(), json)
50 .map_err(|e| EFError::migration(format!("Cannot write snapshot: {e}")))
51}
52
53pub fn save_registered_snapshot(
55 path: impl AsRef<Path>,
56 context_key: Option<&str>,
57 dialect: MigrationDialect,
58) -> EFResult<ModelSnapshot> {
59 let snapshot = build_registered_snapshot(context_key, dialect)?;
60 write_snapshot(path, &snapshot)?;
61 Ok(snapshot)
62}