Search in sources :

Example 1 with JdbcTemplate

use of org.flywaydb.core.internal.dbsupport.JdbcTemplate in project flyway by flyway.

the class FlywayMediumTest method callback.

@Test
public void callback() throws Exception {
    DriverDataSource dataSource = new DriverDataSource(Thread.currentThread().getContextClassLoader(), null, "jdbc:h2:mem:flyway_db_callback;DB_CLOSE_DELAY=-1", "sa", "", null, "SET AUTOCOMMIT OFF");
    Flyway flyway = new Flyway();
    flyway.setDataSource(dataSource);
    flyway.setSqlMigrationPrefix("");
    flyway.setLocations("migration/callback");
    flyway.migrate();
    assertEquals("1", flyway.info().current().getVersion().toString());
    assertEquals(MigrationState.SUCCESS, flyway.info().current().getState());
    assertEquals("Mr Callback", new JdbcTemplate(dataSource.getConnection(), 0).queryForString("SELECT name FROM test_user"));
}
Also used : DriverDataSource(org.flywaydb.core.internal.util.jdbc.DriverDataSource) JdbcTemplate(org.flywaydb.core.internal.dbsupport.JdbcTemplate) Test(org.junit.Test)

Example 2 with JdbcTemplate

use of org.flywaydb.core.internal.dbsupport.JdbcTemplate in project flyway by flyway.

the class FlywayMediumTest method baselineAfterInit.

@Test
public void baselineAfterInit() throws Exception {
    DriverDataSource dataSource = new DriverDataSource(Thread.currentThread().getContextClassLoader(), null, "jdbc:h2:mem:flyway_db_baseline_init;DB_CLOSE_DELAY=-1", "sa", "", null);
    Flyway flyway = new Flyway();
    flyway.setDataSource(dataSource);
    flyway.setSchemas("new1");
    flyway.setLocations("migration/validate");
    flyway.baseline();
    new JdbcTemplate(dataSource.getConnection(), 0).executeStatement("UPDATE \"new1\".\"schema_version\" SET \"type\"='BASELINE' WHERE \"type\"='BASELINE'");
    assertEquals("1", flyway.info().current().getVersion().toString());
    assertEquals(MigrationType.BASELINE, flyway.info().current().getType());
    flyway.baseline();
    assertEquals("1", flyway.info().current().getVersion().toString());
    assertEquals(MigrationType.BASELINE, flyway.info().current().getType());
}
Also used : DriverDataSource(org.flywaydb.core.internal.util.jdbc.DriverDataSource) JdbcTemplate(org.flywaydb.core.internal.dbsupport.JdbcTemplate) Test(org.junit.Test)

Example 3 with JdbcTemplate

use of org.flywaydb.core.internal.dbsupport.JdbcTemplate in project flyway by flyway.

the class SqlScriptFlywayCallback method execute.

private void execute(String key, Connection connection) {
    SqlScript sqlScript = scripts.get(key);
    if (sqlScript != null) {
        LOG.info("Executing SQL callback: " + key);
        sqlScript.execute(new JdbcTemplate(connection, 0));
    }
}
Also used : SqlScript(org.flywaydb.core.internal.dbsupport.SqlScript) JdbcTemplate(org.flywaydb.core.internal.dbsupport.JdbcTemplate)

Example 4 with JdbcTemplate

use of org.flywaydb.core.internal.dbsupport.JdbcTemplate in project flyway by flyway.

the class ConcurrentMigrationTestCase method migrateConcurrently.

@Test
public void migrateConcurrently() throws Exception {
    Runnable runnable = new Runnable() {

        public void run() {
            try {
                createFlyway().migrate();
            } catch (Exception e) {
                LOG.error("Migrate failed", e);
                error = e.getMessage();
            }
        }
    };
    Thread[] threads = new Thread[NUM_THREADS];
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i] = new Thread(runnable);
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i].start();
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i].join();
    }
    assertNull(error, error);
    final MigrationInfo[] applied = flyway.info().applied();
    int expected = 4;
    if (applied[0].getType() == MigrationType.SCHEMA) {
        expected++;
    }
    if (needsBaseline()) {
        expected++;
    }
    assertEquals(expected, applied.length);
    assertEquals("2.0", flyway.info().current().getVersion().toString());
    assertEquals(0, flyway.migrate());
    Connection connection = null;
    try {
        connection = concurrentMigrationDataSource.getConnection();
        assertEquals(2, new JdbcTemplate(connection, 0).queryForInt("SELECT COUNT(*) FROM " + schemaQuoted + ".test_user"));
    } finally {
        JdbcUtils.closeConnection(connection);
    }
}
Also used : MigrationInfo(org.flywaydb.core.api.MigrationInfo) Connection(java.sql.Connection) JdbcTemplate(org.flywaydb.core.internal.dbsupport.JdbcTemplate) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 5 with JdbcTemplate

use of org.flywaydb.core.internal.dbsupport.JdbcTemplate in project flyway by flyway.

the class MySQLMigrationTestCase method lockOnConnectionReUse.

/**
     * Tests whether locking problems occur when Flyway's DB connection gets reused.
     */
@Test
public void lockOnConnectionReUse() throws SQLException {
    DataSource twoConnectionsDataSource = new TwoConnectionsDataSource(flyway.getDataSource());
    flyway.setDataSource(twoConnectionsDataSource);
    flyway.setLocations(getBasedir());
    flyway.migrate();
    Connection connection1 = twoConnectionsDataSource.getConnection();
    Connection connection2 = twoConnectionsDataSource.getConnection();
    assertEquals(2, new JdbcTemplate(connection1, 0).queryForInt("SELECT COUNT(*) FROM test_user"));
    assertEquals(2, new JdbcTemplate(connection2, 0).queryForInt("SELECT COUNT(*) FROM test_user"));
}
Also used : Connection(java.sql.Connection) JdbcTemplate(org.flywaydb.core.internal.dbsupport.JdbcTemplate) SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) AbstractDataSource(org.springframework.jdbc.datasource.AbstractDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Aggregations

JdbcTemplate (org.flywaydb.core.internal.dbsupport.JdbcTemplate)5 Test (org.junit.Test)4 Connection (java.sql.Connection)2 DriverDataSource (org.flywaydb.core.internal.util.jdbc.DriverDataSource)2 SQLException (java.sql.SQLException)1 DataSource (javax.sql.DataSource)1 MigrationInfo (org.flywaydb.core.api.MigrationInfo)1 SqlScript (org.flywaydb.core.internal.dbsupport.SqlScript)1 AbstractDataSource (org.springframework.jdbc.datasource.AbstractDataSource)1 SingleConnectionDataSource (org.springframework.jdbc.datasource.SingleConnectionDataSource)1