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

org.openmetadata.service.migration.MigrationValidationClient 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.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.openmetadata.service.OpenMetadataApplicationConfig;
import org.openmetadata.service.jdbi3.MigrationDAO;

@Slf4j
public class MigrationValidationClient {
  @Getter public static MigrationValidationClient instance;

  private final MigrationDAO migrationDAO;
  private final OpenMetadataApplicationConfig config;
  @Getter private final List expectedMigrationList;

  private MigrationValidationClient(
      MigrationDAO migrationDAO, OpenMetadataApplicationConfig config) {
    this.migrationDAO = migrationDAO;
    this.config = config;
    this.expectedMigrationList = loadExpectedMigrationList();
  }

  public static MigrationValidationClient initialize(
      MigrationDAO migrationDAO, OpenMetadataApplicationConfig config) {

    if (instance == null) {
      instance = new MigrationValidationClient(migrationDAO, config);
    }
    return instance;
  }

  public List getCurrentVersions() {
    return migrationDAO.getMigrationVersions();
  }

  private List loadExpectedMigrationList() {
    try {
      String nativePath = config.getMigrationConfiguration().getNativePath();
      String extensionPath = config.getMigrationConfiguration().getExtensionPath();

      List availableOMNativeMigrations = getMigrationFilesFromPath(nativePath);

      // If we only have OM migrations, return them
      if (extensionPath == null || extensionPath.isEmpty()) {
        return availableOMNativeMigrations;
      }

      // Otherwise, fetch the extension migration and sort the results
      List availableOMExtensionMigrations = getMigrationFilesFromPath(extensionPath);

      return Stream.concat(
              availableOMNativeMigrations.stream(), availableOMExtensionMigrations.stream())
          .sorted()
          .toList();
    } catch (Exception e) {
      LOG.error("Error loading expected migration list", e);
      return List.of();
    }
  }

  private List getMigrationFilesFromPath(String path) {
    return Arrays.stream(Objects.requireNonNull(new File(path).listFiles(File::isDirectory)))
        .map(File::getName)
        .sorted()
        .toList();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy