Skip to content

Commit

Permalink
Fixed #1892: Allow executing info while a migration is running
Browse files Browse the repository at this point in the history
  • Loading branch information
Axel Fontaine committed Apr 6, 2018
1 parent e235f7a commit 30b22d5
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 29 deletions.
6 changes: 6 additions & 0 deletions flyway-core/pom.xml
Expand Up @@ -138,6 +138,12 @@












Expand Down
4 changes: 2 additions & 2 deletions flyway-core/src/main/java/org/flywaydb/core/Flyway.java
Expand Up @@ -1128,7 +1128,7 @@ public int undo() throws FlywayException {
* <li>versions have been applied that aren't resolved locally anymore</li>
* <li>versions have been resolved that haven't been applied yet</li>
* </ul>
* <p>
*
* <img src="https://flywaydb.org/assets/balsamiq/command-validate.png" alt="validate">
*
* @throws FlywayException when the validation failed.
Expand Down Expand Up @@ -1224,7 +1224,7 @@ public MigrationInfoService execute(MigrationResolver migrationResolver, SchemaH

/**
* <p>Baselines an existing database, excluding all migrations up to and including baselineVersion.</p>
* <p>
*
* <img src="https://flywaydb.org/assets/balsamiq/command-baseline.png" alt="baseline">
*
* @throws FlywayException when the schema baselining failed.
Expand Down
Expand Up @@ -305,24 +305,29 @@ public Object call() {
private boolean isExecuteGroupInTransaction(LinkedHashMap<MigrationInfoImpl, Boolean> group) {
boolean executeGroupInTransaction = true;
boolean first = true;

for (Map.Entry<MigrationInfoImpl, Boolean> entry : group.entrySet()) {
ResolvedMigration resolvedMigration = entry.getKey().getResolvedMigration();
boolean inTransaction = resolvedMigration.getExecutor().executeInTransaction();

if (first) {
executeGroupInTransaction = inTransaction;
first = false;
} else {
if (!configuration.isMixed() && executeGroupInTransaction != inTransaction) {
throw new FlywayException(
"Detected both transactional and non-transactional migrations within the same migration group"
+ " (even though mixed is false). First offending migration:"
+ (resolvedMigration.getVersion() == null ? "" : " " + resolvedMigration.getVersion())
+ (StringUtils.hasLength(resolvedMigration.getDescription()) ? " " + resolvedMigration.getDescription() : "")
+ (inTransaction ? "" : " [non-transactional]"));
}
executeGroupInTransaction = executeGroupInTransaction && inTransaction;
continue;
}

if (!configuration.isMixed() && executeGroupInTransaction != inTransaction) {
throw new FlywayException(
"Detected both transactional and non-transactional migrations within the same migration group"
+ " (even though mixed is false). First offending migration:"
+ (resolvedMigration.getVersion() == null ? "" : " " + resolvedMigration.getVersion())
+ (StringUtils.hasLength(resolvedMigration.getDescription()) ? " " + resolvedMigration.getDescription() : "")
+ (inTransaction ? "" : " [non-transactional]"));
}

executeGroupInTransaction &= inTransaction;
}

return executeGroupInTransaction;
}

Expand Down
Expand Up @@ -373,6 +373,22 @@ public String getInsertStatement(Table table) {
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
}

public String getSelectStatement(Table table, int maxCachedInstalledRank) {
return "SELECT " + quote("installed_rank")
+ "," + quote("version")
+ "," + quote("description")
+ "," + quote("type")
+ "," + quote("script")
+ "," + quote("checksum")
+ "," + quote("installed_on")
+ "," + quote("installed_by")
+ "," + quote("execution_time")
+ "," + quote("success")
+ " FROM " + table
+ " WHERE " + quote("installed_rank") + " > " + maxCachedInstalledRank
+ " ORDER BY " + quote("installed_rank");
}

public void close() {
if (!useSingleConnection() && migrationConnection != null) {
migrationConnection.close();
Expand Down
Expand Up @@ -279,6 +279,7 @@ private void addStatement(List<SqlStatement<C>> statements, SqlStatementBuilder
+ (sqlStatementBuilder.executeInTransaction() ? "" : " [non-transactional]"));
}

LOG.debug("Found statement at line " + sqlStatement.getLineNumber() + ": " + sqlStatement.getSql() + (sqlStatementBuilder.executeInTransaction() ? "" : " [non-transactional]"));
LOG.debug("Found statement at line " + sqlStatement.getLineNumber() + ": " + sqlStatement.getSql()
+ (sqlStatementBuilder.executeInTransaction() ? "" : " [non-transactional]"));
}
}
Expand Up @@ -19,6 +19,7 @@
import org.flywaydb.core.api.errorhandler.ErrorHandler;
import org.flywaydb.core.internal.database.Database;
import org.flywaydb.core.internal.database.SqlScript;
import org.flywaydb.core.internal.database.Table;
import org.flywaydb.core.internal.exception.FlywayDbUpgradeRequiredException;
import org.flywaydb.core.internal.util.PlaceholderReplacer;
import org.flywaydb.core.internal.util.scanner.LoadableResource;
Expand Down Expand Up @@ -119,6 +120,13 @@ public LoadableResource getRawCreateScript() {
"CREATE INDEX \"${schema}\".\"${table}_s_idx\" ON \"${schema}\".\"${table}\" (\"success\");");
}

@Override
public String getSelectStatement(Table table, int maxCachedInstalledRank) {
return super.getSelectStatement(table, maxCachedInstalledRank)
// Allow uncommitted reads so info can be invoked while migrate is running
+ " WITH UR";
}

@Override
public String getDbName() {
return "db2";
Expand Down
Expand Up @@ -29,10 +29,10 @@ public class MySQLSqlStatementBuilder extends SqlStatementBuilder {
* The keyword that indicates a change in delimiter.
*/
private static final String DELIMITER_KEYWORD = "DELIMITER";
private final String[] charSets = {
private static final String[] CHARSETS = {
"ARMSCII8", "ASCII", "BIG5", "BINARY", "CP1250", "CP1251", "CP1256", "CP1257", "CP850", "CP852", "CP866", "CP932",
"DEC8", "EUCJPMS", "EUCKR", "GB2312", "GBK", "GEOSTD8", "GREEK", "HEBREW", "HP8", "KEYBCS2", "KOI8R", "KOI8U", "LATIN1",
"LATIN2", "LATIN5", "LATIN7", "MACCE", "MACROMAN", "SJIS", "SWE7", "TIS620", "UCS2", "UJIS", "UTF8"
"LATIN2", "LATIN5", "LATIN7", "MACCE", "MACROMAN", "SJIS", "SWE7", "TIS620", "UCS2", "UJIS", "UTF8", "UTF8MB4"
};

/*private -> testing*/ boolean isInMultiLineCommentDirective = false;
Expand Down Expand Up @@ -98,7 +98,7 @@ protected String cleanToken(String token) {
}

if (token.startsWith("_")) {
for (String charSet : charSets) {
for (String charSet : CHARSETS) {
String cast = "_" + charSet;
if (token.startsWith(cast)) {
return token.substring(cast.length());
Expand Down
Expand Up @@ -181,19 +181,7 @@ public List<AppliedMigration> allAppliedMigrations() {
private void refreshCache() {
int maxCachedInstalledRank = cache.isEmpty() ? -1 : cache.getLast().getInstalledRank();

String query = "SELECT " + database.quote("installed_rank")
+ "," + database.quote("version")
+ "," + database.quote("description")
+ "," + database.quote("type")
+ "," + database.quote("script")
+ "," + database.quote("checksum")
+ "," + database.quote("installed_on")
+ "," + database.quote("installed_by")
+ "," + database.quote("execution_time")
+ "," + database.quote("success")
+ " FROM " + table
+ " WHERE " + database.quote("installed_rank") + " > " + maxCachedInstalledRank
+ " ORDER BY " + database.quote("installed_rank");
String query = database.getSelectStatement(table, maxCachedInstalledRank);

try {
cache.addAll(jdbcTemplate.query(query, new RowMapper<AppliedMigration>() {
Expand Down

0 comments on commit 30b22d5

Please sign in to comment.