VersionResolver { MigrationVersion resolve(SqlScript script, FlywayConfiguration configuration) { String normalizedDir = normalizedDirs.get(script.dir); if (normalizedDir == null) { normalizedDir = NOT_VERSION_CHARS_PATTERN.matcher(script.dir.replace("-", ".")).replaceAll(""); normalizedDirs.put(script.dir, normalizedDir); } final int sepIdx = script.name.indexOf(configuration.getSqlMigrationSeparator()); if (sepIdx == -1) { throw new FlywayException( format( "sql script name '%s' is not valid, name must contain '%s'", script.name, configuration.getSqlMigrationSeparator())); } String version = script.name.substring(0, sepIdx); if (version.isEmpty()) { throw new FlywayException( format( "sql script name '%s' is not valid, name must provide version like " + "'%s4%smigration_description.sql", configuration.getSqlMigrationPrefix(), script.name, configuration.getSqlMigrationSeparator())); } final String prefix = configuration.getSqlMigrationPrefix(); if (!isNullOrEmpty(prefix) && script.name.startsWith(prefix)) { version = version.substring(prefix.length()); } return MigrationVersion.fromVersion(normalizedDir + '.' + version); }    }
@Test(dataProvider = "validScripts") public void resolvesVersion(String dir, String name, String expectedVersion) { final SqlScript script = new SqlScript( new FileSystemResource("sql/" + dir + "/" + name), new Location("filesystem:sql"), dir, null, name); assertEquals(resolver.resolve(script, flyway), MigrationVersion.fromVersion(expectedVersion)); }
@Test(dataProvider = "invalidScripts", expectedExceptions = FlywayException.class) public void failsToResolveVersions(String dir, String name) throws Exception { final SqlScript script = new SqlScript( new FileSystemResource("sql/" + dir + "/" + name), new Location("filesystem:sql"), dir, null, name); resolver.resolve(script, flyway); }