mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-08-18 07:36:24 -07:00
Implement staff and ban migration in place, and the respective tests
This commit is contained in:
parent
f0f0f055da
commit
d3c985bd50
5 changed files with 110 additions and 5 deletions
|
@ -5,6 +5,7 @@ import (
|
|||
"database/sql"
|
||||
"errors"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -38,7 +39,31 @@ type migrationBan struct {
|
|||
}
|
||||
|
||||
func (m *Pre2021Migrator) migrateBansInPlace() error {
|
||||
return common.NewMigrationError("pre2021", "migrateBansInPlace not yet implemented")
|
||||
errEv := common.LogError()
|
||||
defer errEv.Discard()
|
||||
initSQLPath := gcutil.FindResource("sql/initdb_" + m.db.SQLDriver() + ".sql")
|
||||
ba, err := os.ReadFile(initSQLPath)
|
||||
if err != nil {
|
||||
errEv.Err(err).Caller().
|
||||
Str("initDBFile", initSQLPath).
|
||||
Msg("Failed to read initdb file")
|
||||
return err
|
||||
}
|
||||
statements := strings.Split(string(ba), ";")
|
||||
for _, stmt := range statements {
|
||||
stmt = strings.TrimSpace(stmt)
|
||||
if strings.HasPrefix(stmt, "CREATE TABLE DBPREFIXip_ban") || strings.HasPrefix(stmt, "CREATE TABLE DBPREFIXfilter") {
|
||||
_, err = gcsql.ExecSQL(stmt)
|
||||
if err != nil {
|
||||
errEv.Err(err).Caller().
|
||||
Str("statement", stmt).
|
||||
Msg("Failed to create table")
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
// since the table names are different, migrateBansToNewDB can be called directly to migrate bans
|
||||
return m.migrateBansToNewDB()
|
||||
}
|
||||
|
||||
func (m *Pre2021Migrator) migrateBan(tx *sql.Tx, ban *migrationBan, boardID *int, errEv *zerolog.Event) error {
|
||||
|
|
|
@ -51,3 +51,48 @@ func TestMigrateBansToNewDB(t *testing.T) {
|
|||
}
|
||||
assert.Equal(t, 3, len(conditions), "Expected filter to have three conditions")
|
||||
}
|
||||
|
||||
func TestMigrateBansInPlace(t *testing.T) {
|
||||
outDir := t.TempDir()
|
||||
migrator := setupMigrationTest(t, outDir, true)
|
||||
if !assert.True(t, migrator.IsMigratingInPlace(), "This test should be migrating in place") {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
if !assert.NoError(t, migrator.MigrateBoards()) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
if !assert.NoError(t, migrator.MigratePosts()) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
if !assert.NoError(t, migrator.MigrateStaff()) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
if !assert.NoError(t, migrator.MigrateBans()) {
|
||||
t.FailNow()
|
||||
}
|
||||
bans, err := gcsql.GetIPBans(0, 200, false)
|
||||
if !assert.NoError(t, err) {
|
||||
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}))
|
||||
assert.Equal(t, 0, numInvalidBans, "Expected the invalid test to not be migrated")
|
||||
|
||||
filters, err := gcsql.GetAllFilters(gcsql.TrueOrFalse)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
assert.Equal(t, 1, len(filters))
|
||||
conditions, err := filters[0].Conditions()
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
assert.Equal(t, 3, len(conditions), "Expected filter to have three conditions")
|
||||
}
|
||||
|
|
|
@ -52,4 +52,9 @@ var (
|
|||
"ALTER TABLE DBPREFIXposts ADD COLUMN country VARCHAR(80) NOT NULL DEFAULT ''",
|
||||
"UPDATE DBPREFIXposts SET is_top_post = TRUE WHERE thread_id = 0",
|
||||
}
|
||||
staffAlterStatements = []string{
|
||||
"ALTER TABLE DBPREFIXstaff RENAME COLUMN rank TO global_rank",
|
||||
"ALTER TABLE DBPREFIXstaff RENAME COLUMN last_active TO last_login",
|
||||
"ALTER TABLE DBPREFIXstaff ADD COLUMN is_active BOOL NOT NULL DEFAULT TRUE",
|
||||
}
|
||||
)
|
||||
|
|
|
@ -11,10 +11,23 @@ import (
|
|||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
func (*Pre2021Migrator) migrateStaffInPlace() error {
|
||||
err := common.NewMigrationError("pre2021", "migrateStaff not yet implemented")
|
||||
common.LogError().Err(err).Caller().Msg("Failed to migrate staff")
|
||||
return err
|
||||
func (m *Pre2021Migrator) migrateStaffInPlace() error {
|
||||
errEv := common.LogError()
|
||||
defer errEv.Discard()
|
||||
|
||||
for _, stmt := range staffAlterStatements {
|
||||
if _, err := gcsql.ExecSQL(stmt); err != nil {
|
||||
errEv.Err(err).Caller().Msg("Failed to alter staff table")
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
_, err := m.getMigrationUser(errEv)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Pre2021Migrator) getMigrationUser(errEv *zerolog.Event) (*gcsql.Staff, error) {
|
||||
|
|
|
@ -27,3 +27,20 @@ func TestMigrateStaffToNewDB(t *testing.T) {
|
|||
}
|
||||
assert.Equal(t, 3, migratedAdmin.Rank)
|
||||
}
|
||||
|
||||
func TestMigrateStaffInPlace(t *testing.T) {
|
||||
outDir := t.TempDir()
|
||||
migrator := setupMigrationTest(t, outDir, true)
|
||||
if !assert.True(t, migrator.IsMigratingInPlace(), "This test should be migrating in place") {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
if !assert.NoError(t, migrator.MigrateStaff()) {
|
||||
t.FailNow()
|
||||
}
|
||||
migratedAdmin, err := gcsql.GetStaffByUsername("migratedadmin", true)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
assert.Equal(t, 3, migratedAdmin.Rank)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue