Skip to content

Mohith 240 sequence generators for organization tables#243

Open
MohithS04 wants to merge 12 commits into
mainfrom
mohith-240-Sequence-Generators-for-Organization-Tables
Open

Mohith 240 sequence generators for organization tables#243
MohithS04 wants to merge 12 commits into
mainfrom
mohith-240-Sequence-Generators-for-Organization-Tables

Conversation

@MohithS04

Copy link
Copy Markdown

Virginia (ddl_organizations_virginia.sql)

Gives the Virginia organizations table an automatic ID generator that fires whenever a new organization is inserted without an ID
Produces IDs shaped like ORG-00-001-XXX-XXX-XXX, where ORG marks an organization ID, 00 is a fixed namespace marker, 001 is the hardcoded Virginia region sub-block, and the trailing three groups come from a running counter
Works through three objects: a sequence handing out increasing numbers starting at one trillion, a function that slices the next number into three-digit groups and adds the prefix, and a trigger that runs the function automatically before every insert
Starts the sequence at one trillion specifically to prevent disaster-recovery collisions — if Virginia and Ireland both counted from low numbers, they could mint the same ID and corrupt the primary key on sync-back
Has no EU/non-EU split, because the ADR classifies organizations as global operational tables, not region-sensitive PII

Ireland (ddl_organizations_ireland.sql)

Does the same job as Virginia, producing ORG-00-002-XXX-XXX-XXX identifiers
Differs from Virginia in only two values: the sequence starts at two trillion instead of one, and the region sub-block is 002 instead of 001
Those two differences are the entire collision-avoidance mechanism — separate ranges mean the regions can never generate the same ID, even during failover
Has no EU/non-EU dual path, since an organization record holds no personal data needing in-region storage

The Two DDL Change-Scripts
Virginia and Ireland (alter_org_seq_virginia.sql, alter_org_seq_ireland.sql)

