jamesfredley commented on code in PR #15436: URL: https://github.com/apache/grails-core/pull/15436#discussion_r2844109070
########## grails-testing-support-cleanup-postgresql/src/main/groovy/org/apache/grails/testing/cleanup/postgresql/PostgresDatabaseCleanupHelper.groovy: ########## @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.grails.testing.cleanup.postgresql + +import java.sql.Connection +import java.sql.DatabaseMetaData + +import javax.sql.DataSource + +import groovy.transform.CompileStatic +import groovy.util.logging.Slf4j + +/** + * Helper utility for PostgreSQL database cleanup operations. Provides PostgreSQL-specific logic such as + * resolving the current schema from a {@link DataSource} by inspecting JDBC connection metadata + * and parsing PostgreSQL JDBC URLs. + */ +@Slf4j +@CompileStatic +class PostgresDatabaseCleanupHelper { + + /** + * Resolves the current schema for the given PostgreSQL datasource by inspecting the JDBC connection metadata. + * If the JDBC URL contains a `currentSchema` parameter, returns that schema. + * Otherwise, returns the connection's current schema. + * + * @param dataSource the datasource to resolve the schema for + * @return the schema name, or {@code null} if it cannot be determined + */ + static String resolveCurrentSchema(DataSource dataSource) { + Connection connection = null + try { + connection = dataSource.getConnection() + String schema = connection.getSchema() + if (schema) { + log.debug('Resolved current schema from connection: {}', schema) + return schema + } + + // Fallback: try to get the schema from the database metadata URL + DatabaseMetaData metaData = connection.getMetaData() + String url = metaData.getURL() + if (url) { + schema = extractCurrentSchemaFromUrl(url) + if (schema) { + log.debug('Resolved current schema from URL {}: {}', url, schema) + return schema + } + } + } + finally { + if (connection) { + try { + connection.close() + } + catch (Exception ignored) { + // ignore + } + } + } + + throw new IllegalStateException("Because postgres defaults to the search_patch when currentSchema isn't defined, a schema should always be found") Review Comment: search_patch should be search_path -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
