private Map adjustTargetSchema()

in java/clickhouse-connector/src/main/java/com/epam/deltix/timebase/connector/clickhouse/algos/SchemaProcessor.java [517:557]


    private Map<String, TableDeclaration> adjustTargetSchema() throws SQLException {
        Map<String, TableDeclaration> clickhouseTableDeclarations = timebaseStreamToClickhouseTable();

        boolean createIfNotExists = true;

        LOG.debug()
                .append("Creating database ")
                .append(clickhouseProperties.getDatabase())
                .append(" if not exists = ")
                .append(createIfNotExists)
                .commit();

        clickhouseClient.createDatabase(clickhouseProperties.getDatabase(), createIfNotExists);

        for (Map.Entry<String, TableDeclaration> entry : clickhouseTableDeclarations.entrySet()) {
            TableDeclaration tableDeclaration = entry.getValue();
            if (WriteMode.REWRITE == schemaOptions.getWriteMode()) {
                LOG.debug()
                        .append("Drop table if exists ")
                        .append(tableDeclaration.getTableIdentity().toString())
                        .commit();
                clickhouseClient.dropTable(tableDeclaration.getTableIdentity(), true);
            }

            if (clickhouseClient.existsTable(tableDeclaration.getTableIdentity())) {
                TableDeclaration actualTable = clickhouseClient.describeTable(tableDeclaration.getTableIdentity());
                clickhouseTableDeclarations.replace(entry.getKey(), mergeTableDeclaration(tableDeclaration, actualTable));
            } else {
                Engine engine;
                if (schemaOptions.isIncludePartitionColumn()) {
                    engine = new MergeTreeEngine(tableDeclaration.getColumns().get(0), tableDeclaration.getColumns().subList(1, 3));
                } else {
                    engine = new SimpleMergeTreeEngine(tableDeclaration.getColumns().subList(0, 2));
                }
                LOG.debug().append(SqlQueryHelper.getCreateTableQuery(tableDeclaration, engine, createIfNotExists)).commit();
                clickhouseClient.createTable(tableDeclaration, engine, createIfNotExists);
            }

        }
        return clickhouseTableDeclarations;
    }