/*
 * Copyright (C) 2016-2020 the original author or authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package space.npstr.wolfia.db;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import net.ttddyy.dsproxy.support.ProxyDataSource;
import net.ttddyy.dsproxy.support.ProxyDataSourceBuilder;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.configuration.FluentConfiguration;
import org.jooq.exception.DataAccessException;

import javax.sql.DataSource;

public class DatabaseConnection {

    private final HikariDataSource hikariDataSource;
    private final ProxyDataSource proxiedDataSource;

    /**
     * @throws DataAccessException if the connection could not be created due to [reasons]
     */
    public DatabaseConnection(final HikariConfig hikariConfig,
                              final ProxyDataSourceBuilder proxyDataSourceBuilder,
                              final FluentConfiguration flywayConfig) {

        try {
            this.hikariDataSource = new HikariDataSource(hikariConfig);

            flywayConfig.dataSource(this.hikariDataSource);
            new Flyway(flywayConfig).migrate();

            this.proxiedDataSource = proxyDataSourceBuilder
                    .dataSource(this.hikariDataSource)
                    .build();
        } catch (final Exception e) {
            throw new DataAccessException("Failed to create database connection", e);
        }
    }

    public DataSource getDataSource() {
        return this.proxiedDataSource;
    }

    public void shutdown() {
        this.hikariDataSource.close();
    }
}

@@ -835,32 +835,61 @@
 kariDataSource;%0A
+import javax.sql.DataSource;%0A
 import net.ttddy
@@ -1130,38 +1130,8 @@
 n;%0A%0A
-import javax.sql.DataSource;%0A%0A
 publ
@@ -1752,16 +1752,32 @@
         
+ Flyway flyway =
  new Fly
@@ -1793,16 +1793,65 @@
 yConfig)
+;%0A            flyway.repair();%0A            flyway
 .migrate

Repair migrations