Write database migration for last_message_id. #67

This commit is contained in:
Paul
2021-09-14 15:07:41 +01:00
parent 8aa92564fc
commit 9b6893ca48
4 changed files with 59 additions and 4 deletions

View File

@@ -9,7 +9,6 @@ use futures::StreamExt;
use mongodb::options::UpdateOptions;
use mongodb::{
bson::{doc, to_bson, DateTime, Document},
options::FindOptions,
};
use rauth::entities::{Model, Session};
use rocket::serde::json::Value;

View File

@@ -11,7 +11,7 @@ struct MigrationInfo {
revision: i32,
}
pub const LATEST_REVISION: i32 = 9;
pub const LATEST_REVISION: i32 = 10;
pub async fn migrate_database() {
let migrations = get_collection("migrations");
@@ -286,6 +286,62 @@ pub async fn run_migrations(revision: i32) -> i32 {
.unwrap();
}
if revision <= 9 {
info!("Running migration [revision 9 / 2021-09-14]: Switch from last_message to last_message_id.");
let mut cursor = get_collection("channels")
.find(doc! { }, None)
.await
.unwrap();
while let Some(doc) = cursor.next().await {
if let Ok(channel) = doc {
let channel_id = channel.get_str("_id").unwrap();
if let Some(last_message) = channel.get("last_message") {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Obj {
#[serde(rename = "_id")]
id: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum LastMessage {
Obj(Obj),
Id(String)
}
let lm = from_bson::<LastMessage>(last_message.clone()).unwrap();
let id = match lm {
LastMessage::Obj(Obj { id }) => id,
LastMessage::Id(id) => id
};
info!("Converting session {} to new format.", &channel_id);
get_collection("channels")
.update_one(
doc! {
"_id": channel_id
},
doc! {
"$set": {
"last_message_id": id
},
"$unset": {
"last_message": 1,
}
},
None
)
.await
.unwrap();
} else {
info!("{} has no last_message.", &channel_id);
}
}
}
}
// Need to migrate fields on attachments, change `user_id`, `object_id`, etc to `parent`.
// Reminder to update LATEST_REVISION when adding new migrations.

View File

@@ -1 +1 @@
pub const VERSION: &str = "0.5.3-alpha.0-patch.1";
pub const VERSION: &str = "0.5.3-alpha.1";