Search in sources :

Example 26 with MigrationException

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

the class CreateMigrationCommand method createMigrationsFile.

private void createMigrationsFile(final int newVersion, final String migrationsDir) {
    if (newVersion <= 0) {
        throw new MigrationException("Invalid version file version: " + newVersion + ". Version must be a positive integer.");
    }
    if (newVersion > 999999) {
        throw new MigrationException("Invalid version file version: " + newVersion + ". Version must fit into a six-digit integer.");
    }
    final String filename = getNewFileName(newVersion, description);
    final String filePath = Paths.get(migrationsDir, filename).toString();
    try {
        LOGGER.info("Creating migration file: " + filePath);
        final boolean result = new File(filePath).createNewFile();
        if (!result) {
            throw new IllegalStateException("File should not exist");
        }
        LOGGER.info("Migration file successfully created");
    } catch (IOException | IllegalStateException e) {
        throw new MigrationException(String.format("Failed to create file %s: %s", filePath, e.getMessage()));
    }
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) IOException(java.io.IOException) MigrationFile(io.confluent.ksql.tools.migrations.util.MigrationFile) File(java.io.File) MigrationsDirectoryUtil.getMigrationsDirFromConfigFile(io.confluent.ksql.tools.migrations.util.MigrationsDirectoryUtil.getMigrationsDirFromConfigFile)

Example 27 with MigrationException

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

the class InitializeMigrationCommand method command.

@VisibleForTesting
int command(final MigrationConfig config, final Function<MigrationConfig, Client> clientSupplier) {
    final String streamName = config.getString(MigrationConfig.KSQL_MIGRATIONS_STREAM_NAME);
    final String tableName = config.getString(MigrationConfig.KSQL_MIGRATIONS_TABLE_NAME);
    final String eventStreamCommand = createEventStream(streamName, config.getString(MigrationConfig.KSQL_MIGRATIONS_STREAM_TOPIC_NAME), config.getInt(MigrationConfig.KSQL_MIGRATIONS_TOPIC_REPLICAS));
    final String versionTableCommand = createVersionTable(tableName, streamName, config.getString(MigrationConfig.KSQL_MIGRATIONS_TABLE_TOPIC_NAME));
    final Client ksqlClient;
    try {
        ksqlClient = clientSupplier.apply(config);
    } catch (MigrationException e) {
        LOGGER.error(e.getMessage());
        return 1;
    }
    LOGGER.info("Initializing migrations metadata");
    if (ServerVersionUtil.serverVersionCompatible(ksqlClient, config) && tryCreate(ksqlClient, eventStreamCommand, streamName, true) && tryCreate(ksqlClient, versionTableCommand, tableName, false)) {
        LOGGER.info("Migrations metadata initialized successfully");
        ksqlClient.close();
    } else {
        ksqlClient.close();
        return 1;
    }
    return 0;
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Client(io.confluent.ksql.api.client.Client) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 28 with MigrationException

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

the class CommandParser method getCreateConnectorStatement.

private static SqlCreateConnectorStatement getCreateConnectorStatement(final String sql, final Map<String, String> variables) {
    final CreateConnector createConnector;
    try {
        final String substituted = VariableSubstitutor.substitute(KSQL_PARSER.parse(sql).get(0), variables);
        createConnector = (CreateConnector) new AstBuilder(TypeRegistry.EMPTY).buildStatement(KSQL_PARSER.parse(substituted).get(0).getStatement());
    } catch (ParseFailedException e) {
        throw new MigrationException(String.format("Failed to parse CREATE CONNECTOR statement. Statement: %s. Reason: %s", sql, e.getMessage()));
    }
    return new SqlCreateConnectorStatement(sql, preserveCase(createConnector.getName()), createConnector.getType() == Type.SOURCE, createConnector.getConfig().entrySet().stream().collect(Collectors.toMap(Entry::getKey, e -> toFieldType(e.getValue()))));
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) AstBuilder(io.confluent.ksql.parser.AstBuilder) Entry(java.util.Map.Entry) ParseFailedException(io.confluent.ksql.parser.exception.ParseFailedException) CreateConnector(io.confluent.ksql.parser.tree.CreateConnector)

Example 29 with MigrationException

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

the class CommandParser method getInsertValuesStatement.

private static SqlInsertValues getInsertValuesStatement(final String sql, final Map<String, String> variables) {
    final InsertValues parsedStatement;
    try {
        final String substituted = VariableSubstitutor.substitute(KSQL_PARSER.parse(sql).get(0), variables);
        parsedStatement = (InsertValues) new AstBuilder(TypeRegistry.EMPTY).buildStatement(KSQL_PARSER.parse(substituted).get(0).getStatement());
    } catch (ParseFailedException e) {
        throw new MigrationException(String.format("Failed to parse INSERT VALUES statement. Statement: %s. Reason: %s", sql, e.getMessage()));
    }
    return new SqlInsertValues(sql, preserveCase(parsedStatement.getTarget().text()), parsedStatement.getValues(), parsedStatement.getColumns().stream().map(ColumnName::text).collect(Collectors.toList()));
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) AstBuilder(io.confluent.ksql.parser.AstBuilder) ColumnName(io.confluent.ksql.name.ColumnName) InsertValues(io.confluent.ksql.parser.tree.InsertValues) ParseFailedException(io.confluent.ksql.parser.exception.ParseFailedException)

Example 30 with MigrationException

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

the class MigrationsUtil method createClientOptions.

@VisibleForTesting
static // CHECKSTYLE_RULES.OFF: ParameterNumberCheck
ClientOptions createClientOptions(// CHECKSTYLE_RULES.ON: ParameterNumberCheck
final String ksqlServerUrl, final String username, final String password, final String sslTrustStoreLocation, final String sslTrustStorePassword, final String sslKeystoreLocation, final String sslKeystorePassword, final String sslKeyPassword, final String sslKeyAlias, final boolean useAlpn, final boolean verifyHost, final Map<String, String> requestHeaders) {
    final URL url;
    try {
        url = new URL(ksqlServerUrl);
    } catch (MalformedURLException e) {
        throw new MigrationException("Invalid ksql server URL: " + ksqlServerUrl);
    }
    final ClientOptions options = ClientOptions.create().setHost(url.getHost()).setPort(url.getPort());
    if (username != null || password != null) {
        options.setBasicAuthCredentials(username, password);
    }
    final boolean useTls = ksqlServerUrl.trim().toLowerCase().startsWith("https://");
    options.setUseTls(useTls);
    if (useTls) {
        options.setTrustStore(sslTrustStoreLocation);
        options.setTrustStorePassword(sslTrustStorePassword);
        options.setKeyStore(sslKeystoreLocation);
        options.setKeyStorePassword(sslKeystorePassword);
        options.setKeyPassword(sslKeyPassword);
        options.setKeyAlias(sslKeyAlias);
        options.setUseAlpn(useAlpn);
        options.setVerifyHost(verifyHost);
    }
    if (requestHeaders != null) {
        options.setRequestHeaders(requestHeaders);
    }
    return options;
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) ClientOptions(io.confluent.ksql.api.client.ClientOptions) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

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