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));
}
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();
}
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()));
}
}
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()));
}
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;
}
}
Aggregations