<commit_before>package org.valuereporter.helper;

import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.FlywayException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.valuereporter.Main;
import org.valuereporter.ValuereporterException;
import org.valuereporter.ValuereporterTechnicalException;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * Delegate for Main for performing database migrations before application startup.
 */
public class DatabaseMigrationHelper {
    private static final Logger log = LoggerFactory.getLogger(DatabaseMigrationHelper.class);
    public static final String ADMIN_USERNAME = "admin.connection.username";
    public static final String ADMIN_PASSWORD = "<PASSWORD>";
    public static final String JDBC_URL = "jdbc.url";
    private Flyway flyway;
    private final Properties properties;

    public DatabaseMigrationHelper(Properties resources) {
        this.properties = resources;
        try {
//            DataSource masterDs = createMasterDataSource(resources);
            flyway = new Flyway();
            flyway.setValidateOnMigrate(false);
            String jdbcUrl = (String) properties.get(JDBC_URL);
            String adminUser = (String) properties.get(ADMIN_USERNAME);
            String adminPassword = (String) properties.get(ADMIN_PASSWORD);
            flyway.setDataSource(jdbcUrl, adminUser, adminPassword);
//            flyway.setDataSource(masterDs);
        } catch (Exception e) {
            ValuereporterTechnicalException tte = new ValuereporterTechnicalException("Error migrating the database. Please verify properties are in place " +
                    ", " + ADMIN_USERNAME + ", " + ADMIN_PASSWORD + ",admin.connection.host, admin.connection.port, admin.connection.databasename", e, StatusType.RETRY_NOT_POSSIBLE);
            Marker fatal = MarkerFactory.getMarker("FATAL");
            log.error(fatal, "No-Recoverable Error: " + tte.getMessage(), tte);
            throw tte;
        }
    }

    public void upgradeDatabase() {
        Properties configuration = null;
        try {
            configuration = new Properties();
            configuration.setProperty("flyway.validateOnMigrate", "false");
            String sqlLocations = buildSqlMigrationLocations();
            configuration.setProperty("flyway.locations", sqlLocations);
            flyway.configure(configuration);
            flyway.migrate();
        } catch (FlywayException e) {
            log.error("Failed to upgrade using Flyway. Configuration \n{}. \nReason {}. ", properties,e.getMessage(), e);
            throw new ValuereporterTechnicalException("Database upgrade failed.", e,StatusType.RETRY_NOT_POSSIBLE);
        }
    }

    private String buildSqlMigrationLocations() {
        String defaultLocations = "db/migration/mysql";
        String locations = defaultLocations;
        return locations;

    }

    public void cleanDatabase() {
        try {
            flyway.clean();
        } catch (FlywayException e) {
            throw new RuntimeException("Database upgrade failed.", e);
        }<commit_msg>upgraded to flyway 7 API initialization<commit_after>package org.valuereporter.helper;

import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.FlywayException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.valuereporter.Main;
import org.valuereporter.ValuereporterException;
import org.valuereporter.ValuereporterTechnicalException;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * Delegate for Main for performing database migrations before application startup.
 */
public class DatabaseMigrationHelper {
    private static final Logger log = LoggerFactory.getLogger(DatabaseMigrationHelper.class);
    public static final String ADMIN_USERNAME = "admin.connection.username";
    public static final String ADMIN_PASSWORD = "<PASSWORD>";
    public static final String JDBC_URL = "jdbc.url";
    private Flyway flyway;
    private final Properties properties;

    public DatabaseMigrationHelper(Properties resources) {
        this.properties = resources;
        Properties configuration = null;
        try {
//            DataSource masterDs = createMasterDataSource(resources);

            String sqlLocations = buildSqlMigrationLocations();

            String jdbcUrl = (String) properties.get(JDBC_URL);
            String adminUser = (String) properties.get(ADMIN_USERNAME);
            String adminPassword = (String) properties.get(ADMIN_PASSWORD);
            flyway = Flyway.configure().validateOnMigrate(false)
                    .baselineOnMigrate(true)
                    .locations(sqlLocations)
                    .dataSource(jdbcUrl, adminUser, adminPassword).load();
//            flyway = new Flyway();
//            flyway.setValidateOnMigrate(false);
//            flyway.setDataSource(jdbcUrl, adminUser, adminPassword);
//            flyway.setDataSource(masterDs);
        } catch (Exception e) {
            ValuereporterTechnicalException tte = new ValuereporterTechnicalException("Error migrating the database. Please verify properties are in place " +
                    ", " + ADMIN_USERNAME + ", " + ADMIN_PASSWORD + ",admin.connection.host, admin.connection.port, admin.connection.databasename", e, StatusType.RETRY_NOT_POSSIBLE);
            Marker fatal = MarkerFactory.getMarker("FATAL");
            log.error(fatal, "No-Recoverable Error: " + tte.getMessage(), tte);
            throw tte;
        }
    }

    public void upgradeDatabase() {
        Properties configuration = null;
        try {
//            configuration = new Properties();
//            configuration.setProperty("flyway.validateOnMigrate", "false");
//            String sqlLocations = buildSqlMigrationLocations();
//            configuration.setProperty("flyway.locations", sqlLocations);
//            flyway.configure(configuration);
            flyway.migrate();
        } catch (FlywayException e) {
            log.error("Failed to upgrade using Flyway. Configuration \n{}. \nReason {}. ", properties,e.getMessage(), e);
            throw new ValuereporterTechnicalException("Database upgrade failed.", e,StatusType.RETRY_NOT_POSSIBLE);
        }
    }

    private String buildSqlMigrationLocations() {
        String defaultLocations = "db/migration/mysql";
        String locations = defaultLocations;
        return locations;

    }

    public void cleanDatabase() {
        try {
            flyway.clean();
        } catch (FlywayException e) {
            throw new RuntimeException("Database upgrade failed.", e);
        }
{'fraction_non_alphanumeric': 0.09438071729345143, 'fraction_numerical': 0.0013068099317554813, 'mean_word_length': 3.0233644859813085, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4264669', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1772, 'n_words': 408}