提交 d1c791cd authored 作者: Thomas Mueller's avatar Thomas Mueller

The database upgrade (to upgrade from H2 version 1.1.x) has been simplified. The…

The database upgrade (to upgrade from H2 version 1.1.x) has been simplified. The setting NO_UPGRADE is no longer supported, instead use the database URL jdbc:h2v1_1:
上级 c9bc84d0
...@@ -1446,14 +1446,14 @@ renamed from ...@@ -1446,14 +1446,14 @@ renamed from
by default. Also, the temporary script will be written to the database directory instead of a temporary directory. by default. Also, the temporary script will be written to the database directory instead of a temporary directory.
Both defaults can be customized via Both defaults can be customized via
<ul> <ul>
<li><code>org.h2.upgrade.DbUpgradeNonPageStoreToCurrent.setDeleteOldDb(boolean)</code> <li><code>org.h2.upgrade.DbUpgrade.setDeleteOldDb(boolean)</code>
</li><li><code>org.h2.upgrade.DbUpgradeNonPageStoreToCurrent.setScriptInTmpDir(boolean)</code> </li><li><code>org.h2.upgrade.DbUpgrade.setScriptInTmpDir(boolean)</code>
</li></ul> </li></ul>
prior opening a database connection. prior opening a database connection.
</p> </p>
<p> <p>
Since version 1.2.140 it is possible to let the old h2 classes (v 1.2.128) connect to the database. The automatic upgrade .jar file must be present, and the url Since version 1.2.140 it is possible to let the old h2 classes (v 1.2.128) connect to the database.
must contain NO_UPGRADE=TRUE. The automatic upgrade .jar file must be present, and the URL must start with <code>jdbc:h2v1_1:</code>.
</p> </p>
<h2 id="limits_limitations">Limits and Limitations</h2> <h2 id="limits_limitations">Limits and Limitations</h2>
......
...@@ -18,7 +18,10 @@ Change Log ...@@ -18,7 +18,10 @@ Change Log
<h1>Change Log</h1> <h1>Change Log</h1>
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul><li>When starting the H2 Console, the properties file can now be completely disabled. <ul><li>The database upgrade (to upgrade from H2 version 1.1.x) has been simplified.
The setting NO_UPGRADE is no longer supported, instead use the database URL jdbc:h2v1_1:
</li><li>The feature to log all errors (system properties h2.logAllErrors and h2.logAllErrorsFile) has been removed.
</li><li>When starting the H2 Console, the properties file can now be completely disabled.
</li><li>Server.openBrowser no longer writes to System.out directly if opening the URL failed. </li><li>Server.openBrowser no longer writes to System.out directly if opening the URL failed.
</li><li>The INSERT optimizations (INSERT ... DIRECT SORTED SELECT) were not parsed correctly when using a column list. </li><li>The INSERT optimizations (INSERT ... DIRECT SORTED SELECT) were not parsed correctly when using a column list.
</li><li>H2 Console: the tables and columns are now listed for SQLite as well. </li><li>H2 Console: the tables and columns are now listed for SQLite as well.
......
...@@ -180,7 +180,7 @@ public class ConnectionInfo implements Cloneable { ...@@ -180,7 +180,7 @@ public class ConnectionInfo implements Cloneable {
* *
* @return true if it is * @return true if it is
*/ */
boolean isPersistent() { public boolean isPersistent() {
return persistent; return persistent;
} }
......
...@@ -10,14 +10,16 @@ import java.io.File; ...@@ -10,14 +10,16 @@ import java.io.File;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Collections; import java.sql.Statement;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.UUID;
import org.h2.engine.ConnectionInfo;
import org.h2.engine.Database;
import org.h2.jdbc.JdbcConnection;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.store.fs.FileSystem; import org.h2.store.fs.FileSystem;
import org.h2.store.fs.FileSystemDisk; import org.h2.upgrade.v1_1.util.StringUtils;
import org.h2.util.StringUtils; import org.h2.util.IOUtils;
import org.h2.util.Utils; import org.h2.util.Utils;
/** /**
...@@ -27,11 +29,11 @@ import org.h2.util.Utils; ...@@ -27,11 +29,11 @@ import org.h2.util.Utils;
public class DbUpgrade { public class DbUpgrade {
private static boolean upgradeClassesPresent; private static boolean upgradeClassesPresent;
private static Map<String, DbUpgradeFromVersion1> runningConversions; private static boolean scriptInTempDir;
private static boolean deleteOldDb;
static { static {
upgradeClassesPresent = Utils.isClassPresent("org.h2.upgrade.v1_1.Driver"); upgradeClassesPresent = Utils.isClassPresent("org.h2.upgrade.v1_1.Driver");
runningConversions = Collections.synchronizedMap(new Hashtable<String, DbUpgradeFromVersion1>(1));
} }
/** /**
...@@ -44,82 +46,127 @@ public class DbUpgrade { ...@@ -44,82 +46,127 @@ public class DbUpgrade {
* @param info the properties * @param info the properties
* @return the connection if NO_UPGRADE is set * @return the connection if NO_UPGRADE is set
*/ */
public static synchronized Connection connectOrUpgrade(String url, Properties info) throws SQLException { public static Connection connectOrUpgrade(String url, Properties info) throws SQLException {
if (!upgradeClassesPresent) { if (!upgradeClassesPresent) {
return null; return null;
} }
if (StringUtils.toUpperEnglish(url).indexOf(";NO_UPGRADE=TRUE") >= 0) { Properties i2 = new Properties();
url = StringUtils.replaceAllIgnoreCase(url, ";NO_UPGRADE=TRUE", ""); i2.putAll(info);
return connectWithOldVersion(url, info); // clone so that the password (if set as a char array) is not cleared
Object o = info.get("password");
if (o != null && o instanceof char[]) {
i2.put("password", StringUtils.cloneCharArray((char[]) o));
}
info = i2;
ConnectionInfo ci = new ConnectionInfo(url, info);
if (ci.isRemote() || !ci.isPersistent()) {
return null;
}
String name = ci.getName();
if (Database.exists(name)) {
return null;
}
if (!IOUtils.exists(name + ".data.db")) {
return null;
}
synchronized (DbUpgrade.class) {
upgrade(ci, info);
return null;
} }
upgrade(url, info);
return null;
} }
/** /**
* Connects to an old (version 1.1) database. * The conversion script file will per default be created in the db
* directory. Use this method to change the directory to the temp
* directory.
* *
* @param url The connection string * @param scriptInTempDir true if the conversion script should be
* @param info The connection properties * located in the temp directory.
* @return the connection via the upgrade classes
* @throws SQLException
*/ */
private static Connection connectWithOldVersion(String url, Properties info) throws SQLException { public static void setScriptInTempDir(boolean scriptInTempDir) {
try { DbUpgrade.scriptInTempDir = scriptInTempDir;
String oldStartUrlPrefix = (String) Utils.getStaticField("org.h2.upgrade.v1_1.engine.Constants.START_URL");
url = StringUtils.replaceAll(url, org.h2.engine.Constants.START_URL, oldStartUrlPrefix);
url = StringUtils.replaceAllIgnoreCase(url, ";IGNORE_UNKNOWN_SETTINGS=TRUE", "");
url = StringUtils.replaceAllIgnoreCase(url, ";IGNORE_UNKNOWN_SETTINGS=FALSE", "");
url = StringUtils.replaceAllIgnoreCase(url, ";PAGE_STORE=TRUE", "");
url += ";IGNORE_UNKNOWN_SETTINGS=TRUE";
Object ci = Utils.newInstance("org.h2.upgrade.v1_1.engine.ConnectionInfo", url, info);
boolean isRemote = (Boolean) Utils.callMethod(ci, "isRemote");
boolean isPersistent = (Boolean) Utils.callMethod(ci, "isPersistent");
String dbName = (String) Utils.callMethod(ci, "getName");
// remove stackable file systems
int colon = dbName.indexOf(':');
while (colon != -1) {
String fileSystemPrefix = dbName.substring(0, colon+1);
FileSystem fs = FileSystem.getInstance(fileSystemPrefix);
if (fs == null || fs instanceof FileSystemDisk) {
break;
}
dbName = dbName.substring(colon+1);
colon = dbName.indexOf(':');
}
File oldDataFile = new File(dbName + ".data.db");
if (!isRemote && isPersistent && oldDataFile.exists()) {
Utils.callStaticMethod("org.h2.upgrade.v1_1.Driver.load");
Connection connection = DriverManager.getConnection(url, info);
return connection;
} else {
return null;
}
} catch (Exception e) {
throw DbException.toSQLException(e);
}
} }
/** /**
* Starts the conversion if the respective classes are found. Is automatically * Old files will be renamed to .backup after a successful conversion. To
* called on connect. * delete them after the conversion, use this method with the parameter
* 'true'.
* *
* @param url The connection string * @param deleteOldDb if true, the old db files will be deleted.
* @param info The connection properties
* @throws SQLException
*/ */
private static synchronized void upgrade(String url, Properties info) throws SQLException { public static void setDeleteOldDb(boolean deleteOldDb) {
if (runningConversions.containsKey(url)) { DbUpgrade.deleteOldDb = deleteOldDb;
// do not migrate, because we are currently migrating, and this is }
// the connection where "runscript from" will be executed
return; private static void upgrade(ConnectionInfo ci, Properties info) throws SQLException {
} String name = ci.getName();
String data = name + ".data.db";
String index = name + ".index.db";
String lobs = name + ".lobs.db";
String backupData = data + ".backup";
String backupIndex = index + ".backup";
String backupLobs = lobs + ".backup";
String script = null;
try { try {
DbUpgradeFromVersion1 instance = new DbUpgradeFromVersion1(url, info); if (scriptInTempDir) {
runningConversions.put(url, instance); script = File.createTempFile("h2dbmigration", "backup.sql").getAbsolutePath();
instance.upgrade(); } else {
script = name + ".script.sql";
}
String oldUrl = "jdbc:h2v1_1:" + name + ";UNDO_LOG=0;LOG=0;LOCK_MODE=0";
String cipher = ci.getProperty("CIPHER", null);
if (cipher != null) {
oldUrl += ";CIPHER=" + cipher;
}
Connection conn = DriverManager.getConnection(oldUrl, info);
Statement stat = conn.createStatement();
String uuid = UUID.randomUUID().toString();
if (cipher != null) {
stat.execute("script to '" + script + "' cipher aes password '" + uuid + "' --hide--");
} else {
stat.execute("script to '" + script + "'");
}
conn.close();
IOUtils.rename(data, backupData);
IOUtils.rename(index, backupIndex);
if (IOUtils.exists(lobs)) {
IOUtils.rename(lobs, backupLobs);
}
ci.removeProperty("IFEXISTS", false);
conn = new JdbcConnection(ci, true);
stat = conn.createStatement();
if (cipher != null) {
stat.execute("runscript from '" + script + "' cipher aes password '" + uuid + "' --hide--");
} else {
stat.execute("runscript from '" + script + "'");
}
stat.execute("analyze");
stat.execute("shutdown compact");
stat.close();
conn.close();
if (deleteOldDb) {
IOUtils.delete(backupData);
IOUtils.delete(backupIndex);
IOUtils.delete(backupData);
FileSystem.getInstance(name).deleteRecursive(backupLobs, false);
}
} catch (Exception e) {
if (IOUtils.exists(backupData)) {
IOUtils.rename(backupData, data);
}
if (IOUtils.exists(backupData)) {
IOUtils.rename(backupData, data);
}
if (IOUtils.exists(backupData)) {
IOUtils.rename(backupData, data);
}
IOUtils.delete(name + ".h2.db");
throw DbException.toSQLException(e);
} finally { } finally {
runningConversions.remove(url); if (script != null) {
IOUtils.delete(script);
}
} }
} }
......
/*
* Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.upgrade;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.UUID;
import org.h2.message.DbException;
import org.h2.store.fs.FileSystem;
import org.h2.store.fs.FileSystemDisk;
import org.h2.util.StringUtils;
import org.h2.util.Utils;
/**
* Class to convert a 1.1 database (non page store) to the 1.2 (page store)
* format. Conversion is done via "script to" and "runscript from".
*/
public class DbUpgradeFromVersion1 {
private static boolean scriptInTempDir;
private static boolean deleteOldDb;
private String url;
private Properties info;
private boolean mustBeConverted;
private String newName;
private String newUrl;
private String oldUrl;
private File oldDataFile;
private File oldIndexFile;
private File oldLobsDir;
private File newFile;
private File backupDataFile;
private File backupIndexFile;
private File backupLobsDir;
private boolean successful;
/**
* Converts a database from a 1.1 DB (non page store) to a 1.2 DB
* (page store) format.
*
* @param url The connection string
* @param info The connection properties
* @throws SQLException if an exception occurred
*/
public DbUpgradeFromVersion1(String url, Properties info) throws SQLException {
this.url = url;
this.info = info;
init();
}
private void init() throws SQLException {
try {
newUrl = url;
newUrl = StringUtils.replaceAllIgnoreCase(newUrl, ";UNDO_LOG=1", "");
newUrl = StringUtils.replaceAllIgnoreCase(newUrl, ";UNDO_LOG=0", "");
newUrl = StringUtils.replaceAllIgnoreCase(newUrl, ";LOG=0", "");
newUrl = StringUtils.replaceAllIgnoreCase(newUrl, ";LOG=1", "");
newUrl = StringUtils.replaceAllIgnoreCase(newUrl, ";LOG=2", "");
newUrl = StringUtils.replaceAllIgnoreCase(newUrl, ";LOCK_MODE=0", "");
newUrl = StringUtils.replaceAllIgnoreCase(newUrl, ";LOCK_MODE=1", "");
newUrl = StringUtils.replaceAllIgnoreCase(newUrl, ";LOCK_MODE=2", "");
newUrl = StringUtils.replaceAllIgnoreCase(newUrl, ";LOCK_MODE=3", "");
newUrl = StringUtils.replaceAllIgnoreCase(newUrl, ";IFEXISTS=TRUE", "");
newUrl += ";UNDO_LOG=0;LOG=0;LOCK_MODE=0";
String oldStartUrlPrefix = (String) Utils.getStaticField("org.h2.upgrade.v1_1.engine.Constants.START_URL");
oldUrl = url;
oldUrl = StringUtils.replaceAll(oldUrl, org.h2.engine.Constants.START_URL, oldStartUrlPrefix);
oldUrl = StringUtils.replaceAllIgnoreCase(oldUrl, ";IGNORE_UNKNOWN_SETTINGS=TRUE", "");
oldUrl = StringUtils.replaceAllIgnoreCase(oldUrl, ";IGNORE_UNKNOWN_SETTINGS=FALSE", "");
oldUrl = StringUtils.replaceAllIgnoreCase(oldUrl, ";PAGE_STORE=TRUE", "");
oldUrl += ";IGNORE_UNKNOWN_SETTINGS=TRUE";
Object ci = Utils.newInstance("org.h2.upgrade.v1_1.engine.ConnectionInfo", oldUrl, info);
boolean isRemote = (Boolean) Utils.callMethod(ci, "isRemote");
boolean isPersistent = (Boolean) Utils.callMethod(ci, "isPersistent");
String dbName = (String) Utils.callMethod(ci, "getName");
// remove stackable file systems
int colon = dbName.indexOf(':');
while (colon != -1) {
String fileSystemPrefix = dbName.substring(0, colon+1);
FileSystem fs = FileSystem.getInstance(fileSystemPrefix);
if (fs == null || fs instanceof FileSystemDisk) {
break;
}
dbName = dbName.substring(colon+1);
colon = dbName.indexOf(':');
}
if (!isRemote && isPersistent) {
String oldDataName = dbName + ".data.db";
String oldIndexName = dbName + ".index.db";
String oldLobsName = dbName + ".lobs.db";
newName = dbName + ".h2.db";
oldDataFile = new File(oldDataName).getAbsoluteFile();
oldIndexFile = new File(oldIndexName).getAbsoluteFile();
oldLobsDir = new File(oldLobsName).getAbsoluteFile();
newFile = new File(newName).getAbsoluteFile();
backupDataFile = new File(oldDataFile.getAbsolutePath() + ".backup");
backupIndexFile = new File(oldIndexFile.getAbsolutePath() + ".backup");
backupLobsDir = new File(oldLobsDir.getAbsolutePath() + ".backup");
mustBeConverted = oldDataFile.exists() && !newFile.exists();
}
} catch (Exception e) {
throw DbException.toSQLException(e);
}
}
/**
* Returns if a database must be converted by this class.
*
* @return if the conversion classes were found and the database must be
* converted
* @throws SQLException
*/
public boolean mustBeConverted() throws SQLException {
return mustBeConverted;
}
/**
* Converts the database from 1.1 (non page store) to current (page store).
*
* @throws SQLException
*/
public void upgrade() throws SQLException {
successful = true;
if (!mustBeConverted) {
return;
}
File scriptFile = null;
try {
if (scriptInTempDir) {
scriptFile = File.createTempFile("h2dbmigration", "backup.sql");
} else {
scriptFile = new File(oldDataFile.getAbsolutePath() + "_script.sql");
}
Utils.callStaticMethod("org.h2.upgrade.v1_1.Driver.load");
Connection connection = DriverManager.getConnection(oldUrl, info);
Statement stmt = connection.createStatement();
boolean isEncrypted = StringUtils.toUpperEnglish(url).indexOf(";CIPHER=") >= 0;
String uuid = UUID.randomUUID().toString();
if (isEncrypted) {
stmt.execute("script to '" + scriptFile + "' CIPHER AES PASSWORD '" + uuid + "' --hide--");
} else {
stmt.execute("script to '" + scriptFile + "'");
}
stmt.close();
connection.close();
oldDataFile.renameTo(backupDataFile);
oldIndexFile.renameTo(backupIndexFile);
oldLobsDir.renameTo(backupLobsDir);
connection = DriverManager.getConnection(newUrl, info);
stmt = connection.createStatement();
if (isEncrypted) {
stmt.execute("runscript from '" + scriptFile + "' CIPHER AES PASSWORD '" + uuid + "' --hide--");
} else {
stmt.execute("runscript from '" + scriptFile + "'");
}
stmt.execute("analyze");
stmt.execute("shutdown compact");
stmt.close();
connection.close();
if (deleteOldDb) {
backupDataFile.delete();
backupIndexFile.delete();
FileSystem.getInstance(backupLobsDir.getAbsolutePath()).deleteRecursive(backupLobsDir.getAbsolutePath(), false);
}
} catch (Exception e) {
successful = false;
if (backupDataFile.exists()) {
backupDataFile.renameTo(oldDataFile);
}
if (backupIndexFile.exists()) {
backupIndexFile.renameTo(oldIndexFile);
}
if (backupLobsDir.exists()) {
backupLobsDir.renameTo(oldLobsDir);
}
newFile.delete();
throw DbException.toSQLException(e);
} finally {
if (scriptFile != null) {
scriptFile.delete();
}
}
}
/**
* Returns if the database upgrade was successful.
*
* @return if the database upgrade was successful
*/
public boolean wasSuccessful() {
return successful;
}
/**
* The conversion script file will per default be created in the db
* directory. Use this method to change the directory to the temp
* directory.
*
* @param scriptInTempDir true if the conversion script should be
* located in the temp directory.
*/
public static void setScriptInTempDir(boolean scriptInTempDir) {
DbUpgradeFromVersion1.scriptInTempDir = scriptInTempDir;
}
/**
* Old files will be renamed to .backup after a successful conversion. To
* delete them after the conversion, use this method with the parameter
* 'true'.
*
* @param deleteOldDb if true, the old db files will be deleted.
*/
public static void setDeleteOldDb(boolean deleteOldDb) {
DbUpgradeFromVersion1.deleteOldDb = deleteOldDb;
}
}
...@@ -715,24 +715,7 @@ public class StringUtils { ...@@ -715,24 +715,7 @@ public class StringUtils {
* @return the string with the before string replaced * @return the string with the before string replaced
*/ */
public static String replaceAll(String s, String before, String after) { public static String replaceAll(String s, String before, String after) {
return replaceAll(s, s, before, after); int next = s.indexOf(before);
}
/**
* Replace all occurrences of the "before" string with the "after" string.
* A case-insensitive comparison is made.
*
* @param s the string
* @param before the old text
* @param after the new text
* @return the string with the before string replaced
*/
public static String replaceAllIgnoreCase(String s, String before, String after) {
return replaceAll(s, toUpperEnglish(s), toUpperEnglish(before), after);
}
private static String replaceAll(String s, String test, String before, String after) {
int next = test.indexOf(before);
if (next < 0) { if (next < 0) {
return s; return s;
} }
...@@ -741,7 +724,7 @@ public class StringUtils { ...@@ -741,7 +724,7 @@ public class StringUtils {
while (true) { while (true) {
buff.append(s.substring(index, next)).append(after); buff.append(s.substring(index, next)).append(after);
index = next + before.length(); index = next + before.length();
next = test.indexOf(before, index); next = s.indexOf(before, index);
if (next < 0) { if (next < 0) {
buff.append(s.substring(index)); buff.append(s.substring(index));
break; break;
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
*/ */
package org.h2.test.db; package org.h2.test.db;
import java.io.File;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
...@@ -43,12 +42,15 @@ public class TestUpgrade extends TestBase { ...@@ -43,12 +42,15 @@ public class TestUpgrade extends TestBase {
deleteDb("upgrade"); deleteDb("upgrade");
Connection conn = getConnection("upgrade"); Connection conn = getConnection("upgrade");
conn.close(); conn.close();
assertTrue(new File("data/test/upgrade.h2.db").exists()); assertTrue(IOUtils.exists(getBaseDir() + "/upgrade.h2.db"));
deleteDb("upgrade"); deleteDb("upgrade");
conn = getConnection("upgrade;NO_UPGRADE=TRUE"); conn = getConnection("jdbc:h2v1_1:" + getBaseDir() + "/upgrade");
conn.close(); conn.close();
assertTrue(new File("data/test/upgrade.h2.db").exists());
conn = getConnection("upgrade");
conn.close();
assertTrue(IOUtils.exists(getBaseDir() + "/upgrade.h2.db"));
deleteDb("upgrade"); deleteDb("upgrade");
} }
...@@ -59,49 +61,39 @@ public class TestUpgrade extends TestBase { ...@@ -59,49 +61,39 @@ public class TestUpgrade extends TestBase {
// Create old db // Create old db
Utils.callStaticMethod("org.h2.upgrade.v1_1.Driver.load"); Utils.callStaticMethod("org.h2.upgrade.v1_1.Driver.load");
Connection connOld = DriverManager.getConnection("jdbc:h2v1_1:data/test/upgradeOld;PAGE_STORE=FALSE" + additionalParameters); Connection connOld = DriverManager.getConnection("jdbc:h2v1_1:" + getBaseDir() + "/upgradeOld;PAGE_STORE=FALSE" + additionalParameters);
// Test auto server, too // Test auto server, too
Connection connOld2 = DriverManager.getConnection("jdbc:h2v1_1:data/test/upgradeOld;PAGE_STORE=FALSE" + additionalParameters); Connection connOld2 = DriverManager.getConnection("jdbc:h2v1_1:" + getBaseDir() + "/upgradeOld;PAGE_STORE=FALSE" + additionalParameters);
Statement statOld = connOld.createStatement(); Statement statOld = connOld.createStatement();
statOld.execute("create table testOld(id int)"); statOld.execute("create table testOld(id int)");
connOld.close(); connOld.close();
connOld2.close(); connOld2.close();
Utils.callStaticMethod("org.h2.upgrade.v1_1.Driver.unload"); assertTrue(IOUtils.exists(getBaseDir() + "/upgradeOld.data.db"));
assertTrue(new File("data/test/upgradeOld.data.db").exists());
// Connect to old DB without upgrade
Connection connOldViaNew = DriverManager.getConnection("jdbc:h2:data/test/upgradeOld;NO_UPGRADE=TRUE" + additionalParameters);
// Test auto server, too
Connection connOldViaNew2 = DriverManager.getConnection("jdbc:h2:data/test/upgradeOld;NO_UPGRADE=TRUE" + additionalParameters);
Statement statOldViaNew = connOldViaNew.createStatement();
statOldViaNew.executeQuery("select * from testOld");
connOldViaNew.close();
connOldViaNew2.close();
assertTrue(new File("data/test/upgradeOld.data.db").exists());
// Create new DB // Create new DB
Connection connNew = DriverManager.getConnection("jdbc:h2:data/test/upgrade" + additionalParameters); Connection connNew = DriverManager.getConnection("jdbc:h2:" + getBaseDir() + "/upgrade" + additionalParameters);
Connection connNew2 = DriverManager.getConnection("jdbc:h2:data/test/upgrade" + additionalParameters); Connection connNew2 = DriverManager.getConnection("jdbc:h2:" + getBaseDir() + "/upgrade" + additionalParameters);
Statement statNew = connNew.createStatement(); Statement statNew = connNew.createStatement();
statNew.execute("create table test(id int)"); statNew.execute("create table test(id int)");
// Link to old DB without upgrade // Link to old DB without upgrade
statNew.executeUpdate("CREATE LOCAL TEMPORARY LINKED TABLE linkedTestOld('org.h2.Driver', 'jdbc:h2:data/test/upgradeOld;NO_UPGRADE=TRUE" + additionalParameters + "', '', '', 'TestOld')"); statNew.executeUpdate("CREATE LOCAL TEMPORARY LINKED TABLE linkedTestOld('org.h2.Driver', 'jdbc:h2v1_1:" + getBaseDir() + "/upgradeOld" + additionalParameters + "', '', '', 'TestOld')");
statNew.executeQuery("select * from linkedTestOld"); statNew.executeQuery("select * from linkedTestOld");
connNew.close(); connNew.close();
connNew2.close(); connNew2.close();
assertTrue(new File("data/test/upgradeOld.data.db").exists()); assertTrue(IOUtils.exists(getBaseDir() + "/upgradeOld.data.db"));
assertTrue(new File("data/test/upgrade.h2.db").exists()); assertTrue(IOUtils.exists(getBaseDir() + "/upgrade.h2.db"));
connNew = DriverManager.getConnection("jdbc:h2:data/test/upgrade" + additionalParameters); connNew = DriverManager.getConnection("jdbc:h2:" + getBaseDir() + "/upgrade" + additionalParameters);
connNew2 = DriverManager.getConnection("jdbc:h2:data/test/upgrade" + additionalParameters); connNew2 = DriverManager.getConnection("jdbc:h2:" + getBaseDir() + "/upgrade" + additionalParameters);
statNew = connNew.createStatement(); statNew = connNew.createStatement();
// Link to old DB with upgrade // Link to old DB with upgrade
statNew.executeUpdate("CREATE LOCAL TEMPORARY LINKED TABLE linkedTestOld('org.h2.Driver', 'jdbc:h2:data/test/upgradeOld" + additionalParameters + "', '', '', 'TestOld')"); statNew.executeUpdate("CREATE LOCAL TEMPORARY LINKED TABLE linkedTestOld('org.h2.Driver', 'jdbc:h2:" + getBaseDir() + "/upgradeOld" + additionalParameters + "', '', '', 'TestOld')");
statNew.executeQuery("select * from linkedTestOld"); statNew.executeQuery("select * from linkedTestOld");
connNew.close(); connNew.close();
connNew2.close(); connNew2.close();
assertTrue(new File("data/test/upgradeOld.h2.db").exists()); assertTrue(IOUtils.exists(getBaseDir() + "/upgradeOld.h2.db"));
assertTrue(new File("data/test/upgrade.h2.db").exists()); assertTrue(IOUtils.exists(getBaseDir() + "/upgrade.h2.db"));
deleteDb("upgrade"); deleteDb("upgrade");
deleteDb("upgradeOld"); deleteDb("upgradeOld");
...@@ -110,24 +102,23 @@ public class TestUpgrade extends TestBase { ...@@ -110,24 +102,23 @@ public class TestUpgrade extends TestBase {
private void testIfExists() throws Exception { private void testIfExists() throws Exception {
deleteDb("upgrade"); deleteDb("upgrade");
// Create old db // Create old
Utils.callStaticMethod("org.h2.upgrade.v1_1.Driver.load"); Utils.callStaticMethod("org.h2.upgrade.v1_1.Driver.load");
Connection connOld = DriverManager.getConnection("jdbc:h2v1_1:data/test/upgrade;PAGE_STORE=FALSE"); Connection connOld = DriverManager.getConnection("jdbc:h2v1_1:" + getBaseDir() + "/upgrade;PAGE_STORE=FALSE");
// Test auto server, too // Test auto server, too
Connection connOld2 = DriverManager.getConnection("jdbc:h2v1_1:data/test/upgrade;PAGE_STORE=FALSE"); Connection connOld2 = DriverManager.getConnection("jdbc:h2v1_1:" + getBaseDir() + "/upgrade;PAGE_STORE=FALSE");
Statement statOld = connOld.createStatement(); Statement statOld = connOld.createStatement();
statOld.execute("create table test(id int)"); statOld.execute("create table test(id int)");
connOld.close(); connOld.close();
connOld2.close(); connOld2.close();
Utils.callStaticMethod("org.h2.upgrade.v1_1.Driver.unload"); assertTrue(IOUtils.exists(getBaseDir() + "/upgrade.data.db"));
assertTrue(new File("data/test/upgrade.data.db").exists());
// Connect to old DB with upgrade // Upgrade
Connection connOldViaNew = DriverManager.getConnection("jdbc:h2:data/test/upgrade;ifexists=true"); Connection connOldViaNew = DriverManager.getConnection("jdbc:h2:" + getBaseDir() + "/upgrade;ifexists=true");
Statement statOldViaNew = connOldViaNew.createStatement(); Statement statOldViaNew = connOldViaNew.createStatement();
statOldViaNew.executeQuery("select * from test"); statOldViaNew.executeQuery("select * from test");
connOldViaNew.close(); connOldViaNew.close();
assertTrue(new File("data/test/upgrade.h2.db").exists()); assertTrue(IOUtils.exists(getBaseDir() + "/upgrade.h2.db"));
deleteDb("upgrade"); deleteDb("upgrade");
} }
...@@ -137,19 +128,18 @@ public class TestUpgrade extends TestBase { ...@@ -137,19 +128,18 @@ public class TestUpgrade extends TestBase {
// Create old db // Create old db
Utils.callStaticMethod("org.h2.upgrade.v1_1.Driver.load"); Utils.callStaticMethod("org.h2.upgrade.v1_1.Driver.load");
Connection conn = DriverManager.getConnection("jdbc:h2v1_1:data/test/upgrade;PAGE_STORE=FALSE;CIPHER=AES", "abc", "abc abc"); Connection conn = DriverManager.getConnection("jdbc:h2v1_1:" + getBaseDir() + "/upgrade;PAGE_STORE=FALSE;CIPHER=AES", "abc", "abc abc");
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
stat.execute("create table test(id int)"); stat.execute("create table test(id int)");
conn.close(); conn.close();
Utils.callStaticMethod("org.h2.upgrade.v1_1.Driver.unload"); assertTrue(IOUtils.exists(getBaseDir() + "/upgrade.data.db"));
assertTrue(new File("data/test/upgrade.data.db").exists());
// Connect to old DB with upgrade // Connect to old DB with upgrade
conn = DriverManager.getConnection("jdbc:h2:data/test/upgrade;CIPHER=AES", "abc", "abc abc"); conn = DriverManager.getConnection("jdbc:h2:" + getBaseDir() + "/upgrade;CIPHER=AES", "abc", "abc abc");
stat = conn.createStatement(); stat = conn.createStatement();
stat.executeQuery("select * from test"); stat.executeQuery("select * from test");
conn.close(); conn.close();
assertTrue(new File("data/test/upgrade.h2.db").exists()); assertTrue(IOUtils.exists(getBaseDir() + "/upgrade.h2.db"));
deleteDb("upgrade"); deleteDb("upgrade");
} }
...@@ -157,11 +147,12 @@ public class TestUpgrade extends TestBase { ...@@ -157,11 +147,12 @@ public class TestUpgrade extends TestBase {
public void deleteDb(String dbName) throws SQLException { public void deleteDb(String dbName) throws SQLException {
super.deleteDb(dbName); super.deleteDb(dbName);
try { try {
Utils.callStaticMethod("org.h2.upgrade.v1_1.tools.DeleteDbFiles.execute", "data/test", dbName, true); Utils.callStaticMethod("org.h2.upgrade.v1_1.tools.DeleteDbFiles.execute", getBaseDir(), dbName, true);
} catch (Exception e) { } catch (Exception e) {
throw new SQLException(e.getMessage()); throw new SQLException(e.getMessage());
} }
IOUtils.delete("data/test/" + dbName + ".data.db.backup"); IOUtils.delete(getBaseDir() + "/" + dbName + ".data.db.backup");
IOUtils.delete("data/test/" + dbName + ".index.db.backup"); IOUtils.delete(getBaseDir() + "/" + dbName + ".index.db.backup");
} }
} }
\ No newline at end of file
...@@ -178,10 +178,6 @@ public class TestStringUtils extends TestBase { ...@@ -178,10 +178,6 @@ public class TestStringUtils extends TestBase {
assertEquals("abc def", StringUtils.replaceAll("abc def", "xyz", "abc")); assertEquals("abc def", StringUtils.replaceAll("abc def", "xyz", "abc"));
assertEquals("", StringUtils.replaceAll("abcabcabc", "abc", "")); assertEquals("", StringUtils.replaceAll("abcabcabc", "abc", ""));
assertEquals("abcabcabc", StringUtils.replaceAll("abcabcabc", "aBc", "")); assertEquals("abcabcabc", StringUtils.replaceAll("abcabcabc", "aBc", ""));
assertEquals("d", StringUtils.replaceAllIgnoreCase("abcd", "aBc", ""));
assertEquals("d", StringUtils.replaceAllIgnoreCase("abcd", "abc", ""));
assertEquals("D", StringUtils.replaceAllIgnoreCase("abcD", "aBc", ""));
assertEquals("D", StringUtils.replaceAllIgnoreCase("abcD", "abc", ""));
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论