// Path: src/main/java/uk/sky/cqlmigrate/SchemaUpdates.java
// public static final String SCHEMA_UPDATES_TABLE = "schema_updates";


import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.metadata.schema.RelationMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import static uk.sky.cqlmigrate.SchemaUpdates.SCHEMA_UPDATES_TABLE;
package uk.sky.cqlmigrate;




public class PreMigrationChecker {
    private static final Logger LOGGER = LoggerFactory.getLogger(PreMigrationChecker.class);

    private final SessionContext sessionContext;
    private final String keyspace;
    private final SchemaChecker schemaChecker;
    private final CqlPaths paths;

    public PreMigrationChecker(SessionContext sessionContext, String keyspace, SchemaChecker schemaChecker, CqlPaths paths) {
        this.sessionContext = sessionContext;
        this.keyspace = keyspace;
        this.schemaChecker = schemaChecker;
        this.paths = paths;
    }

    boolean migrationIsNeeded() {
        return !keyspaceExists() || !schemaUpdatesTableExists() || !allMigrationFilesApplied();
    }

    private boolean keyspaceExists() {
        return sessionContext.getSession().getMetadata().getKeyspace(keyspace).isPresent();
    }

    private boolean schemaUpdatesTableExists() {
        if(keyspaceExists()){
            return sessionContext.getSession().getMetadata().getKeyspace(keyspace)
// Path: src/main/java/uk/sky/cqlmigrate/SchemaUpdates.java
// public static final String SCHEMA_UPDATES_TABLE = "schema_updates";


// Path: src/main/java/uk/sky/cqlmigrate/PreMigrationChecker.java
import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.metadata.schema.RelationMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import static uk.sky.cqlmigrate.SchemaUpdates.SCHEMA_UPDATES_TABLE;

package uk.sky.cqlmigrate;




public class PreMigrationChecker {
    private static final Logger LOGGER = LoggerFactory.getLogger(PreMigrationChecker.class);

    private final SessionContext sessionContext;
    private final String keyspace;
    private final SchemaChecker schemaChecker;
    private final CqlPaths paths;

    public PreMigrationChecker(SessionContext sessionContext, String keyspace, SchemaChecker schemaChecker, CqlPaths paths) {
        this.sessionContext = sessionContext;
        this.keyspace = keyspace;
        this.schemaChecker = schemaChecker;
        this.paths = paths;
    }

    boolean migrationIsNeeded() {
        return !keyspaceExists() || !schemaUpdatesTableExists() || !allMigrationFilesApplied();
    }

    private boolean keyspaceExists() {
        return sessionContext.getSession().getMetadata().getKeyspace(keyspace).isPresent();
    }

    private boolean schemaUpdatesTableExists() {
        if(keyspaceExists()){
            return sessionContext.getSession().getMetadata().getKeyspace(keyspace)
                    .flatMap(keyspaceMetadata -> keyspaceMetadata.getTable(SCHEMA_UPDATES_TABLE)).isPresent();