Clean-up stale ack pos rows

or else we can't enforce non-null user ids in this table
This commit is contained in:
David Robertson 2023-05-10 11:22:20 +01:00
parent 1dd19b6ace
commit a2d8900268
No known key found for this signature in database
GPG Key ID: 903ECE108A39DEDD
2 changed files with 24 additions and 2 deletions

View File

@ -23,7 +23,7 @@ func NewTransactionsTable(db *sqlx.DB) *TransactionsTable {
// make sure tables are made // make sure tables are made
db.MustExec(` db.MustExec(`
CREATE TABLE IF NOT EXISTS syncv3_txns ( CREATE TABLE IF NOT EXISTS syncv3_txns (
user_id TEXT NOT NULL, -- was actually device_id before migration user_id TEXT NOT NULL,
device_id TEXT NOT NULL, device_id TEXT NOT NULL,
event_id TEXT NOT NULL, event_id TEXT NOT NULL,
txn_id TEXT NOT NULL, txn_id TEXT NOT NULL,

View File

@ -306,9 +306,31 @@ func exec(txn *sqlx.Tx, query string, checkRowsAffected func(ra int64) bool, arg
func expectOneRowAffected(ra int64) bool { return ra == 1 } func expectOneRowAffected(ra int64) bool { return ra == 1 }
func expectAnyNumberOfRowsAffected(ra int64) bool { return true } func expectAnyNumberOfRowsAffected(ra int64) bool { return true }
func expectAtMostOneRowAffected(ra int64) bool { return ra == 0 || ra == 1 } func logRowsAffected(msg string) func(ra int64) bool {
return func(ra int64) bool {
logger.Info().Msgf(msg, ra)
return true
}
}
func expectAtMostOneRowAffected(ra int64) bool { return ra == 0 || ra == 1 }
func finish(txn *sqlx.Tx) (err error) { func finish(txn *sqlx.Tx) (err error) {
// OnExpiredToken used to delete from the to-device table, but not from the
// to-device ack pos table. Fix this up by deleting any orphaned ack pos rows.
err = exec(
txn,
`
DELETE FROM syncv3_to_device_ack_pos
WHERE device_id IN (
SELECT syncv3_to_device_ack_pos.device_id
FROM syncv3_to_device_ack_pos LEFT JOIN syncv3_sync2_devices USING (device_id)
);`,
logRowsAffected("Deleted %d stale rows from syncv3_to_device_ack_pos"),
)
if err != nil {
return
}
_, err = txn.Exec(` _, err = txn.Exec(`
ALTER TABLE syncv3_sync2_devices ALTER TABLE syncv3_sync2_devices
DROP COLUMN v2_token_encrypted, DROP COLUMN v2_token_encrypted,