Skip to content
Permalink
Browse files

Fix TestUtil.dropXyz(...) object not exists errors (#1359)

* test: Change drop object utilities in TestUtils to use IF EXISTS

Changes most drop object helper methods in TestUtils to check if a transaction
is in progress and, if not, then use IF EXISTS to suppress errors for missing
objects during the DROP command.

If a transaction is in progress then the command then the drop is executed
without the IF EXISTS clause so that missing objects results in an exception
and test failure.

In either case an unrelated error such as the server being offline will be now
throw an exception. This is a change from the prior behavior where all errors
that are not part of a transaction were being suppressed.
errors if the object being dropped does not exist.

* test: Change TestUtil.dropSequence(...) to add CASCADE

Adds CASCADE option to TestUtils.dropSequence(...) helper to handle
dependency errors in drop statements that previously being ignored.

* test: Add shouldDrop param to TestUtils.createCompositeType(...)

Also adds a method matching the previous signature without the new parameter
and defaulting to the older behaviour of true (i.e. drop then create).

* test: Use explicit drop type statements for metadata tests

Adds explicit drop statements for types in metadata tests to ensure
the drop statements are executed in the correct order. Also adds a
drop statement for the reference table.

The revised drop order is required as TestUtils no longer skips over
non-missing object errors when dropping objects.

* test: Clean up throws clauses in TestUtil

Restricts throws clauses in TestUtil to SQLException rather than a
more broad Exception and removes throws clause from initDriver().
  • Loading branch information...
sehrope authored and davecramer committed Dec 5, 2018
1 parent 5b0c05f commit 0999bb78ecb0a305f830d8b76ae24f6e64444ec8
@@ -242,7 +242,7 @@ public static Properties loadPropertyFiles(String... names) {
return p;
}

public static void initDriver() throws Exception {
public static void initDriver() {
synchronized (TestUtil.class) {
if (initialized) {
return;
@@ -281,7 +281,7 @@ public static File getFile(String name) {
* @return connection using a privileged user mostly for tests that the ability to load C
* functions now as of 4/14
*/
public static Connection openPrivilegedDB() throws Exception {
public static Connection openPrivilegedDB() throws SQLException {
initDriver();
Properties properties = new Properties();
properties.setProperty("user", getPrivilegedUser());
@@ -295,15 +295,15 @@ public static Connection openPrivilegedDB() throws Exception {
*
* @return connection
*/
public static Connection openDB() throws Exception {
public static Connection openDB() throws SQLException {
return openDB(new Properties());
}

/*
* Helper - opens a connection with the allowance for passing additional parameters, like
* "compatible".
*/
public static Connection openDB(Properties props) throws Exception {
public static Connection openDB(Properties props) throws SQLException {
initDriver();

// Allow properties to override the user name.
@@ -375,17 +375,15 @@ public static void createSchema(Connection con, String schema) throws SQLExcepti
public static void dropSchema(Connection con, String schema) throws SQLException {
Statement stmt = con.createStatement();
try {
String sql = "DROP SCHEMA " + schema + " CASCADE ";

stmt.executeUpdate(sql);
} catch (SQLException ex) {
// Since every create schema issues a drop schema
// it's easy to get a schema doesn't exist error.
// we want to ignore these, but if we're in a
// transaction then we've got trouble
if (!con.getAutoCommit()) {
throw ex;
if (con.getAutoCommit()) {
// Not in a transaction so ignore error for missing object
stmt.executeUpdate("DROP SCHEMA IF EXISTS " + schema + " CASCADE");
} else {
// In a transaction so do not ignore errors for missing object
stmt.executeUpdate("DROP SCHEMA " + schema + " CASCADE");
}
} finally {
closeQuietly(stmt);
}
}

@@ -459,16 +457,26 @@ public static void createEnumType(Connection con, String name, String values)
* @param name String
* @param values String
*/
public static void createCompositeType(Connection con, String name, String values) throws SQLException {
createCompositeType(con, name, values, true);
}

public static void createCompositeType(Connection con, String name, String values)
/**
* Helper creates an composite type.
*
* @param con Connection
* @param name String
* @param values String
*/
public static void createCompositeType(Connection con, String name, String values, boolean shouldDrop)
throws SQLException {
Statement st = con.createStatement();
try {
dropType(con, name);


// Now create the table
st.executeUpdate("create type " + name + " as (" + values + ")");
if (shouldDrop) {
dropType(con, name);
}
// Now create the type
st.executeUpdate("CREATE TYPE " + name + " AS (" + values + ")");
} finally {
closeQuietly(st);
}
@@ -482,15 +490,17 @@ public static void createCompositeType(Connection con, String name, String value
*/
public static void dropDomain(Connection con, String name)
throws SQLException {
Statement st = con.createStatement();
Statement stmt = con.createStatement();
try {
st.executeUpdate("drop domain " + name + " cascade");
} catch (SQLException ex) {
if (!con.getAutoCommit()) {
throw ex;
if (con.getAutoCommit()) {
// Not in a transaction so ignore error for missing object
stmt.executeUpdate("DROP DOMAIN IF EXISTS " + name + " CASCADE");
} else {
// In a transaction so do not ignore errors for missing object
stmt.executeUpdate("DROP DOMAIN " + name + " CASCADE");
}
} finally {
closeQuietly(st);
closeQuietly(stmt);
}
}

@@ -519,12 +529,15 @@ public static void createDomain(Connection con, String name, String values)
public static void dropSequence(Connection con, String sequence) throws SQLException {
Statement stmt = con.createStatement();
try {
String sql = "DROP SEQUENCE " + sequence;
stmt.executeUpdate(sql);
} catch (SQLException sqle) {
if (!con.getAutoCommit()) {
throw sqle;
if (con.getAutoCommit()) {
// Not in a transaction so ignore error for missing object
stmt.executeUpdate("DROP SEQUENCE IF EXISTS " + sequence + " CASCADE");
} else {
// In a transaction so do not ignore errors for missing object
stmt.executeUpdate("DROP SEQUENCE " + sequence + " CASCADE");
}
} finally {
closeQuietly(stmt);
}
}

@@ -534,16 +547,15 @@ public static void dropSequence(Connection con, String sequence) throws SQLExcep
public static void dropTable(Connection con, String table) throws SQLException {
Statement stmt = con.createStatement();
try {
String sql = "DROP TABLE " + table + " CASCADE ";
stmt.executeUpdate(sql);
} catch (SQLException ex) {
// Since every create table issues a drop table
// it's easy to get a table doesn't exist error.
// we want to ignore these, but if we're in a
// transaction then we've got trouble
if (!con.getAutoCommit()) {
throw ex;
if (con.getAutoCommit()) {
// Not in a transaction so ignore error for missing object
stmt.executeUpdate("DROP TABLE IF EXISTS " + table + " CASCADE ");
} else {
// In a transaction so do not ignore errors for missing object
stmt.executeUpdate("DROP TABLE " + table + " CASCADE ");
}
} finally {
closeQuietly(stmt);
}
}

@@ -553,12 +565,15 @@ public static void dropTable(Connection con, String table) throws SQLException {
public static void dropType(Connection con, String type) throws SQLException {
Statement stmt = con.createStatement();
try {
String sql = "DROP TYPE " + type + " CASCADE";
stmt.executeUpdate(sql);
} catch (SQLException ex) {
if (!con.getAutoCommit()) {
throw ex;
if (con.getAutoCommit()) {
// Not in a transaction so ignore error for missing object
stmt.executeUpdate("DROP TYPE IF EXISTS " + type + " CASCADE");
} else {
// In a transaction so do not ignore errors for missing object
stmt.executeUpdate("DROP TYPE " + type + " CASCADE");
}
} finally {
closeQuietly(stmt);
}
}

@@ -52,8 +52,10 @@ public void setUp() throws Exception {
TestUtil.createTable(con, "\"a'\"", "a int4");
TestUtil.createTable(con, "arraytable", "a numeric(5,2)[], b varchar(100)[]");
TestUtil.createTable(con, "intarraytable", "a int4[], b int4[][]");
TestUtil.createCompositeType(con, "custom", "i int");
TestUtil.createCompositeType(con, "_custom", "f float");
TestUtil.dropType(con, "custom");
TestUtil.dropType(con, "_custom");
TestUtil.createCompositeType(con, "custom", "i int", false);
TestUtil.createCompositeType(con, "_custom", "f float", false);


// 8.2 does not support arrays of composite types
@@ -107,7 +109,7 @@ public void tearDown() throws Exception {
stmt.execute("DROP FUNCTION f1(int, varchar)");
stmt.execute("DROP FUNCTION f2(int, varchar)");
stmt.execute("DROP FUNCTION f3(int, varchar)");
TestUtil.dropType(con, "domaintable");
TestUtil.dropTable(con, "domaintable");
TestUtil.dropDomain(con, "nndom");

TestUtil.closeDB(con);

0 comments on commit 0999bb7

Please sign in to comment.
You can’t perform that action at this time.