Search in sources :

Example 21 with MigrationException

use of io.confluent.ksql.tools.migrations.MigrationException in project ksql by confluentinc.

the class MetadataUtil method getOptionalInfoForVersion.

public static Optional<MigrationVersionInfo> getOptionalInfoForVersion(final String version, final MigrationConfig config, final Client ksqlClient) {
    final String migrationTableName = config.getString(MigrationConfig.KSQL_MIGRATIONS_TABLE_NAME);
    final BatchedQueryResult result = ksqlClient.executeQuery("SELECT version, checksum, previous, state, name, started_on, completed_on, error_reason " + "FROM " + migrationTableName + " WHERE version_key = '" + version + "';");
    final Row resultRow;
    try {
        final List<Row> resultRows = result.get();
        if (resultRows.size() == 0) {
            return Optional.empty();
        }
        resultRow = resultRows.get(0);
    } catch (InterruptedException | ExecutionException e) {
        throw new MigrationException(String.format("Failed to query state for migration with version %s: %s", version, e.getMessage()));
    }
    return Optional.of(MigrationVersionInfo.fromResultRow(resultRow));
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Row(io.confluent.ksql.api.client.Row) ExecutionException(java.util.concurrent.ExecutionException) BatchedQueryResult(io.confluent.ksql.api.client.BatchedQueryResult)

Example 22 with MigrationException

use of io.confluent.ksql.tools.migrations.MigrationException in project ksql by confluentinc.

the class MigrationsDirectoryUtil method getMigrationForVersion.

public static Optional<MigrationFile> getMigrationForVersion(final String version, final String migrationsDir) {
    final String prefix = getFilePrefixForVersion(version);
    final File directory = new File(migrationsDir);
    if (!directory.isDirectory()) {
        throw new MigrationException(migrationsDir + " is not a directory.");
    }
    final String[] names = directory.list();
    if (names == null) {
        throw new MigrationException("Failed to retrieve files from " + migrationsDir);
    }
    final List<MigrationFile> matches = Arrays.stream(names).filter(name -> name.startsWith(prefix)).map(name -> getMigrationFromFilename(migrationsDir, name)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
    // throw on multiple matches
    validateMigrationVersionsUnique(matches);
    return matches.size() > 0 ? Optional.of(matches.get(0)) : Optional.empty();
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Files(java.nio.file.Files) MessageDigest(java.security.MessageDigest) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Matcher(java.util.regex.Matcher) Paths(java.nio.file.Paths) MigrationException(io.confluent.ksql.tools.migrations.MigrationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Path(java.nio.file.Path) Optional(java.util.Optional) File(java.io.File)

Example 23 with MigrationException

use of io.confluent.ksql.tools.migrations.MigrationException in project ksql by confluentinc.

the class MigrationsDirectoryUtil method computeHashForFile.

public static String computeHashForFile(final String filename) {
    try {
        final byte[] bytes = Files.readAllBytes(Paths.get(filename));
        final StringBuilder builder = new StringBuilder();
        for (final byte b : MessageDigest.getInstance("MD5").digest(bytes)) {
            builder.append(String.format("%02x", b));
        }
        return builder.toString();
    } catch (NoSuchAlgorithmException | IOException e) {
        throw new MigrationException(String.format("Could not compute hash for file '%s': %s", filename, e.getMessage()));
    }
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException)

Example 24 with MigrationException

use of io.confluent.ksql.tools.migrations.MigrationException in project ksql by confluentinc.

the class MigrationsDirectoryUtil method getMigrationFromFilename.

private static Optional<MigrationFile> getMigrationFromFilename(final String migrationsDir, final String filename) {
    final Matcher matcher = MIGRATION_FILE_MATCHER.matcher(filename);
    if (!matcher.find()) {
        LOGGER.warn("Skipping file does not match expected migration file pattern " + "'V<six digit number>__<name>.sql': {}", filename);
        return Optional.empty();
    }
    final int version = Integer.parseInt(matcher.group(1));
    if (version <= 0) {
        throw new MigrationException("MigrationFile file versions must be positive. Found: " + filename);
    }
    final String description = matcher.group(2).replace('_', ' ');
    return Optional.of(new MigrationFile(version, description, Paths.get(migrationsDir, filename).toString()));
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Matcher(java.util.regex.Matcher)

Example 25 with MigrationException

use of io.confluent.ksql.tools.migrations.MigrationException in project ksql by confluentinc.

the class ServerVersionUtil method serverVersionCompatible.

public static boolean serverVersionCompatible(final Client ksqlClient, final MigrationConfig config) {
    final String ksqlServerUrl = config.getString(MigrationConfig.KSQL_SERVER_URL);
    final ServerInfo serverInfo;
    try {
        serverInfo = getServerInfo(ksqlClient, ksqlServerUrl);
    } catch (MigrationException e) {
        LOGGER.error("Failed to get server info to verify version compatibility: {}", e.getMessage());
        return false;
    }
    final String serverVersion = serverInfo.getServerVersion();
    try {
        return isSupportedVersion(serverVersion);
    } catch (MigrationException e) {
        LOGGER.warn(e.getMessage() + ". Proceeding anyway.");
        return true;
    }
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) ServerInfo(io.confluent.ksql.api.client.ServerInfo)

Aggregations

MigrationException (io.confluent.ksql.tools.migrations.MigrationException)34 Test (org.junit.Test)11 VisibleForTesting (com.google.common.annotations.VisibleForTesting)7 Client (io.confluent.ksql.api.client.Client)7 ExecutionException (java.util.concurrent.ExecutionException)6 IOException (java.io.IOException)4 List (java.util.List)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 BatchedQueryResult (io.confluent.ksql.api.client.BatchedQueryResult)3 Row (io.confluent.ksql.api.client.Row)3 MigrationFile (io.confluent.ksql.tools.migrations.util.MigrationFile)3 File (java.io.File)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 Matcher (java.util.regex.Matcher)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 KsqlObject (io.confluent.ksql.api.client.KsqlObject)2 ServerInfo (io.confluent.ksql.api.client.ServerInfo)2 AstBuilder (io.confluent.ksql.parser.AstBuilder)2