Skip to content

Commit

Permalink
Write the migration in go instead
Browse files Browse the repository at this point in the history
so we can have meaningful logging
  • Loading branch information
David Robertson committed Aug 22, 2023
1 parent 68b5eb0 commit 3da8b25
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 27 deletions.
27 changes: 0 additions & 27 deletions state/migrations/20230822174049_bogus_snapshot_cleanup.sql

This file was deleted.

93 changes: 93 additions & 0 deletions state/migrations/20230822180807_bogus_snapshot_cleanup.go
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 %t", 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
}

0 comments on commit 3da8b25

Please sign in to comment.