Migration versions of the create-scripts, for when the organizations table already exists and you only need to add or refresh the generator without recreating the table
Are idempotent — safe to run more than once with the same end result
Achieve that three ways: the sequence is created only if missing (so re-running never resets the counter or breaks ID continuity), the function is replaced in place, and the trigger is dropped if present before being recreated (so a second run won't error)
Each carries a commented-out rollback block that drops the trigger, function, and sequence in the correct dependency order
The Virginia change-script adds a note on how to convert to the alternative four-segment STD- format if the team chooses it later

MohithS04 added 6 commits July 1, 2026 17:57
Create sequence and function to generate organization IDs for Virginia.
Create a sequence and trigger for generating organization IDs in Ireland with a starting value of 2 trillion and a hardcoded region prefix.
This SQL script creates a sequence for organization IDs, defines a function to generate organization IDs based on that sequence, and sets up a trigger to automatically assign organization IDs during insertions.
This script creates a sequence and a trigger for generating organization IDs in the Ireland database. The sequence starts at 2 trillion and includes a specific region prefix.
@MohithS04 MohithS04 self-assigned this Jul 1, 2026
MohithS04 added 6 commits July 7, 2026 21:22
Updated the sequence and ID generation logic for Virginia organizations. Changed format and constraints for organization IDs.
Updated the test script for organizations in Virginia database to include additional tests for sequential ID generation and format checks, while removing redundant code.
Updated test script for Ireland organization ID generation, including new tests for sequence rollover, exhaustion, and collision checks.
Updated sequence and ID generation logic for Ireland's organizations. Adjusted sequence start, formatting, and trigger for organization ID generation.
This script modifies the organization ID sequence generator for Ireland, updating the sequence range and format. It drops existing triggers, functions, and sequences before recreating them with the new specifications.
This script migrates the organization sequence format from the old PR format to the new format, dropping and recreating the sequence and associated trigger and function to reset the counter. It ensures that the new sequence starts from 1 and follows the updated format for organization IDs.
@MohithS04

MohithS04 commented Jul 8, 2026

Copy link
Copy Markdown
Author

Summary

This PR implements regional organization ID sequence generators for Virginia and Ireland (#240). It introduces a band-from-sequence format ORG-XXX-XXX-XXX-XXXX, with each region assigned a non-overlapping sequence range so IDs stay globally unique. It supersedes the initial ORG-00-001-... approach based on review feedback.

Problem

The initial generate_org_id() approach had the following issues:

  • Redundant region encoding — the format ORG-00-001-XXX-XXX-XXX carried a constant 00 marker and a hardcoded 001/002 sub-block; the region was recorded twice and the 00 differentiated nothing.
  • Per-region function bodies — because the region label was hardcoded, Virginia and Ireland ran different functions instead of one shared body.
  • Math formatting — IDs were built with FLOOR/MOD arithmetic, which is harder to verify at review time than direct string slicing.
  • No width guard — a counter value longer than the format could silently produce a malformed ID.
  • IDs must stay unique across regions and survive DR copy-back without cross-region coordination.

Approach

New ID format: ORG-XXX-XXX-XXX-XXXX (13-digit counter, grouped 3-3-3-4; no separate band segment — the region shows up in the leading group)

Sequence range partitioning:

Region Band Sequence range Example first ID
Virginia 000 1 → 999,999,999,999 ORG-000-000-000-0001
Ireland (DR) 100 1,000,000,000,000 → 1,999,999,999,999 ORG-100-000-000-0000

Test plan

  • Deploy Virginia DDL on a test DB; run test_organizations_virginia.sql — all 4 checks return PASS.
  • Deploy Ireland DDL on a test DB; run test_organizations_ireland.sql — all 4 checks return PASS.
  • Migration verified: an old ORG-00-001-000-000-000 deployment, after alter_org_seq_virginia.sql, mints the new ORG-000-000-000-0001.
  • Trust-hook verified: a pre-supplied ID is preserved and the sequence is not consumed (next mint lands on …0051, not …0052).
  • Exhaustion wall verified: last legal value mints (ORG-099-999-999-9999 / ORG-199-999-999-9999), the next insert errors with reached maximum value of sequence.

Test cases

Each region has a self-contained suite (test_organizations_<region>.sql) with four assertions that print PASS/FAIL via RAISE NOTICE. All 8 assertions pass.

Virginia (ORG-000-... band):

    Screenshot 2026-07-07 at 9 44 23 PM
  • T1 — Sequential mint in the 000-band. Two rows inserted with no ID → ORG-000-000-000-0001, ORG-000-000-000-0002. Confirms the trigger mints from Virginia's sequence, the counter increments, and the region renders as the leading 000 group.
  • T2 — Trust-hook (copy-back safety). A row inserted with a pre-supplied ORG-100-000-000-0000 is kept unchanged, and the next auto-mint is ORG-000-000-000-0051 (not …0052). Proves a replicated / DR copy-back row is preserved and does not consume the local sequence.
  • T3 — Segment rollover. At counter values 1,000 / 10,000 / 1,000,000 → ORG-000-000-000-1000, ORG-000-000-001-0000, ORG-000-000-100-0000. Confirms the 3-3-3-4 SUBSTRING slicing carries correctly across group boundaries (last group is 4 wide, so it rolls at 10,000, not 1,000).
  • T4 — Exhaustion wall. The last legal value ORG-099-999-999-9999 mints, then the next insert errors with reached maximum value of sequence. Confirms MAXVALUE + NO CYCLE is a hard stop (input for exhaustion monitoring).

Ireland (ORG-100-... band, DR for Virginia):

    Screenshot 2026-07-07 at 9 45 27 PM
  • T1 — Sequential mint in the 100-band. ORG-100-000-000-0000, ORG-100-000-000-0001. Confirms Ireland mints from its 1-trillion range, so its IDs are disjoint from Virginia's by construction.
  • T2 — Trust-hook. A pre-supplied ORG-000-000-000-0007 (as if replicated from Virginia) is kept, and the next auto-mint is ORG-100-000-000-0051. Proves the DR sequence isn't consumed by replicated rows.
  • T3 — Segment rollover. ORG-100-000-000-1000, ORG-100-000-001-0000. Same slicing check, inside the 100-band.
  • T4 — Exhaustion wall. Last legal value ORG-199-999-999-9999 mints, next insert errors. Confirms Ireland's own MAXVALUE 1,999,999,999,999 hard stop.

Together these establish that each region mints unique, sequential IDs in its own disjoint band, pre-supplied IDs are preserved without consuming the counter, the formatting is correct at the tricky digit boundaries, and each sequence stops safely at its ceiling — so a Virginia 000-band ID and an Ireland 100-band ID coexist in one table with no collision.

Closes #240

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.

1 participant