Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in the casefolding script that would cause some deletions to be skipped if e-mail sending was enabled. #489

Merged
merged 6 commits into from Jan 28, 2022
1 change: 1 addition & 0 deletions changelog.d/489.bugfix
@@ -0,0 +1 @@
Fix bug in the casefolding script that would cause some deletions to be skipped if e-mail sending was enabled.
44 changes: 20 additions & 24 deletions scripts/casefold_db.py
Expand Up @@ -219,35 +219,39 @@ def update_local_associations(

try:
# Delete each association, and send an email mentioning the affected MXID.
if delta.to_delete is not None:
if delta.to_delete is not None and not dry_run:
for to_delete in delta.to_delete:
if send_email and not dry_run:
if send_email and to_delete.mxid != delta.to_update.mxid:
babolivier marked this conversation as resolved.
Show resolved Hide resolved
# If the MXID is one that will still be associated with this
# email address after this run, don't send an email for it.
if to_delete.mxid == delta.to_update.mxid:
continue

sendEmailWithBackoff(
sydent,
to_delete.address,
to_delete.mxid,
test=test,
)

if not dry_run:
cur = db.cursor()
cur.execute(
"DELETE FROM local_threepid_associations WHERE medium = 'email' AND address = ?",
(to_delete.address,),
)
db.commit()
logger.debug(
"Deleting %s from table local_threepid_associations",
to_delete.address,
)
logger.debug(
"Deleting %s from table local_threepid_associations",
to_delete.address,
)
cur = db.cursor()
cur.execute(
"DELETE FROM local_threepid_associations WHERE medium = 'email' AND address = ?",
(to_delete.address,),
)
db.commit()

# Update the row now that there's no duplicate.
if not dry_run:
logger.debug(
"Updating table local threepid associations setting address to %s, "
"lookup_hash to %s, where medium = email and address = %s and mxid = %s",
casefolded_address,
delta.to_update.lookup_hash,
delta.to_update.address,
delta.to_update.mxid,
)
cur = db.cursor()
cur.execute(
"UPDATE local_threepid_associations SET address = ?, lookup_hash = ? WHERE medium = 'email' AND address = ? AND mxid = ?",
Expand All @@ -258,14 +262,6 @@ def update_local_associations(
delta.to_update.mxid,
),
)
logger.debug(
"Updating table local threepid associations setting address to %s, "
"lookup_hash to %s, where medium = email and address = %s and mxid = %s",
casefolded_address,
delta.to_update.lookup_hash,
delta.to_update.address,
delta.to_update.mxid,
)
db.commit()

except CantSendEmailException:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_casefold_migration.py
Expand Up @@ -70,6 +70,18 @@ def setUp(self):
}
)

associations.append(
{
"medium": "email",
"address": "BoB4@example.com",
"lookup_hash": calculate_lookup_hash(self.sydent, "BoB4@example.com"),
"mxid": "@otherbob4:example.com",
"ts": 42000,
"not_before": 0,
"not_after": 99999999999,
}
)

# add all associations to db
cur = self.sydent.db.cursor()

Expand Down