You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by dj...@apache.org on 2006/10/27 21:06:20 UTC
svn commit: r468502 - in
/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit:
CleanDatabaseTestSetup.java JDBC.java
Author: djd
Date: Fri Oct 27 12:06:19 2006
New Revision: 468502
URL: http://svn.apache.org/viewvc?view=rev&rev=468502
Log:
DERBY-2007 (partial) Improve CleanDatabaseTestSetup to handle tables in a dependency loop due
to foreign key constraints and simple cases for objects that are dependent across schemas.
Modified:
db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/CleanDatabaseTestSetup.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBC.java
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/CleanDatabaseTestSetup.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/CleanDatabaseTestSetup.java?view=diff&rev=468502&r1=468501&r2=468502
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/CleanDatabaseTestSetup.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/CleanDatabaseTestSetup.java Fri Oct 27 12:06:19 2006
@@ -109,28 +109,44 @@
public static void cleanDatabase(Connection conn) throws SQLException {
DatabaseMetaData dmd = conn.getMetaData();
- // Fetch all the user schemas into a list
- List schemas = new ArrayList();
- ResultSet rs = dmd.getSchemas();
- while (rs.next()) {
-
- String schema = rs.getString("TABLE_SCHEM");
- if (schema.startsWith("SYS"))
- continue;
- if (schema.equals("SQLJ"))
- continue;
- if (schema.equals("NULLID"))
- continue;
-
- schemas.add(schema);
- }
- rs.close();
-
- // DROP all the user schemas.
- for (Iterator i = schemas.iterator(); i.hasNext();) {
- String schema = (String) i.next();
- JDBC.dropSchema(dmd, schema);
+ SQLException sqle = null;
+ // Loop a number of arbitary times to catch cases
+ // where objects are dependent on objects in
+ // different schemas.
+ for (int count = 0; count < 5; count++) {
+ // Fetch all the user schemas into a list
+ List schemas = new ArrayList();
+ ResultSet rs = dmd.getSchemas();
+ while (rs.next()) {
+
+ String schema = rs.getString("TABLE_SCHEM");
+ if (schema.startsWith("SYS"))
+ continue;
+ if (schema.equals("SQLJ"))
+ continue;
+ if (schema.equals("NULLID"))
+ continue;
+
+ schemas.add(schema);
+ }
+ rs.close();
+
+ // DROP all the user schemas.
+ sqle = null;
+ for (Iterator i = schemas.iterator(); i.hasNext();) {
+ String schema = (String) i.next();
+ try {
+ JDBC.dropSchema(dmd, schema);
+ } catch (SQLException e) {
+ sqle = null;
+ }
+ }
+ // No errors means all the schemas we wanted to
+ // drop were dropped, so nothing more to do.
+ if (sqle == null)
+ return;
}
+ throw sqle;
}
}
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBC.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBC.java?view=diff&rev=468502&r1=468501&r2=468502
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBC.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBC.java Fri Oct 27 12:06:19 2006
@@ -197,6 +197,36 @@
new String[] {"TABLE"});
dropUsingDMD(s, rs, schema, "TABLE_NAME", "TABLE");
+
+ // At this point there may be tables left due to
+ // foreign key constraints leading to a dependency loop.
+ // Drop any constraints that remain and then drop the tables.
+ // If there are no tables then this should be a quick no-op.
+ rs = dmd.getExportedKeys((String) null, schema, (String) null);
+ while (rs.next())
+ {
+ short keyPosition = rs.getShort("KEY_SEQ");
+ if (keyPosition != 1)
+ continue;
+ String fkName = rs.getString("FK_NAME");
+ // No name, probably can't happen but couldn't drop it anyway.
+ if (fkName == null)
+ continue;
+ String fkSchema = rs.getString("FKTABLE_SCHEM");
+ String fkTable = rs.getString("FKTABLE_NAME");
+
+ String ddl = "ALTER TABLE " +
+ JDBC.escape(fkSchema, fkTable) +
+ " DROP FOREIGN KEY " +
+ JDBC.escape(fkName);
+ s.executeUpdate(ddl);
+ }
+ conn.commit();
+
+ // Tables (again)
+ rs = dmd.getTables((String) null, schema, (String) null,
+ new String[] {"TABLE"});
+ dropUsingDMD(s, rs, schema, "TABLE_NAME", "TABLE");
// Synonyms - need work around for DERBY-1790 where
// passing a table type of SYNONYM fails.
@@ -207,7 +237,7 @@
// Finally drop the schema if it is not APP
if (!schema.equals("APP")) {
- s.execute("DROP SCHEMA " + JDBC.escape(schema) + " RESTRICT");
+ s.executeUpdate("DROP SCHEMA " + JDBC.escape(schema) + " RESTRICT");
}
conn.commit();
s.close();