1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use std::{
    path::{Path, PathBuf},
    str::FromStr,
    sync::Arc,
};

use crate::db::migration::v0_16_0::Migration0_15_2_0_16_0;
use crate::db::migration::v0_19_0::Migration0_18_0_0_19_0;
use crate::Config;
use anyhow::bail;
use anyhow::Context as _;
use itertools::Itertools;
use multimap::MultiMap;
use once_cell::sync::Lazy;
use semver::Version;
use tracing::{debug, info};

use super::v0_12_1::Migration0_12_1_0_13_0;
use super::void_migration::MigrationVoid;

/// Migration trait. It is expected that the [`MigrationOperation::migrate`] method will pick up the relevant database
/// existing under `chain_data_path` and create a new migration database in the same directory.
pub(super) trait MigrationOperation {
    fn new(from: Version, to: Version) -> Self
    where
        Self: Sized;
    /// Performs pre-migration checks. This is the place to check if the database is in a valid
    /// state and if the migration can be performed. Note that some of the higher-level checks
    /// (like checking if the database exists) are performed by the [`Migration`].
    fn pre_checks(&self, chain_data_path: &Path) -> anyhow::Result<()>;
    /// Performs the actual migration. All the logic should be implemented here.
    /// Ideally, the migration should use as little of the Forest codebase as possible to avoid
    /// potential issues with the migration code itself and having to update it in the future.
    /// Returns the path to the migrated database (which is not yet validated)
    fn migrate(&self, chain_data_path: &Path, config: &Config) -> anyhow::Result<PathBuf>;
    /// Performs post-migration checks. This is the place to check if the migration database is
    /// ready to be used by Forest and renamed into a versioned database.
    fn post_checks(&self, chain_data_path: &Path) -> anyhow::Result<()>;
}

/// Migrations map. The key is the starting version and the value is the tuple of the target version
/// and the [`MigrationOperation`] implementation.
///
/// In the future we might want to drop legacy migrations (e.g., to clean-up the database
/// dependency that may get several breaking changes).
// If need be, we should introduce "jump" migrations here, e.g. 0.12.0 -> 0.12.2, 0.12.2 -> 0.12.3, etc.
// This would allow us to skip migrations in case of bugs or just for performance reasons.
type Migrator = Arc<dyn MigrationOperation + Send + Sync>;
type MigrationsMap = MultiMap<Version, (Version, Migrator)>;

/// A utility macro to make the migrations easier to declare.
/// The usage is:
/// `<FROM version> -> <TO version> @ <Migrator object>`
macro_rules! create_migrations {
    ($($from:literal -> $to:literal @ $migration:tt),* $(,)?) => {
pub(super) static MIGRATIONS: Lazy<MigrationsMap> = Lazy::new(|| {
    MigrationsMap::from_iter(
        [
            $((
            Version::from_str($from).unwrap(),
            (
                Version::from_str($to).unwrap(),
                Arc::new($migration::new(
                        $from.parse().expect("invalid <from> version"),
                        $to.parse().expect("invalid <to> version")))
                as _,
            )),
            )*
        ]
        .iter()
        .cloned(),
    )
});
}}

create_migrations!(
    "0.12.1" -> "0.13.0" @ Migration0_12_1_0_13_0,
    "0.15.2" -> "0.16.0" @ Migration0_15_2_0_16_0,
    "0.18.0" -> "0.19.0" @ Migration0_18_0_0_19_0,
);

pub struct Migration {
    from: Version,
    to: Version,
    migrator: Migrator,
}

impl Migration {
    pub fn migrate(&self, chain_data_path: &Path, config: &Config) -> anyhow::Result<()> {
        info!(
            "Migrating database from version {} to {}",
            self.from, self.to
        );

        self.pre_checks(chain_data_path)?;
        let migrated_db = self.migrator.migrate(chain_data_path, config)?;
        self.post_checks(chain_data_path)?;

        let new_db = chain_data_path.join(format!("{}", self.to));
        debug!(
            "Renaming database {} to {}",
            migrated_db.display(),
            new_db.display()
        );
        std::fs::rename(migrated_db, new_db)?;

        let old_db = chain_data_path.join(format!("{}", self.from));
        debug!("Deleting database {}", old_db.display());
        std::fs::remove_dir_all(old_db)?;

        info!("Database migration complete");
        Ok(())
    }

