Skip to content

Make cross-service Auth role/user creation idempotent in migrations - #1573

Open
3keyroman wants to merge 3 commits into
mainfrom
fix/idempotent-cross-service-auth-migrations
Open

Make cross-service Auth role/user creation idempotent in migrations#1573
3keyroman wants to merge 3 commits into
mainfrom
fix/idempotent-cross-service-auth-migrations

Conversation

@3keyroman

Copy link
Copy Markdown
Contributor

Migrations create roles and users in the Auth Service via a cross-service call. If a migration was interrupted and then re-run, that call failed with ENTITY_NOT_UNIQUE ("already exists") and wedged Core startup. Creation now returns the existing entity instead, so migrations are safe to re-run.

Adds getOrCreateRole/getOrCreateUser in DatabaseAuthMigration and routes the affected migrations (V202209211100, V202303211718, V202404021100, plus the helper already used by V202411141900) through them. The three edited migrations are flagged isAltered with their checksum constants left unchanged, so Flyway validation against existing databases is unaffected.

Migrations create roles and users in the Auth Service via a cross-service call.
If a migration was interrupted and then re-run, that call failed with
ENTITY_NOT_UNIQUE ("already exists") and wedged Core startup. Creation now returns
the existing entity instead, so migrations are safe to re-run.

Adds getOrCreateRole/getOrCreateUser in DatabaseAuthMigration and routes the
affected migrations through them; the three edited migrations are flagged
isAltered with their checksum constants left unchanged.
Copilot AI review requested due to automatic review settings June 9, 2026 12:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR makes cross-service Auth role/user creation during Flyway Java migrations idempotent, preventing Core startup from failing when a migration is interrupted and re-run after the Auth Service has already created the role/user.

Changes:

  • Added DatabaseAuthMigration.getOrCreateRole / getOrCreateUser helpers and routed Auth creation through them.
  • Updated impacted migrations to use the new idempotent helpers for role/user creation.
  • Marked the altered Java migrations as isAltered=true in DatabaseMigration.JavaMigrationChecksums while keeping existing checksum constants unchanged to preserve Flyway validation behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/main/java/com/czertainly/core/util/DatabaseAuthMigration.java Adds idempotent get-or-create helpers and uses them from existing helper methods.
src/main/java/db/migration/V202209211100__Access_Control.java Routes user/role creation through idempotent helpers.
src/main/java/db/migration/V202303211718__Scep_Roles.java Routes SCEP role/user creation through idempotent helpers.
src/main/java/db/migration/V202404021100__CreateCmpUserAndPermissions.java Routes CMP role/user creation through idempotent helpers.
src/main/java/com/czertainly/core/util/DatabaseMigration.java Flags the modified Java migrations as altered (without changing stored checksum constants).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +116 to +123
public static RoleDetailDto getOrCreateRole(RoleManagementApiClient roleClient, RoleRequestDto request) {
for (RoleDto existing : roleClient.getRoles().getData()) {
if (existing.getName().equals(request.getName())) {
return roleClient.getRoleDetail(existing.getUuid());
}
}
return roleClient.createRole(request);
}
Comment on lines +129 to +135
public static UserDetailDto getOrCreateUser(UserManagementApiClient userClient, UserRequestDto request) {
for (UserDto existing : userClient.getUsers().getData()) {
if (existing.getUsername().equals(request.getUsername())) {
return userClient.getUserDetail(existing.getUuid());
}
}
return userClient.createUser(request);
@lubomirw lubomirw changed the title fix(migrations): make cross-service Auth role/user creation idempotent Make cross-service Auth role/user creation idempotent in migrations Jun 9, 2026
@lubomirw lubomirw added the bug Bug fix label Jun 9, 2026
@sonarqubecloud

sonarqubecloud Bot commented Jun 9, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants