2021-12-21 08:23:17 +00:00
|
|
|
use crate::{database::CURRENT_SCHEMA_VERSION, Error, SlasherDB};
|
2020-12-28 05:09:19 +00:00
|
|
|
use types::EthSpec;
|
|
|
|
|
|
|
|
impl<E: EthSpec> SlasherDB<E> {
|
|
|
|
/// If the database exists, and has a schema, attempt to migrate it to the current version.
|
2021-12-21 08:23:17 +00:00
|
|
|
pub fn migrate(self) -> Result<Self, Error> {
|
|
|
|
let mut txn = self.begin_rw_txn()?;
|
|
|
|
let schema_version = self.load_schema_version(&mut txn)?;
|
|
|
|
drop(txn);
|
|
|
|
|
|
|
|
if let Some(schema_version) = schema_version {
|
2020-12-28 05:09:19 +00:00
|
|
|
match (schema_version, CURRENT_SCHEMA_VERSION) {
|
2021-12-21 08:23:17 +00:00
|
|
|
// Schema v3 changed the underlying database from LMDB to MDBX. Unless the user did
|
|
|
|
// some manual hacking it should be impossible to read an MDBX schema version < 3.
|
|
|
|
(from, _) if from < 3 => Err(Error::IncompatibleSchemaVersion {
|
|
|
|
database_schema_version: schema_version,
|
|
|
|
software_schema_version: CURRENT_SCHEMA_VERSION,
|
|
|
|
}),
|
|
|
|
(x, y) if x == y => Ok(self),
|
|
|
|
(_, _) => Err(Error::IncompatibleSchemaVersion {
|
|
|
|
database_schema_version: schema_version,
|
|
|
|
software_schema_version: CURRENT_SCHEMA_VERSION,
|
|
|
|
}),
|
2020-12-28 05:09:19 +00:00
|
|
|
}
|
2021-12-21 08:23:17 +00:00
|
|
|
} else {
|
|
|
|
Ok(self)
|
2020-12-28 05:09:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|