public void upgrade() {
        for (final Document document : collection.find()) {
            LOG.debug("Migrate view sharing: {}", document);

            final ObjectId sharingId = document.getObjectId("_id");
            final String sharingType = document.get("type", String.class);
            final String viewId = document.get("view_id", String.class);

            try {
                switch (sharingType) {
                    case "users":
                        //noinspection unchecked
                        migrateUsers(viewId, (Collection<String>) document.get("users", Collection.class));
                        break;
                    case "roles":
                        //noinspection unchecked
                        migrateRoles(viewId, (Collection<String>) document.get("roles", Collection.class));
                        break;
                    case "all_of_instance":
                        migrateAllOfInstance(viewId);
                        break;
                    default:
                        LOG.warn("Skipping unknown view sharing type: {}", sharingType);
                        continue; // Continue here so we don't delete the sharing document
                }

                // The view sharing document should be removed after successful migration
                deleteViewSharing(sharingId);
            } catch (Exception e) {
                LOG.error("Couldn't migrate view sharing: {}", document, e);
            }
        }
    }
@Test
    @DisplayName("migrate role shares")
    void migrateRoleShares() throws Exception {
        final User userJane = createUser("jane");
        final User userJohn = createUser("john");
        final Role role1 = createRole("role1");
        final Role role2 = createRole("role2");

        when(userService.loadAllForRole(role1)).thenReturn(ImmutableSet.of(userJane, userJohn));
        when(userService.loadAllForRole(role2)).thenReturn(Collections.emptySet());
        when(roleService.load(role1.getName())).thenReturn(role1);
        when(roleService.load(role2.getName())).thenReturn(role2);

        final GRN jane = GRNTypes.USER.toGRN(userJane.getName());
        final GRN john = GRNTypes.USER.toGRN(userJohn.getName());
        final GRN dashboard1 = GRNTypes.DASHBOARD.toGRN("54e3deadbeefdeadbeef0002");

        assertThat(grantService.hasGrantFor(jane, Capability.VIEW, dashboard1)).isFalse();
        assertThat(grantService.hasGrantFor(john, Capability.VIEW, dashboard1)).isFalse();

        migration.upgrade();

        assertThat(grantService.hasGrantFor(jane, Capability.VIEW, dashboard1)).isTrue();
        assertThat(grantService.hasGrantFor(john, Capability.VIEW, dashboard1)).isTrue();

        assertThat(grantService.hasGrantFor(jane, Capability.OWN, dashboard1)).isFalse();
        assertThat(grantService.hasGrantFor(jane, Capability.MANAGE, dashboard1)).isFalse();
        assertThat(grantService.hasGrantFor(john, Capability.OWN, dashboard1)).isFalse();
        assertThat(grantService.hasGrantFor(john, Capability.MANAGE, dashboard1)).isFalse();

        assertDeletedViewSharing("54e3deadbeefdeadbeef0002");
    }