Search in sources :

Example 1 with ClassPathResource

use of org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource in project flyway by flyway.

the class MetaDataTableImpl method upgradeIfNecessary.

@Override
public boolean upgradeIfNecessary() {
    if (table.exists() && table.hasColumn("version_rank")) {
        new TransactionTemplate(jdbcTemplate.getConnection()).execute(new Callable<Object>() {

            @Override
            public Void call() {
                lock(new Callable<Object>() {

                    @Override
                    public Object call() throws Exception {
                        LOG.info("Upgrading metadata table " + table + " to the Flyway 4.0 format ...");
                        String resourceName = "org/flywaydb/core/internal/dbsupport/" + dbSupport.getDbName() + "/upgradeMetaDataTable.sql";
                        String source = new ClassPathResource(resourceName, getClass().getClassLoader()).loadAsString("UTF-8");
                        Map<String, String> placeholders = new HashMap<String, String>();
                        placeholders.put("schema", table.getSchema().getName());
                        placeholders.put("table", table.getName());
                        String sourceNoPlaceholders = new PlaceholderReplacer(placeholders, "${", "}").replacePlaceholders(source);
                        SqlScript sqlScript = new SqlScript(sourceNoPlaceholders, dbSupport);
                        sqlScript.execute(jdbcTemplate);
                        return null;
                    }
                });
                return null;
            }
        });
        return true;
    }
    return false;
}
Also used : HashMap(java.util.HashMap) TransactionTemplate(org.flywaydb.core.internal.util.jdbc.TransactionTemplate) PlaceholderReplacer(org.flywaydb.core.internal.util.PlaceholderReplacer) SqlScript(org.flywaydb.core.internal.dbsupport.SqlScript) Callable(java.util.concurrent.Callable) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource)

Example 2 with ClassPathResource

use of org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource in project flyway by flyway.

the class FlywayMediumTest method setUp.

@BeforeClass
public static void setUp() throws IOException {
    oldClassLoader = getClassLoader();
    String jar = new ClassPathResource("no-directory-entries.jar", getClassLoader()).getLocationOnDisk();
    assertTrue(new File(jar).isFile());
    ClassUtils.addJarOrDirectoryToClasspath(jar);
}
Also used : File(java.io.File) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) BeforeClass(org.junit.BeforeClass)

Example 3 with ClassPathResource

use of org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource in project flyway by flyway.

the class EnterpriseDBSqlScriptSmallTest method parseSqlStatements.

@Test
public void parseSqlStatements() throws Exception {
    String source = new ClassPathResource("migration/dbsupport/enterprisedb/sql/placeholders/V1__Placeholders.sql", Thread.currentThread().getContextClassLoader()).loadAsString("UTF-8");
    SqlScript sqlScript = new SqlScript(source, new EnterpriseDBDbSupport(null));
    List<SqlStatement> sqlStatements = sqlScript.getSqlStatements();
    assertEquals(3, sqlStatements.size());
    assertEquals(18, sqlStatements.get(0).getLineNumber());
    assertEquals(27, sqlStatements.get(1).getLineNumber());
    assertEquals(32, sqlStatements.get(2).getLineNumber());
    assertEquals("COMMIT", sqlStatements.get(2).getSql());
}
Also used : SqlStatement(org.flywaydb.core.internal.dbsupport.SqlStatement) SqlScript(org.flywaydb.core.internal.dbsupport.SqlScript) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) Test(org.junit.Test)

Example 4 with ClassPathResource

use of org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource in project flyway by flyway.

the class EnterpriseDBSqlScriptSmallTest method parseSqlStatementsWithInlineCommentsInsidePlSqlBlocks.

@Test
public void parseSqlStatementsWithInlineCommentsInsidePlSqlBlocks() throws Exception {
    String source = new ClassPathResource("migration/dbsupport/enterprisedb/sql/function/V2__FunctionWithConditionals.sql", Thread.currentThread().getContextClassLoader()).loadAsString("UTF-8");
    SqlScript sqlScript = new SqlScript(source, new EnterpriseDBDbSupport(null));
    List<SqlStatement> sqlStatements = sqlScript.getSqlStatements();
    assertEquals(1, sqlStatements.size());
    assertEquals(18, sqlStatements.get(0).getLineNumber());
    assertTrue(sqlStatements.get(0).getSql().contains("/* for the rich */"));
}
Also used : SqlStatement(org.flywaydb.core.internal.dbsupport.SqlStatement) SqlScript(org.flywaydb.core.internal.dbsupport.SqlScript) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) Test(org.junit.Test)

Example 5 with ClassPathResource

use of org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource in project flyway by flyway.

the class EnterpriseDBSqlScriptSmallTest method parseFunctionsAndProcedures.

@Test
public void parseFunctionsAndProcedures() throws Exception {
    String source = new ClassPathResource("migration/dbsupport/enterprisedb/sql/function/V1__Function.sql", Thread.currentThread().getContextClassLoader()).loadAsString("UTF-8");
    SqlScript sqlScript = new SqlScript(source, new EnterpriseDBDbSupport(null));
    List<SqlStatement> sqlStatements = sqlScript.getSqlStatements();
    assertEquals(5, sqlStatements.size());
    assertEquals(18, sqlStatements.get(0).getLineNumber());
    assertEquals(33, sqlStatements.get(1).getLineNumber());
    assertEquals(41, sqlStatements.get(2).getLineNumber());
    assertEquals(43, sqlStatements.get(3).getLineNumber());
    assertEquals(51, sqlStatements.get(4).getLineNumber());
    assertEquals("COMMIT", sqlStatements.get(4).getSql());
}
Also used : SqlStatement(org.flywaydb.core.internal.dbsupport.SqlStatement) SqlScript(org.flywaydb.core.internal.dbsupport.SqlScript) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource) Test(org.junit.Test)

Aggregations

ClassPathResource (org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource)31 Test (org.junit.Test)21 SqlScript (org.flywaydb.core.internal.dbsupport.SqlScript)17 SqlStatement (org.flywaydb.core.internal.dbsupport.SqlStatement)11 File (java.io.File)4 SQLException (java.sql.SQLException)4 HashMap (java.util.HashMap)4 PlaceholderReplacer (org.flywaydb.core.internal.util.PlaceholderReplacer)4 Resource (org.flywaydb.core.internal.util.scanner.Resource)4 ClassPathScanner (org.flywaydb.core.internal.util.scanner.classpath.ClassPathScanner)4 BeforeClass (org.junit.BeforeClass)4 FlywayException (org.flywaydb.core.api.FlywayException)3 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 FlywaySqlException (org.flywaydb.core.internal.dbsupport.FlywaySqlException)2 FlywaySqlScriptException (org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException)2 Schema (org.flywaydb.core.internal.dbsupport.Schema)2 Location (org.flywaydb.core.internal.util.Location)2 AfterClass (org.junit.AfterClass)2 InputStreamReader (java.io.InputStreamReader)1