use of io.confluent.ksql.tools.migrations.MigrationException in project ksql by confluentinc.
the class CommandParserTest method shouldThrowOnMissingSemicolon.
@Test
public void shouldThrowOnMissingSemicolon() {
// When:
final MigrationException e = assertThrows(MigrationException.class, () -> parse("create stream foo as select * from no_semicolon_after_this"));
// Then:
assertThat(e.getMessage(), containsString("Unmatched command at end of file; missing semicolon"));
}
use of io.confluent.ksql.tools.migrations.MigrationException 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.MigrationException in project ksql by confluentinc.
the class ValidateMigrationsCommand method command.
@VisibleForTesting
int command(final MigrationConfig config, final Function<MigrationConfig, Client> clientSupplier, final String migrationsDir) {
final Client ksqlClient;
try {
ksqlClient = clientSupplier.apply(config);
} catch (MigrationException e) {
LOGGER.error(e.getMessage());
return 1;
}
if (!validateMetadataInitialized(ksqlClient, config)) {
ksqlClient.close();
return 1;
}
boolean success;
try {
success = validate(config, migrationsDir, ksqlClient);
} catch (MigrationException e) {
LOGGER.error(e.getMessage());
success = false;
} finally {
ksqlClient.close();
}
if (success) {
LOGGER.info("Successfully validated checksums for migrations that have already been applied");
return 0;
} else {
return 1;
}
}
use of io.confluent.ksql.tools.migrations.MigrationException in project ksql by confluentinc.
the class MetadataUtil method getCurrentVersion.
public static String getCurrentVersion(final MigrationConfig config, final Client client) {
final String migrationTableName = config.getString(MigrationConfig.KSQL_MIGRATIONS_TABLE_NAME);
final BatchedQueryResult result = client.executeQuery("SELECT VERSION FROM " + migrationTableName + " WHERE version_key = '" + CURRENT_VERSION_KEY + "';");
try {
final List<Row> resultRows = result.get();
if (resultRows.size() == 0) {
return NONE_VERSION;
}
return resultRows.get(0).getString("VERSION");
} catch (InterruptedException | ExecutionException e) {
throw new MigrationException(String.format("Could not query %s: %s", migrationTableName, e.getMessage()));
}
}
use of io.confluent.ksql.tools.migrations.MigrationException 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