Search in sources :

Example 11 with SqlScript

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

the class PostgreSQLSqlScriptSmallTest method parseSqlStatementsDo.

@Test
public void parseSqlStatementsDo() throws Exception {
    String source = new ClassPathResource("migration/dbsupport/postgresql/sql/dollar/V2__Even_more_dollars.sql", Thread.currentThread().getContextClassLoader()).loadAsString("UTF-8");
    SqlScript sqlScript = new SqlScript(source, new PostgreSQLDbSupport(null));
    List<SqlStatement> sqlStatements = sqlScript.getSqlStatements();
    assertEquals(3, sqlStatements.size());
    assertEquals(17, sqlStatements.get(0).getLineNumber());
    assertEquals(23, sqlStatements.get(1).getLineNumber());
    assertEquals(28, sqlStatements.get(2).getLineNumber());
}
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 12 with SqlScript

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

the class OracleSqlScriptSmallTest method parseProcedure.

@Test
public void parseProcedure() throws Exception {
    String source = "CREATE OR REPLACE PROCEDURE set_right_value_for_sequence(seq_name in VARCHAR2, table_name in VARCHAR2, column_id in VARCHAR2)\n" + "IS\n" + "    seq_val NUMBER(6);\n" + "    row_count NUMBER(6);\n" + "BEGIN\n" + "    EXECUTE IMMEDIATE\n" + "    'select ' || seq_name || '.nextval from dual' INTO seq_val;\n" + "\n" + "    EXECUTE IMMEDIATE\n" + "    'alter sequence  ' || seq_name || ' increment by -' || seq_val || ' minvalue 0';\n" + "\n" + "    EXECUTE IMMEDIATE\n" + "    'select ' || seq_name || '.nextval from dual' INTO seq_val;\n" + "\n" + "    EXECUTE IMMEDIATE\n" + "    'select case when max(' || column_id || ') is null then 1 else max(' || column_id || ') end from ' || table_name INTO row_count;\n" + "\n" + "    EXECUTE IMMEDIATE\n" + "    'alter sequence ' || seq_name || ' increment by ' || row_count || ' minvalue 0';\n" + "\n" + "    EXECUTE IMMEDIATE\n" + "    'select ' || seq_name || '.nextval from dual' INTO seq_val;\n" + "\n" + "    EXECUTE IMMEDIATE\n" + "    'alter sequence ' || seq_name || ' increment by 1 minvalue 1';\n" + "END;\n" + "/\n" + "\n" + "EXECUTE set_right_value_for_sequence('SEQ_ATR', 'TOTCATTRIB', 'ATTRIB_ID');";
    SqlScript sqlScript = new SqlScript(source, new OracleDbSupport(null));
    List<SqlStatement> sqlStatements = sqlScript.getSqlStatements();
    assertEquals(2, sqlStatements.size());
}
Also used : SqlStatement(org.flywaydb.core.internal.dbsupport.SqlStatement) SqlScript(org.flywaydb.core.internal.dbsupport.SqlScript) Test(org.junit.Test)

Example 13 with SqlScript

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

the class OracleSqlScriptSmallTest method parseMergeInsert.

@Test
public void parseMergeInsert() throws Exception {
    String source = "INSERT INTO ss.CODETBL(FIELDNAME,FIELDVALUE,IDS)\n" + "SELECT FIELDNAME,FIELDVALUE,IDS FROM\n" + "(\n" + "SELECT 'ACCT_TYPE_CD' FIELDNAME, '$' FIELDVALUE,'SAMP' IDS FROM DUAL UNION ALL\n" + "SELECT 'ACCT_TYPE_CD', 'L','SAMP' FROM DUAL UNION ALL\n" + "SELECT 'ACCT_TYPE_CD', 'C','SAMP' FROM DUAL \n" + ")\n" + "D\n" + "WHERE NOT EXISTS\n" + "(\n" + "SELECT 1 FROM SS.CODETBL \n" + "WHERE D.FIELDNAME = FIELDNAME \n" + "AND D.FIELDVALUE = FIELDVALUE\n" + "AND D.IDS = IDS\n" + ");";
    SqlScript sqlScript = new SqlScript(source, new OracleDbSupport(null));
    List<SqlStatement> sqlStatements = sqlScript.getSqlStatements();
    assertEquals(1, sqlStatements.size());
}
Also used : SqlStatement(org.flywaydb.core.internal.dbsupport.SqlStatement) SqlScript(org.flywaydb.core.internal.dbsupport.SqlScript) Test(org.junit.Test)

Example 14 with SqlScript

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

the class OracleSqlScriptSmallTest method parsePackages.

@Test
public void parsePackages() throws Exception {
    String source = new ClassPathResource("migration/dbsupport/oracle/sql/package/V1__Package.sql", Thread.currentThread().getContextClassLoader()).loadAsString("UTF-8");
    SqlScript sqlScript = new SqlScript(source, new OracleDbSupport(null));
    List<SqlStatement> sqlStatements = sqlScript.getSqlStatements();
    assertEquals(2, sqlStatements.size());
    assertEquals(17, sqlStatements.get(0).getLineNumber());
    assertEquals(33, sqlStatements.get(1).getLineNumber());
}
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 15 with SqlScript

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

the class MetaDataTableImpl method createIfNotExists.

/**
     * Creates the metatable if it doesn't exist, upgrades it if it does.
     */
private void createIfNotExists() {
    int retries = 0;
    while (!table.exists()) {
        if (retries == 0) {
            LOG.info("Creating Metadata table: " + table);
        }
        try {
            String resourceName = "org/flywaydb/core/internal/dbsupport/" + dbSupport.getDbName() + "/createMetaDataTable.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);
            final SqlScript sqlScript = new SqlScript(sourceNoPlaceholders, dbSupport);
            sqlScript.execute(jdbcTemplate);
            LOG.debug("Metadata table " + table + " created.");
        } catch (FlywayException e) {
            if (++retries >= 10) {
                throw e;
            }
            try {
                LOG.debug("Metadata table creation failed. Retrying in 1 sec ...");
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
            // Ignore
            }
        }
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) HashMap(java.util.HashMap) PlaceholderReplacer(org.flywaydb.core.internal.util.PlaceholderReplacer) SqlScript(org.flywaydb.core.internal.dbsupport.SqlScript) ClassPathResource(org.flywaydb.core.internal.util.scanner.classpath.ClassPathResource)

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