Skip to content

Commit

Permalink
Prevent error when unlocking the changelog in MySQL affects zero rows…
Browse files Browse the repository at this point in the history
… and useAffectedRows is true. Fixes #5502 (#5777)

* Prevent error when unlocking the changelog in MySQL affects zero rows and useAffectedRows is true

* Move getUseAffectedRows() to MySQLDatabase
  • Loading branch information
mpvvliet committed Apr 15, 2024
1 parent 298438d commit b24f1a4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -692,4 +692,9 @@ public boolean supportsCreateIfNotExists(Class<? extends DatabaseObject> type) {
public boolean supportsDatabaseChangeLogHistory() {
return true;
}

public boolean getUseAffectedRows() throws DatabaseException {
return getConnection().getURL().contains("useAffectedRows=true");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import liquibase.database.core.DB2Database;
import liquibase.database.core.DerbyDatabase;
import liquibase.database.core.MSSQLDatabase;
import liquibase.database.core.MySQLDatabase;
import liquibase.diff.output.DiffOutputControl;
import liquibase.diff.output.changelog.ChangeGeneratorFactory;
import liquibase.exception.DatabaseException;
Expand Down Expand Up @@ -370,6 +371,16 @@ public void releaseLock() throws LockException {
database.rollback();
SqlStatement unlockStatement = new UnlockDatabaseChangeLogStatement();
int updatedRows = ChangelogJdbcMdcListener.query(database, ex -> ex.update(unlockStatement));
if ((updatedRows == 0) && (database instanceof MySQLDatabase)) {
Scope.getCurrentScope().getLog(getClass()).fine(
"Database did not return a proper row count (Might have useAffectedRows enabled.)"
);
// NOTE: if using useAffectedRows, MySQL will return 0 rows affected if the changelog lock was not set or already released
if (((MySQLDatabase) database).getUseAffectedRows()) {
// Assume the lock was released successfully
updatedRows = 1;
}
}
if ((updatedRows == -1) && (database instanceof MSSQLDatabase)) {
Scope.getCurrentScope().getLog(getClass()).fine(
"Database did not return a proper row count (Might have NOCOUNT enabled.)"
Expand Down

0 comments on commit b24f1a4

Please sign in to comment.