Skip to content

Commit

Permalink
Add announcement migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Eggbertx committed Jan 6, 2025
1 parent 4f9f1e0 commit 175710b
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 7 deletions.
66 changes: 66 additions & 0 deletions cmd/gochan-migration/internal/pre2021/announcements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package pre2021

import (
"errors"
"time"

"github.com/gochan-org/gochan/cmd/gochan-migration/internal/common"
"github.com/gochan-org/gochan/pkg/gcsql"
)

func (*Pre2021Migrator) migrateAnnouncementsInPlace() error {
return common.NewMigrationError("pre2021", "migrateAnnouncementsInPlace not implemented")
}

func (m *Pre2021Migrator) migrateAnnouncementsToNewDB() error {
errEv := common.LogError()
rows, err := m.db.QuerySQL(announcementsQuery)
if err != nil {
errEv.Err(err).Caller().Msg("Failed to get announcements")
return err
}
defer rows.Close()

if _, err = m.getMigrationUser(errEv); err != nil {
return err
}

for rows.Next() {
var id int
var subject, message, staff string
var timestamp time.Time
if err = rows.Scan(&id, &subject, &message, &staff, &timestamp); err != nil {
errEv.Err(err).Caller().Msg("Failed to scan announcement row")
return err
}
staffID, err := gcsql.GetStaffID(staff)
if errors.Is(err, gcsql.ErrUnrecognizedUsername) {
// user doesn't exist, use migration user
common.LogWarning().Str("staff", staff).Msg("Staff username not found in database")
message += "\n(originally by " + staff + ")"
} else if err != nil {
errEv.Err(err).Caller().Str("staff", staff).Msg("Failed to get staff ID")
return err
}
if _, err = gcsql.ExecSQL(
"INSERT INTO DBPREFIXannouncements(staff_id,subject,message,timestamp) values(?,?,?,?)",
staffID, subject, message, timestamp,
); err != nil {
errEv.Err(err).Caller().Str("staff", staff).Msg("Failed to migrate announcement")
return err
}
}

if err = rows.Close(); err != nil {
errEv.Err(err).Caller().Msg("Failed to close announcement rows")
return err
}
return nil
}

func (m *Pre2021Migrator) MigrateAnnouncements() error {
if m.IsMigratingInPlace() {
return m.migrateAnnouncementsInPlace()
}
return m.migrateAnnouncementsToNewDB()
}
15 changes: 8 additions & 7 deletions cmd/gochan-migration/internal/pre2021/pre2021.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,19 @@ func (m *Pre2021Migrator) MigrateDB() (bool, error) {
if err = m.MigrateStaff(); err != nil {
return false, err
}
common.LogInfo().Msg("Migrated staff successfully")

if err = m.MigrateBans(); err != nil {
return false, err
}
// if err = m.MigrateAnnouncements(); err != nil {
// return false, err
// }
common.LogInfo().Msg("Migrated bans and filters successfully")

return true, nil
}
if err = m.MigrateAnnouncements(); err != nil {
return false, err
}
common.LogInfo().Msg("Migrated staff announcements successfully")

func (*Pre2021Migrator) MigrateAnnouncements() error {
return common.NewMigrationError("pre2021", "MigrateAnnouncements not yet implemented")
return true, nil
}

func (m *Pre2021Migrator) Close() error {
Expand Down
11 changes: 11 additions & 0 deletions cmd/gochan-migration/internal/pre2021/pre2021_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,14 @@ func setupMigrationTest(t *testing.T, outDir string, migrateInPlace bool) *Pre20

return migrator
}

func TestPre2021Migration(t *testing.T) {
outDir := t.TempDir()
migrator := setupMigrationTest(t, outDir, false)
if !assert.False(t, migrator.IsMigratingInPlace(), "This test should not be migrating in place") {
t.FailNow()
}
migrated, err := migrator.MigrateDB()
assert.True(t, migrated)
assert.NoError(t, err)
}
2 changes: 2 additions & 0 deletions cmd/gochan-migration/internal/pre2021/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ bumped, stickied, locked FROM DBPREFIXposts WHERE deleted_timestamp IS NULL`

bansQuery = `SELECT id, allow_read, COALESCE(ip, '') as ip, name, name_is_regex, filename, file_checksum, boards, staff,
timestamp, expires, permaban, reason, type, staff_note, appeal_at, can_appeal FROM DBPREFIXbanlist`

announcementsQuery = "SELECT id, subject, message, poster, timestamp FROM DBPREFIXannouncements"
)
Binary file modified tools/gochan-pre2021.sqlite3db
Binary file not shown.

0 comments on commit 175710b

Please sign in to comment.