Skip to content

Commit

Permalink
Properly migrate ban staff ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Eggbertx committed Jan 5, 2025
1 parent 7cb3100 commit 01d7a72
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cmd/gochan-migration/internal/pre2021/bans.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (m *Pre2021Migrator) migrateBan(tx *sql.Tx, ban *migrationBan, boardID *int
migratedBan.ExpiresAt = ban.expires
migratedBan.Permanent = ban.permaban
migratedBan.Message = ban.reason
migratedBan.StaffID = ban.staffID
migratedBan.StaffNote = ban.staffNote
if err := gcsql.NewIPBanTx(tx, migratedBan); err != nil {
errEv.Err(err).Caller().
Expand Down Expand Up @@ -109,6 +110,20 @@ func (m *Pre2021Migrator) migrateBansToNewDB() error {
}
}

ban.staffID, err = gcsql.GetStaffID(ban.staff)
if errors.Is(err, gcsql.ErrUnrecognizedUsername) {
// username not found after staff were migrated, use a stand-in account to be updated by the admin later
migrationUser, err := m.getMigrationUser(errEv)
if err != nil {
return err
}
common.LogWarning().
Str("username", ban.staff).
Str("migrationUser", migrationUser.Username).
Msg("Ban staff not found in migrated staff table, using migration user instead")
ban.staffID = migrationUser.ID
}

if ban.ip != "" {
if net.ParseIP(ban.ip) == nil {
gcutil.LogWarning().
Expand Down
5 changes: 5 additions & 0 deletions cmd/gochan-migration/internal/pre2021/bans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func TestMigrateBansToNewDB(t *testing.T) {
t.FailNow()
}

if !assert.NoError(t, migrator.MigrateStaff()) {
t.FailNow()
}

if !assert.NoError(t, migrator.MigrateBans()) {
t.FailNow()
}
Expand All @@ -30,6 +34,7 @@ func TestMigrateBansToNewDB(t *testing.T) {
t.FailNow()
}
assert.Equal(t, 6, len(bans), "Expected to have 4 valid bans")
assert.NotZero(t, bans[0].StaffID, "Expected ban staff ID field to be set")

var numInvalidBans int
assert.NoError(t, gcsql.QueryRowSQL("SELECT COUNT(*) FROM DBPREFIXip_ban WHERE message = ?", []any{"Full ban on 8.8.0.0/16"}, []any{&numInvalidBans}))
Expand Down

0 comments on commit 01d7a72

Please sign in to comment.