Search in sources :

Example 1 with Restore

use of org.h2.tools.Restore in project h2database by h2database.

the class TestWeb method testTools.

private void testTools() throws Exception {
    if (config.memory || config.cipher != null) {
        return;
    }
    deleteDb(getTestName());
    Connection conn = getConnection(getTestName());
    conn.createStatement().execute("create table test(id int) as select 1");
    conn.close();
    Server server = new Server();
    server.setOut(new PrintStream(new ByteArrayOutputStream()));
    server.runTool("-web", "-webPort", "8182", "-properties", "null", "-tcp", "-tcpPort", "9101");
    try {
        String url = "http://localhost:8182";
        WebClient client;
        String result;
        client = new WebClient();
        result = client.get(url);
        client.readSessionId(result);
        result = client.get(url, "tools.jsp");
        FileUtils.delete(getBaseDir() + "/backup.zip");
        result = client.get(url, "tools.do?tool=Backup&args=-dir," + getBaseDir() + ",-db," + getTestName() + ",-file," + getBaseDir() + "/backup.zip");
        deleteDb(getTestName());
        assertTrue(FileUtils.exists(getBaseDir() + "/backup.zip"));
        result = client.get(url, "tools.do?tool=DeleteDbFiles&args=-dir," + getBaseDir() + ",-db," + getTestName());
        String fn = getBaseDir() + "/" + getTestName();
        if (config.mvStore) {
            fn += Constants.SUFFIX_MV_FILE;
        } else {
            fn += Constants.SUFFIX_PAGE_FILE;
        }
        assertFalse(FileUtils.exists(fn));
        result = client.get(url, "tools.do?tool=Restore&args=-dir," + getBaseDir() + ",-db," + getTestName() + ",-file," + getBaseDir() + "/backup.zip");
        assertTrue(FileUtils.exists(fn));
        FileUtils.delete(getBaseDir() + "/web.h2.sql");
        FileUtils.delete(getBaseDir() + "/backup.zip");
        result = client.get(url, "tools.do?tool=Recover&args=-dir," + getBaseDir() + ",-db," + getTestName());
        assertTrue(FileUtils.exists(getBaseDir() + "/" + getTestName() + ".h2.sql"));
        FileUtils.delete(getBaseDir() + "/web.h2.sql");
        result = client.get(url, "tools.do?tool=RunScript&args=-script," + getBaseDir() + "/" + getTestName() + ".h2.sql,-url," + getURL(getTestName(), true) + ",-user," + getUser() + ",-password," + getPassword());
        FileUtils.delete(getBaseDir() + "/" + getTestName() + ".h2.sql");
        assertTrue(FileUtils.exists(fn));
        deleteDb(getTestName());
    } finally {
        server.shutdown();
    }
}
Also used : PrintStream(java.io.PrintStream) Server(org.h2.tools.Server) Connection(java.sql.Connection) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with Restore

use of org.h2.tools.Restore in project h2database by h2database.

the class TestGeneralCommonTableQueries method testSimple3RowRecursiveQueryWithLazyEval.

private void testSimple3RowRecursiveQueryWithLazyEval() throws Exception {
    String[] expectedRowData = new String[] { "|6" };
    String[] expectedColumnTypes = new String[] { "BIGINT" };
    String[] expectedColumnNames = new String[] { "SUM(N)" };
    // back up the config - to restore it after this test
    TestAll backupConfig = config;
    config = new TestAll();
    try {
        // Test with settings: lazy mvStore memory mvcc multiThreaded
        // connection url is
        // mem:script;MV_STORE=true;LOG=1;LOCK_TIMEOUT=50;MVCC=TRUE;
        // MULTI_THREADED=TRUE;LAZY_QUERY_EXECUTION=1
        config.lazy = true;
        config.mvStore = true;
        config.memory = true;
        config.mvcc = true;
        config.multiThreaded = true;
        String setupSQL = "--no config set";
        String withQuery = "select sum(n) from (\n" + "    with recursive r(n) as (\n" + "        (select 1) union all (select n+1 from r where n < 3) \n" + "    )\n" + "    select n from r \n" + ")\n";
        int maxRetries = 10;
        int expectedNumberOfRows = expectedRowData.length;
        testRepeatedQueryWithSetup(maxRetries, expectedRowData, expectedColumnNames, expectedNumberOfRows, setupSQL, withQuery, maxRetries - 1, expectedColumnTypes);
    } finally {
        config = backupConfig;
    }
}
Also used : TestAll(org.h2.test.TestAll)

