fix: delete attachments by used_for.id and add corresponding index

This commit is contained in:
Paul Makles
2024-09-29 15:05:56 +01:00
parent 3080ec1f5a
commit cbf9e81256
4 changed files with 30 additions and 2 deletions

View File

@@ -222,6 +222,12 @@ pub async fn create_database(db: &MongoDb) {
"hash": 1_i32
},
"name": "hash"
},
{
"key": {
"used_for.id": 1_i32
},
"name": "used_for_id"
}
]
},

View File

@@ -1187,6 +1187,28 @@ pub async fn run_migrations(db: &MongoDb, revision: i32) -> i32 {
}
}
if revision <= 30 {
info!("Running migration [revision 30 / 29-09-2024]: Add index for used_for.id to attachments.");
db.db()
.run_command(
doc! {
"createIndexes": "attachments",
"indexes": [
{
"key": {
"used_for.id": 1_i32
},
"name": "used_for_id"
}
]
},
None,
)
.await
.expect("Failed to create attachments index.");
}
// 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

@@ -261,7 +261,7 @@ impl AbstractChannels for MongoDb {
// Delete associated attachments
self.delete_many_attachments(doc! {
"object_id": &id
"used_for.id": &id
})
.await?;

View File

@@ -260,7 +260,7 @@ impl MongoDb {
// Update many attachments with parent id.
self.delete_many_attachments(doc! {
"object_id": &server_id
"used_for.id": &server_id
})
.await?;