Check if PK for DATABASECHANGELOG already exists

Closes #41082


(cherry picked from commit 164274ac51)

Signed-off-by: rmartinc <rmartinc@redhat.com>
This commit is contained in:
Ricardo Martin
2025-07-14 12:03:56 +02:00
committed by GitHub
parent 5fff25edc8
commit 68f4cd392a

View File

@@ -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;
}
}
}
}