Skip to content

fix: let existing databases pass the new unique indexes and role rename at boot - #2981

Open
hongwei1 wants to merge 17 commits into
OpenBankProject:developfrom
hongwei1:develop
Open

hongwei1 wants to merge 17 commits into
OpenBankProject:developfrom
hongwei1:develop

Conversation

@hongwei1

Copy link
Copy Markdown
Contributor

This lets an existing, long-lived database boot on the current develop without manual SQL. Before Schemifier creates the new unique indexes, the pre-schemify step removes duplicate account holders and entitlements, using the table and column names taken from the models, so it now finds mapperaccountholders.user_c. MigrationOfUserIdIndexes gives each resource user its own user id before resourceuser.userid_ becomes unique. MigrationOfConsentReferenceIdUuid fills consent reference ids without gen_random_uuid, so it no longer needs pgcrypto on Postgres. MigrationOfDynamicEntityRoleNames merges grants that end up with the same new role name instead of failing on the unique index. The commented api_disabled_versions example in sample.props.template is now written without quotes.

Each fix has a test that builds a database in the broken state and then runs the migration: DeduplicateBeforeUniqueIndexTest, ResourceUserUserIdUniqueIndexTest and DynamicEntityRoleRenameMigrationTest. With these changes, an existing SQL Server 2017 deployment booted and ran all migrations.

getDisabledVersions strips only the brackets and splits on commas, so a value copied with the quotes keeps
them on the first and last entries and those two versions stay enabled.
docs: write the api_disabled_versions example without quotes
The consent reference id migration now fills empty reference ids with the
same statement Boot's pre-Schemifier step uses. On Postgres that is an md5
of random input cast to a uuid, which every Postgres version has.
gen_random_uuid() exists only from Postgres 13 or with the pgcrypto
extension, and Postgres resolves it when it plans the statement, so the
migration failed on older versions even when no row needed a new id.
DeduplicateBeforeUniqueIndexTest rebuilds the old shape of the account
holder and entitlement tables: no unique index on the natural key, and
two rows with the same key. It then runs Boot's pre-Schemifier steps and
Schemifier, and checks that the row with the lowest id is kept and the
unique index is created.
…eated

The step that removes duplicate rows before Schemifier creates a unique
index now takes the table and column names from the models. The account
holder table is mapperaccountholders and its user column is user_c, so
the step found no table under the names written by hand and left the
duplicates in place. SQL Server then refused to create the unique index
on (user_c, accountbankpermalink, accountpermalink) and the boot stopped.
…eaks it

Two users share a user id and two have none. The migration must create the
index without removing a user, keep the shared user id on the lowest row id
and give the others their own.
The migration that creates the unique index on resourceuser.userid_ now
first gives a new user id to each resource user that has none and to each
one that shares its user id with a lower row id. No row is removed, and the
lowest row id keeps the shared value. Without this, a database holding such
rows stopped the boot: every database refuses a repeated value, and SQL
Server also refuses a second NULL. The changed rows are written to the
migration log.
A System twin and its plain Role, the System and BankLevel variant of a
Definition Role, and an old grant whose successor is already stored (as an
interrupted run leaves it) all map onto one (bank id, user id, role name).
The migration must keep one grant instead of breaking the unique index.
The rename migrations visit Entitlements by id and work out where each one
goes. When that key (bank id, user id, role name) is already held, by a
grant already under its new name or by an earlier row of the same run, the
row is deleted instead of renamed, so the unique index holds and the boot
goes on. The user keeps the grant. Keys are compared the way the database
compares them, so SQL Server and MySQL ignore case and trailing spaces.
Each merged row is listed in the migration log.
@sonarqubecloud

Copy link
Copy Markdown

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