feat: Create unique index for SenderId
and Recipient
Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
parent
9039ee66f0
commit
9c1ba97791
2 changed files with 24 additions and 9 deletions
|
@ -69,21 +69,23 @@ impl OutChatRequestsExt for DatabaseConnection {
|
||||||
requester: &UserModel,
|
requester: &UserModel,
|
||||||
recipient: &PublicKey,
|
recipient: &PublicKey,
|
||||||
) -> ServerResult<()> {
|
) -> ServerResult<()> {
|
||||||
if self
|
if let Err(err) = (OutChatRequestsActiveModel {
|
||||||
.have_chat_request_to(requester, recipient)
|
|
||||||
.await?
|
|
||||||
.is_some()
|
|
||||||
{
|
|
||||||
return Err(WsError::AlreadySendChatRequest.into());
|
|
||||||
}
|
|
||||||
OutChatRequestsActiveModel {
|
|
||||||
sender_id: Set(requester.id),
|
sender_id: Set(requester.id),
|
||||||
recipient: Set(recipient.to_string()),
|
recipient: Set(recipient.to_string()),
|
||||||
out_on: Set(Utc::now()),
|
out_on: Set(Utc::now()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
.save(self)
|
.save(self)
|
||||||
.await?;
|
.await)
|
||||||
|
{
|
||||||
|
match err.sql_err() {
|
||||||
|
Some(SqlErr::UniqueConstraintViolation(_)) => {
|
||||||
|
return Err(WsError::AlreadySendChatRequest.into());
|
||||||
|
}
|
||||||
|
_ => return Err(err.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,19 @@ 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(OutChatRequests::Table)
|
||||||
|
.col(OutChatRequests::SenderId)
|
||||||
|
.col(OutChatRequests::Recipient)
|
||||||
|
.unique()
|
||||||
|
.to_owned(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
manager
|
manager
|
||||||
.create_table(
|
.create_table(
|
||||||
Table::create()
|
Table::create()
|
||||||
|
|
Loading…
Reference in a new issue