Skip to content

Comments

FINERACT-248: Prevent duplicate SMS campaign names#5439

Open
nickus wants to merge 2 commits intoapache:developfrom
nickus:FINERACT-248-prevent-duplicate-sms-campaign-names
Open

FINERACT-248: Prevent duplicate SMS campaign names#5439
nickus wants to merge 2 commits intoapache:developfrom
nickus:FINERACT-248-prevent-duplicate-sms-campaign-names

Conversation

@nickus
Copy link

@nickus nickus commented Feb 3, 2026

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

  • Add existsByCampaignName() and existsByCampaignNameAndIdNot() methods to SmsCampaignRepository
  • Validate campaign name uniqueness in create() method before saving
  • Validate campaign name uniqueness in update() method when name changes
  • Add SmsCampaignNameAlreadyExistsException for clear error messages

Test plan

  • Integration tests added for:
    • Creating a campaign with a duplicate name should fail with appropriate error
    • Creating campaigns with unique names should succeed

@nickus nickus force-pushed the FINERACT-248-prevent-duplicate-sms-campaign-names branch from bc4e52c to a8a8b7a Compare February 3, 2026 22:10
Comment on lines +113 to +115
if (this.smsCampaignRepository.existsByCampaignName(campaignName)) {
throw new SmsCampaignNameAlreadyExistsException(campaignName);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@galovics could you take a fresh look on this please

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

@adamsaghy adamsaghy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@nickus nickus force-pushed the FINERACT-248-prevent-duplicate-sms-campaign-names branch from 6ba5316 to 0730d48 Compare February 21, 2026 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants