Search in sources :

Example 21 with Flyway

use of org.flywaydb.core.Flyway in project flyway by flyway.

the class Main method main.

/**
     * Main method.
     *
     * @param args The command-line arguments.
     */
public static void main(String[] args) {
    Level logLevel = getLogLevel(args);
    initLogging(logLevel);
    try {
        printVersion();
        if (isPrintVersionAndExit(args)) {
            System.exit(0);
        }
        List<String> operations = determineOperations(args);
        if (operations.isEmpty()) {
            printUsage();
            return;
        }
        Properties properties = new Properties();
        initializeDefaults(properties);
        loadConfiguration(properties, args);
        overrideConfiguration(properties, args);
        if (!isSuppressPrompt(args)) {
            promptForCredentialsIfMissing(properties);
        }
        dumpConfiguration(properties);
        loadJdbcDrivers();
        loadJavaMigrationsFromJarDirs(properties);
        Flyway flyway = new Flyway();
        filterProperties(properties);
        flyway.configure(properties);
        for (String operation : operations) {
            executeOperation(flyway, operation);
        }
    } catch (Exception e) {
        if (logLevel == Level.DEBUG) {
            LOG.error("Unexpected error", e);
        } else {
            if (e instanceof FlywayException) {
                LOG.error(e.getMessage());
            } else {
                LOG.error(e.toString());
            }
        }
        System.exit(1);
    }
}
Also used : Flyway(org.flywaydb.core.Flyway) FlywayException(org.flywaydb.core.api.FlywayException) Level(org.flywaydb.core.internal.util.logging.console.ConsoleLog.Level) Properties(java.util.Properties) FlywayException(org.flywaydb.core.api.FlywayException) IOException(java.io.IOException)

Example 22 with Flyway

use of org.flywaydb.core.Flyway in project che by eclipse.

the class FlywaySchemaInitializer method init.

@Override
public void init() throws SchemaInitializationException {
    try (final Connection conn = dataSource.getConnection()) {
        final Flyway flyway = new Flyway();
        flyway.setDataSource(dataSource);
        flyway.setLocations(locations);
        flyway.setClassLoader(Thread.currentThread().getContextClassLoader());
        final DbSupport dbSupport = DbSupportFactory.createDbSupport(conn, true);
        final String productName = conn.getMetaData().getDatabaseProductName().toLowerCase();
        flyway.setResolvers(new CustomSqlMigrationResolver(productName, dbSupport, placeholderReplacer));
        flyway.setSkipDefaultResolvers(true);
        flyway.setBaselineOnMigrate(baselineOnMigrate);
        if (baselineOnMigrate) {
            flyway.setBaselineVersionAsString(baselineVersion);
        }
        flyway.setSqlMigrationSeparator(versionSeparator);
        flyway.setSqlMigrationSuffix(scriptsSuffix);
        flyway.setSqlMigrationPrefix(scriptsPrefix);
        flyway.migrate();
    } catch (SQLException | RuntimeException x) {
        throw new SchemaInitializationException(x.getLocalizedMessage(), x);
    }
}
Also used : Flyway(org.flywaydb.core.Flyway) SQLException(java.sql.SQLException) Connection(java.sql.Connection) DbSupport(org.flywaydb.core.internal.dbsupport.DbSupport) SchemaInitializationException(org.eclipse.che.core.db.schema.SchemaInitializationException)

Example 23 with Flyway

use of org.flywaydb.core.Flyway in project flyway by flyway.

the class AbstractFlywayMojoSmallTest method execute.

@Test
public void execute() throws Exception {
    AbstractFlywayMojo mojo = new AbstractFlywayMojo() {

        @Override
        protected void doExecute(Flyway flyway) throws Exception {
            assertEquals(2, flyway.getSchemas().length);
            assertEquals("first", flyway.getSchemas()[0]);
            assertEquals("second", flyway.getSchemas()[1]);
        }
    };
    mojo.driver = Driver.class.getName();
    mojo.url = "jdbc:h2:mem:dummy";
    mojo.user = "sa";
    mojo.settings = new DefaultMavenSettingsBuilder().buildSettings();
    mojo.mavenProject = new MavenProject();
    mojo.mavenProject.setBasedir(new File("."));
    mojo.mavenProject.getProperties().setProperty("flyway.schemas", "first,second");
    mojo.execute();
}
Also used : Flyway(org.flywaydb.core.Flyway) MavenProject(org.apache.maven.project.MavenProject) Driver(org.h2.Driver) DefaultMavenSettingsBuilder(org.apache.maven.settings.DefaultMavenSettingsBuilder) File(java.io.File) Test(org.junit.Test)

Example 24 with Flyway

use of org.flywaydb.core.Flyway in project flyway by flyway.

the class Environment method createFlyway.

/**
     * Creates a new Flyway instance.
     *
     * @return The fully configured Flyway instance.
     */
public static Flyway createFlyway() {
    Flyway flyway = new Flyway();
    if (runningOnGoogleAppEngine()) {
        flyway.setDataSource("jdbc:google:rdbms://flyway-test-project:flywaycloudsql/flyway_cloudsql_db", null, null);
    } else {
        flyway.setDataSource("jdbc:h2:mem:flyway_db;DB_CLOSE_DELAY=-1", "sa", "");
    }
    flyway.setLocations("db.migration", "db/more/migrations", "org.flywaydb.sample.migration", "org/flywaydb/sample/webapp/migration");
    return flyway;
}
Also used : Flyway(org.flywaydb.core.Flyway)

Example 25 with Flyway

use of org.flywaydb.core.Flyway in project flyway by flyway.

the class Main method main.

/**
     * Runs the sample.
     *
     * @param args None supported.
     */
public static void main(String[] args) throws Exception {
    Flyway flyway = new Flyway();
    flyway.setDataSource("jdbc:hsqldb:file:db/flyway_sample;shutdown=true", "SA", "");
    flyway.setLocations("db/migration", "org.flywaydb.sample.migration");
    flyway.setResolvers(new SampleResolver());
    flyway.migrate();
    SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate(flyway.getDataSource());
    List<Map<String, Object>> results = jdbcTemplate.queryForList("select name from test_user");
    for (Map<String, Object> result : results) {
        System.out.println("Name: " + result.get("NAME"));
    }
}
Also used : Flyway(org.flywaydb.core.Flyway) SimpleJdbcTemplate(org.springframework.jdbc.core.simple.SimpleJdbcTemplate) SampleResolver(org.flywaydb.sample.resolver.SampleResolver) Map(java.util.Map)

Aggregations

Flyway (org.flywaydb.core.Flyway)44 Test (org.junit.Test)26 Connection (java.sql.Connection)7 DriverDataSource (org.flywaydb.core.internal.util.jdbc.DriverDataSource)6 File (java.io.File)5 SQLException (java.sql.SQLException)5 Properties (java.util.Properties)5 DataSource (javax.sql.DataSource)5 MetricRegistry (com.codahale.metrics.MetricRegistry)4 Statement (java.sql.Statement)4 FlywayException (org.flywaydb.core.api.FlywayException)4 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 MavenProject (org.apache.maven.project.MavenProject)2 MigrationInfo (org.flywaydb.core.api.MigrationInfo)2 DbSupport (org.flywaydb.core.internal.dbsupport.DbSupport)2 Before (org.junit.Before)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1