use of io.confluent.ksql.tools.migrations.MigrationConfig in project ksql by confluentinc.
the class ApplyMigrationCommand method executeCommands.
/**
* If validateOnly is set to true, then this parses each of the commands but only executes
* DEFINE/UNDEFINE commands (variables are needed for parsing INSERT INTO... VALUES, SET/UNSET
* and DEFINE commands). If validateOnly is set to false, then each command will execute after
* parsing.
*/
private void executeCommands(final List<String> commands, final Client ksqlClient, final MigrationConfig config, final String executionStart, final MigrationFile migration, final Clock clock, final String previous, final boolean validateOnly) {
setUpJavaClientVariables(ksqlClient);
final Map<String, Object> properties = new HashMap<>();
for (final String command : commands) {
try {
final Map<String, String> variables = ksqlClient.getVariables().entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().toString()));
executeCommand(CommandParser.transformToSqlCommand(command, variables), ksqlClient, properties, validateOnly);
} catch (InterruptedException | ExecutionException | MigrationException e) {
final String action = validateOnly ? "parse" : "execute";
final String errorMsg = String.format("Failed to %s sql: %s. Error: %s", action, command, e.getMessage());
updateState(config, ksqlClient, MigrationState.ERROR, executionStart, migration, clock, previous, Optional.of(errorMsg));
throw new MigrationException(errorMsg);
}
}
}
use of io.confluent.ksql.tools.migrations.MigrationConfig in project ksql by confluentinc.
the class MetadataUtil method getOptionalInfoForVersions.
public static Map<Integer, Optional<MigrationVersionInfo>> getOptionalInfoForVersions(final List<Integer> versions, final MigrationConfig config, final Client ksqlClient) {
if (serverSupportsMultiKeyPullQuery(ksqlClient, config)) {
// issue a single, multi-key pull query
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 IN ('" + versions.stream().map(String::valueOf).collect(Collectors.joining("', '")) + "');");
final Map<Integer, MigrationVersionInfo> resultSet;
try {
resultSet = result.get().stream().map(MigrationVersionInfo::fromResultRow).collect(Collectors.toMap(MigrationVersionInfo::getVersion, vInfo -> vInfo));
} catch (InterruptedException | ExecutionException e) {
throw new MigrationException(String.format("Failed to query state for migration with versions %s: %s", versions, e.getMessage()));
}
return versions.stream().collect(Collectors.toMap(v -> v, v -> Optional.ofNullable(resultSet.get(v))));
} else {
// issue multiple, single-key pull queries
return versions.stream().collect(Collectors.toMap(v -> v, v -> getOptionalInfoForVersion(String.valueOf(v), config, ksqlClient)));
}
}
Aggregations