You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by lu...@apache.org on 2020/08/31 09:04:54 UTC

[shardingsphere] branch master updated: #7174, Integration test of PostgreSQL failed to drop database

This is an automated email from the ASF dual-hosted git repository.

lujingshang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new a60ce7b  #7174, Integration test of PostgreSQL failed to drop database
     new 7263267  Merge pull request #7175 from tuohai666/7147
a60ce7b is described below

commit a60ce7b29a0f018b3ea2778cdf5bccfc76f2c3ca
Author: Zhang Yonglun <zh...@apache.org>
AuthorDate: Mon Aug 31 16:53:17 2020 +0800

    #7174, Integration test of PostgreSQL failed to drop database
---
 .../dbtest/env/schema/SchemaEnvironmentManager.java    | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/dbtest/env/schema/SchemaEnvironmentManager.java b/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/dbtest/env/schema/SchemaEnvironmentManager.java
index de74aa7..f2d9dbc 100644
--- a/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/dbtest/env/schema/SchemaEnvironmentManager.java
+++ b/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/dbtest/env/schema/SchemaEnvironmentManager.java
@@ -88,6 +88,15 @@ public final class SchemaEnvironmentManager {
         SchemaEnvironment databaseInitialization = unmarshal(EnvironmentPath.getDatabaseEnvironmentResourceFile(ruleType));
         for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseTypes()) {
             DataSource dataSource = DataSourceUtil.createDataSource(each, null);
+            if ("PostgreSQL".equals(each.getName())) {
+                try (
+                        Connection connection = dataSource.getConnection();
+                        StringReader stringReader = new StringReader(Joiner.on(";\n").skipNulls().join(generateTerminateConnectionSQLs(databaseInitialization.getDatabases())))) {
+                    RunScript.execute(connection, stringReader);
+                } catch (final SQLException ex) {
+                    // TODO database maybe not exist
+                }
+            }
             try (
                     Connection connection = dataSource.getConnection();
                     StringReader stringReader = new StringReader(Joiner.on(";\n").skipNulls().join(generateDropDatabaseSQLs(each, databaseInitialization.getDatabases())))) {
@@ -116,6 +125,15 @@ public final class SchemaEnvironmentManager {
         return result;
     }
     
+    private static Collection<String> generateTerminateConnectionSQLs(final List<String> databases) {
+        String sql = "SELECT pg_terminate_backend (pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '%s'";
+        Collection<String> result = new LinkedList<>();
+        for (String each : databases) {
+            result.add(String.format(sql, each));
+        }
+        return result;
+    }
+    
     private static Collection<String> generateDropDatabaseSQLs(final DatabaseType databaseType, final List<String> databases) {
         if ("H2".equals(databaseType.getName())) {
             return Collections.emptyList();