feat: Create unique index for SenderId and Recipient

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-07-17 22:59:47 +03:00
parent 9039ee66f0
commit 9c1ba97791
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
2 changed files with 24 additions and 9 deletions

View file

@ -69,21 +69,23 @@ impl OutChatRequestsExt for DatabaseConnection {
requester: &UserModel,
recipient: &PublicKey,
) -> ServerResult<()> {
if self
.have_chat_request_to(requester, recipient)
.await?
.is_some()
{
return Err(WsError::AlreadySendChatRequest.into());
}
OutChatRequestsActiveModel {
if let Err(err) = (OutChatRequestsActiveModel {
sender_id: Set(requester.id),
recipient: Set(recipient.to_string()),
out_on: Set(Utc::now()),
..Default::default()
}
.save(self)
.await?;
.await)
{
match err.sql_err() {
Some(SqlErr::UniqueConstraintViolation(_)) => {
return Err(WsError::AlreadySendChatRequest.into());
}
_ => return Err(err.into()),
}
}
Ok(())
}

View file

@ -29,6 +29,19 @@ pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_index(
Index::create()
.if_not_exists()
.name("sep_request")
.table(OutChatRequests::Table)
.col(OutChatRequests::SenderId)
.col(OutChatRequests::Recipient)
.unique()
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()