Skip to content

Commit

Permalink
Fix flyway#3786 collations not dropped on clean
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihitoko committed Nov 3, 2023
1 parent a7ce51e commit 972ebcc
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ protected void doClean() throws SQLException {
jdbcTemplate.execute(statement);
}

for (String statement : generateDropStatementsForCollations()) {
jdbcTemplate.execute(statement);
}

for (String statement : generateDropStatementsForExtensions()) {
jdbcTemplate.execute(statement);
}
Expand Down Expand Up @@ -340,6 +344,31 @@ private List<String> generateDropStatementsForViews() throws SQLException {
return statements;
}

/**
* Generates the statements for dropping the collations in this schema.
*
* @return The drop statements.
* @throws SQLException when the clean statements could not be generated.
*/
private List<String> generateDropStatementsForCollations() throws SQLException {
List<String> collationNames =
jdbcTemplate.queryForStringList(
// Search for all collations in current schema
"SELECT c.collname FROM pg_catalog.pg_collation c " +
// that don't depend on an extension
"JOIN pg_namespace n ON c.collnamespace = n.oid " +
"LEFT JOIN pg_depend dep ON dep.objid = c.oid AND dep.deptype = 'e' " +
"WHERE n.nspname = ? AND dep.objid IS NULL",
name);

List<String> statements = new ArrayList<>();
for (String collationName : collationNames) {
statements.add("DROP COLLATION IF EXISTS " + database.quote(name, collationName) + " CASCADE");
}

return statements;
}

@Override
protected PostgreSQLTable[] doAllTables() throws SQLException {
List<String> tableNames =
Expand Down

0 comments on commit 972ebcc

Please sign in to comment.