Skip to content

Add script to clean up orphaned Matrix rooms

Eric Eastwood requested to merge 2836-clean-up-orphaned-matrix-rooms into develop

Add script to clean up orphaned Matrix rooms

Part of #2836 (closed)

Dev notes

Aggregate query for the orphaned rooms


$ mongo gitter
> db.matricesbridgedroom.aggregate([
  {
    // Lookup troupes._id === matricesbridgedroom.troupeId
    "$lookup": {
      from: 'troupes',
      localField: 'troupeId',
      foreignField: '_id',
      as: 'troupe'
    }
  },
  {
    "$match": {
      "troupe._id": {
        "$exists": false
      }
    }
  }
]);

Get the number of orphaned rooms

In production. 1001 of the 28903 bridged rooms are orphaned on Matrix. This also includes Matrix rooms that have been deleted, we just don't delete the bridged room entry in the database.

$ mongo gitter
> db.matricesbridgedroom.aggregate([
  {
    // Lookup troupes._id === matricesbridgedroom.troupeId
    $lookup: {
      from: 'troupes',
      localField: 'troupeId',
      foreignField: '_id',
      as: 'troupe'
    }
  },
  {
    $match: {
      'troupe._id': {
        $exists: false
      }
    }
  },
  {
    $group: {
      _id : null, 
      count : {$sum : 1}
    }
  }
])

> db.matricesbridgedroom.count()

  // This is the date that we shipped
  // https://gitlab.com/gitterHQ/webapp/-/merge_requests/2265 and started
  // bridging Gitter room deletions to Matrix automatically. Shipped in
  // https://gitlab.com/gitterHQ/webapp/-/blob/develop/CHANGELOG.md#21450-2021-12-08
  // which shipped to production on 2021-12-14
  // (https://twitter.com/gitchat/status/1470908553787772931). We chose
  // 2021-12-16 just to be safe by a day.
  //
  // We only need to look at rooms that were created before that time.
  const cutoffId = mongoUtils.createIdForTimestamp(new Date('2021-12-16').getTime());
Edited by Eric Eastwood

Merge request reports