-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
so we can have meaningful logging
- Loading branch information
David Robertson
committed
Aug 22, 2023
1 parent
68b5eb0
commit 7cbefdf
Showing
2 changed files
with
93 additions
and
27 deletions.
There are no files selected for viewing
27 changes: 0 additions & 27 deletions
27
state/migrations/20230822174049_bogus_snapshot_cleanup.sql
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"github.com/lib/pq" | ||
"github.com/pressly/goose/v3" | ||
"github.com/rs/zerolog" | ||
"os" | ||
) | ||
|
||
var logger = zerolog.New(os.Stdout).With().Timestamp().Logger().Output(zerolog.ConsoleWriter{ | ||
Out: os.Stderr, | ||
TimeFormat: "15:04:05", | ||
}) | ||
|
||
func init() { | ||
goose.AddMigrationContext(upBogusSnapshotCleanup, downBogusSnapshotCleanup) | ||
} | ||
|
||
func upBogusSnapshotCleanup(ctx context.Context, tx *sql.Tx) error { | ||
// Run a one-off script to delete rooms with bogus snapshots. | ||
// "Bogus" means "lacking a create event". | ||
bogusRooms, err := getBogusRooms(ctx, tx) | ||
if err != nil { | ||
return err | ||
} | ||
if len(bogusRooms) == 0 { | ||
return nil | ||
} | ||
logger.Info().Strs("room_ids", bogusRooms). | ||
Msgf("Found %d bogus rooms to cleanup", len(bogusRooms)) | ||
|
||
tables := []string{"syncv3_snapshots", "syncv3_events", "syncv3_rooms"} | ||
for _, table := range tables { | ||
err = deleteFromTable(ctx, tx, table, bogusRooms) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func deleteFromTable(ctx context.Context, tx *sql.Tx, table string, roomIDs []string) error { | ||
result, err := tx.ExecContext( | ||
ctx, | ||
`DELETE FROM `+table+` WHERE room_id = ANY($1)`, | ||
pq.StringArray(roomIDs)) | ||
if err != nil { | ||
return fmt.Errorf("failed to delete from %s: %w", table, err) | ||
} | ||
ra, err := result.RowsAffected() | ||
if err != nil { | ||
logger.Warn().Err(err).Msgf("Couldn't get number of rows deleted from %s", table) | ||
} else { | ||
logger.Info().Msgf("Deleted %d snapshots from %s", ra, table) | ||
} | ||
return nil | ||
} | ||
|
||
func getBogusRooms(ctx context.Context, tx *sql.Tx) ([]string, error) { | ||
rows, err := tx.QueryContext(ctx, ` | ||
SELECT room_id | ||
FROM syncv3_rooms | ||
WHERE room_id NOT IN ( | ||
SELECT room_id | ||
FROM syncv3_events | ||
WHERE event_type = 'm.room.create' AND state_key = '' | ||
) | ||
`) | ||
defer rows.Close() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to select bogus rooms: %w", err) | ||
} | ||
|
||
var bogusRooms []string | ||
for rows.Next() { | ||
var roomID string | ||
err = rows.Scan(&roomID) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to scan bogus room: %w", err) | ||
} | ||
bogusRooms = append(bogusRooms, roomID) | ||
} | ||
|
||
return bogusRooms, nil | ||
} | ||
|
||
func downBogusSnapshotCleanup(ctx context.Context, tx *sql.Tx) error { | ||
// No-op. | ||
return nil | ||
} |