Repository navigation
Description
Describe the bug
With migration-strategy=manual and an empty database, the generated keycloak-database-update.sql announces
itself as a creation script:
-- *********************************************************************
-- * Keycloak database creation script - apply this script to empty DB *
-- *********************************************************************
but it inserts 210 rows into DATABASECHANGELOG without ever creating that table, so it cannot be applied to an
empty database. Applying it fails on the first insert.
This was reported and fixed before, in #32535 / PR #37204, which also added two assertions to
BasicDatabaseTest.assertManualDbInitialization guarding it: the script must contain
Create Database Change Log Table and exactly one create table ...databasechangelog.
Those assertions were removed in #41299 / PR #41490 ([quarkus-next] Missing comment generated by Liquibase executor in the custom script) rather than the underlying behaviour being restored, so the regression has been
unguarded since.
Root cause
PR #37204 removed an explicit CreateDatabaseChangeLogTableStatement because Liquibase emitted the DDL itself —
Liquibase only does that while the changelog table does not yet exist.
Since Liquibase 4.33, DatabaseChangeLog initializes the change log history service while a changelog is being
parsed, which creates the table on the live JDBC connection. Comparing the shipped jars:
| liquibase-core 4.29.1 | liquibase-core 4.33.0 | |
|---|---|---|
ChangeLogHistoryServiceFactory / init() referenced from DatabaseChangeLog |
absent | present |
So by the time the export is written the table already exists, Liquibase no longer emits its DDL, and the script
is left inserting into a table it never creates.
Expected behavior
The generated script can be applied to an empty database, i.e. it creates the DATABASECHANGELOG table it
inserts into.
Actual behavior
Applying the generated script to an empty database fails:
psql:export.sql:197: ERROR: relation "keycloak.databasechangelog" does not exist
LINE 1: INSERT INTO keycloak.databasechangelog (ID, AUTHOR, FILENAME...
How to Reproduce?
PostgreSQL 17, empty schema keycloak:
kc.sh start --optimized \
--db=postgres --db-url=jdbc:postgresql://<host>:5432/keycloak \
--db-username=<user> --db-password=<pw> --db-schema=keycloak \
--spi-connections-jpa--quarkus--migration-strategy=manual \
--spi-connections-jpa--quarkus--initialize-empty=false \
--http-enabled=true --hostname-strict=false
# exits with: Database not initialized, please initialize database with .../keycloak-database-update.sql
grep -ci 'CREATE TABLE keycloak.databasechangelog (' keycloak-database-update.sql # 0
grep -ci 'INSERT INTO keycloak.databasechangelog (' keycloak-database-update.sql # 210
# apply to an untouched database
psql -U <owner> -d virgin -v ON_ERROR_STOP=1 -f keycloak-database-update.sql
# ERROR at line 197: relation "keycloak.databasechangelog" does not exist (29 tables created)Anything else?
Measured on PostgreSQL 17, main vs. a local fix that emits the statement when the table did not exist before
startup:
| main | with fix | |
|---|---|---|
CREATE TABLE ...databasechangelog in export |
0 | 1 |
INSERT INTO ...databasechangelog |
210 | 210 |
apply to untouched DB (ON_ERROR_STOP=1) |
fails at line 197, 29 tables | exit 0, 99 tables |
then starting with migration-strategy=validate |
— | starts in 4.2 s |
One nuance worth recording: the statement must not be emitted unconditionally. An administrator may create
the two bookkeeping tables up front so that a database user without DDL rights can produce the script at all
(this is the only way manual works on an empty database today). In that case the target database already has
the table and emitting the DDL breaks the script with relation "databasechangelog" already exists — which is
what PR #37204 was originally fixing. The condition that distinguishes the two cases is whether the table
existed before this server started, not whether it exists at export time.
Related: #36756 describes the same export being unusable for a different reason (preconditions are not evaluated
in SQL output mode); it was closed by the stale bot without a fix.
Metadata
Metadata
Assignees
Type
Fields
Projects
- StatusShow more project fieldsDone
Activity
Create the changelog table in the manual migration export for an empt…