    fn pre_checks(&self, chain_data_path: &Path) -> anyhow::Result<()> {
        let source_db = chain_data_path.join(self.from.to_string());
        if !source_db.exists() {
            bail!(
                "source database {source_db} does not exist",
                source_db = source_db.display()
            );
        }

        let target_db = chain_data_path.join(self.to.to_string());
        if target_db.exists() {
            bail!(
                "target database {target_db} already exists",
                target_db = target_db.display()
            );
        }

        self.migrator.pre_checks(chain_data_path)
    }

    fn post_checks(&self, chain_data_path: &Path) -> anyhow::Result<()> {
        self.migrator.post_checks(chain_data_path)
    }
}

/// Creates a migration chain from `start` to `goal`. The chain is chosen to be the shortest
/// possible. If there are multiple shortest paths, any of them is chosen. This method will use
/// the pre-defined migrations map.
pub(super) fn create_migration_chain(
    start: &Version,
    goal: &Version,
) -> anyhow::Result<Vec<Migration>> {
    create_migration_chain_from_migrations(start, goal, &MIGRATIONS, |from, to| {
        Arc::new(MigrationVoid::new(from.clone(), to.clone()))
    })
}

/// Same as [`create_migration_chain`], but uses any provided migrations map.
fn create_migration_chain_from_migrations(
    start: &Version,
    goal: &Version,
    migrations_map: &MigrationsMap,
    void_migration: impl Fn(&Version, &Version) -> Arc<dyn MigrationOperation + Send + Sync>,
) -> anyhow::Result<Vec<Migration>> {
    let sorted_from_versions = migrations_map.keys().sorted().collect_vec();
    let result = pathfinding::directed::bfs::bfs(
        start,
        |from| {
            if let Some(migrations) = migrations_map.get_vec(from) {
                migrations.iter().map(|(to, _)| to).cloned().collect()
            } else if let Some(&next) =
                sorted_from_versions.get(sorted_from_versions.partition_point(|&i| i <= from))
            {
                // Jump straight to the next smallest from version in the migration map
                vec![next.clone()]
            } else if goal > from {
                // Or to the goal
                vec![goal.clone()]
            } else {
                // Or fail for downgrading
                vec![]
            }
        },
        |to| to == goal,
    )
    .with_context(|| format!("No migration path found from version {start} to {goal}"))?
    .iter()
    .tuple_windows()
    .map(|(from, to)| {
        let migrator = migrations_map
            .get_vec(from)
            .map(|v| {
                v.iter()
                    .find_map(|(version, migration)| {
                        if version == to {
                            Some(migration.clone())
                        } else {
                            None
                        }
                    })
                    .expect("Migration must exist")
            })
            .unwrap_or_else(|| void_migration(from, to));

        Migration {
            from: from.clone(),
            to: to.clone(),
            migrator,
        }
    })
    .collect_vec();

    if result.is_empty() {
        bail!(
            "No migration path found from version {start} to {goal}",
            start = start,
            goal = goal
        );
    }

    Ok(result)
}

/// Returns the name of the temporary database that will be created during the migration.
pub(crate) fn temporary_db_name(from: &Version, to: &Version) -> String {
    format!("migration_{}_{}", from, to).replace('.', "_")
}

#[cfg(test)]
mod tests {
    use std::fs;

    use crate::db::migration::migration_map::temporary_db_name;
    use tempfile::TempDir;

    use super::*;
    use crate::utils::version::FOREST_VERSION;

    #[test]
    fn test_possible_to_migrate_to_current_version() {
        // This test ensures that it is possible to migrate from the oldest supported version to the current
        // version.
        let earliest_version = MIGRATIONS
            .iter_all()
            .map(|(from, _)| from)
            .min()
            .expect("At least one migration must exist");
        let current_version = &FOREST_VERSION;

        let migrations = create_migration_chain(earliest_version, current_version).unwrap();
        assert!(!migrations.is_empty());
    }

    #[test]
    fn test_ensure_migration_possible_from_anywhere_to_latest() {
        // This test ensures that it is possible to find migration chain from any version to the
        // current version.
        let current_version = &FOREST_VERSION;

        for (from, _) in MIGRATIONS.iter_all() {
            let migrations = create_migration_chain(from, current_version).unwrap();
            assert!(!migrations.is_empty());
        }
    }

    #[test]
    fn test_ensure_migration_not_possible_if_higher_than_latest() {
        // This test ensures that it is not possible to migrate from a version higher than the
        // current version.
        let current_version = &FOREST_VERSION;

        let higher_version = Version::new(
            current_version.major,
            current_version.minor,
            current_version.patch + 1,
        );
        let migrations = create_migration_chain(&higher_version, current_version);
        assert!(migrations.is_err());
    }

