private void insertOrUpdate(Connection conn, ResultSet oldRow, ResultSet newRow) throws SQLException {
        String apiOrgId = newRow.getString("api_org_id");
        String apiId = newRow.getString("api_id");
        String apiVersion = newRow.getString("version");
        DiscoverabilityLevel level = Optional.ofNullable(newRow.getString("discoverability"))
                .map(DiscoverabilityLevel::valueOf)
                .orElse(DiscoverabilityLevel.ORG_MEMBERS);

        String pdeKey = String.join(":", apiOrgId, apiId, apiVersion);

        if (oldRow != null) {
            DiscoverabilityLevel oldLevel = Optional.ofNullable(oldRow.getString("discoverability"))
                    .map(DiscoverabilityLevel::valueOf)
                    .orElse(DiscoverabilityLevel.ORG_MEMBERS);
            // If no change, just exit.
            if (oldLevel == level) {
                return;
            }
        }
        Jdbi.create(conn).withHandle(h -> h.createUpdate(
                        "MERGE INTO discoverability(id, org_id, api_id, api_version, plan_id, plan_version, discoverability) KEY(id) "
                                + "VALUES (:id, :orgId, :apiId, :apiVersion, NULL, NULL, :discoverability)")
                .bind("id", pdeKey)
                .bind("orgId", apiOrgId)
                .bind("apiId", apiId)
                .bind("apiVersion", apiVersion)
                .bind("discoverability", level)
                .execute()
        );
    }
Synchronize DiscoverabilityEntity materialized view when any public ApiVersion that has a DiscoverabilityLevel is inserted, updated, or deleted.

@author Marc Savy {@literal <marc@blackparrotlabs.io>}
manager/api/jpa/src/main/java/io/apiman/manager/api/jpa/h2/ApiVersionDiscoverabilityTrigger.java