From 41ac1d7ceaa70488b534334179663aed9e9d07f7 Mon Sep 17 00:00:00 2001 From: Akulij Date: Sat, 26 Apr 2025 00:21:41 +0300 Subject: [PATCH] create db migration function will contain indexes --- src/db/mod.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/db/mod.rs b/src/db/mod.rs index c0aa15d..a02057d 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -3,12 +3,13 @@ use chrono::{DateTime, Utc}; use enum_stringify::EnumStringify; use futures::stream::{StreamExt, TryStreamExt}; -use mongodb::Database; +use mongodb::options::IndexOptions; use mongodb::{ bson::doc, options::{ClientOptions, ResolverConfig}, Client, }; +use mongodb::{Database, IndexModel}; use serde::{Deserialize, Serialize}; #[derive(EnumStringify)] @@ -80,6 +81,19 @@ impl DB { DB { client } } + + pub async fn migrate(&mut self) -> Result<(), mongodb::error::Error> { + let events = self.get_database().await.collection::("events"); + events + .create_index( + IndexModel::builder() + .keys(doc! {"time": 1}) + .options(IndexOptions::builder().unique(true).build()) + .build(), + ) + .await?; + Ok(()) + } } #[async_trait]