Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/documentation/upgrading/topics/changes/changes-26_8_0.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
Breaking changes are identified as those that might require changes for existing users to their configurations or applications.
In minor or patch releases, {project_name} will only introduce breaking changes to fix bugs.

=== Empty database and an explicit migration strategy

@michalvavrik michalvavrik Sep 11, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

AFAICT when user configures update migration strategy, this initialize-empty has no effect. Regardless of true or false or unset. Should documentation mention it or it is already doc-ed or implied somehow?

Copy link
Copy Markdown
Contributor Author

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 update an empty database is always initialized, regardless of initialize-empty. That was already the case before this change; both the initializeEmpty branch and the UPDATE case of the switch end up in update().

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 for update. I'd add one sentence to this upgrade note and fix the help text:

initialize-empty only affects the manual and validate strategies. With update, an empty database is always initialized regardless of this setting.

Want me to push that here, or would you rather keep it out of this PR?

@michalvavrik michalvavrik Sep 16, 2026 •

Copy link
Copy Markdown
Member

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" 😁 Thanks

Copy link
Copy Markdown
Contributor Author

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!


An explicitly configured `migration-strategy` now wins over the implicit initialization of an empty database.
Previously the default `initialize-empty=true` initialized the schema in place even when `manual` or `validate`
was configured, so no export script was written and `validate` created the schema instead of failing.
Now, on an empty database, the `manual` strategy writes the export script and exits, and the `validate`
strategy fails startup. To restore the previous behavior, explicitly set the `initialize-empty` property of the
`connections-jpa` provider to `true`.
Comment thread
shawkins marked this conversation as resolved.
Resolved
The `initialize-empty` property only affects the `manual` and `validate` strategies. With the `update` strategy an empty
database is always initialized, regardless of this property.

=== Client secret no longer returned for view-only client permissions
// https://github.com/keycloak/keycloak/issues/49220

Expand Down
4 changes: 2 additions & 2 deletions docs/guides/server/db.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -727,10 +727,10 @@ To setup the JPA migrationStrategy (manual/update/validate) you should setup JPA
.Setting the `migration-strategy` for the `quarkus` provider of the `connections-jpa` SPI
<@kc.start parameters="--spi-connections-jpa--quarkus--migration-strategy=manual"/>

If you want to get a SQL file for DB initialization, too, you have to add this additional SPI initializeEmpty (true/false):
With the manual strategy, an empty database also results in a SQL file for the DB initialization. If you instead want an empty database to be initialized automatically while keeping manual migrations for upgrades, set the additional SPI option initializeEmpty to "true":

.Setting the `initialize-empty` for the `quarkus` provider of the `connections-jpa` SPI
<@kc.start parameters="--spi-connections-jpa--quarkus--initialize-empty=false"/>
<@kc.start parameters="--spi-connections-jpa--quarkus--initialize-empty=true"/>

In the same way the migrationExport to point to a specific file and location:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ public List<ProviderConfigProperty> getConfigMetadata() {
.property()
.name("initializeEmpty")
.type("boolean")
.helpText("Initialize database if empty. If set to false the database has to be manually initialized. If you want to manually initialize the database set migrationStrategy to manual which will create a file with SQL commands to initialize the database.")
.defaultValue(true)
.helpText("Initialize database if empty. Defaults to true, unless migrationStrategy is set to manual or validate. Only affects the manual and validate strategies: with the update strategy an empty database is always initialized. If set to false the database has to be manually initialized. If you want to manually initialize the database set migrationStrategy to manual which will create a file with SQL commands to initialize the database.")
.add()
.property()
.name("migrationStrategy")
Expand Down Expand Up @@ -272,7 +271,9 @@ private void createOperationalInfo(Connection connection) {

private boolean createOrUpdateSchema(String schema, String version, Connection connection, KeycloakSession session) {
MigrationStrategy strategy = getMigrationStrategy();
boolean initializeEmpty = config.getBoolean("initializeEmpty", true);
// An explicit migration strategy wins over the implicit initialization of an empty database, so the
// default of initializeEmpty depends on the strategy. An explicitly configured value always wins.
boolean initializeEmpty = config.getBoolean("initializeEmpty", strategy == MigrationStrategy.UPDATE);
File databaseUpdateFile = getDatabaseUpdateFile();

JpaUpdaterProvider updater = session.getProvider(JpaUpdaterProvider.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2026 Red Hat, Inc. and/or its affiliates

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ private void lazyInit(KeycloakSession session) {
}

MigrationStrategy migrationStrategy = getMigrationStrategy();
boolean initializeEmpty = config.getBoolean("initializeEmpty", true);
// An explicit migration strategy wins over the implicit initialization of an empty database, so the
// default of initializeEmpty depends on the strategy. An explicitly configured value always wins.
boolean initializeEmpty = config.getBoolean("initializeEmpty", migrationStrategy == MigrationStrategy.UPDATE);
File databaseUpdateFile = getDatabaseUpdateFile();

properties.put("hibernate.show_sql", config.getBoolean("showSql", false));
Expand Down
Loading