    #[test]
    fn test_migration_down_not_possible() {
        // This test ensures that it is not possible to migrate down from the latest version.
        // This is not a strict requirement and we may want to allow this in the future.
        let current_version = &*FOREST_VERSION;

        for (from, _) in MIGRATIONS.iter_all() {
            let migrations = create_migration_chain(current_version, from);
            assert!(migrations.is_err());
        }
    }

    #[derive(Debug, Clone)]
    struct EmptyMigration;

    impl MigrationOperation for EmptyMigration {
        fn pre_checks(&self, _chain_data_path: &Path) -> anyhow::Result<()> {
            Ok(())
        }

        fn migrate(&self, _chain_data_path: &Path, _config: &Config) -> anyhow::Result<PathBuf> {
            Ok("".into())
        }

        fn post_checks(&self, _chain_data_path: &Path) -> anyhow::Result<()> {
            Ok(())
        }

        fn new(_from: Version, _to: Version) -> Self
        where
            Self: Sized,
        {
            Self {}
        }
    }

    #[test]
    fn test_migration_should_use_shortest_path() {
        let migrations = MigrationsMap::from_iter(
            [
                (
                    Version::new(0, 1, 0),
                    (Version::new(0, 2, 0), Arc::new(EmptyMigration) as _),
                ),
                (
                    Version::new(0, 2, 0),
                    (Version::new(0, 3, 0), Arc::new(EmptyMigration) as _),
                ),
                (
                    Version::new(0, 1, 0),
                    (Version::new(0, 3, 0), Arc::new(EmptyMigration) as _),
                ),
            ]
            .iter()
            .cloned(),
        );

        let migrations = create_migration_chain_from_migrations(
            &Version::new(0, 1, 0),
            &Version::new(0, 3, 0),
            &migrations,
            |_, _| unimplemented!("void migration"),
        )
        .unwrap();

        // The shortest path is 0.1.0 to 0.3.0 (without going through 0.2.0)
        assert_eq!(1, migrations.len());
        assert_eq!(Version::new(0, 1, 0), migrations[0].from);
        assert_eq!(Version::new(0, 3, 0), migrations[0].to);
    }

    #[test]
    fn test_migration_complex_path() {
        let migrations = MigrationsMap::from_iter(
            [
                (
                    Version::new(0, 1, 0),
                    (Version::new(0, 2, 0), Arc::new(EmptyMigration) as _),
                ),
                (
                    Version::new(0, 2, 0),
                    (Version::new(0, 3, 0), Arc::new(EmptyMigration) as _),
                ),
                (
                    Version::new(0, 1, 0),
                    (Version::new(0, 3, 0), Arc::new(EmptyMigration) as _),
                ),
                (
                    Version::new(0, 3, 0),
                    (Version::new(0, 3, 1), Arc::new(EmptyMigration) as _),
                ),
            ]
            .iter()
            .cloned(),
        );

        let migrations = create_migration_chain_from_migrations(
            &Version::new(0, 1, 0),
            &Version::new(0, 3, 1),
            &migrations,
            |_, _| unimplemented!("void migration"),
        )
        .unwrap();

        // The shortest path is 0.1.0 -> 0.3.0 -> 0.3.1
        assert_eq!(2, migrations.len());
        assert_eq!(Version::new(0, 1, 0), migrations[0].from);
        assert_eq!(Version::new(0, 3, 0), migrations[0].to);
        assert_eq!(Version::new(0, 3, 0), migrations[1].from);
        assert_eq!(Version::new(0, 3, 1), migrations[1].to);
    }

    #[test]
    fn test_void_migration() {
        let migrations = MigrationsMap::from_iter(
            [
                (
                    Version::new(0, 12, 1),
                    (Version::new(0, 13, 0), Arc::new(EmptyMigration) as _),
                ),
                (
                    Version::new(0, 15, 2),
                    (Version::new(0, 16, 0), Arc::new(EmptyMigration) as _),
                ),
            ]
            .iter()
            .cloned(),
        );

        let start = Version::new(0, 12, 0);
        let goal = Version::new(1, 0, 0);
        let migrations =
            create_migration_chain_from_migrations(&start, &goal, &migrations, |_, _| {
                Arc::new(EmptyMigration)
            })
            .unwrap();

        // The shortest path is 0.12.0 -> 0.12.1 -> 0.13.0 -> 0.15.2 -> 0.16.0 -> 1.0.0
        assert_eq!(5, migrations.len());
        for (a, b) in migrations.iter().zip(migrations.iter().skip(1)) {
            assert_eq!(a.to, b.from);
        }
        assert_eq!(start, migrations[0].from);
        assert_eq!(Version::new(0, 12, 1), migrations[1].from);
        assert_eq!(Version::new(0, 13, 0), migrations[2].from);
        assert_eq!(Version::new(0, 15, 2), migrations[3].from);
        assert_eq!(Version::new(0, 16, 0), migrations[4].from);
        assert_eq!(goal, migrations[4].to);
    }

