Skip to content

Commit

Permalink
Fixed flyway#2341: MySQL: Reset SQL_SAFE_UPDATES and FOREIGN_KEY_CHEC…
Browse files Browse the repository at this point in the history
…KS after each migration
  • Loading branch information
Axel Fontaine committed Apr 17, 2019
1 parent 7beb4ea commit b63d198
Showing 1 changed file with 19 additions and 0 deletions.
Expand Up @@ -21,6 +21,7 @@
import org.flywaydb.core.internal.database.base.Connection;
import org.flywaydb.core.internal.database.base.Schema;
import org.flywaydb.core.internal.database.base.Table;
import org.flywaydb.core.internal.exception.FlywaySqlException;
import org.flywaydb.core.internal.util.StringUtils;

import java.sql.SQLException;
Expand All @@ -36,10 +37,15 @@ public class MySQLConnection extends Connection<MySQLDatabase> {

private static final String USER_VARIABLES_TABLE_MARIADB = "information_schema.user_variables";
private static final String USER_VARIABLES_TABLE_MYSQL = "performance_schema.user_variables_by_thread";
private static final String FOREIGN_KEY_CHECKS = "foreign_key_checks";
private static final String SQL_SAFE_UPDATES = "sql_safe_updates";

private final String userVariablesQuery;
private final boolean canResetUserVariables;

private final int originalForeignKeyChecks;
private final int originalSqlSafeUpdates;

MySQLConnection(Configuration configuration, MySQLDatabase database, java.sql.Connection connection
, boolean originalAutoCommit

Expand All @@ -56,6 +62,17 @@ public class MySQLConnection extends Connection<MySQLDatabase> {
+ (database.isMariaDB() ? USER_VARIABLES_TABLE_MARIADB : USER_VARIABLES_TABLE_MYSQL)
+ " WHERE variable_value IS NOT NULL";
canResetUserVariables = hasUserVariableResetCapability();

originalForeignKeyChecks = getIntVariableValue(FOREIGN_KEY_CHECKS);
originalSqlSafeUpdates = getIntVariableValue(SQL_SAFE_UPDATES);
}

private int getIntVariableValue(String varName) {
try {
return jdbcTemplate.queryForInt("SELECT @@" + varName);
} catch (SQLException e) {
throw new FlywaySqlException("Unable to determine value for '"+ varName + "' variable", e);
}
}

// #2215: ensure the database is recent enough and the current user has the necessary SELECT grant
Expand Down Expand Up @@ -87,6 +104,8 @@ private boolean hasUserVariableResetCapability() {
@Override
protected void doRestoreOriginalState() throws SQLException {
resetUserVariables();
jdbcTemplate.execute("SET " + FOREIGN_KEY_CHECKS + "=?, " + SQL_SAFE_UPDATES + "=?",
originalForeignKeyChecks, originalSqlSafeUpdates);
}

// #2197: prevent user-defined variables from leaking beyond the scope of a migration
Expand Down

0 comments on commit b63d198

Please sign in to comment.