mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-18 00:01:24 +01:00
factor out helper function for db migrations
This commit is contained in:
parent
e2318cad8a
commit
059dfe54e3
1 changed files with 54 additions and 77 deletions
131
src/database.rs
131
src/database.rs
|
|
@ -570,7 +570,7 @@ impl KeyValueDatabase {
|
||||||
|
|
||||||
if services().users.count()? > 0 {
|
if services().users.count()? > 0 {
|
||||||
// MIGRATIONS
|
// MIGRATIONS
|
||||||
if services().globals.database_version()? < 1 {
|
migration(1, || {
|
||||||
for (roomserverid, _) in db.roomserverids.iter() {
|
for (roomserverid, _) in db.roomserverids.iter() {
|
||||||
let mut parts = roomserverid.split(|&b| b == 0xFF);
|
let mut parts = roomserverid.split(|&b| b == 0xFF);
|
||||||
let room_id =
|
let room_id =
|
||||||
|
|
@ -585,13 +585,10 @@ impl KeyValueDatabase {
|
||||||
|
|
||||||
db.serverroomids.insert(&serverroomid, &[])?;
|
db.serverroomids.insert(&serverroomid, &[])?;
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
|
||||||
services().globals.bump_database_version(1)?;
|
migration(2, || {
|
||||||
|
|
||||||
warn!("Migration: 0 -> 1 finished");
|
|
||||||
}
|
|
||||||
|
|
||||||
if services().globals.database_version()? < 2 {
|
|
||||||
// We accidentally inserted hashed versions of "" into the db
|
// We accidentally inserted hashed versions of "" into the db
|
||||||
// instead of just ""
|
// instead of just ""
|
||||||
for (userid, password) in db.userid_password.iter() {
|
for (userid, password) in db.userid_password.iter() {
|
||||||
|
|
@ -606,13 +603,10 @@ impl KeyValueDatabase {
|
||||||
db.userid_password.insert(&userid, b"")?;
|
db.userid_password.insert(&userid, b"")?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
|
||||||
services().globals.bump_database_version(2)?;
|
migration(3, || {
|
||||||
|
|
||||||
warn!("Migration: 1 -> 2 finished");
|
|
||||||
}
|
|
||||||
|
|
||||||
if services().globals.database_version()? < 3 {
|
|
||||||
// Move media to filesystem
|
// Move media to filesystem
|
||||||
for (key, content) in db.mediaid_file.iter() {
|
for (key, content) in db.mediaid_file.iter() {
|
||||||
let key = MediaFileKey::new(key);
|
let key = MediaFileKey::new(key);
|
||||||
|
|
@ -625,13 +619,10 @@ impl KeyValueDatabase {
|
||||||
file.write_all(&content)?;
|
file.write_all(&content)?;
|
||||||
db.mediaid_file.insert(key.as_bytes(), &[])?;
|
db.mediaid_file.insert(key.as_bytes(), &[])?;
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
|
||||||
services().globals.bump_database_version(3)?;
|
migration(4, || {
|
||||||
|
|
||||||
warn!("Migration: 2 -> 3 finished");
|
|
||||||
}
|
|
||||||
|
|
||||||
if services().globals.database_version()? < 4 {
|
|
||||||
// Add federated users to services() as deactivated
|
// Add federated users to services() as deactivated
|
||||||
for our_user in services().users.iter() {
|
for our_user in services().users.iter() {
|
||||||
let our_user = our_user?;
|
let our_user = our_user?;
|
||||||
|
|
@ -654,13 +645,10 @@ impl KeyValueDatabase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
|
||||||
services().globals.bump_database_version(4)?;
|
migration(5, || {
|
||||||
|
|
||||||
warn!("Migration: 3 -> 4 finished");
|
|
||||||
}
|
|
||||||
|
|
||||||
if services().globals.database_version()? < 5 {
|
|
||||||
// Upgrade user data store
|
// Upgrade user data store
|
||||||
for (roomuserdataid, _) in db.roomuserdataid_accountdata.iter()
|
for (roomuserdataid, _) in db.roomuserdataid_accountdata.iter()
|
||||||
{
|
{
|
||||||
|
|
@ -679,13 +667,10 @@ impl KeyValueDatabase {
|
||||||
db.roomusertype_roomuserdataid
|
db.roomusertype_roomuserdataid
|
||||||
.insert(&key, &roomuserdataid)?;
|
.insert(&key, &roomuserdataid)?;
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
|
||||||
services().globals.bump_database_version(5)?;
|
migration(6, || {
|
||||||
|
|
||||||
warn!("Migration: 4 -> 5 finished");
|
|
||||||
}
|
|
||||||
|
|
||||||
if services().globals.database_version()? < 6 {
|
|
||||||
// Set room member count
|
// Set room member count
|
||||||
for (roomid, _) in db.roomid_shortstatehash.iter() {
|
for (roomid, _) in db.roomid_shortstatehash.iter() {
|
||||||
let string = utils::string_from_bytes(&roomid).unwrap();
|
let string = utils::string_from_bytes(&roomid).unwrap();
|
||||||
|
|
@ -695,13 +680,10 @@ impl KeyValueDatabase {
|
||||||
.state_cache
|
.state_cache
|
||||||
.update_joined_count(room_id)?;
|
.update_joined_count(room_id)?;
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
|
||||||
services().globals.bump_database_version(6)?;
|
migration(7, || {
|
||||||
|
|
||||||
warn!("Migration: 5 -> 6 finished");
|
|
||||||
}
|
|
||||||
|
|
||||||
if services().globals.database_version()? < 7 {
|
|
||||||
// Upgrade state store
|
// Upgrade state store
|
||||||
let mut last_roomstates: HashMap<OwnedRoomId, ShortStateHash> =
|
let mut last_roomstates: HashMap<OwnedRoomId, ShortStateHash> =
|
||||||
HashMap::new();
|
HashMap::new();
|
||||||
|
|
@ -833,13 +815,10 @@ impl KeyValueDatabase {
|
||||||
&mut last_roomstates,
|
&mut last_roomstates,
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
|
||||||
services().globals.bump_database_version(7)?;
|
migration(8, || {
|
||||||
|
|
||||||
warn!("Migration: 6 -> 7 finished");
|
|
||||||
}
|
|
||||||
|
|
||||||
if services().globals.database_version()? < 8 {
|
|
||||||
// Generate short room ids for all rooms
|
// Generate short room ids for all rooms
|
||||||
for (room_id, _) in db.roomid_shortstatehash.iter() {
|
for (room_id, _) in db.roomid_shortstatehash.iter() {
|
||||||
let shortroomid =
|
let shortroomid =
|
||||||
|
|
@ -892,13 +871,10 @@ impl KeyValueDatabase {
|
||||||
});
|
});
|
||||||
|
|
||||||
db.eventid_pduid.insert_batch(&mut batch2)?;
|
db.eventid_pduid.insert_batch(&mut batch2)?;
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
|
||||||
services().globals.bump_database_version(8)?;
|
migration(9, || {
|
||||||
|
|
||||||
warn!("Migration: 7 -> 8 finished");
|
|
||||||
}
|
|
||||||
|
|
||||||
if services().globals.database_version()? < 9 {
|
|
||||||
// Update tokenids db layout
|
// Update tokenids db layout
|
||||||
let mut iter = db
|
let mut iter = db
|
||||||
.tokenids
|
.tokenids
|
||||||
|
|
@ -942,13 +918,10 @@ impl KeyValueDatabase {
|
||||||
for key in batch2 {
|
for key in batch2 {
|
||||||
db.tokenids.remove(&key)?;
|
db.tokenids.remove(&key)?;
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
|
||||||
services().globals.bump_database_version(9)?;
|
migration(10, || {
|
||||||
|
|
||||||
warn!("Migration: 8 -> 9 finished");
|
|
||||||
}
|
|
||||||
|
|
||||||
if services().globals.database_version()? < 10 {
|
|
||||||
// Add other direction for shortstatekeys
|
// Add other direction for shortstatekeys
|
||||||
for (statekey, shortstatekey) in
|
for (statekey, shortstatekey) in
|
||||||
db.statekey_shortstatekey.iter()
|
db.statekey_shortstatekey.iter()
|
||||||
|
|
@ -962,20 +935,15 @@ impl KeyValueDatabase {
|
||||||
for user_id in services().users.iter().filter_map(Result::ok) {
|
for user_id in services().users.iter().filter_map(Result::ok) {
|
||||||
services().users.mark_device_key_update(&user_id)?;
|
services().users.mark_device_key_update(&user_id)?;
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
|
||||||
services().globals.bump_database_version(10)?;
|
migration(11, || {
|
||||||
|
|
||||||
warn!("Migration: 9 -> 10 finished");
|
|
||||||
}
|
|
||||||
|
|
||||||
if services().globals.database_version()? < 11 {
|
|
||||||
db.db.open_tree("userdevicesessionid_uiaarequest")?.clear()?;
|
db.db.open_tree("userdevicesessionid_uiaarequest")?.clear()?;
|
||||||
services().globals.bump_database_version(11)?;
|
Ok(())
|
||||||
|
})?;
|
||||||
|
|
||||||
warn!("Migration: 10 -> 11 finished");
|
migration(12, || {
|
||||||
}
|
|
||||||
|
|
||||||
if services().globals.database_version()? < 12 {
|
|
||||||
for username in services().users.list_local_users()? {
|
for username in services().users.list_local_users()? {
|
||||||
let user = match UserId::parse_with_server_name(
|
let user = match UserId::parse_with_server_name(
|
||||||
username.clone(),
|
username.clone(),
|
||||||
|
|
@ -1072,15 +1040,12 @@ impl KeyValueDatabase {
|
||||||
.expect("to json value always works"),
|
.expect("to json value always works"),
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
services().globals.bump_database_version(12)?;
|
})?;
|
||||||
|
|
||||||
warn!("Migration: 11 -> 12 finished");
|
|
||||||
}
|
|
||||||
|
|
||||||
// This migration can be reused as-is anytime the server-default
|
// This migration can be reused as-is anytime the server-default
|
||||||
// rules are updated.
|
// rules are updated.
|
||||||
if services().globals.database_version()? < 13 {
|
migration(13, || {
|
||||||
for username in services().users.list_local_users()? {
|
for username in services().users.list_local_users()? {
|
||||||
let user = match UserId::parse_with_server_name(
|
let user = match UserId::parse_with_server_name(
|
||||||
username.clone(),
|
username.clone(),
|
||||||
|
|
@ -1131,11 +1096,8 @@ impl KeyValueDatabase {
|
||||||
.expect("to json value always works"),
|
.expect("to json value always works"),
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
services().globals.bump_database_version(13)?;
|
})?;
|
||||||
|
|
||||||
warn!("Migration: 12 -> 13 finished");
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
services().globals.database_version().unwrap(),
|
services().globals.database_version().unwrap(),
|
||||||
|
|
@ -1277,3 +1239,18 @@ fn set_emergency_access() -> Result<bool> {
|
||||||
|
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// If the current version is older than `new_version`, execute a migration
|
||||||
|
/// function.
|
||||||
|
fn migration<F>(new_version: u64, migration: F) -> Result<(), Error>
|
||||||
|
where
|
||||||
|
F: FnOnce() -> Result<(), Error>,
|
||||||
|
{
|
||||||
|
let current_version = services().globals.database_version()?;
|
||||||
|
if current_version < new_version {
|
||||||
|
migration()?;
|
||||||
|
services().globals.bump_database_version(new_version)?;
|
||||||
|
warn!("Migration: {current_version} -> {new_version} finished");
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue