org.flywaydb.core.api.MigrationType Java Examples

The following examples show how to use org.flywaydb.core.api.MigrationType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ShellMigrationResolver.java    From registry with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts the migration info for this resource.
 *
 * @param resource The resource to analyse.
 * @return The migration info.
 */
private ResolvedMigrationImpl extractMigrationInfo(Resource resource) {
    ResolvedMigrationImpl migration = new ResolvedMigrationImpl();

    Pair<MigrationVersion, String> info =
            MigrationInfoHelper.extractVersionAndDescription(resource.getFilename(),
                    shellMigrationPrefix, shellMigrationSeparator, new String [] {shellMigrationSuffix}, false);
    migration.setVersion(info.getLeft());
    migration.setDescription(info.getRight());

    migration.setScript(extractScriptName(resource));

    byte [] migrationScriptAsBytes = null;
    try {
        migrationScriptAsBytes = IOUtils.toByteArray(new FileInputStream(resource.getLocation()));
    } catch (Exception e) {
        throw new ShellMigrationException(String.format("Failed to read the migration script : %s", resource.getLocation()), e);
    }

    migration.setChecksum(calculateChecksum(migrationScriptAsBytes));

    migration.setType(MigrationType.CUSTOM);
    return migration;
}
 
Example #2
Source File: SkipperFlywayMigrationStrategy.java    From spring-cloud-skipper with Apache License 2.0 5 votes vote down vote up
@Override
public void migrate(Flyway flyway) {
	MigrationInfo current = flyway.info().current();
	if (current != null && current.getVersion().equals(INITIAL) && current.getType() == MigrationType.SQL) {
		logger.info("Detected initial version based on SQL scripts, doing repair to switch to Java based migrations.");
		flyway.repair();
	}
	flyway.migrate();
}
 
Example #3
Source File: CustomSqlMigrationResolver.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
private List<ResolvedMigration> resolveSqlMigrations() throws IOException, SQLException {
  LOG.info(
      "Searching for SQL scripts in locations {}",
      Arrays.toString(flywayConfiguration.getLocations()));
  final Map<Location, List<Resource>> allResources = finder.findResources(flywayConfiguration);
  LOG.debug("Found scripts: {}", allResources);

  final Map<String, Map<String, SqlScript>> scriptsInDir = new HashMap<>();
  for (Location location : allResources.keySet()) {
    final List<Resource> resources = allResources.get(location);
    for (Resource resource : resources) {
      final SqlScript newScript = scriptsCreator.createScript(location, resource);
      if (!scriptsInDir.containsKey(newScript.dir)) {
        scriptsInDir.put(newScript.dir, new HashMap<>(4));
      }
      final Map<String, SqlScript> existingScripts = scriptsInDir.get(newScript.dir);
      final SqlScript existingScript = existingScripts.get(newScript.name);
      if (existingScript == null) {
        existingScripts.put(newScript.name, newScript);
      } else if (Objects.equals(existingScript.vendor, newScript.vendor)) {
        throw new FlywayException(
            format(
                "More than one script with name '%s' is registered for "
                    + "database vendor '%s', script '%s' conflicts with '%s'",
                newScript.name, existingScript.vendor, newScript, existingScript));
      } else if (vendorName.equals(newScript.vendor)) {
        existingScripts.put(newScript.name, newScript);
      }
    }
  }

  final Map<MigrationVersion, ResolvedMigration> migrations = new HashMap<>();
  for (SqlScript script :
      scriptsInDir
          .values()
          .stream()
          .flatMap(scripts -> scripts.values().stream())
          .collect(toList())) {
    final ResolvedMigrationImpl migration = new ResolvedMigrationImpl();
    migration.setVersion(versionResolver.resolve(script, flywayConfiguration));
    migration.setScript(script.resource.getLocation());
    migration.setPhysicalLocation(script.resource.getLocationOnDisk());
    migration.setType(MigrationType.SQL);
    migration.setDescription(script.name);
    migration.setChecksum(
        ByteSource.wrap(script.resource.loadAsBytes()).hash(Hashing.crc32()).asInt());
    migration.setExecutor(
        new SqlMigrationExecutor(
            dbSupport, script.resource, placeholderReplacer, flywayConfiguration));
    if (migrations.put(migration.getVersion(), migration) != null) {
      throw new FlywayException("Two migrations with the same version detected");
    }
  }
  return new ArrayList<>(migrations.values());
}