From fa9134328a4b9665f10848313f1889059b09b1a3 Mon Sep 17 00:00:00 2001 From: Michael Muller Date: Fri, 27 Mar 2020 14:58:01 -0400 Subject: [PATCH] Improve error information in coverage test. (#537) * Improve error information in coverage test. If the golden schema isn't up-to-date with the persistence model, the coverage tests fail with an exception chain that ends in a PSQLException 'relation "TableName" does not exist' which is kind of misleading when the problem is that your golden schema isn't up-to-date. Check for this error in the coverage tests and generate a more informative error message indicating a likely root cause. --- .../transaction/JpaEntityCoverage.java | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/core/src/test/java/google/registry/persistence/transaction/JpaEntityCoverage.java b/core/src/test/java/google/registry/persistence/transaction/JpaEntityCoverage.java index 4cc5f4771..ba28ca2d7 100644 --- a/core/src/test/java/google/registry/persistence/transaction/JpaEntityCoverage.java +++ b/core/src/test/java/google/registry/persistence/transaction/JpaEntityCoverage.java @@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import google.registry.persistence.PersistenceXmlUtility; +import java.sql.SQLException; import java.util.List; import java.util.Map; import java.util.Set; @@ -96,18 +97,34 @@ public class JpaEntityCoverage extends ExternalResource { * @return true if an instance of {@code entityType} is found in the database and can be read */ private static boolean isPersisted(Class entityType) { - List result = - jpaTm() - .transact( - () -> - jpaTm() - .getEntityManager() - .createQuery( - String.format("SELECT e FROM %s e", getJpaEntityName(entityType)), - entityType) - .setMaxResults(1) - .getResultList()); - return !result.isEmpty() && entityType.isInstance(result.get(0)); + try { + List result = + jpaTm() + .transact( + () -> + jpaTm() + .getEntityManager() + .createQuery( + String.format("SELECT e FROM %s e", getJpaEntityName(entityType)), + entityType) + .setMaxResults(1) + .getResultList()); + return !result.isEmpty() && entityType.isInstance(result.get(0)); + } catch (RuntimeException e) { + // See if this was caused by a "relation does not exist" error. + Throwable cause = e; + while ((cause = cause.getCause()) != null) { + if (cause instanceof SQLException + && cause.getMessage().matches("(?s).*relation .* does not exist.*")) { + throw new RuntimeException( + "SQLException occurred. If you've updated the set of entities, make sure you've " + + "also updated the golden schema. See db/README.md for details.", + e); + } + } + + throw e; + } } private static String getJpaEntityName(Class entityType) {