feat: Create unique index for RecipientId
and Sender
Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
parent
02a0ac09cc
commit
9039ee66f0
2 changed files with 29 additions and 17 deletions
|
@ -19,7 +19,7 @@
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use oxidetalis_core::types::PublicKey;
|
use oxidetalis_core::types::PublicKey;
|
||||||
use oxidetalis_entities::prelude::*;
|
use oxidetalis_entities::prelude::*;
|
||||||
use sea_orm::DatabaseConnection;
|
use sea_orm::{sea_query::OnConflict, DatabaseConnection};
|
||||||
|
|
||||||
use crate::errors::ServerResult;
|
use crate::errors::ServerResult;
|
||||||
|
|
||||||
|
@ -40,22 +40,22 @@ impl InChatRequestsExt for DatabaseConnection {
|
||||||
sender: &PublicKey,
|
sender: &PublicKey,
|
||||||
recipient: &UserModel,
|
recipient: &UserModel,
|
||||||
) -> ServerResult<()> {
|
) -> ServerResult<()> {
|
||||||
if recipient
|
InChatRequestsEntity::insert(InChatRequestsActiveModel {
|
||||||
.find_related(InChatRequestsEntity)
|
|
||||||
.filter(InChatRequestsColumn::Sender.eq(sender.to_string()))
|
|
||||||
.one(self)
|
|
||||||
.await?
|
|
||||||
.is_none()
|
|
||||||
{
|
|
||||||
InChatRequestsActiveModel {
|
|
||||||
recipient_id: Set(recipient.id),
|
recipient_id: Set(recipient.id),
|
||||||
sender: Set(sender.to_string()),
|
sender: Set(sender.to_string()),
|
||||||
in_on: Set(Utc::now()),
|
in_on: Set(Utc::now()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
})
|
||||||
.save(self)
|
.on_conflict(
|
||||||
|
OnConflict::columns([
|
||||||
|
InChatRequestsColumn::RecipientId,
|
||||||
|
InChatRequestsColumn::Sender,
|
||||||
|
])
|
||||||
|
.do_nothing()
|
||||||
|
.to_owned(),
|
||||||
|
)
|
||||||
|
.exec(self)
|
||||||
.await?;
|
.await?;
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,18 @@ pub struct Migration;
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
impl MigrationTrait for Migration {
|
impl MigrationTrait for Migration {
|
||||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||||
|
manager
|
||||||
|
.create_index(
|
||||||
|
Index::create()
|
||||||
|
.if_not_exists()
|
||||||
|
.name("sep_request")
|
||||||
|
.table(InChatRequests::Table)
|
||||||
|
.col(InChatRequests::RecipientId)
|
||||||
|
.col(InChatRequests::Sender)
|
||||||
|
.unique()
|
||||||
|
.to_owned(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
manager
|
manager
|
||||||
.create_table(
|
.create_table(
|
||||||
Table::create()
|
Table::create()
|
||||||
|
|
Loading…
Reference in a new issue