Package ch.inftec.flyway.core

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

package ch.inftec.flyway.core;

import org.flywaydb.core.internal.dbsupport.DbSupport;
import org.flywaydb.core.internal.dbsupport.DbSupportFactory;
import org.flywaydb.core.internal.dbsupport.SqlScript;
import org.flywaydb.core.internal.util.ClassUtils;
import org.flywaydb.core.internal.util.jdbc.DriverDataSource;
import org.flywaydb.core.internal.util.scanner.Resource;
import org.flywaydb.core.internal.util.scanner.filesystem.FileSystemResource;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
* Created by rotscher on 7/2/14.
*/
public class SqlFileRunner {
    private final File baseDirectory;
    private final DbSupport support;

    public SqlFileRunner(File baseDirectory, String url, String user, String password, String... initSql)
            throws IOException, SQLException {
        this.baseDirectory = baseDirectory;

        if (this.baseDirectory == null || !this.baseDirectory.exists() || !this.baseDirectory.isDirectory()) {
            throw new IOException(String.format("directory %s should exists and be a directory", this.baseDirectory));
        }

        // used for Flyway 3.0:
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        DataSource ds = new DriverDataSource(classLoader, null, url, user, password, initSql);
        support = DbSupportFactory.createDbSupport(ds.getConnection(), true);

    }

    public int execute() {
        try {
            int filesExecuted = 0;
            List<File> sqlFiles = new ArrayList<File>();
            collectSqlFiles(baseDirectory, sqlFiles);

            for (File sqlFile : sqlFiles) {
                Resource resource = new FileSystemResource(sqlFile.getAbsolutePath());
                SqlScript script = new SqlScript(resource.loadAsString("UTF-8"), support);
                System.out.format("execute sql file: %s%n", sqlFile.getAbsolutePath());
                script.execute(support.getJdbcTemplate());
                filesExecuted++;
            }

            return filesExecuted;
        } finally {
            try {
                support.getJdbcTemplate().getConnection().close();
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    void collectSqlFiles(File directory, List<File> collectedFiles) {

        for (String fileName : directory.list()) {
            File file = new File(directory, fileName);
            if (file.isDirectory()) {
                collectSqlFiles(file, collectedFiles);
            } else {
                if (fileName.endsWith(".sql")) {
                    collectedFiles.add(file);
                }
            }
        }
    }
}
TOP

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

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.