1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-18 11:46:23 -07:00

Properly migrate ban staff ID

This commit is contained in:
Eggbertx 2025-01-05 15:37:46 -08:00
parent 7cb3100140
commit 01d7a722a5
2 changed files with 20 additions and 0 deletions

View file

@ -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().
@ -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().

View file

@ -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()
}
@ -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}))