Skip to content

Manual SQL migration does not include databasechangeloglock #10149

Closed as not planned

Description

@JaimySmets

Describe the bug

When preparing a manual schema migration as described in the docs, the generated script does not include the databasechangeloglock table.

It includes the databasechangelog, but when starting Keycloak afterwards with initializeEmpty=false and migrationStrategy=manual or migrationStrategy=validate, it tries to create the databasechangeloglock table.

As I run Keycloak with a user without DDL privileges (script provided to DBA), it fails to boot. There also seems to be no option to tell it to ignore Liquibase in general. Only update/manual/validate is available, which all require the table.

09:41:47,382 WARN  [org.keycloak.connections.jpa.updater.liquibase.lock.CustomLockService] (ServerService Thread Pool -- 64) Failed to create lock table. Maybe other transaction created in the meantime. Retrying...
09:41:47,847 WARN  [org.keycloak.connections.jpa.updater.liquibase.lock.CustomLockService] (ServerService Thread Pool -- 64) Failed to create lock table. Maybe other transaction created in the meantime. Retrying...
09:41:51,473 WARN  [org.keycloak.connections.jpa.updater.liquibase.lock.CustomLockService] (ServerService Thread Pool -- 64) Failed to create lock table. Maybe other transaction created in the meantime. Retrying...
09:41:51,477 FATAL [org.keycloak.services] (ServerService Thread Pool -- 64) Error during startup: org.keycloak.connections.jpa.updater.liquibase.lock.LockRetryException: liquibase.exception.DatabaseException: ERROR: permission denied for schema keycloak
  Position: 14 [Failed SQL: CREATE TABLE keycloak.databasechangeloglock (ID INT NOT NULL, LOCKED BOOLEAN NOT NULL, LOCKGRANTED TIMESTAMP WITHOUT TIME ZONE, LOCKEDBY VARCHAR(255), CONSTRAINT PK_DATABASECHANGELOGLOCK PRIMARY KEY (ID))]

Version

16.1.1

Expected behavior

Either the databasechangeloglock is included in the generated script during manual migration, or Keycloak doesn't require this table during validation. Or an option to ignore Liquibase all around.

Actual behavior

No response

How to Reproduce?

On an empty database (Postgres 13 in my case), run Keycloak with the follow migration settings:

<provider name="default" enabled="true">
    <properties>
        <property name="dataSource" value="java:jboss/datasources/KeycloakDS"/>
        <property name="initializeEmpty" value="false"/>
        <property name="migrationStrategy" value="manual"/>
        <property name="migrationExport" value="${jboss.server.config.dir}/install-schema.sql"/>
        <property name="schema" value="keycloak"/>
    </properties>
</provider>

Inspect the generated SQL file and see it has no databasechangeloglock table. It did create the databasechangeloglock in the DB, even though it was set to manual.

On an empty database, execute the sql file and revoke DDL access of the Keycloak user. Then start Keycloak again with the settings (or remove migrationStrategy):

<provider name="default" enabled="true">
    <properties>
        <property name="dataSource" value="java:jboss/datasources/KeycloakDS"/>
        <property name="initializeEmpty" value="false"/>
        <property name="migrationStrategy" value="validate"/>
        <property name="schema" value="keycloak"/>
    </properties>
</provider>

It will fail with the above error.

Anything else?

No response

Activity

  1. added
    kind/bug
    on Feb 11, 2022
  2. added
    area/storage
    on Feb 14, 2022
  3. self-assigned this
    on Feb 15, 2022
  4. ahus1 commented on Feb 15, 2022

    @ahus1
    Member

    Hi @JaimySmets - the current status is (as you observed) that even in Manual mode, Keycloak will try to create the databasechangeloglock table. Once it is created (either by Keycloak or by a DBA), it will use this table to lock the schema migration even in manual and validate mode.

    I see the point that in manual/validate mode a Keycloak instance might for good reasons not have permissions to create the table.

    To remedy your situation at hand, please forward the SQL statement in the log to your DBA to create the table.

    For the future, I see two options here:

    • Option 1: update the documentation to describe the scenario where Keycloak is not allowed to create a table in the database, and that the table needs to be created first by using the SQL from the log, assuming that this is dependent on the Liquibase version and the database used. Initializing the database would then be a two-step approach: in the first step run Keycloak to get the lock statement, in a second step run Keycloak to get the migration script.

    • Option 2: when in manual mode, don't lock the database, thereby not creating the databasechangeloglock table. Then add it to the SQL created for the manual migration.

    Other things to consider: The database is currently locked for the validate mode, IMHO that would only make sense in a scenario with "validate" is mixed with Keycloak instances that are in "update" mode, as the SQL scripts created from the "manual" mode don't lock schema migration via the databasechangeloglock. Not locking in validate mode could speed up starting up multiple Keycloak instances as they wouldn't synchronize on the lock.

    Please comment on which options you consider as feasible and favorable. // cc: @hmlnarik

  5. hmlnarik commented on Feb 15, 2022

    @hmlnarik
    Contributor

    Table locking takes place upon startup e.g. to perform initial import, so the table needs to be created.

    I agree that it should be part of the SQL script. For the immediate workaround, please create the table manually according to https://docs.liquibase.com/concepts/basic/databasechangeloglock-table.html.

    For dev / testing purposes, you could leverage none lock but never do this in production:

    /subsystem=keycloak-server/spi=dblock:add(default-provider=none)
    

    Sidenote: Locking will change in the new storage: #9388.

  6. hmlnarik commented on Feb 15, 2022

    @hmlnarik
    Contributor

    Option 1 can be leveraged for the near-future deployments.

    Long-term the locking needs to be revisited from the perspective of work done in #9388

  7. ahus1 commented on Feb 15, 2022

    @ahus1
    Member

    For Option 1 (improving the docs) we're looking for a community contribution. It should document the current behavior, and how the necessary table databasechangeloglock can be created, either from Keycloak's log or the Liquibase documentation.

    How and if the databasechangeloglock should be created will be discussed in the issue referenced above, and not in this issue.

  8. added
    area/docs
    on Feb 15, 2022
  9. removed their assignment
    on Feb 15, 2022
  10. added this to the milestone on Mar 6, 2023
  11. removed this from the milestone on Mar 6, 2024
  12. keycloak-github-bot commented on Mar 6, 2024

    @keycloak-github-bot

    Due to the amount of issues reported by the community we are not able to prioritise resolving this issue at the moment.

    If you are affected by this issue, upvote it by adding a 👍 to the description. We would also welcome a contribution to fix the issue.

  13. keycloak-github-bot commented on Jun 5, 2024

    @keycloak-github-bot

    Due to lack of updates in the last 180 days this issue will be automatically closed.

  14. moved this from 📤 Todo to ✅ Done in Storageon Jun 5, 2024
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/docsarea/storagehelp wantedkind/bugpriority/lowstatus/auto-bumpstatus/auto-expirestatus/expired-by-botteam/core-iamteam/core-shared

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    • Status
      ✅ Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions