From 175710b43b3ea693e2e158658d7db7eeaa4fa1f1 Mon Sep 17 00:00:00 2001 From: Eggbertx Date: Sun, 5 Jan 2025 17:23:18 -0800 Subject: [PATCH] Add announcement migration --- .../internal/pre2021/announcements.go | 66 ++++++++++++++++++ .../internal/pre2021/pre2021.go | 15 ++-- .../internal/pre2021/pre2021_test.go | 11 +++ .../internal/pre2021/queries.go | 2 + tools/gochan-pre2021.sqlite3db | Bin 81920 -> 81920 bytes 5 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 cmd/gochan-migration/internal/pre2021/announcements.go diff --git a/cmd/gochan-migration/internal/pre2021/announcements.go b/cmd/gochan-migration/internal/pre2021/announcements.go new file mode 100644 index 00000000..e885e296 --- /dev/null +++ b/cmd/gochan-migration/internal/pre2021/announcements.go @@ -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, ×tamp); 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() +} diff --git a/cmd/gochan-migration/internal/pre2021/pre2021.go b/cmd/gochan-migration/internal/pre2021/pre2021.go index 105c14a0..a4fe542b 100644 --- a/cmd/gochan-migration/internal/pre2021/pre2021.go +++ b/cmd/gochan-migration/internal/pre2021/pre2021.go @@ -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 { diff --git a/cmd/gochan-migration/internal/pre2021/pre2021_test.go b/cmd/gochan-migration/internal/pre2021/pre2021_test.go index e28eb26c..71a651b9 100644 --- a/cmd/gochan-migration/internal/pre2021/pre2021_test.go +++ b/cmd/gochan-migration/internal/pre2021/pre2021_test.go @@ -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) +} diff --git a/cmd/gochan-migration/internal/pre2021/queries.go b/cmd/gochan-migration/internal/pre2021/queries.go index 526a00a1..b8fbbc68 100644 --- a/cmd/gochan-migration/internal/pre2021/queries.go +++ b/cmd/gochan-migration/internal/pre2021/queries.go @@ -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" ) diff --git a/tools/gochan-pre2021.sqlite3db b/tools/gochan-pre2021.sqlite3db index 04ab152fb15a32b7ac781003429af6abedf74943..9c99c2aba1ac978c25bc47d4d5982d02f3015c20 100644 GIT binary patch delta 54 zcmZo@U~On%ogmGqIZ?)$QFCL$GJci~4E!553kodbXZK@dV=y&UpS)jQbF#gkSo1ge K?cd}XxfB4go)8EC delta 50 zcmZo@U~On%ogmGqF;T{uQDbAmGJcjF4E#Gb3nr}NXLn;{V=y%ppS)dOv-ySm_80Pu G5()r)Dh~<(