Search in sources :

Example 6 with MigrationException

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

the class DestroyMigrationsCommand 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 Client ksqlClient;
    try {
        ksqlClient = clientSupplier.apply(config);
    } catch (MigrationException e) {
        LOGGER.error(e.getMessage());
        return 1;
    }
    LOGGER.info("Cleaning migrations metadata stream and table from ksqlDB server");
    if (ServerVersionUtil.serverVersionCompatible(ksqlClient, config) && deleteMigrationsTable(ksqlClient, tableName) && deleteMigrationsStream(ksqlClient, streamName)) {
        LOGGER.info("Migrations metadata cleaned successfully");
        ksqlClient.close();
        return 0;
    } else {
        ksqlClient.close();
        return 1;
    }
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Client(io.confluent.ksql.api.client.Client) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 7 with MigrationException

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

the class MigrationInfoCommand 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 {
        printCurrentVersion(config, ksqlClient);
        printVersionInfoTable(config, ksqlClient, migrationsDir);
        success = true;
    } catch (MigrationException e) {
        LOGGER.error(e.getMessage());
        success = false;
    } finally {
        ksqlClient.close();
    }
    return success ? 0 : 1;
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Client(io.confluent.ksql.api.client.Client) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 8 with MigrationException

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

the class ValidateMigrationsCommand method validate.

/**
 * @return true if validation passes, else false.
 */
static boolean validate(final MigrationConfig config, final String migrationsDir, final Client ksqlClient) {
    String version = getLatestMigratedVersion(config, ksqlClient);
    String nextVersion = null;
    while (!version.equals(MetadataUtil.NONE_VERSION)) {
        final MigrationVersionInfo versionInfo = getInfoForVersion(version, config, ksqlClient);
        if (nextVersion != null) {
            validateVersionIsMigrated(version, versionInfo, nextVersion);
        }
        final String filename;
        try {
            filename = getMigrationForVersion(version, migrationsDir).get().getFilepath();
        } catch (MigrationException | NoSuchElementException e) {
            LOGGER.error("No migrations file found for version with status {}. Version: {}", MigrationState.MIGRATED, version);
            return false;
        }
        final String hash = computeHashForFile(filename);
        final String expectedHash = versionInfo.getExpectedHash();
        if (!expectedHash.equals(hash)) {
            LOGGER.error("Migrations file found for version {} does not match the checksum saved " + "for this version. Expected checksum: {}. Actual checksum: {}. File name: {}", version, expectedHash, hash, filename);
            return false;
        }
        nextVersion = version;
        version = versionInfo.getPrevVersion();
    }
    return true;
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) MigrationVersionInfo(io.confluent.ksql.tools.migrations.util.MigrationVersionInfo) NoSuchElementException(java.util.NoSuchElementException)

Example 9 with MigrationException

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

the class MigrationsDirectoryUtil method getAllMigrations.

/**
 * @return all migration files in sorted order
 */
public static List<MigrationFile> getAllMigrations(final String migrationsDir) {
    final File directory = new File(migrationsDir);
    if (!directory.isDirectory()) {
        throw new MigrationException(migrationsDir + " is not a directory.");
    }
    final String[] allNames;
    try {
        allNames = directory.list();
    } catch (SecurityException e) {
        throw new MigrationException("Failed to retrieve files from " + migrationsDir + ": " + e.getMessage());
    }
    if (allNames == null) {
        throw new MigrationException("Failed to retrieve files from " + migrationsDir);
    }
    final List<String> filenames = Arrays.stream(allNames).sorted().filter(name -> !new File(name).isDirectory()).collect(Collectors.toList());
    final List<MigrationFile> migrations = filenames.stream().map(name -> getMigrationFromFilename(migrationsDir, name)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
    validateMigrationVersionsUnique(migrations);
    return migrations;
}
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 10 with MigrationException

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

the class CommandParserTest method shouldThrowOnDescribeStatement.

@Test
public void shouldThrowOnDescribeStatement() {
    // When:
    final MigrationException e = assertThrows(MigrationException.class, () -> parse("describe my_stream;"));
    // Then:
    assertThat(e.getMessage(), is("'DESCRIBE' statements are not supported."));
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Test(org.junit.Test)

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