Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add a background database update to purge account data for deactivated users. #11655

Merged
merged 22 commits into from
Feb 2, 2022
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
68b663d
Remove account data upon user deactivation
reivilibre Dec 21, 2021
13fec0d
Document that account data is removed upon user deactivation
reivilibre Dec 21, 2021
c1fed49
Newsfile
reivilibre Dec 21, 2021
4da869e
Remove account data upon user deactivation
reivilibre Dec 21, 2021
b132bba
Test the removal of account data upon deactivation
reivilibre Dec 29, 2021
1589d6a
Pull out the account data transaction so it can be reused in other tr…
reivilibre Dec 29, 2021
5344446
Add a background job to retroactively purge account data for deactiva…
reivilibre Dec 29, 2021
da884c0
Add a schema delta to trigger the clean-up job
reivilibre Dec 29, 2021
288b0e8
Newsfile
reivilibre Dec 29, 2021
2690bd6
Add tests for the background update job
reivilibre Jan 17, 2022
9413a46
Fix port_db script not being able to run the background job
reivilibre Jan 17, 2022
3b48c9d
Merge branch 'develop' into rei/add_job
reivilibre Jan 24, 2022
2337b28
Reformat after merge
reivilibre Jan 24, 2022
2ee3d8f
Simplify test after merge
reivilibre Jan 24, 2022
d362e31
Fix order of args after merge
reivilibre Jan 24, 2022
3ecc6bc
Move schema delta to correct version
reivilibre Jan 24, 2022
9a4ec65
Fix database port script's Method Resolution Order
reivilibre Jan 24, 2022
0543946
Fix abstract method error in synapse_port_db
reivilibre Jan 28, 2022
d18e2dd
job → update
reivilibre Jan 28, 2022
99353f3
Remove obsolete manual cache invalidations in tests
reivilibre Feb 2, 2022
2587e51
Merge branch 'develop' into rei/add_job
reivilibre Feb 2, 2022
2f79559
Fix up background update delta number
reivilibre Feb 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add a background job to retroactively purge account data for deactiva…
…ted users
  • Loading branch information
reivilibre committed Jan 17, 2022
commit 534444624a5711fa286f092bd8f4eab31cae6047
52 changes: 52 additions & 0 deletions synapse/storage/databases/main/account_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ def __init__(
"AccountDataAndTagsChangeCache", account_max
)

self.db_pool.updates.register_background_update_handler(
"delete_account_data_for_deactivated_users",
self._delete_account_data_for_deactivated_users,
)

def get_max_account_data_stream_id(self) -> int:
"""Get the current max stream ID for account data stream

Expand Down Expand Up @@ -591,6 +596,53 @@ def _purge_account_data_for_user_txn(
txn, table="push_rules_stream", keyvalues={"user_id": user_id}
)

async def _delete_account_data_for_deactivated_users(
self, progress: dict, batch_size: int
) -> int:
"""
Retroactively purges account data for users that have already been deactivated.
Gets run as a background update caused by a schema delta.
"""

last_user: str = progress.get("last_user", "")

def _delete_account_data_for_deactivated_users_txn(
txn: LoggingTransaction,
) -> int:
sql = """
SELECT name FROM users
WHERE deactivated = ? and name > ?
ORDER BY name ASC
LIMIT ?
"""

txn.execute(sql, (1, last_user, batch_size))
users = [row[0] for row in txn]

for user in users:
self._purge_account_data_for_user_txn(txn, user_id=user)

if users:
self.db_pool.updates._background_update_progress_txn(
txn,
"delete_account_data_for_deactivated_users",
{"last_user": users[-1]},
)

return len(users)

number_deleted = await self.db_pool.runInteraction(
"_delete_account_data_for_deactivated_users",
_delete_account_data_for_deactivated_users_txn,
)

if number_deleted < batch_size:
await self.db_pool.updates._end_background_update(
"delete_account_data_for_deactivated_users"
)

return number_deleted


class AccountDataStore(AccountDataWorkerStore):
pass