From 972ebcc2a672c3519a10f34228fe5cab71f8f334 Mon Sep 17 00:00:00 2001 From: Mihitoko Date: Sat, 4 Nov 2023 00:58:35 +0100 Subject: [PATCH] Fix #3786 collations not dropped on clean --- .../database/postgresql/PostgreSQLSchema.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/flyway-database/flyway-database-postgresql/src/main/java/org/flywaydb/database/postgresql/PostgreSQLSchema.java b/flyway-database/flyway-database-postgresql/src/main/java/org/flywaydb/database/postgresql/PostgreSQLSchema.java index 9f8f553e9..c6521f3ef 100644 --- a/flyway-database/flyway-database-postgresql/src/main/java/org/flywaydb/database/postgresql/PostgreSQLSchema.java +++ b/flyway-database/flyway-database-postgresql/src/main/java/org/flywaydb/database/postgresql/PostgreSQLSchema.java @@ -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); } @@ -340,6 +344,31 @@ private List 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 generateDropStatementsForCollations() throws SQLException { + List 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 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 tableNames =