use super::migration_map::MigrationOperation;
use crate::Config;
use crate::db::migration::migration_map::MigrationOperationExt as _;
use anyhow::Context;
use semver::Version;
use std::path::{Path, PathBuf};
use tracing::info;
pub(super) struct Migration0_34_0_0_34_1 {
from: Version,
to: Version,
}
impl MigrationOperation for Migration0_34_0_0_34_1 {
fn new(from: Version, to: Version) -> Self
where
Self: Sized,
{
Self { from, to }
}
fn from(&self) -> &Version {
&self.from
}
fn to(&self) -> &Version {
&self.to
}
fn migrate_core(&self, chain_data_path: &Path, _: &Config) -> anyhow::Result<PathBuf> {
let old_db = self.old_db_path(chain_data_path);
let temp_db = self.temporary_db_path(chain_data_path);
info!(
"Renaming database directory from {} to {}",
old_db.display(),
temp_db.display()
);
std::fs::rename(&old_db, &temp_db).context("failed to rename database directory")?;
info!("Adding EthBlockBloom column to database");
let mut opts = paritydb_0_34_0::to_options(temp_db.clone());
if let Err(e) =
parity_db::Db::add_column(&mut opts, paritydb_0_34_0::eth_block_bloom_column_options())
{
if let Err(restore) = std::fs::rename(&temp_db, &old_db) {
tracing::error!(
"failed to restore database to {}; data is preserved at {}: {restore}",
old_db.display(),
temp_db.display()
);
}
return Err(e).context("failed to add EthBlockBloom column");
}
std::fs::create_dir_all(&old_db).context("failed to create placeholder directory")?;
info!("Migration completed successfully");
Ok(temp_db)
}
}
mod paritydb_0_34_0 {
use parity_db::{ColumnOptions, CompressionType, Options};
use std::path::PathBuf;
use strum::{Display, EnumIter, IntoEnumIterator};
#[derive(Copy, Clone, Debug, PartialEq, EnumIter, Display)]
#[repr(u8)]
pub(super) enum DbColumn {
GraphDagCborBlake2b256,
GraphFull,
Settings,
EthMappings,
PersistentGraph,
}
impl DbColumn {
fn create_column_options(compression: CompressionType) -> Vec<ColumnOptions> {
DbColumn::iter()
.map(|col| match col {
DbColumn::GraphDagCborBlake2b256 | DbColumn::PersistentGraph => ColumnOptions {
preimage: true,
compression,
..Default::default()
},
DbColumn::GraphFull => ColumnOptions {
preimage: true,
btree_index: true,
compression,
..Default::default()
},
DbColumn::Settings => ColumnOptions {
preimage: false,
btree_index: true,
compression,
..Default::default()
},
DbColumn::EthMappings => ColumnOptions {
preimage: false,
btree_index: false,
compression,
..Default::default()
},
})
.collect()
}
}
pub(super) fn eth_block_bloom_column_options() -> ColumnOptions {
ColumnOptions {
preimage: false,
btree_index: true,
compression: CompressionType::Lz4,
..Default::default()
}
}
pub(super) fn to_options(path: PathBuf) -> Options {
Options {
path,
sync_wal: true,
sync_data: true,
stats: false,
salt: None,
columns: DbColumn::create_column_options(CompressionType::Lz4),
compression_threshold: [(0, 128)].into_iter().collect(),
}
}
}