Repository navigation
Description
Before reporting an issue
- I have read and understood the above terms for submitting issues, and I understand that my issue may be closed without action if I do not follow them.
Area
storage
Describe the bug
With migration-strategy=manual on an empty database, the generated SQL script creates the whole schema and, with #51730, also the Liquibase DATABASECHANGELOG table. It still does not create DATABASECHANGELOGLOCK.
The omission is intentional and dates back to 3d47ab3 (KEYCLOAK-3698, 2016): the lock table is created by CustomLockService before the migration even runs, so on the database the script was generated from it always exists, and a CREATE TABLE in the script would fail with "already exists". That reasoning holds as long as the script is applied to the same database it was generated on.
It does not hold for the flow the manual strategy exists for: generate the script on a throwaway database, then have a DBA apply it to a really empty target database whose Keycloak runtime user has no DDL rights. On that target the lock table is missing, and the first startup fails in CustomLockService.init() with a CREATE TABLE ...DATABASECHANGELOGLOCK the runtime user is not allowed to run. The script is offered as a complete creation script for an empty database, but the resulting database cannot be started against without DDL rights.
This was reported before:
- Manual SQL migration does not include databasechangeloglock #10149 (2022) is the same scenario. @ahus1 proposed there to add the table to the generated script, @hmlnarik agreed ("it should be part of the SQL script"), the issue was marked
help wantedand the decision was deferred to Global lock interface #9388, which has since been closed. Manual SQL migration does not include databasechangeloglock #10149 then expired without a change. - Invalid migration export for empty database #32535 asked for the same ("The migration export should have CREATE TABLE statement for databasechangeloglock table"); Invalid migration export for empty database #37204 fixed only the duplicate
CREATEhalf of that report.
This is the follow-up requested in #51730 (review). The documentation added in #51730 states the limitation explicitly, which covers the documentation option from #10149; this issue is about the remaining half.
Version
main (999.0.0-SNAPSHOT)
Regression
No response
Expected behavior
A script generated for an empty database creates everything Keycloak needs to start against the target database, including DATABASECHANGELOGLOCK, so that a DBA can apply it to an empty database and a runtime user without DDL rights can start the server with migration-strategy=validate.
Applying the script to the database it was generated on must keep working: when the lock table already existed before the export run, no CREATE TABLE for it is emitted. This is the same pre-existence guard #51730 introduced for DATABASECHANGELOG, extended to the lock table. The lock table is created before the changelog is read, so the pre-existence check has to happen before the lock is acquired.
Actual behavior
grep -ci databasechangeloglock keycloak-database-update.sql returns 0, on main and with #51730. Applying the script to an empty database yields the full schema without the lock table. Starting Keycloak against it with a user that has no CREATE privilege on the schema fails at startup:
ERROR: permission denied for schema keycloak
[Failed SQL: CREATE TABLE keycloak.databasechangeloglock (...)]
How to Reproduce?
- Empty PostgreSQL database, user with DDL rights:
kc.sh start --spi-connections-jpa--quarkus--migration-strategy=manual --spi-connections-jpa--quarkus--initialize-empty=false ...and takekeycloak-database-update.sql. - Apply the script to a second, empty database as the owner.
- Grant a second user DML only (no
CREATEon the schema). - Start Keycloak against the second database with that user and
--spi-connections-jpa--quarkus--migration-strategy=validate. - Startup fails in
CustomLockServicecreatingDATABASECHANGELOGLOCK.
With a script that contains the lock table this sequence starts cleanly (verified on PostgreSQL 17: 100 tables after step 2, startup in step 4 with zero permission denied).
Anything else?
I have a change ready that records whether the lock table existed before the export run (next to the changelog table check from #51730) and emits CreateDatabaseChangeLogLockTableStatement under the same condition. It builds on #51730, so I will open the PR once that one is merged.
Metadata
Metadata
Assignees
Labels
Type
Fields
Projects
- StatusShow more project fieldsDone
Activity
Create the lock table in the manual migration export for an empty dat…
Create the lock table in the manual migration export for an empty dat…
Create the lock table in the manual migration export for an empty dat…
Create the lock table in the manual migration export for an empty dat…
vmuzikar commentedon Oct 26, 2026
@michalvavrik Since you reviewed the linked PR, I reckon this issue is valid, hence bringing it out from Triage.
Create the lock table in the manual migration export for an empty dat…
Create the lock table in the manual migration export for an empty dat…
Create the lock table in the manual migration export for an empty dat…