diff --git a/crates/core/database/src/models/admin_migrations/ops/mongodb/scripts.rs b/crates/core/database/src/models/admin_migrations/ops/mongodb/scripts.rs index ecc14672..a8eda5ac 100644 --- a/crates/core/database/src/models/admin_migrations/ops/mongodb/scripts.rs +++ b/crates/core/database/src/models/admin_migrations/ops/mongodb/scripts.rs @@ -4,8 +4,7 @@ use crate::{ mongodb::{ bson::{doc, from_bson, from_document, to_document, Bson, DateTime, Document}, options::FindOptions, - }, - MongoDb, DISCRIMINATOR_SEARCH_SPACE, + }, Invite, MongoDb, DISCRIMINATOR_SEARCH_SPACE }; use futures::StreamExt; use rand::seq::SliceRandom; @@ -19,7 +18,7 @@ struct MigrationInfo { revision: i32, } -pub const LATEST_REVISION: i32 = 26; +pub const LATEST_REVISION: i32 = 27; pub async fn migrate_database(db: &MongoDb) { let migrations = db.col::("migrations"); @@ -983,6 +982,56 @@ pub async fn run_migrations(db: &MongoDb, revision: i32) -> i32 { .expect("Failed to create ratelimit_events index."); } + if revision <= 26 { + info!("Running migration [revision 26 / 15-05-2024]: fix invites being incorrectly serialized with wrong enum tagging."); + + auto_derived!( + pub enum OldInvite { + Server { + #[serde(rename = "_id")] + code: String, + server: String, + creator: String, + channel: String, + }, + Group { + #[serde(rename = "_id")] + code: String, + creator: String, + channel: String, + } + } + ); + + let mut invites = db.db() + .collection::("channel_invites") + .find(doc! { + "type": { "$exists": false } + }, None) + .await + .expect("failed to find invites"); + + while let Some(Ok(invite)) = invites.next().await { + let new_invite = match invite { + OldInvite::Server { code, server, creator, channel } => Invite::Server { code, server, creator, channel }, + OldInvite::Group { code, creator, channel } => Invite::Group { code, creator, channel } + }; + + db.db() + .collection("channel_invites") + .replace_one(doc! { "$or": [ + { + "Server._id": new_invite.code() + }, + { + "Group._id": new_invite.code() + } + ]}, new_invite, None) + .await + .unwrap(); + } + } + // Need to migrate fields on attachments, change `user_id`, `object_id`, etc to `parent`. // Reminder to update LATEST_REVISION when adding new migrations. diff --git a/crates/core/database/src/models/channel_invites/model.rs b/crates/core/database/src/models/channel_invites/model.rs index aaaff55e..e3af921a 100644 --- a/crates/core/database/src/models/channel_invites/model.rs +++ b/crates/core/database/src/models/channel_invites/model.rs @@ -10,6 +10,7 @@ static ALPHABET: [char; 54] = [ auto_derived!( /// Invite + #[serde(tag = "type")] pub enum Invite { /// Invite to a specific server channel Server { diff --git a/crates/core/models/src/v0/channel_invites.rs b/crates/core/models/src/v0/channel_invites.rs index f0e889d5..475bf6fc 100644 --- a/crates/core/models/src/v0/channel_invites.rs +++ b/crates/core/models/src/v0/channel_invites.rs @@ -2,6 +2,7 @@ use super::{Channel, File, Server, User}; auto_derived!( /// Invite + #[serde(tag = "type")] pub enum Invite { /// Invite to a specific server channel Server {