All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openmetadata.service.migration.Migration Maven / Gradle / Ivy

There is a newer version: 1.5.11
Show newest version
package org.openmetadata.service.migration;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.statement.StatementException;
import org.openmetadata.service.jdbi3.MigrationDAO;

@Slf4j
public final class Migration {
  private Migration() {}

  /**
   * Run a query to MySQL to retrieve the last migrated Flyway version. If the Flyway table DATABASE_CHANGE_LOG does not
   * exist, we will stop the Catalog App and inform users how to run Flyway.
   */
  public static Optional lastMigrated(Jdbi jdbi) {
    try {
      return jdbi.withExtension(MigrationDAO.class, MigrationDAO::getMaxVersion);
    } catch (StatementException e) {
      throw new IllegalArgumentException(
          "Exception encountered when trying to obtain last migrated Flyway version."
              + " Make sure you have run `./bootstrap/openmetadata-ops.sh migrate` at least once.",
          e);
    }
  }

  public static String lastMigrationFile(MigrationConfiguration conf) throws IOException {
    List migrationFiles = getMigrationVersions(conf);
    return Collections.max(migrationFiles);
  }

  /** Read the migrations path from the Catalog YAML config and return a list of all the files' versions. */
  private static List getMigrationVersions(MigrationConfiguration conf) throws IOException {
    try (Stream names =
        Files.walk(Paths.get(conf.getFlywayPath()))
            .filter(Files::isRegularFile)
            .map(Path::toFile)
            .map(File::getName)
            .map(Migration::cleanName)) {

      return names.collect(Collectors.toList());
    }
  }

  /** Given a Flyway migration filename, e.g., v001__my_file.sql, return the version information "001". */
  private static String cleanName(String name) {
    return Arrays.asList(name.split("_")).get(0).replace("v", "");
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy