diff --git a/crates/core/database/src/models/admin_migrations/ops/mongodb/init.rs b/crates/core/database/src/models/admin_migrations/ops/mongodb/init.rs index 82ff3ee9..ab25819b 100644 --- a/crates/core/database/src/models/admin_migrations/ops/mongodb/init.rs +++ b/crates/core/database/src/models/admin_migrations/ops/mongodb/init.rs @@ -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" } ] }, 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 525b1551..5a22cd6d 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 @@ -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. diff --git a/crates/core/database/src/models/channels/ops/mongodb.rs b/crates/core/database/src/models/channels/ops/mongodb.rs index 5f847dd7..51c6b66e 100644 --- a/crates/core/database/src/models/channels/ops/mongodb.rs +++ b/crates/core/database/src/models/channels/ops/mongodb.rs @@ -261,7 +261,7 @@ impl AbstractChannels for MongoDb { // Delete associated attachments self.delete_many_attachments(doc! { - "object_id": &id + "used_for.id": &id }) .await?; diff --git a/crates/core/database/src/models/servers/ops/mongodb.rs b/crates/core/database/src/models/servers/ops/mongodb.rs index 639cd76b..e6bee03f 100644 --- a/crates/core/database/src/models/servers/ops/mongodb.rs +++ b/crates/core/database/src/models/servers/ops/mongodb.rs @@ -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?;