/** * Migrate a database. * * @param file the database file (must end with .data.db) or directory * @param recursive if the file parameter is in fact a directory (in which case the directory is * scanned recursively) * @param user the user name of the database * @param password the password * @param runQuiet to run in quiet mode * @throws Exception if conversion fails */ public void execute(File file, boolean recursive, String user, String password, boolean runQuiet) throws Exception { String pathToJavaExe = getJavaExecutablePath(); this.quiet = runQuiet; if (file.isDirectory() && recursive) { for (File f : file.listFiles()) { execute(f, recursive, user, password, runQuiet); } return; } if (!file.getName().endsWith(".data.db")) { return; } LOGGER.info( "Migrating the database at " + file.getAbsolutePath() + " to the new format. This might take a while based on the size of your database."); String fileNameWithoutExtension = truncateFileExtension(file.getAbsolutePath()); File newDatabaseFile = new File(fileNameWithoutExtension + ".h2.db"); if (newDatabaseFile.exists()) { LOGGER.info("Removing " + newDatabaseFile.getAbsolutePath() + " [the new database file]"); FileUtils.deleteQuietly(newDatabaseFile); } if (!OLD_H2_FILE.exists()) { throw new IllegalStateException( String.format( "h2 file %s not found, migration could not be completed successfully", OLD_H2_FILE.getAbsolutePath())); } String url = "jdbc:h2:" + fileNameWithoutExtension; exec( new String[] { pathToJavaExe, "-Xmx" + MAX_MEMORY_FOR_MIGRATION, "-cp", OLD_H2_FILE.getAbsolutePath(), "org.h2.tools.Script", "-script", TEMP_SCRIPT, "-url", url, "-user", user }); file.renameTo(new File(file.getAbsoluteFile() + ".backup")); RunScript.execute(url, user, password, TEMP_SCRIPT, "UTF-8", true); new File(TEMP_SCRIPT).delete(); LOGGER.info("Migrating the h2db to the new format is complete."); }
public void compact() { LOGGER.info("Compacting database..."); PMS.get().getFrame().setStatusLine(Messages.getString("DLNAMediaDatabase.3")); String filename = "database/backup.sql"; try { Script.execute(url, "sa", "", filename); DeleteDbFiles.execute(dbDir, dbName, true); RunScript.execute(url, "sa", "", filename, null, false); } catch (SQLException se) { LOGGER.error("Error in compacting database: ", se); } finally { File testsql = new File(filename); if (testsql.exists() && !testsql.delete()) { testsql.deleteOnExit(); } } PMS.get().getFrame().setStatusLine(null); }
/** * This method is called when executing this sample application from the command line. * * @param args the command line parameters */ public static void main(String... args) throws Exception { String targetDir = args.length == 0 ? "." : args[0]; Class.forName("org.h2.Driver"); Connection conn = DriverManager.getConnection("jdbc:h2:mem:", "sa", ""); InputStream in = Newsfeed.class.getResourceAsStream("newsfeed.sql"); ResultSet rs = RunScript.execute(conn, new InputStreamReader(in, "ISO-8859-1")); in.close(); while (rs.next()) { String file = rs.getString("FILE"); String content = rs.getString("CONTENT"); if (file.endsWith(".txt")) { content = convertHtml2Text(content); } new File(targetDir).mkdirs(); FileOutputStream out = new FileOutputStream(targetDir + "/" + file); Writer writer = new OutputStreamWriter(out, "UTF-8"); writer.write(content); writer.close(); out.close(); } conn.close(); }
private void initTestData() throws SQLException, FileNotFoundException { RunScript.execute(pds.getConnection(), new FileReader("src/file/customer-schema-h2.sql")); }
private static void initTestData(DataSource ds) throws FileNotFoundException, SQLException { RunScript.execute(ds.getConnection(), new FileReader("metadata/customer-schema-h2.sql")); }