Skip to content

FINERACT-248: Prevent duplicate SMS campaign names - #5439

Merged
adamsaghy merged 1 commit into
apache:developfrom
nickus:FINERACT-248-prevent-duplicate-sms-campaign-names
Feb 26, 2026
Merged

FINERACT-248: Prevent duplicate SMS campaign names#5439
adamsaghy merged 1 commit into
apache:developfrom
nickus:FINERACT-248-prevent-duplicate-sms-campaign-names

Conversation

@nickus

@nickus nickus commented Feb 3, 2026

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
Contributor 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.

@adamsaghy adamsaghy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Kindly review my concerns!

@nickus
nickus force-pushed the FINERACT-248-prevent-duplicate-sms-campaign-names branch from 6ba5316 to 0730d48 Compare February 21, 2026 13:35
@adamsaghy

Copy link
Copy Markdown
Contributor

@nickus Please rebase this PR.

@nickus
nickus force-pushed the FINERACT-248-prevent-duplicate-sms-campaign-names branch 3 times, most recently from b0cbd6f to 70ed936 Compare February 25, 2026 22:56
Add validation to check for duplicate campaign names before creating or
updating SMS campaigns. Also add a Liquibase migration to create the
campaign_name_UNIQUE constraint on sms_campaign table, since the
@UniqueConstraint annotation on the entity was just Hibernate metadata
with no actual DB constraint behind it.
@nickus
nickus force-pushed the FINERACT-248-prevent-duplicate-sms-campaign-names branch from 70ed936 to 0d74f1c Compare February 26, 2026 11:33
@nickus

nickus commented Feb 26, 2026

Copy link
Copy Markdown
Contributor Author

So many flaky tests...

@nickus

nickus commented Feb 26, 2026

Copy link
Copy Markdown
Contributor Author

@adamsaghy finally green, could you please take a new look and merge if everything is okay?

@adamsaghy
adamsaghy merged commit c0f22f0 into apache:develop Feb 26, 2026
38 checks passed
@nickus
nickus deleted the FINERACT-248-prevent-duplicate-sms-campaign-names branch February 26, 2026 16:53
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