From d2f4823ceba4b2be4910c683595e7cba67a5b5c2 Mon Sep 17 00:00:00 2001 From: Zomatree Date: Sun, 21 Jul 2024 22:12:55 +0100 Subject: [PATCH] chore: Add index and migration --- .../admin_migrations/ops/mongodb/init.rs | 9 ++++++- .../admin_migrations/ops/mongodb/scripts.rs | 25 ++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) 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 2bfd7ff1..7e6f3d7f 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 @@ -146,7 +146,14 @@ pub async fn create_database(db: &MongoDb) { "author": 1_i32 }, "name": "author" - } + }, + { + "key": { + "channel": 1_i32, + "pinned": 1_i32 + }, + "name": "channel_pinned_compound" + }, ] }, None, 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 acde3021..9898be7e 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 @@ -20,7 +20,7 @@ struct MigrationInfo { revision: i32, } -pub const LATEST_REVISION: i32 = 27; +pub const LATEST_REVISION: i32 = 28; pub async fn migrate_database(db: &MongoDb) { let migrations = db.col::("migrations"); @@ -1071,6 +1071,29 @@ pub async fn run_migrations(db: &MongoDb, revision: i32) -> i32 { } } + if revision <= 27 { + info!("Running migration [revision 27 / 21-07-2024]: create message pinned index."); + + db.db() + .run_command( + doc! { + "createIndexes": "messages", + "indexes": [ + { + "key": { + "channel": 1_i32, + "pinned": 1_i32 + }, + "name": "channel_pinned_compound" + } + ] + }, + None, + ) + .await + .expect("Failed to create message index."); + } + // Need to migrate fields on attachments, change `user_id`, `object_id`, etc to `parent`. // Reminder to update LATEST_REVISION when adding new migrations.