提交 a72020a7 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:
上级 1331dcc7
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
*/ */
package org.h2; package org.h2;
import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.DriverPropertyInfo; import java.sql.DriverPropertyInfo;
import java.sql.SQLException; import java.sql.SQLException;
...@@ -47,7 +46,7 @@ public class Driver implements java.sql.Driver { ...@@ -47,7 +46,7 @@ public class Driver implements java.sql.Driver {
* @param info the connection properties * @param info the connection properties
* @return the new connection or null if the URL is not supported * @return the new connection or null if the URL is not supported
*/ */
public Connection connect(String url, Properties info) throws SQLException { public JdbcConnection connect(String url, Properties info) throws SQLException {
try { try {
if (info == null) { if (info == null) {
info = new Properties(); info = new Properties();
...@@ -55,10 +54,7 @@ public class Driver implements java.sql.Driver { ...@@ -55,10 +54,7 @@ public class Driver implements java.sql.Driver {
if (!acceptsURL(url)) { if (!acceptsURL(url)) {
return null; return null;
} }
Connection conn = DbUpgrade.connectOrUpgrade(url, info); DbUpgrade.upgradeIfRequired(url, info);
if (conn != null) {
return conn;
}
return new JdbcConnection(url, info); return new JdbcConnection(url, info);
} catch (Exception e) { } catch (Exception e) {
throw DbException.toSQLException(e); throw DbException.toSQLException(e);
......
...@@ -177,13 +177,11 @@ implements XADataSource, DataSource, ConnectionPoolDataSource, Serializable, Ref ...@@ -177,13 +177,11 @@ implements XADataSource, DataSource, ConnectionPoolDataSource, Serializable, Ref
Properties info = new Properties(); Properties info = new Properties();
info.setProperty("user", user); info.setProperty("user", user);
info.put("password", password); info.put("password", password);
Connection conn = Driver.load().connect(url, info); JdbcConnection conn = Driver.load().connect(url, info);
if (conn == null) { if (conn == null) {
throw new SQLException("No suitable driver found for " + url, "08001", 8001); throw new SQLException("No suitable driver found for " + url, "08001", 8001);
} else if (!(conn instanceof JdbcConnection)) {
throw new SQLException("Unsupported connection type " + conn.getClass().getName(), "08001", 8001);
} }
return (JdbcConnection) conn; return conn;
} }
/** /**
......
...@@ -44,11 +44,10 @@ public class DbUpgrade { ...@@ -44,11 +44,10 @@ public class DbUpgrade {
* *
* @param url the database URL * @param url the database URL
* @param info the properties * @param info the properties
* @return the connection if NO_UPGRADE is set
*/ */
public static Connection connectOrUpgrade(String url, Properties info) throws SQLException { public static void upgradeIfRequired(String url, Properties info) throws SQLException {
if (!upgradeClassesPresent) { if (!upgradeClassesPresent) {
return null; return;
} }
Properties i2 = new Properties(); Properties i2 = new Properties();
i2.putAll(info); i2.putAll(info);
...@@ -60,22 +59,21 @@ public class DbUpgrade { ...@@ -60,22 +59,21 @@ public class DbUpgrade {
info = i2; info = i2;
ConnectionInfo ci = new ConnectionInfo(url, info); ConnectionInfo ci = new ConnectionInfo(url, info);
if (ci.isRemote() || !ci.isPersistent()) { if (ci.isRemote() || !ci.isPersistent()) {
return null; return;
} }
String name = ci.getName(); String name = ci.getName();
if (Database.exists(name)) { if (Database.exists(name)) {
return null; return;
} }
if (!IOUtils.exists(name + ".data.db")) { if (!IOUtils.exists(name + ".data.db")) {
return null; return;
} }
synchronized (DbUpgrade.class) { synchronized (DbUpgrade.class) {
upgrade(ci, info); upgrade(ci, info);
return null; return;
} }
} }
/** /**
* The conversion script file will per default be created in the db * The conversion script file will per default be created in the db
* directory. Use this method to change the directory to the temp * directory. Use this method to change the directory to the temp
...@@ -148,18 +146,17 @@ public class DbUpgrade { ...@@ -148,18 +146,17 @@ public class DbUpgrade {
if (deleteOldDb) { if (deleteOldDb) {
IOUtils.delete(backupData); IOUtils.delete(backupData);
IOUtils.delete(backupIndex); IOUtils.delete(backupIndex);
IOUtils.delete(backupData);
FileSystem.getInstance(name).deleteRecursive(backupLobs, false); FileSystem.getInstance(name).deleteRecursive(backupLobs, false);
} }
} catch (Exception e) { } catch (Exception e) {
if (IOUtils.exists(backupData)) { if (IOUtils.exists(backupData)) {
IOUtils.rename(backupData, data); IOUtils.rename(backupData, data);
} }
if (IOUtils.exists(backupData)) { if (IOUtils.exists(backupIndex)) {
IOUtils.rename(backupData, data); IOUtils.rename(backupIndex, index);
} }
if (IOUtils.exists(backupData)) { if (IOUtils.exists(backupLobs)) {
IOUtils.rename(backupData, data); IOUtils.rename(backupLobs, lobs);
} }
IOUtils.delete(name + ".h2.db"); IOUtils.delete(name + ".h2.db");
throw DbException.toSQLException(e); throw DbException.toSQLException(e);
......
...@@ -6,11 +6,15 @@ ...@@ -6,11 +6,15 @@
*/ */
package org.h2.test.db; package org.h2.test.db;
import java.io.OutputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import org.h2.store.fs.FileSystem;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.upgrade.DbUpgrade;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.Utils; import org.h2.util.Utils;
...@@ -32,12 +36,53 @@ public class TestUpgrade extends TestBase { ...@@ -32,12 +36,53 @@ public class TestUpgrade extends TestBase {
if (!Utils.isClassPresent("org.h2.upgrade.v1_1.Driver")) { if (!Utils.isClassPresent("org.h2.upgrade.v1_1.Driver")) {
return; return;
} }
testLobs();
testErrorUpgrading();
testNoDb(); testNoDb();
testNoUpgradeOldAndNew(); testNoUpgradeOldAndNew();
testIfExists(); testIfExists();
testCipher(); testCipher();
} }
private void testLobs() throws Exception {
deleteDb("upgrade");
Connection conn;
conn = DriverManager.getConnection("jdbc:h2v1_1:" + getBaseDir() + "/upgrade;PAGE_STORE=FALSE", getUser(), getPassword());
conn.createStatement().execute("create table test(data clob) as select space(100000)");
conn.close();
assertTrue(IOUtils.exists(getBaseDir() + "/upgrade.data.db"));
assertTrue(IOUtils.exists(getBaseDir() + "/upgrade.index.db"));
DbUpgrade.setDeleteOldDb(true);
conn = getConnection("upgrade");
assertFalse(IOUtils.exists(getBaseDir() + "/upgrade.data.db"));
assertFalse(IOUtils.exists(getBaseDir() + "/upgrade.index.db"));
ResultSet rs = conn.createStatement().executeQuery("select * from test");
rs.next();
assertEquals(new String(new char[100000]).replace((char) 0, ' '), rs.getString(1));
conn.close();
deleteDb("upgrade");
}
private void testErrorUpgrading() throws Exception {
deleteDb("upgrade");
OutputStream out;
out = IOUtils.openFileOutputStream(getBaseDir() + "/upgrade.data.db", false);
out.write(new byte[10000]);
out.close();
out = IOUtils.openFileOutputStream(getBaseDir() + "/upgrade.index.db", false);
out.write(new byte[10000]);
out.close();
try {
getConnection("upgrade");
fail();
} catch (Exception e) {
// expected
}
assertTrue(IOUtils.exists(getBaseDir() + "/upgrade.data.db"));
assertTrue(IOUtils.exists(getBaseDir() + "/upgrade.index.db"));
deleteDb("upgrade");
}
private void testNoDb() throws SQLException { private void testNoDb() throws SQLException {
deleteDb("upgrade"); deleteDb("upgrade");
Connection conn = getConnection("upgrade"); Connection conn = getConnection("upgrade");
...@@ -153,6 +198,7 @@ public class TestUpgrade extends TestBase { ...@@ -153,6 +198,7 @@ public class TestUpgrade extends TestBase {
} }
IOUtils.delete(getBaseDir() + "/" + dbName + ".data.db.backup"); IOUtils.delete(getBaseDir() + "/" + dbName + ".data.db.backup");
IOUtils.delete(getBaseDir() + "/" + dbName + ".index.db.backup"); IOUtils.delete(getBaseDir() + "/" + dbName + ".index.db.backup");
FileSystem.getInstance(getBaseDir()).deleteRecursive(getBaseDir() + "/" + dbName + ".lobs.db.backup", false);
} }
} }
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论