jdaugherty commented on code in PR #15436:
URL: https://github.com/apache/grails-core/pull/15436#discussion_r2844140612


##########
grails-testing-support-cleanup-h2/src/main/groovy/org/apache/grails/testing/cleanup/h2/H2DatabaseCleaner.groovy:
##########
@@ -0,0 +1,131 @@
+/*
+ *  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.h2
+
+import java.sql.Connection
+import java.sql.DatabaseMetaData
+
+import javax.sql.DataSource
+
+import groovy.sql.GroovyResultSet
+import groovy.sql.Sql
+import groovy.transform.CompileDynamic
+import groovy.transform.CompileStatic
+import groovy.util.logging.Slf4j
+
+import org.springframework.context.ApplicationContext
+import org.springframework.util.ClassUtils
+
+import org.apache.grails.testing.cleanup.core.DatabaseCleaner
+import org.apache.grails.testing.cleanup.core.DatabaseCleanupStats
+
+/**
+ * {@link DatabaseCleaner} implementation for H2 databases. Truncates all 
tables in the
+ * resolved schema and optionally evicts the Hibernate second-level cache.
+ *
+ * <p>This cleaner identifies H2 databases by inspecting the JDBC URL of the 
datasource's
+ * connection metadata. It supports both in-memory ({@code jdbc:h2:mem:}) and 
file-based
+ * ({@code jdbc:h2:}) H2 databases.</p>
+ */
+@Slf4j
+@CompileStatic
+class H2DatabaseCleaner implements DatabaseCleaner {
+
+    private static final String DATABASE_TYPE = 'h2'
+
+    @Override
+    String databaseType() {
+        DATABASE_TYPE
+    }
+
+    @Override
+    boolean supports(DataSource dataSource) {
+        Connection connection = null
+        try {
+            connection = dataSource.getConnection()
+            DatabaseMetaData metaData = connection.getMetaData()
+            String url = metaData.getURL()
+            return url && url.startsWith('jdbc:h2:')
+        }
+        catch (Exception e) {
+            log.debug('Could not determine if datasource is H2', e)
+            return false
+        }
+        finally {
+            if (connection) {
+                try {
+                    connection.close()
+                }
+                catch (Exception ignored) {
+                    // ignore
+                }
+            }
+        }
+    }
+
+    @SuppressWarnings('SqlNoDataSourceInspection')
+    @Override
+    DatabaseCleanupStats cleanup(ApplicationContext applicationContext, 
DataSource dataSource) {
+        DatabaseCleanupStats stats = new DatabaseCleanupStats()
+        stats.start()
+
+        String schemaName = 
H2DatabaseCleanupHelper.resolveSchemaName(dataSource)
+        if (!schemaName) {
+            log.warn('Could not resolve schema name for datasource, skipping 
cleanup')
+            stats.stop()
+            return stats
+        }
+
+        Sql sql = new Sql(dataSource)
+        try {
+            sql.execute('SET REFERENTIAL_INTEGRITY FALSE')
+            String query = "SELECT table_name, row_count_estimate FROM 
information_schema.tables WHERE table_schema = '${schemaName}' AND 
row_count_estimate > 0" as String

Review Comment:
   FYI: 
https://github.com/h2database/h2database/blob/cfe04fd3de9b32fc0540284bb921b9b3ec34e0cf/h2/src/docsrc/help/information_schema.csv#L912
 it looks like with H2 it's not estimated.  



-- 
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]

Reply via email to