forest/db/migration/
void_migration.rs

1// Copyright 2019-2025 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4//! Migration logic from any version that requires no migration logic.
5
6use super::migration_map::MigrationOperationExt as _;
7use crate::Config;
8use semver::Version;
9use std::path::{Path, PathBuf};
10
11use super::migration_map::MigrationOperation;
12
13pub(super) struct MigrationVoid {
14    from: Version,
15    to: Version,
16}
17
18impl MigrationOperation for MigrationVoid {
19    fn migrate_core(&self, _: &Path, _config: &Config) -> anyhow::Result<PathBuf> {
20        unimplemented!("Overriding migrate implementation instead")
21    }
22
23    fn migrate(&self, chain_data_path: &Path, _: &Config) -> anyhow::Result<()> {
24        self.pre_checks(chain_data_path)?;
25        let old_db = self.old_db_path(chain_data_path);
26        let new_db = self.new_db_path(chain_data_path);
27        tracing::debug!(
28            "Renaming database {} to {}",
29            old_db.display(),
30            new_db.display()
31        );
32        std::fs::rename(old_db, new_db)?;
33        Ok(())
34    }
35
36    fn new(from: Version, to: Version) -> Self
37    where
38        Self: Sized,
39    {
40        Self { from, to }
41    }
42
43    fn from(&self) -> &Version {
44        &self.from
45    }
46
47    fn to(&self) -> &Version {
48        &self.to
49    }
50}
51
52#[cfg(test)]
53mod test {
54    use super::*;
55    use semver::Version;
56    use tempfile::TempDir;
57
58    #[test]
59    fn test_migration_void() {
60        let migration = MigrationVoid::new(Version::new(1, 0, 0), Version::new(1, 0, 1));
61        let chain_data_path = TempDir::new().unwrap();
62
63        // create a file in the temporary database directory, under a directory (to ensure that the
64        // directory is copied recursively).
65        let content_dir = chain_data_path.path().join("1.0.0").join("R'lyeh");
66        std::fs::create_dir_all(&content_dir).unwrap();
67
68        let content_file = content_dir.join("cthulhu");
69        let chant = "ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn";
70        std::fs::write(content_file, chant).unwrap();
71
72        let path = chain_data_path.path();
73        migration.migrate(path, &Config::default()).unwrap();
74        let new_db_path = migration.new_db_path(path);
75
76        // check that the target database directory exists and contains the file with the
77        // expected content.
78        let new_db_content_dir = new_db_path.join("R'lyeh");
79        let new_db_content_file = new_db_content_dir.join("cthulhu");
80        assert!(new_db_content_file.exists());
81        assert_eq!(std::fs::read_to_string(new_db_content_file).unwrap(), chant);
82    }
83}