@Override
    public void upgrade() {
        if (clusterConfigService.get(MigrationCompleted.class) != null) {
            LOG.debug("Migration already completed.");
            return;
        }

        final Set<String> legacyViewIds = StreamSupport.stream(viewsCollection.find(exists(FIELD_DASHBOARD_STATE)).spliterator(), false)
                .map(doc -> doc.getObjectId(FIELD_ID))
                .map(ObjectId::toString)
                .collect(Collectors.toSet());
        final UpdateResult updateResult = viewsCollection.updateMany(exists(FIELD_DASHBOARD_STATE), unset(FIELD_DASHBOARD_STATE));
        LOG.debug("Migrated " + updateResult.getModifiedCount() + " views.");

        clusterConfigService.write(MigrationCompleted.create(updateResult.getModifiedCount(), legacyViewIds));
    }
@Test
    @MongoDBFixtures("V20190805115800_RemoveDashboardStateFromViewsTest.json")
    public void removesDashboardStateFromExistingViews() {
        final Migration migration = new V20190805115800_RemoveDashboardStateFromViews(clusterConfigService, mongodb.mongoConnection());

        migration.upgrade();

        final ArgumentCaptor<V20190805115800_RemoveDashboardStateFromViews.MigrationCompleted> argumentCaptor = ArgumentCaptor.forClass(V20190805115800_RemoveDashboardStateFromViews.MigrationCompleted.class);
        verify(clusterConfigService, times(1)).write(argumentCaptor.capture());
        assertThat(argumentCaptor.getValue().modifiedViewsCount()).isEqualTo(4);

        MongoCollection<Document> collection = mongodb.mongoConnection().getMongoDatabase().getCollection("views");
        assertThat(collection.countDocuments()).isEqualTo(4);
    }