Monday, July 22, 2013

Flyway Validate: Cannot determine latest applied migration. Was the metadata table manually modified?

In our project we use Flyway to control modifications to the database. Recently I started seeing this error on my local environment, which doesn't give too many clues on why it could be failing:


Flyway Validate: Cannot determine latest applied migration. Was the metadata table manually modified?

Given that Flyway is an Open Source project, it wasn't very hard to find the code and search for the error string. We can see what the code does is to look for the column "CURRENT_VERSION":
   /**
     * @return The latest migration applied on the schema. {@code null} if no migration has been applied so far.
     */
    public MetaDataTableRow latestAppliedMigration() {
        if (!hasRows()) {
            return null;
        }

        String query = getSelectStatement() + " where current_version=" + dbSupport.getBooleanTrue();
        @SuppressWarnings({"unchecked"})
        final List metaDataTableRows = jdbcTemplate.query(query, new MetaDataTableRowMapper());

        if (metaDataTableRows.isEmpty()) {
            if (hasRows()) {
                throw new FlywayException("Cannot determine latest applied migration. Was the metadata table manually modified?");
            }
            return null;
        }

        return metaDataTableRows.get(0);
    }

    ...

    /**
     * @return The select statement for reading the metadata table.
     */
    private String getSelectStatement() {
        return "select VERSION, DESCRIPTION, TYPE, SCRIPT, CHECKSUM, INSTALLED_ON, EXECUTION_TIME, STATE from " + schema + "." + table;
    }

So I just identified the last applied migration and manually set the column "CURRENT_VERSION" in 1. Problem solved!