cleanup migration function a bit

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-06-23 19:55:17 +00:00
parent 19d8f0b27e
commit efbdced535

View file

@ -6,7 +6,7 @@ use std::{
sync::Arc,
};
use conduit::{debug_info, debug_warn};
use conduit::{debug, debug_info, debug_warn, error, info, utils, warn, Config, Error, Result};
use database::KeyValueDatabase;
use itertools::Itertools;
use ruma::{
@ -14,9 +14,14 @@ use ruma::{
push::Ruleset,
EventId, OwnedRoomId, RoomId, UserId,
};
use tracing::{debug, error, info, warn};
use crate::{services, utils, Config, Error, Result};
use crate::services;
/// The current schema version.
/// * If the database is opened at lesser version we apply migrations up to this
/// version.
/// * If the database is opened at greater version we reject with error.
const DATABASE_VERSION: u64 = 13 + cfg!(feature = "sha256_media") as u64;
pub(crate) async fn migrations(db: &KeyValueDatabase, config: &Config) -> Result<()> {
// Matrix resource ownership is based on the server name; changing it
@ -32,16 +37,34 @@ pub(crate) async fn migrations(db: &KeyValueDatabase, config: &Config) -> Result
}
}
// If the database has any data, perform data migrations before starting
// do not increment the db version if the user is not using sha256_media
let latest_database_version = if cfg!(feature = "sha256_media") {
14
} else {
13
};
if services().users.count()? > 0 {
// MIGRATIONS
migrate(db, config).await
} else {
fresh(db, config).await
}
}
async fn fresh(db: &KeyValueDatabase, config: &Config) -> Result<()> {
services().globals.bump_database_version(DATABASE_VERSION)?;
db.global
.insert(b"fix_bad_double_separator_in_state_cache", &[])?;
db.global
.insert(b"retroactively_fix_bad_data_from_roomuserid_joined", &[])?;
// Create the admin room and server user on first run
crate::admin::create_admin_room().await?;
warn!(
"Created new {} database with version {DATABASE_VERSION}",
config.database_backend,
);
Ok(())
}
/// Apply any migrations
async fn migrate(db: &KeyValueDatabase, config: &Config) -> Result<()> {
if services().globals.database_version()? < 1 {
db_lt_1(db, config).await?;
}
@ -97,7 +120,7 @@ pub(crate) async fn migrations(db: &KeyValueDatabase, config: &Config) -> Result
}
#[cfg(feature = "sha256_media")]
if services().globals.database_version()? < 14 && cfg!(feature = "sha256_media") {
if services().globals.database_version()? < 14 {
feat_sha256_media(db, config).await?;
}
@ -119,10 +142,10 @@ pub(crate) async fn migrations(db: &KeyValueDatabase, config: &Config) -> Result
assert_eq!(
services().globals.database_version().unwrap(),
latest_database_version,
DATABASE_VERSION,
"Failed asserting local database version {} is equal to known latest conduwuit database version {}",
services().globals.database_version().unwrap(),
latest_database_version
DATABASE_VERSION,
);
{
@ -176,27 +199,9 @@ pub(crate) async fn migrations(db: &KeyValueDatabase, config: &Config) -> Result
}
info!(
"Loaded {} database with schema version {}",
config.database_backend, latest_database_version
"Loaded {} database with schema version {DATABASE_VERSION}",
config.database_backend,
);
} else {
services()
.globals
.bump_database_version(latest_database_version)?;
db.global
.insert(b"fix_bad_double_separator_in_state_cache", &[])?;
db.global
.insert(b"retroactively_fix_bad_data_from_roomuserid_joined", &[])?;
// Create the admin room and server user on first run
crate::admin::create_admin_room().await?;
warn!(
"Created new {} database with version {}",
config.database_backend, latest_database_version
);
}
Ok(())
}
@ -686,15 +691,16 @@ async fn db_lt_13(_db: &KeyValueDatabase, config: &Config) -> Result<()> {
#[cfg(feature = "sha256_media")]
async fn feat_sha256_media(db: &KeyValueDatabase, _config: &Config) -> Result<()> {
use std::path::PathBuf;
warn!("sha256_media feature flag is enabled, migrating legacy base64 file names to sha256 file names");
warn!("Mgrating legacy base64 file names to sha256 file names");
// Move old media files to new names
let mut changes = Vec::<(PathBuf, PathBuf)>::new();
for (key, _) in db.mediaid_file.iter() {
let old_path = services().globals.get_media_file(&key);
debug!("Old file path: {old_path:?}");
let path = services().globals.get_media_file_new(&key);
debug!("New file path: {path:?}");
changes.push((old_path, path));
let old = services().globals.get_media_file(&key);
let new = services().globals.get_media_file_new(&key);
changes.push((old, new));
debug!(?old, ?new, num = changes.len(), "change");
}
// move the file to the new location
for (old_path, path) in changes {