Skip to content

Commit

Permalink
fix: must not use replace_one as it expects no change in _id
Browse files Browse the repository at this point in the history
  • Loading branch information
insertish committed May 16, 2024
1 parent bfcdb13 commit 19e72ba
Showing 1 changed file with 55 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::{
mongodb::{
bson::{doc, from_bson, from_document, to_document, Bson, DateTime, Document},
options::FindOptions,
}, Invite, MongoDb, DISCRIMINATOR_SEARCH_SPACE
},
Invite, MongoDb, DISCRIMINATOR_SEARCH_SPACE,
};
use bson::oid::ObjectId;
use futures::StreamExt;
Expand Down Expand Up @@ -1000,44 +1001,72 @@ pub async fn run_migrations(db: &MongoDb, revision: i32) -> i32 {
code: String,
creator: String,
channel: String,
}
},
}
);

#[derive(serde::Serialize, serde::Deserialize)]
struct Outer {
_id: ObjectId,
#[serde(flatten)]
invite: OldInvite
invite: OldInvite,
}

let mut invites = db.db()
let invites = db
.db()
.collection::<Outer>("channel_invites")
.find(doc! {
"type": { "$exists": false }
}, None)
.find(
doc! {
"type": { "$exists": false }
},
None,
)
.await
.expect("failed to find invites");
.expect("failed to find invites")
.filter_map(|s| async { s.ok() })
.collect::<Vec<Outer>>()
.await
.into_iter()
.map(|invite| match invite.invite {
OldInvite::Server {
code,
server,
creator,
channel,
} => Invite::Server {
code,
server,
creator,
channel,
},
OldInvite::Group {
code,
creator,
channel,
} => Invite::Group {
code,
creator,
channel,
},
})
.collect::<Vec<Invite>>();

while let Some(Ok(invite)) = invites.next().await {
let new_invite = match invite.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")
.insert_many(invites, None)
.await
.expect("failed to insert corrected invite");

db.db()
.collection("channel_invites")
.replace_one(doc! { "$or": [
{
"Server._id": new_invite.code()
},
{
"Group._id": new_invite.code()
}
]}, new_invite, None)
.await
.unwrap();
}
db.db()
.collection::<Outer>("channel_invites")
.delete_many(
doc! {
"type": { "$exists": false }
},
None,
)
.await
.expect("failed to find invites");
}

// Need to migrate fields on attachments, change `user_id`, `object_id`, etc to `parent`.
Expand Down

0 comments on commit 19e72ba

Please sign in to comment.