Mohith 240 sequence generators for organization tables#243
Conversation
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.
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.
SummaryThis PR implements regional organization ID sequence generators for Virginia and Ireland (#240). It introduces a band-from-sequence format ProblemThe initial
ApproachNew ID format: Sequence range partitioning:
Test plan
Test casesEach region has a self-contained suite ( Virginia (
Ireland (
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 Closes #240 |


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