Search in sources :

Example 1 with SqlScript

use of org.flywaydb.core.internal.dbsupport.SqlScript 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 SqlScript

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

the class MySQLSqlScriptSmallTest method unquotedMultiLineCommentDirective.

@Test
public void unquotedMultiLineCommentDirective() throws Exception {
    String source = "INSERT INTO tablename VALUES ('a','b');\n" + "/*!50001 CREATE TABLE `viewname` (\n" + "`id` int(10) unsigned,\n" + "`name` varchar(10)\n" + ") ENGINE=MyISAM */;\n" + "INSERT INTO tablename VALUES ('a','b');";
    SqlScript sqlScript = new SqlScript(source, new MySQLDbSupport(null));
    List<SqlStatement> sqlStatements = sqlScript.getSqlStatements();
    assertEquals(3, sqlStatements.size());
    assertEquals(1, sqlStatements.get(0).getLineNumber());
    assertEquals(2, sqlStatements.get(1).getLineNumber());
    assertEquals(6, sqlStatements.get(2).getLineNumber());
}
Also used : SqlStatement(org.flywaydb.core.internal.dbsupport.SqlStatement) SqlScript(org.flywaydb.core.internal.dbsupport.SqlScript) Test(org.junit.Test)

Example 3 with SqlScript

use of org.flywaydb.core.internal.dbsupport.SqlScript 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 SqlScript

use of org.flywaydb.core.internal.dbsupport.SqlScript 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 SqlScript

use of org.flywaydb.core.internal.dbsupport.SqlScript 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

SqlScript (org.flywaydb.core.internal.dbsupport.SqlScript)26 Test (org.junit.Test)21 SqlStatement (org.flywaydb.core.internal.dbsupport.SqlStatement)19 ClassPathResource (org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource)17 SQLException (java.sql.SQLException)4 HashMap (java.util.HashMap)4 PlaceholderReplacer (org.flywaydb.core.internal.util.PlaceholderReplacer)4 FlywayException (org.flywaydb.core.api.FlywayException)3 FlywaySqlException (org.flywaydb.core.internal.dbsupport.FlywaySqlException)2 FlywaySqlScriptException (org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException)2 Schema (org.flywaydb.core.internal.dbsupport.Schema)2 Callable (java.util.concurrent.Callable)1 MigrationVersion (org.flywaydb.core.api.MigrationVersion)1 JdbcTemplate (org.flywaydb.core.internal.dbsupport.JdbcTemplate)1 TransactionTemplate (org.flywaydb.core.internal.util.jdbc.TransactionTemplate)1