Package ch.inftec.flyway.core

Source Code of ch.inftec.flyway.core.RepeatableCallback

package ch.inftec.flyway.core;

import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.MigrationInfo;
import org.flywaydb.core.api.callback.FlywayCallback;
import org.flywaydb.core.internal.dbsupport.DbSupport;
import org.flywaydb.core.internal.dbsupport.DbSupportFactory;
import org.flywaydb.core.internal.util.ClassUtils;
import org.flywaydb.core.internal.util.logging.Log;
import org.flywaydb.core.internal.util.logging.LogFactory;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
* Created by rotscher on 5/6/14.
*/
public class RepeatableCallback implements FlywayCallback {

    private static final Log LOG = LogFactory.getLog(RepeatableCallback.class);
    private final Flyway flyway;
    private String tmpFlywayTable;
    private Properties properties;

    public RepeatableCallback(Flyway flyway, Properties properties) throws SQLException {
        this.flyway = flyway;
        tmpFlywayTable = flyway.getTable() + "_";
        if (properties == null) {
            this.properties = new Properties();
        } else {
            this.properties = properties;
        }
    }


    @Override
    public void beforeClean(Connection connection) {


    }

    @Override
    public void afterClean(Connection connection) {

    }

    @Override
    public void beforeMigrate(Connection connection) {
        try {

            String beforeLocation = properties.getProperty("before.scripts.location");

            if (beforeLocation != null && beforeLocation.length() > 0) {
                String path = ClassUtils.getLocationOnDisk(RepeatableCallback.class);
                path = path.substring(0, path.lastIndexOf("/")) + "/..";

                File baseDir = new File(path + "/" + beforeLocation);
                SqlFileRunner runner = new SqlFileRunner(baseDir, properties.getProperty("flyway.url"),
                        properties.getProperty("before.user"), properties.getProperty("before.password"), "select * from dual");
                int filesExecuted = runner.execute();
                LOG.info(String.format("executed %d files%n", filesExecuted));
            }

            DbSupport dbSupport = DbSupportFactory.createDbSupport(connection, false);
            if (!dbSupport.getCurrentSchema().getTable(flyway.getTable()).exists()) {
                return;
            }

            String sql = "DELETE FROM " + flyway.getTable()
                + " WHERE " + dbSupport.quote("type") + "='CUSTOM'";
            connection.prepareCall(sql).execute();
        } catch (SQLException e) {
            LOG.error(e.getMessage(), e);
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        }
    }

    @Override
    public void afterMigrate(Connection connection) {
        try {
            String afterLocation = properties.getProperty("after.scripts.location");

            if (afterLocation != null && afterLocation.length() > 0) {
                String path = ClassUtils.getLocationOnDisk(RepeatableCallback.class);
                path = path.substring(0, path.lastIndexOf("/")) + "/..";

                File baseDir = new File(path + "/" + afterLocation);
                SqlFileRunner runner = new SqlFileRunner(baseDir, properties.getProperty("flyway.url"),
                        properties.getProperty("after.user"), properties.getProperty("after.password"), "select * from dual");
                int filesExecuted = runner.execute();
                LOG.info(String.format("executed %d files%n", filesExecuted));
            }
        } catch (SQLException e) {
            LOG.error(e.getMessage(), e);
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        }
    }

    @Override
    public void beforeEachMigrate(Connection connection, MigrationInfo info) {

    }

    @Override
    public void afterEachMigrate(Connection connection, MigrationInfo info) {

    }

    @Override
    public void beforeValidate(Connection connection) {
        //beforeInfo(connection);

    }

    @Override
    public void afterValidate(Connection connection) {
        //afterInfo(connection);
    }

    @Override
    public void beforeInit(Connection connection) {

    }

    @Override
    public void afterInit(Connection connection) {

    }

    @Override
    public void beforeRepair(Connection connection) {

    }

    @Override
    public void afterRepair(Connection connection) {

    }

    @Override
    public void beforeInfo(Connection connection) {
        try {
            DbSupport dbSupport = DbSupportFactory.createDbSupport(connection, false);
            if (dbSupport.getCurrentSchema().getTable(tmpFlywayTable).exists()) {
                dbSupport.getJdbcTemplate().executeStatement("DROP TABLE " + tmpFlywayTable);
            }
            String sql = "CREATE TABLE " + tmpFlywayTable + " AS SELECT * FROM " + flyway.getTable() +
                    " WHERE " + dbSupport.quote("type") + "='CUSTOM'";
            connection.prepareCall(sql).execute();

            sql = "DELETE FROM " + flyway.getTable()
                    + " WHERE " + dbSupport.quote("type") + "='CUSTOM'";
            connection.prepareCall(sql).execute();
        } catch (SQLException e) {
            LOG.error(e.getMessage(), e);
        }
    }

    @Override
    public void afterInfo(Connection connection) {
        try {
            String sql = "INSERT INTO " + flyway.getTable() +
                    " (SELECT * FROM " + tmpFlywayTable + ")";
            connection.prepareCall(sql).execute();
            connection.prepareCall("DROP TABLE " + tmpFlywayTable).execute();
        } catch (SQLException e) {
            LOG.error(e.getMessage(), e);
        }
    }
}
TOP

Related Classes of ch.inftec.flyway.core.RepeatableCallback

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.