Java Code Examples for org.h2.tools.RunScript#main()

The following examples show how to use org.h2.tools.RunScript#main() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ToolMain.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@RequiresNonNull("startupLogger")
private static void recreate(File dataDir) throws Exception {
    File backupFile = new File(dataDir, "backup.sql");
    if (backupFile.exists() && !backupFile.delete()) {
        startupLogger.warn("recreate failed: cannot delete existing backup.sql");
    }
    Script.main("-url", "jdbc:h2:" + dataDir.getPath() + File.separator + "data", "-user", "sa",
            "-script", backupFile.getPath());
    File dbFile = new File(dataDir, "data.h2.db");
    File dbBakFile = new File(dataDir, "data.h2.db.bak");
    if (dbBakFile.exists() && !dbBakFile.delete()) {
        startupLogger.warn("recreate failed, cannot delete existing file: {}",
                dbBakFile.getPath());
    }
    if (!dbFile.renameTo(dbBakFile)) {
        startupLogger.warn("recreate failed, cannot rename {} to {}", dbFile.getPath(),
                dbBakFile.getPath());
        return;
    }
    RunScript.main("-url", "jdbc:h2:" + dataDir.getPath() + File.separator + "data", "-script",
            backupFile.getPath());
    startupLogger.info("recreate succeeded");

    // clean up
    if (!dbBakFile.delete()) {
        startupLogger.info("failed to clean-up, cannot delete file: {}", dbBakFile.getPath());
    }
    if (!backupFile.delete()) {
        startupLogger.info("failed to clean-up, cannot delete file: {}", backupFile.getPath());
    }
}
 
Example 2
Source File: ToolMain.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@RequiresNonNull("startupLogger")
private static void recover(File dataDir) throws Exception {
    File recoverFile = new File(dataDir, "data.h2.sql");
    if (recoverFile.exists() && !recoverFile.delete()) {
        startupLogger.warn("recover failed: cannot delete existing data.h2.sql");
    }
    Recover.main("-dir", dataDir.getPath(), "-db", "data");
    File dbFile = new File(dataDir, "data.h2.db");
    File dbBakFile = new File(dataDir, "data.h2.db.bak");
    if (dbBakFile.exists() && !dbBakFile.delete()) {
        startupLogger.warn("recover failed, cannot delete existing file: {}",
                dbBakFile.getPath());
    }
    if (!dbFile.renameTo(dbBakFile)) {
        startupLogger.warn("recover failed, cannot rename {} to {}", dbFile.getPath(),
                dbBakFile.getPath());
        return;
    }
    RunScript.main("-url", "jdbc:h2:" + dataDir.getPath() + File.separator + "data", "-script",
            recoverFile.getPath());
    startupLogger.info("recover succeeded");

    // clean up
    if (!dbBakFile.delete()) {
        startupLogger.info("failed to clean-up, cannot delete file: {}", dbBakFile.getPath());
    }
    if (!recoverFile.delete()) {
        startupLogger.info("failed to clean-up, cannot delete file: {}", recoverFile.getPath());
    }
}
 
Example 3
Source File: ToolMain.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@RequiresNonNull("startupLogger")
private static void maskCentralData(File dataDir) throws Exception {
    File maskScriptFile = File.createTempFile("mask-central-data", ".sql");
    PrintWriter out = new PrintWriter(Files.newWriter(maskScriptFile, UTF_8));
    try {
        // mask agent ids and agent rollup ids
        out.println("update trace set headline = left(headline, position(': ', headline) + 1)"
                + " || " + applyHash("substr(headline, position(': ', headline) + 2)")
                + " where transaction_type <> 'Web' and headline like '%: %';");
        // mask query strings
        out.println("update trace set headline = left(headline, position('?', headline))"
                + " || " + applyHash("substr(headline, position('?', headline) + 1)")
                + " where transaction_type = 'Web' and headline like '%?%';");
        // mask usernames
        out.println("update trace set user = " + applyHash("user")
                + " where transaction_type = 'Web'" + " and user is not null;");
    } finally {
        out.close();
    }
    RunScript.main("-url", "jdbc:h2:" + dataDir.getPath() + File.separator + "data", "-user",
            "sa", "-script", maskScriptFile.getPath());
    if (!maskScriptFile.delete()) {
        startupLogger.info("failed to clean-up, cannot delete file: {}",
                maskScriptFile.getPath());
    }
    // re-create data file to eliminate any trace of previous values
    recover(dataDir);
}