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 user shares")
    void migrateUserShares() throws Exception {
        final GRN jane = GRNTypes.USER.toGRN("jane");
        final GRN john = GRNTypes.USER.toGRN("john");
        final GRN search = GRNTypes.SEARCH.toGRN("54e3deadbeefdeadbeef0001");

        when(roleService.load(anyString())).thenThrow(new NotFoundException());

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

        migration.upgrade();

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

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

        assertDeletedViewSharing("54e3deadbeefdeadbeef0001");
    }