mirror of
https://github.com/keycloak/keycloak.git
synced 2026-01-10 15:32:05 -03:30
Check if PK for DATABASECHANGELOG already exists
Closes #41082 Signed-off-by: rmartinc <rmartinc@redhat.com>
This commit is contained in:
parent
8a694a585b
commit
164274ac51
@ -40,7 +40,12 @@ import liquibase.database.core.MySQLDatabase;
|
||||
import liquibase.exception.DatabaseException;
|
||||
import liquibase.executor.jvm.ChangelogJdbcMdcListener;
|
||||
import liquibase.logging.LogFactory;
|
||||
import liquibase.snapshot.InvalidExampleException;
|
||||
import liquibase.snapshot.SnapshotGeneratorFactory;
|
||||
import liquibase.statement.core.AddPrimaryKeyStatement;
|
||||
import liquibase.structure.core.PrimaryKey;
|
||||
import liquibase.structure.core.Schema;
|
||||
import liquibase.structure.core.Table;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -62,12 +67,11 @@ public class CustomChangeLogHistoryService extends StandardChangeLogHistoryServi
|
||||
|
||||
if (serviceInitialized) return;
|
||||
|
||||
AddPrimaryKeyStatement pkStatement = new AddPrimaryKeyStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(),
|
||||
ColumnConfig.arrayFromNames("ID, AUTHOR, FILENAME"), "PK_DATABASECHANGELOG");
|
||||
ChangelogJdbcMdcListener.execute(getDatabase(), ex -> ex.execute(pkStatement));
|
||||
getDatabase().commit();
|
||||
|
||||
serviceInitialized = true;
|
||||
|
||||
if (!existsDatabaseChangelogPK()) {
|
||||
createDatabaseChangelogPK();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -136,4 +140,31 @@ public class CustomChangeLogHistoryService extends StandardChangeLogHistoryServi
|
||||
public int getPriority() {
|
||||
return super.getPriority() + 1; // Ensure bigger priority than StandardChangeLogHistoryService
|
||||
}
|
||||
|
||||
private boolean existsDatabaseChangelogPK() throws DatabaseException {
|
||||
try {
|
||||
PrimaryKey example = new PrimaryKey();
|
||||
Table table = new Table();
|
||||
table.setSchema(new Schema(getLiquibaseCatalogName(), getLiquibaseSchemaName()));
|
||||
table.setName(getDatabaseChangeLogTableName());
|
||||
example.setTable(table);
|
||||
return SnapshotGeneratorFactory.getInstance().has(example, getDatabase());
|
||||
} catch (InvalidExampleException e) {
|
||||
throw new DatabaseException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void createDatabaseChangelogPK() throws DatabaseException {
|
||||
AddPrimaryKeyStatement pkStatement = new AddPrimaryKeyStatement(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName(),
|
||||
ColumnConfig.arrayFromNames("ID, AUTHOR, FILENAME"), "PK_DATABASECHANGELOG");
|
||||
try {
|
||||
ChangelogJdbcMdcListener.execute(getDatabase(), ex -> ex.execute(pkStatement));
|
||||
getDatabase().commit();
|
||||
} catch (DatabaseException e) {
|
||||
// if PK already exists just ignore the exception
|
||||
if (!existsDatabaseChangelogPK()) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user