Exemple #1
0
  /** Creates the metatable if it doesn't exist, upgrades it if it does. */
  private void createIfNotExists() {
    if (table.existsNoQuotes() || table.exists()) {
      if (!upgraded) {
        new MetaDataTableTo20FormatUpgrader(dbSupport, table, migrationResolver).upgrade();
        new MetaDataTableTo202FormatUpgrader(dbSupport, table).upgrade();
        upgraded = true;
      }
      return;
    }

    LOG.info("Creating Metadata table: " + table);

    final String source =
        new ClassPathResource(dbSupport.getScriptLocation() + "createMetaDataTable.sql")
            .loadAsString("UTF-8");

    Map<String, String> placeholders = new HashMap<String, String>();
    placeholders.put("schema", table.getSchema().getName());
    placeholders.put("table", table.getName());
    final String sourceNoPlaceholders =
        new PlaceholderReplacer(placeholders, "${", "}").replacePlaceholders(source);

    SqlScript sqlScript = new SqlScript(sourceNoPlaceholders, dbSupport);
    sqlScript.execute(jdbcTemplate);

    LOG.debug("Metadata table " + table + " created.");
  }
  @Test
  public void parseQQuotes() throws Exception {
    String source =
        new ClassPathResource("migration/dbsupport/oracle/sql/qquote/V1__Q_Quote.sql")
            .loadAsString("UTF-8");

    SqlScript sqlScript = new SqlScript(source, new OracleDbSupport(null));
    List<SqlStatement> sqlStatements = sqlScript.getSqlStatements();
    assertEquals(10, sqlStatements.size());
  }
  @Test
  public void parseSqlStatementsWithInlineCommentsInsidePlSqlBlocks() throws Exception {
    String source =
        new ClassPathResource(
                "migration/dbsupport/oracle/sql/function/V2__FunctionWithConditionals.sql")
            .loadAsString("UTF-8");

    SqlScript sqlScript = new SqlScript(source, new OracleDbSupport(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 */"));
  }
  @Test
  public void parseFunctionsAndProcedures() throws Exception {
    String source =
        new ClassPathResource("migration/dbsupport/oracle/sql/function/V1__Function.sql")
            .loadAsString("UTF-8");

    SqlScript sqlScript = new SqlScript(source, new OracleDbSupport(null));
    List<SqlStatement> sqlStatements = sqlScript.getSqlStatements();
    assertEquals(3, sqlStatements.size());
    assertEquals(17, sqlStatements.get(0).getLineNumber());
    assertEquals(26, sqlStatements.get(1).getLineNumber());
    assertEquals(34, sqlStatements.get(2).getLineNumber());
    assertEquals("COMMIT", sqlStatements.get(2).getSql());
  }
  @Test
  public void parseSqlStatements() throws Exception {
    String source =
        new ClassPathResource("migration/dbsupport/oracle/sql/placeholders/V1__Placeholders.sql")
            .loadAsString("UTF-8");

    SqlScript sqlScript = new SqlScript(source, new OracleDbSupport(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());
  }
  @Test
  public void parseCompoundTrigger() throws Exception {
    String source =
        "CREATE OR REPLACE TRIGGER triggername\n"
            + "  FOR insert ON tablename\n"
            + "    COMPOUND TRIGGER\n"
            + "\n"
            + "  -- Global declaration.\n"
            + "  g_global_variable VARCHAR2(10);\n"
            + "\n"
            + "  BEFORE STATEMENT IS\n"
            + "  BEGIN\n"
            + "    NULL; -- Do something here.\n"
            + "  END BEFORE STATEMENT;\n"
            + "\n"
            + "  BEFORE EACH ROW IS\n"
            + "  BEGIN\n"
            + "    NULL; -- Do something here.\n"
            + "  END BEFORE EACH ROW;\n"
            + "\n"
            + "  AFTER EACH ROW IS\n"
            + "  BEGIN\n"
            + "    NULL; -- Do something here.\n"
            + "  END AFTER EACH ROW;\n"
            + "\n"
            + "  AFTER STATEMENT IS\n"
            + "  BEGIN\n"
            + "    NULL; -- Do something here.\n"
            + "  END AFTER STATEMENT;\n"
            + "\n"
            + "END <trigger-name>;\n"
            + "/";

    SqlScript sqlScript = new SqlScript(source, new OracleDbSupport(null));
    List<SqlStatement> sqlStatements = sqlScript.getSqlStatements();
    assertEquals(1, sqlStatements.size());
  }