FINERACT-248: Prevent duplicate SMS campaign names#5439
FINERACT-248: Prevent duplicate SMS campaign names#5439nickus wants to merge 2 commits intoapache:developfrom
Conversation
bc4e52c to
a8a8b7a
Compare
| if (this.smsCampaignRepository.existsByCampaignName(campaignName)) { | ||
| throw new SmsCampaignNameAlreadyExistsException(campaignName); | ||
| } |
There was a problem hiding this comment.
I think the default isolation level we use is read_committed, meaning only committed things will be revealed by this check. It not guarantees anything.
2 transactions concurrently writing the same campaign name is possible.
If you wanna prevent it, ensure having a unique key on the DB level. This is good for a best-effort check but it's not bullet-proof,.
There was a problem hiding this comment.
Actually, there is already a unique key on the DB level. You might see further I catch DataIntegrityViolationException and throw the same exception.
see realCause.getMessage().contains("campaign_name_UNIQUE")
There was a problem hiding this comment.
@galovics could you take a fresh look on this please
There was a problem hiding this comment.
The mentioned unique constraint is not existing. It's mentioned on the entity, but in reality it does not exist. Please add this missing unique constraint.
There was a problem hiding this comment.
Good catch @adamsaghy, you're right — the @UniqueConstraint annotation on the entity was just Hibernate metadata with no actual DB constraint behind it (since DDL auto-generation is disabled). I've added a Liquibase migration to create the campaign_name_UNIQUE constraint on the sms_campaign table.
adamsaghy
left a comment
There was a problem hiding this comment.
Kindly review my concerns!
Add validation to check for duplicate campaign names before creating or updating SMS campaigns. This provides a user-friendly error message instead of relying on database constraint violations. Changes: - Add existsByCampaignName() and existsByCampaignNameAndIdNot() to repository - Validate campaign name uniqueness in create() method - Validate campaign name uniqueness in update() when name changes - Add SmsCampaignNameAlreadyExistsException for clear error messages - Add integration tests for duplicate name validation - Update CampaignsHelper with methods for testing specific campaign names
…unique constraint The @UniqueConstraint annotation on the SmsCampaign entity declared a campaign_name_UNIQUE constraint, but no corresponding database constraint existed since Hibernate DDL auto-generation is disabled. Add a Liquibase migration to create the actual unique constraint.
6ba5316 to
0730d48
Compare
Summary
Add validation to check for duplicate campaign names before creating or updating SMS campaigns. This provides a user-friendly error message instead of relying on database constraint violations.
Changes
existsByCampaignName()andexistsByCampaignNameAndIdNot()methods toSmsCampaignRepositorycreate()method before savingupdate()method when name changesSmsCampaignNameAlreadyExistsExceptionfor clear error messagesTest plan