Skip to main content

kmp_embedded/
migration.rs

1//! Store migration, composed.
2//!
3//! The machinery lives in the storage adapter; what belongs here is the one
4//! decision it deliberately does not make — how a context event becomes
5//! projection mutations. That derivation is the application's, and injecting
6//! it here is what keeps the adapter free of the layer above it and the MCP
7//! binary free of the layer below.
8
9use std::path::Path;
10
11use kmp_adapter_embedded::{EmbeddedKernelStore, StorageEngine, StoreMigrationReceipt};
12use kmp_domain::PortError;
13
14/// Migrates `source_dir` into `destination_dir`, returning what was moved.
15/// The destination is created with the default engine.
16///
17/// Retired layouts are rejected without touching the source or destination.
18pub async fn migrate_data_dir(
19    source_dir: &Path,
20    destination_dir: &Path,
21) -> Result<StoreMigrationReceipt, PortError> {
22    migrate_data_dir_to(source_dir, destination_dir, StorageEngine::Sqlite).await
23}
24
25/// [`migrate_data_dir`] with the destination engine chosen. Kept for API
26/// compatibility; current binaries only contain SQLite and cannot read
27/// format-1 stores.
28pub async fn migrate_data_dir_to(
29    source_dir: &Path,
30    destination_dir: &Path,
31    destination_engine: StorageEngine,
32) -> Result<StoreMigrationReceipt, PortError> {
33    let (_store, receipt) = EmbeddedKernelStore::migrate_data_dir_to(
34        source_dir,
35        destination_dir,
36        destination_engine,
37        kmp_application::projection_mutations_for_context_event,
38    )
39    .await?;
40    Ok(receipt)
41}
42
43/// Migrate once, reopen afterwards. Safe on every start: a destination that
44/// already holds a store is opened as it is, and the returned receipt says
45/// whether this call was the one that migrated it.
46pub async fn open_or_migrate_data_dir(
47    source_dir: &Path,
48    destination_dir: &Path,
49) -> Result<Option<StoreMigrationReceipt>, PortError> {
50    let (_store, receipt) = EmbeddedKernelStore::open_or_migrate_data_dir(
51        source_dir,
52        destination_dir,
53        kmp_application::projection_mutations_for_context_event,
54    )
55    .await?;
56    Ok(receipt)
57}