提交 c1022e87 authored 作者: noelgrandin's avatar noelgrandin

Fix DROP ALL OBJECTS and DROP SCHEMA in the presence of tables with computed column dependencies.

上级 16abdf2f
......@@ -64,11 +64,23 @@ public class DropDatabase extends DefineCommand {
db.removeSchemaObject(session, t);
}
}
for (Table t : tables) {
if (t.getName() != null && Table.TABLE.equals(t.getTableType()) && !t.isHidden()) {
db.removeSchemaObject(session, t);
// There can be dependencies between tables e.g. using computed columns,
// so we might need to loop over them multiple times.
boolean runLoopAgain = false;
do {
runLoopAgain = false;
for (Table t : tables) {
if (t.getName() != null && Table.TABLE.equals(t.getTableType()) && !t.isHidden()) {
if (db.getDependentTable(t, t) == null) {
db.removeSchemaObject(session, t);
} else {
runLoopAgain = true;
}
}
}
}
} while (runLoopAgain);
for (Table t : tables) {
if (t.getName() != null && Table.EXTERNAL_TABLE_ENGINE.equals(t.getTableType()) && !t.isHidden()) {
db.removeSchemaObject(session, t);
......
......@@ -120,10 +120,30 @@ public class Schema extends DbObjectBase {
Constraint obj = (Constraint) constraints.values().toArray()[0];
database.removeSchemaObject(session, obj);
}
while (tablesAndViews != null && tablesAndViews.size() > 0) {
Table obj = (Table) tablesAndViews.values().toArray()[0];
while (constraints != null && constraints.size() > 0) {
Constraint obj = (Constraint) constraints.values().toArray()[0];
database.removeSchemaObject(session, obj);
}
// There can be dependencies between tables e.g. using computed columns,
// so we might need to loop over them multiple times.
boolean runLoopAgain = false;
do {
runLoopAgain = false;
if (tablesAndViews != null) {
// Loop over a copy because the map is modified underneath us.
for (Table obj : New.arrayList(tablesAndViews.values())) {
// Check for null because multiple tables might be deleted in one go
// underneath us.
if (obj.getName() != null) {
if (database.getDependentTable(obj, obj) == null) {
database.removeSchemaObject(session, obj);
} else {
runLoopAgain = true;
}
}
}
}
} while (runLoopAgain);
while (indexes != null && indexes.size() > 0) {
Index obj = (Index) indexes.values().toArray()[0];
database.removeSchemaObject(session, obj);
......
......@@ -26,6 +26,7 @@ import org.h2.test.db.TestCompatibility;
import org.h2.test.db.TestCsv;
import org.h2.test.db.TestDateStorage;
import org.h2.test.db.TestDeadlock;
import org.h2.test.db.TestDrop;
import org.h2.test.db.TestEncryptedDb;
import org.h2.test.db.TestExclusive;
import org.h2.test.db.TestFullText;
......@@ -594,6 +595,7 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
new TestCsv().runTest(this);
new TestDateStorage().runTest(this);
new TestDeadlock().runTest(this);
new TestDrop().runTest(this);
new TestEncryptedDb().runTest(this);
new TestExclusive().runTest(this);
new TestFullText().runTest(this);
......
/*
* Copyright 2004-2013 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.test.db;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.test.TestBase;
/**
* Test DROP statement
*/
public class TestDrop extends TestBase {
private Connection conn;
private Statement stat;
/**
* Run just this test.
*
* @param a ignored
*/
public static void main(String... a) throws Exception {
TestBase.createCaller().init().test();
}
@Override
public void test() throws Exception {
deleteDb("drop");
conn = getConnection("drop");
stat = conn.createStatement();
testComputedColumnDependency();
conn.close();
deleteDb("drop");
}
private void testComputedColumnDependency() throws SQLException {
stat.execute("DROP ALL OBJECTS");
stat.execute("CREATE TABLE A (A INT);");
stat.execute("CREATE TABLE B (B INT AS SELECT A FROM A);");
stat.execute("DROP ALL OBJECTS");
stat.execute("CREATE SCHEMA TEST_SCHEMA");
stat.execute("CREATE TABLE TEST_SCHEMA.A (A INT);");
stat.execute("CREATE TABLE TEST_SCHEMA.B (B INT AS SELECT A FROM TEST_SCHEMA.A);");
stat.execute("DROP SCHEMA TEST_SCHEMA");
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论