Skip to content

Conversation

annvelents
Copy link
Contributor

@annvelents annvelents commented Jun 12, 2025

Context

We tried to introduce the uniqueness index on billing_entity_sequential_id, but we populated this value with organization_sequential_id, and inherited some old duplicated organization_sequential_id (invoices created before april 2024)

So now we want to update the historical data to be able to introduce the uniqueness index

Description

Added a migration that:

  1. updates all invoices related to billing_entity with document_numbering "per_customer" with duplicated billing_entity_sequential_id to have billing_entity_sequential_id = nil
  2. goes through all billing entities with numbering per entity, finds invoices with duplicated billing_entity_sequential_id and updates it to nil, if in the database a value already exist with bigger billing_entity_sequential_id
  3. if latest billing_entity_sequential_id is duplicated - means something went completely wrong, we want to manually investigate it

Note that just updating all invoices except one with maximum billing_entity_sequential_id to have nil is easier, but this way we'd erase a lot of correct data + loose information in case we want to investigate something, so it was decided to proceed with selective update

@annvelents annvelents force-pushed the feat/multientities/uniqueness_fix_for_billing_entitiy_sequential_id branch from 6079048 to e900b65 Compare June 12, 2025 12:12
@annvelents annvelents force-pushed the feat/multientities/uniqueness_fix_for_billing_entitiy_sequential_id branch from e900b65 to 5aeeb61 Compare June 12, 2025 13:05
@annvelents annvelents marked this pull request as ready for review June 12, 2025 13:32
Copy link
Contributor

@ancorcruz ancorcruz left a comment

Choose a reason for hiding this comment

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

great work.

@annvelents annvelents force-pushed the feat/multientities/uniqueness_fix_for_billing_entitiy_sequential_id branch from 8bf66a9 to ccef0a6 Compare June 26, 2025 08:31
@annvelents annvelents merged commit ddd30b3 into main Jun 26, 2025
14 checks passed
@annvelents annvelents deleted the feat/multientities/uniqueness_fix_for_billing_entitiy_sequential_id branch June 26, 2025 08:41
diegocharles pushed a commit that referenced this pull request Jun 30, 2025
…voices (#3820)

## Context

We tried to introduce the uniqueness index on
billing_entity_sequential_id, but we populated this value with
organization_sequential_id, and inherited some old duplicated
organization_sequential_id (invoices created before april 2024)

So now we want to update the historical data to be able to introduce the
uniqueness index

## Description

Added a migration that:
1) updates all invoices related to billing_entity with
document_numbering "per_customer" with duplicated
billing_entity_sequential_id to have billing_entity_sequential_id = nil
2) goes through all billing entities with numbering per entity, finds
invoices with duplicated billing_entity_sequential_id and updates it to
nil, if in the database a value already exist with bigger
billing_entity_sequential_id
3) if latest billing_entity_sequential_id is duplicated - means
something went completely wrong, we want to manually investigate it

Note that just updating all invoices except one with maximum
billing_entity_sequential_id to have nil is easier, but this way we'd
erase a lot of correct data + loose information in case we want to
investigate something, so it was decided to proceed with selective
update
diegocharles pushed a commit that referenced this pull request Jul 11, 2025
…voices (#3820)

We tried to introduce the uniqueness index on
billing_entity_sequential_id, but we populated this value with
organization_sequential_id, and inherited some old duplicated
organization_sequential_id (invoices created before april 2024)

So now we want to update the historical data to be able to introduce the
uniqueness index

Added a migration that:
1) updates all invoices related to billing_entity with
document_numbering "per_customer" with duplicated
billing_entity_sequential_id to have billing_entity_sequential_id = nil
2) goes through all billing entities with numbering per entity, finds
invoices with duplicated billing_entity_sequential_id and updates it to
nil, if in the database a value already exist with bigger
billing_entity_sequential_id
3) if latest billing_entity_sequential_id is duplicated - means
something went completely wrong, we want to manually investigate it

Note that just updating all invoices except one with maximum
billing_entity_sequential_id to have nil is easier, but this way we'd
erase a lot of correct data + loose information in case we want to
investigate something, so it was decided to proceed with selective
update
diegocharles pushed a commit that referenced this pull request Jul 11, 2025
…voices (#3820)

We tried to introduce the uniqueness index on
billing_entity_sequential_id, but we populated this value with
organization_sequential_id, and inherited some old duplicated
organization_sequential_id (invoices created before april 2024)

So now we want to update the historical data to be able to introduce the
uniqueness index

Added a migration that:
1) updates all invoices related to billing_entity with
document_numbering "per_customer" with duplicated
billing_entity_sequential_id to have billing_entity_sequential_id = nil
2) goes through all billing entities with numbering per entity, finds
invoices with duplicated billing_entity_sequential_id and updates it to
nil, if in the database a value already exist with bigger
billing_entity_sequential_id
3) if latest billing_entity_sequential_id is duplicated - means
something went completely wrong, we want to manually investigate it

Note that just updating all invoices except one with maximum
billing_entity_sequential_id to have nil is easier, but this way we'd
erase a lot of correct data + loose information in case we want to
investigate something, so it was decided to proceed with selective
update
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