Example 3 with Restore

use of org.h2.tools.Restore in project h2database by h2database.

the class WebApp method tools.

private String tools() {
    try {
        String toolName = (String) attributes.get("tool");
        session.put("tool", toolName);
        String args = (String) attributes.get("args");
        String[] argList = StringUtils.arraySplit(args, ',', false);
        Tool tool = null;
        if ("Backup".equals(toolName)) {
            tool = new Backup();
        } else if ("Restore".equals(toolName)) {
            tool = new Restore();
        } else if ("Recover".equals(toolName)) {
            tool = new Recover();
        } else if ("DeleteDbFiles".equals(toolName)) {
            tool = new DeleteDbFiles();
        } else if ("ChangeFileEncryption".equals(toolName)) {
            tool = new ChangeFileEncryption();
        } else if ("Script".equals(toolName)) {
            tool = new Script();
        } else if ("RunScript".equals(toolName)) {
            tool = new RunScript();
        } else if ("ConvertTraceFile".equals(toolName)) {
            tool = new ConvertTraceFile();
        } else if ("CreateCluster".equals(toolName)) {
            tool = new CreateCluster();
        } else {
            throw DbException.throwInternalError(toolName);
        }
        ByteArrayOutputStream outBuff = new ByteArrayOutputStream();
        PrintStream out = new PrintStream(outBuff, false, "UTF-8");
        tool.setOut(out);
        try {
            tool.runTool(argList);
            out.flush();
            String o = new String(outBuff.toByteArray(), StandardCharsets.UTF_8);
            String result = PageParser.escapeHtml(o);
            session.put("toolResult", result);
        } catch (Exception e) {
            session.put("toolResult", getStackTrace(0, e, true));
        }
    } catch (Exception e) {
        server.traceError(e);
    }
    return "tools.jsp";
}
Also used : ConvertTraceFile(org.h2.tools.ConvertTraceFile) RunScript(org.h2.tools.RunScript) Script(org.h2.tools.Script) PrintStream(java.io.PrintStream) Backup(org.h2.tools.Backup) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Restore(org.h2.tools.Restore) DeleteDbFiles(org.h2.tools.DeleteDbFiles) DbException(org.h2.message.DbException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException) JdbcSQLException(org.h2.jdbc.JdbcSQLException) Recover(org.h2.tools.Recover) RunScript(org.h2.tools.RunScript) Tool(org.h2.util.Tool) ChangeFileEncryption(org.h2.tools.ChangeFileEncryption) CreateCluster(org.h2.tools.CreateCluster)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintStream (java.io.PrintStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 JdbcSQLException (org.h2.jdbc.JdbcSQLException)1 DbException (org.h2.message.DbException)1 TestAll (org.h2.test.TestAll)1 Backup (org.h2.tools.Backup)1 ChangeFileEncryption (org.h2.tools.ChangeFileEncryption)1 ConvertTraceFile (org.h2.tools.ConvertTraceFile)1 CreateCluster (org.h2.tools.CreateCluster)1 DeleteDbFiles (org.h2.tools.DeleteDbFiles)1 Recover (org.h2.tools.Recover)1 Restore (org.h2.tools.Restore)1 RunScript (org.h2.tools.RunScript)1 Script (org.h2.tools.Script)1 Server (org.h2.tools.Server)1 Tool (org.h2.util.Tool)1