Skip to content

Manual migration export for an empty database does not create the DATABASECHANGELOGLOCK table #53088

Closed

Description

@Alnyli07

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:

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?

  1. 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 take keycloak-database-update.sql.
  2. Apply the script to a second, empty database as the owner.
  3. Grant a second user DML only (no CREATE on the schema).
  4. Start Keycloak against the second database with that user and --spi-connections-jpa--quarkus--migration-strategy=validate.
  5. Startup fails in CustomLockService creating DATABASECHANGELOGLOCK.

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.

Activity

  1. added
    kind/enhancement
    on Sep 23, 2026
  2. changed the issue type fromtoon Sep 23, 2026
  3. added
    kind/bug
    and removed
    kind/enhancement
    on Sep 23, 2026
  4. added
    area/storage
    area/dist/quarkus
    on Sep 23, 2026
  5. added and removed
    area/dist/quarkus
    on Sep 23, 2026
  6. added 3 commits that reference this issue on Sep 23, 2026
    d2e7f46
    20e3617
    59a6a09
  7. added a commit that references this issue on Oct 21, 2026
    37bda78
  8. vmuzikar commented on Oct 26, 2026

    @vmuzikar
    Contributor

    @michalvavrik Since you reviewed the linked PR, I reckon this issue is valid, hence bringing it out from Triage.

  9. added a commit that references this issue on Oct 29, 2026
    8472f78
  10. added a commit that references this issue on Oct 30, 2026
    2d7607c
  11. added a commit that references this issue on Nov 3, 2026
    7831bc2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/storagekind/bugpriority/importantrelease/27.0.0team/cloud-native

    Type

    Fields

    No fields configured for bug.

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions