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

Properly migrate board and sections, and have board point to correct section

This commit is contained in:
Eggbertx 2025-01-02 17:00:25 -08:00
parent beb048716e
commit 525c5953d9
3 changed files with 80 additions and 35 deletions

View file

@ -40,48 +40,59 @@ func (m *Pre2021Migrator) migrateBoardsInPlace() error {
func (m *Pre2021Migrator) migrateSectionsToNewDB() error {
// creates sections in the new db if they don't exist, and also creates a migration section that
// boards will be set to, to be moved to the correct section by the admin after migration
errEv := common.LogError()
defer errEv.Discard()
// populate m.sections with all sections from the new db
currentAllSections, err := gcsql.GetAllSections(false)
if err != nil {
errEv.Err(err).Caller().Msg("Failed to get all sections from new db")
return err
}
for _, section := range currentAllSections {
m.sections = append(m.sections, migrationSection{
oldID: -1,
Section: section,
})
}
rows, err := m.db.QuerySQL(sectionsQuery)
if err != nil {
errEv.Err(err).Caller().Msg("Failed to query old database sections")
return err
}
defer rows.Close()
errEv := common.LogError()
defer errEv.Discard()
for rows.Next() {
var section gcsql.Section
if err = rows.Scan(
&section.ID,
&section.Position,
&section.Hidden,
&section.Name,
&section.Abbreviation,
); err != nil {
if err = rows.Scan(&section.ID, &section.Position, &section.Hidden, &section.Name, &section.Abbreviation); err != nil {
errEv.Err(err).Caller().Msg("Failed to scan row into section")
return err
}
m.sections = append(m.sections, migrationSection{
oldID: section.ID,
Section: section,
})
var found bool
for s, newSection := range m.sections {
if section.Name == newSection.Name {
m.sections[s].oldID = section.ID
for _, newSection := range gcsql.AllSections {
if newSection.Name == section.Name || newSection.Abbreviation == section.Abbreviation {
common.LogWarning().Str("section", section.Name).Msg("Section already exists in new db, moving on")
m.sections[len(m.sections)-1].ID = newSection.ID
found = true
break
}
}
if _, err = gcsql.NewSection(section.Name, section.Abbreviation, false, section.Position); err != nil {
errEv.Err(err).Caller().
Str("sectionName", section.Name).
Msg("Failed to create section")
return err
if !found {
migratedSection, err := gcsql.NewSection(section.Name, section.Abbreviation, section.Hidden, section.Position)
if err != nil {
errEv.Err(err).Caller().Str("sectionName", section.Name).Msg("Failed to migrate section")
return err
}
m.sections = append(m.sections, migrationSection{
Section: *migratedSection,
})
}
}
if err = rows.Close(); err != nil {
errEv.Caller().Msg("Failed to close section rows")
return err
}
return err
return nil
}
func (m *Pre2021Migrator) migrateBoardsToNewDB() error {
@ -99,7 +110,7 @@ func (m *Pre2021Migrator) migrateBoardsToNewDB() error {
}
if err = m.migrateSectionsToNewDB(); err != nil {
errEv.Err(err).Caller().Msg("Failed to migrate sections")
// error already logged
return err
}
@ -110,6 +121,7 @@ func (m *Pre2021Migrator) migrateBoardsToNewDB() error {
return err
}
defer rows.Close()
for rows.Next() {
var board migrationBoard
var maxPages int
@ -134,9 +146,18 @@ func (m *Pre2021Migrator) migrateBoardsToNewDB() error {
break
}
}
if !found {
for _, section := range m.sections {
if section.oldID == board.oldSectionID {
board.SectionID = section.ID
break
}
}
}
m.boards[board.Dir] = board
if found {
// TODO: update board title, subtitle, section etc. in new db
continue
}
@ -148,6 +169,10 @@ func (m *Pre2021Migrator) migrateBoardsToNewDB() error {
}
common.LogInfo().Str("board", board.Dir).Msg("Board successfully created")
}
if err = gcsql.ResetBoardSectionArrays(); err != nil {
errEv.Err(err).Caller().Msg("Failed to reset board and section arrays")
return err
}
return nil
}

View file

@ -14,7 +14,7 @@ const (
sqlite3DBPath = "tools/gochan-pre2021.sqlite3db" // relative to gochan project root
)
func setupMigrationTest(t *testing.T) *Pre2021Migrator {
func setupMigrationTest(t *testing.T, outDir string) *Pre2021Migrator {
dir, err := testutil.GoToGochanRoot(t)
if !assert.NoError(t, err) {
t.FailNow()
@ -40,9 +40,10 @@ func setupMigrationTest(t *testing.T) *Pre2021Migrator {
t.FailNow()
}
migrator.db = db
migratedDBPath := path.Join(outDir, "gochan-migrated.sqlite3db")
t.Log("Migrated DB path:", migratedDBPath)
outDir := t.TempDir()
config.SetTestDBConfig("sqlite3", path.Join(outDir, "gochan-migrated.sqlite3db"), "gochan-migrated.sqlite3db", "gochan", "password", "gc_")
config.SetTestDBConfig("sqlite3", migratedDBPath, path.Base(migratedDBPath), "gochan", "password", "gc_")
sqlConfig := config.GetSQLConfig()
sqlConfig.DBTimeoutSeconds = 600
@ -56,26 +57,45 @@ func setupMigrationTest(t *testing.T) *Pre2021Migrator {
return migrator
}
func TestMigrateToNewDB(t *testing.T) {
migrator := setupMigrationTest(t)
func TestMigrateBoardsToNewDB(t *testing.T) {
outDir := t.TempDir()
migrator := setupMigrationTest(t, outDir)
assert.NoError(t, gcsql.ResetBoardSectionArrays())
numBoards := len(gcsql.AllBoards)
numSections := len(gcsql.AllSections)
assert.Equal(t, 1, numBoards, "Expected to have 1 board pre-migration (/test/ is automatically created during provisioning)")
assert.Equal(t, 1, numSections, "Expected to have 1 section pre-migration (Main is automatically created during provisioning)")
assert.NoError(t, migrator.migrateBoardsToNewDB())
newBoards, err := gcsql.GetAllBoards(false)
migratedBoards, err := gcsql.GetAllBoards(false)
if !assert.NoError(t, err) {
return
t.FailNow()
}
assert.GreaterOrEqual(t, len(newBoards), 2, "Expected new boards list to have at least 2 boards") // old DB has 2 boards, /test/ and /hidden/
migratedSections, err := gcsql.GetAllSections(false)
if !assert.NoError(t, err) {
t.FailNow()
}
assert.Equal(t, len(migratedBoards), 2, "Expected updated boards list to have two boards")
assert.Equal(t, len(migratedSections), 2, "Expected updated sections list to have two sections")
hiddenBoard, err := gcsql.GetBoardFromDir("hidden")
if !assert.NoError(t, err) {
return
t.FailNow()
}
t.Logf("Hidden board section ID: %d", hiddenBoard.SectionID)
t.Log("Number of sections:", len(migratedSections))
for _, section := range migratedSections {
t.Logf("Section ID %d: %#v", section.ID, section)
}
hiddenSection, err := gcsql.GetSectionFromID(hiddenBoard.SectionID)
if !assert.NoError(t, err) {
return
t.FailNow()
}
assert.Equal(t, "Hidden", hiddenSection.Name, "Expected Hidden section to have name 'Hidden'")
assert.Equal(t, "Hidden section", hiddenSection.Name, "Expected /hidden/ board's section to have name 'Hidden'")
assert.True(t, hiddenSection.Hidden, "Expected Hidden section to be hidden")
}

Binary file not shown.