Repository navigation
Honor an explicit migration strategy on an empty database #51933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Alnyli07
wants to merge
1
commit into
keycloak:main
Choose a base branch
from
Alnyli07:fix/empty-database-explicit-migration-strategy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...ration/src/test/java/org/keycloak/it/cli/dist/EmptyDatabaseMigrationStrategyDistTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * Copyright 2026 Red Hat, Inc. and/or its affiliates | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keep it, just fyi: it is not necessary anymore to add this |
||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.keycloak.it.cli.dist; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
|
|
||
| import org.keycloak.it.junit5.extension.CLIResult; | ||
| import org.keycloak.it.junit5.extension.DistributionTest; | ||
| import org.keycloak.it.junit5.extension.KeycloakRunner; | ||
| import org.keycloak.it.junit5.extension.RawDistOnly; | ||
| import org.keycloak.it.utils.RawDistRootPath; | ||
| import org.keycloak.it.utils.RawKeycloakDistribution; | ||
|
|
||
| import io.quarkus.deployment.util.FileUtil; | ||
| import org.junit.jupiter.api.Tag; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * An explicit migration strategy must win over the implicit initialization of an empty database. | ||
| * Without an explicit {@code initialize-empty}, {@code manual} has to produce the export script and | ||
| * {@code validate} has to fail startup instead of both being silently overridden by the default | ||
| * {@code initialize-empty=true} and initializing the schema in place. | ||
| */ | ||
| @DistributionTest | ||
| @RawDistOnly(reason = "Containers are immutable") | ||
| @Tag(DistributionTest.SLOW) | ||
| public class EmptyDatabaseMigrationStrategyDistTest { | ||
|
|
||
| @Test | ||
| void testManualStrategyAloneExportsScriptForEmptyDatabase(KeycloakRunner runner, RawDistRootPath rawDistRootPath) throws IOException { | ||
| forceEmptyDatabase(runner); | ||
|
|
||
| CLIResult result = runner.run("start-dev", | ||
| "--spi-connections-jpa-quarkus-migration-strategy=manual"); | ||
|
|
||
| result.assertMessage("Database not initialized, please initialize database with"); | ||
|
|
||
| File script = rawDistRootPath.getDistRootPath().resolve("bin").resolve("keycloak-database-update.sql").toFile(); | ||
| assertTrue(script.isFile(), "Export file must exist when migration-strategy=manual"); | ||
| } | ||
|
|
||
| @Test | ||
| void testValidateStrategyAloneFailsStartupForEmptyDatabase(KeycloakRunner runner) throws IOException { | ||
| forceEmptyDatabase(runner); | ||
|
|
||
| CLIResult result = runner.run("start-dev", | ||
| "--spi-connections-jpa-quarkus-migration-strategy=validate"); | ||
|
|
||
| result.assertMessage("Database not initialized, please enable database initialization"); | ||
| } | ||
|
|
||
| @Test | ||
| void testExplicitInitializeEmptyStillOverridesManualStrategy(KeycloakRunner runner) throws IOException { | ||
| forceEmptyDatabase(runner); | ||
|
|
||
| CLIResult result = runner.run("start-dev", | ||
| "--spi-connections-jpa-quarkus-migration-strategy=manual", | ||
| "--spi-connections-jpa-quarkus-initialize-empty=true"); | ||
|
|
||
| result.assertStartedDevMode(); | ||
| } | ||
|
|
||
| private static void forceEmptyDatabase(KeycloakRunner runner) throws IOException { | ||
| RawKeycloakDistribution rawDist = runner.getDistribution(RawKeycloakDistribution.class); | ||
| FileUtil.deleteDirectory(rawDist.getDistPath().resolve("data").resolve("h2").toAbsolutePath()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAICT when user configures
updatemigration strategy, thisinitialize-emptyhas no effect. Regardless oftrueorfalseor unset. Should documentation mention it or it is already doc-ed or implied somehow?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right @michalvavrik , with
updatean empty database is always initialized, regardless ofinitialize-empty. That was already the case before this change; both theinitializeEmptybranch and theUPDATEcase of the switch end up inupdate().It isn't documented anywhere though, and the help text of
initializeEmpty("If set to false the database has to be manually initialized") is actually misleading forupdate. I'd add one sentence to this upgrade note and fix the help text:Want me to push that here, or would you rather keep it out of this PR?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just thought we should document it somehow, I understand it is preexisting "fact". But since you are fixing the
initialize-empty, doc-ing this would be good. If you don't mind, do it. As I say these days "short concise note will do" 😁 ThanksThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, short and concise as requested :) One sentence in the upgrade note plus the help text, and rebased onto main. Thanks!