    #[test]
    fn test_same_distance_paths_should_yield_any() {
        let migrations = MigrationsMap::from_iter(
            [
                (
                    Version::new(0, 1, 0),
                    (Version::new(0, 2, 0), Arc::new(EmptyMigration) as _),
                ),
                (
                    Version::new(0, 2, 0),
                    (Version::new(0, 4, 0), Arc::new(EmptyMigration) as _),
                ),
                (
                    Version::new(0, 1, 0),
                    (Version::new(0, 3, 0), Arc::new(EmptyMigration) as _),
                ),
                (
                    Version::new(0, 3, 0),
                    (Version::new(0, 4, 0), Arc::new(EmptyMigration) as _),
                ),
            ]
            .iter()
            .cloned(),
        );

        let migrations = create_migration_chain_from_migrations(
            &Version::new(0, 1, 0),
            &Version::new(0, 4, 0),
            &migrations,
            |_, _| unimplemented!("void migration"),
        )
        .unwrap();

        // there are two possible shortest paths:
        // 0.1.0 -> 0.2.0 -> 0.4.0
        // 0.1.0 -> 0.3.0 -> 0.4.0
        // Both of them are correct and should be accepted.
        assert_eq!(2, migrations.len());
        if migrations[0].to == Version::new(0, 2, 0) {
            assert_eq!(Version::new(0, 1, 0), migrations[0].from);
            assert_eq!(Version::new(0, 2, 0), migrations[0].to);
            assert_eq!(Version::new(0, 2, 0), migrations[1].from);
            assert_eq!(Version::new(0, 4, 0), migrations[1].to);
        } else {
            assert_eq!(Version::new(0, 1, 0), migrations[0].from);
            assert_eq!(Version::new(0, 3, 0), migrations[0].to);
            assert_eq!(Version::new(0, 3, 0), migrations[1].from);
            assert_eq!(Version::new(0, 4, 0), migrations[1].to);
        }
    }

    struct SimpleMigration0_1_0_0_2_0 {
        from: Version,
        to: Version,
    }

    impl MigrationOperation for SimpleMigration0_1_0_0_2_0 {
        fn pre_checks(&self, chain_data_path: &Path) -> anyhow::Result<()> {
            let path = chain_data_path.join(self.from.to_string());
            if !path.exists() {
                anyhow::bail!("{} does not exist", self.from);
            }
            Ok(())
        }

        fn migrate(&self, chain_data_path: &Path, _config: &Config) -> anyhow::Result<PathBuf> {
            let temp_db_path = chain_data_path.join(temporary_db_name(&self.from, &self.to));
            fs::create_dir(&temp_db_path).unwrap();
            Ok(temp_db_path)
        }

        fn post_checks(&self, chain_data_path: &Path) -> anyhow::Result<()> {
            let path = chain_data_path.join(temporary_db_name(&self.from, &self.to));
            if !path.exists() {
                anyhow::bail!("{} does not exist", path.display());
            }
            Ok(())
        }

        fn new(from: Version, to: Version) -> Self
        where
            Self: Sized,
        {
            Self { from, to }
        }
    }

    #[test]
    fn test_migration_map_migration() {
        let from = Version::new(0, 1, 0);
        let to = Version::new(0, 2, 0);
        let migration = Migration {
            from: from.clone(),
            to: to.clone(),
            migrator: Arc::new(SimpleMigration0_1_0_0_2_0::new(from, to)),
        };

        let temp_dir = TempDir::new().unwrap();

        assert!(migration.pre_checks(temp_dir.path()).is_err());
        fs::create_dir(temp_dir.path().join("0.1.0")).unwrap();
        assert!(migration.pre_checks(temp_dir.path()).is_ok());

        migration
            .migrate(temp_dir.path(), &Config::default())
            .unwrap();
        assert!(temp_dir.path().join("0.2.0").exists());

        assert!(migration.post_checks(temp_dir.path()).is_err());
        fs::create_dir(temp_dir.path().join("migration_0_1_0_0_2_0")).unwrap();
        assert!(migration.post_checks(temp_dir.path()).is_ok());
    }
}