2022-02-09 10:32:36 -08:00
|
|
|
package pre2021
|
|
|
|
|
|
|
|
import (
|
2022-08-14 14:27:58 -07:00
|
|
|
"log"
|
|
|
|
|
2022-02-09 10:32:36 -08:00
|
|
|
"github.com/gochan-org/gochan/pkg/gcsql"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (m *Pre2021Migrator) MigrateBoards() error {
|
2022-02-19 14:55:45 -08:00
|
|
|
if m.oldBoards == nil {
|
|
|
|
m.oldBoards = map[int]string{}
|
|
|
|
}
|
|
|
|
if m.newBoards == nil {
|
|
|
|
m.newBoards = map[int]string{}
|
|
|
|
}
|
2022-02-09 10:32:36 -08:00
|
|
|
// get all boards from new db
|
2022-10-11 14:26:31 -07:00
|
|
|
err := gcsql.ResetBoardSectionArrays()
|
2022-02-09 10:32:36 -08:00
|
|
|
if err != nil {
|
2022-10-11 14:26:31 -07:00
|
|
|
return nil
|
2022-02-09 10:32:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// get boards from old db
|
2022-02-24 17:26:29 -08:00
|
|
|
rows, err := m.db.QuerySQL(boardsQuery)
|
2022-02-09 10:32:36 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-31 12:43:39 -07:00
|
|
|
defer rows.Close()
|
2022-02-09 10:32:36 -08:00
|
|
|
for rows.Next() {
|
2022-02-17 22:45:47 -08:00
|
|
|
var id int
|
2022-02-09 10:32:36 -08:00
|
|
|
var dir string
|
|
|
|
var title string
|
|
|
|
var subtitle string
|
|
|
|
var description string
|
|
|
|
var section int
|
|
|
|
var max_file_size int
|
|
|
|
var max_pages int
|
|
|
|
var default_style string
|
|
|
|
var locked bool
|
|
|
|
var anonymous string
|
|
|
|
var forced_anon bool
|
|
|
|
var max_age int
|
|
|
|
var autosage_after int
|
|
|
|
var no_images_after int
|
|
|
|
var max_message_length int
|
|
|
|
var embeds_allowed bool
|
|
|
|
var redirect_to_thread bool
|
|
|
|
var require_file bool
|
|
|
|
var enable_catalog bool
|
2022-02-19 14:55:45 -08:00
|
|
|
if err = rows.Scan(
|
|
|
|
&id,
|
|
|
|
&dir,
|
2022-02-09 10:32:36 -08:00
|
|
|
&title,
|
|
|
|
&subtitle,
|
|
|
|
&description,
|
|
|
|
§ion,
|
|
|
|
&max_file_size,
|
|
|
|
&max_pages,
|
|
|
|
&default_style,
|
|
|
|
&locked,
|
|
|
|
&anonymous,
|
|
|
|
&forced_anon,
|
|
|
|
&max_age,
|
|
|
|
&autosage_after,
|
|
|
|
&no_images_after,
|
|
|
|
&max_message_length,
|
|
|
|
&embeds_allowed,
|
|
|
|
&redirect_to_thread,
|
|
|
|
&require_file,
|
|
|
|
&enable_catalog); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
found := false
|
2022-10-11 14:26:31 -07:00
|
|
|
for b := range gcsql.AllBoards {
|
2022-02-17 22:45:47 -08:00
|
|
|
if _, ok := m.oldBoards[id]; !ok {
|
|
|
|
m.oldBoards[id] = dir
|
|
|
|
}
|
2022-10-11 14:26:31 -07:00
|
|
|
if gcsql.AllBoards[b].Dir == dir {
|
2022-08-14 14:27:58 -07:00
|
|
|
log.Printf("Board /%s/ already exists in new db, moving on\n", dir)
|
2022-02-09 10:32:36 -08:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// create new board using the board data from the old db
|
|
|
|
// omitting things like ID and creation date since we don't really care
|
|
|
|
if err = gcsql.CreateBoard(&gcsql.Board{
|
2022-10-31 12:43:39 -07:00
|
|
|
Dir: dir,
|
|
|
|
Title: title,
|
|
|
|
Subtitle: subtitle,
|
|
|
|
Description: description,
|
|
|
|
SectionID: section,
|
|
|
|
MaxFilesize: max_file_size,
|
|
|
|
// MaxPages: max_pages,
|
|
|
|
DefaultStyle: default_style,
|
|
|
|
Locked: locked,
|
|
|
|
AnonymousName: anonymous,
|
|
|
|
ForceAnonymous: forced_anon,
|
|
|
|
// MaxAge: max_age,
|
2022-02-09 10:32:36 -08:00
|
|
|
AutosageAfter: autosage_after,
|
|
|
|
NoImagesAfter: no_images_after,
|
|
|
|
MaxMessageLength: max_message_length,
|
2022-10-11 14:26:31 -07:00
|
|
|
AllowEmbeds: embeds_allowed,
|
2022-02-09 10:32:36 -08:00
|
|
|
RedirectToThread: redirect_to_thread,
|
|
|
|
RequireFile: require_file,
|
|
|
|
EnableCatalog: enable_catalog,
|
2022-10-11 14:26:31 -07:00
|
|
|
}, false); err != nil {
|
2022-02-09 10:32:36 -08:00
|
|
|
return err
|
|
|
|
}
|
2022-02-17 22:45:47 -08:00
|
|
|
m.newBoards[id] = dir
|
2022-08-14 14:27:58 -07:00
|
|
|
log.Printf("/%s/ successfully migrated in the database", dir)
|
2022-03-06 10:28:31 -08:00
|
|
|
// Automatic directory migration has the potential to go horribly wrong, so I'm leaving this
|
|
|
|
// commented out for now
|
2022-02-10 17:11:10 -08:00
|
|
|
// switch m.options.DirAction {
|
|
|
|
// case common.DirCopy:
|
2022-02-09 10:32:36 -08:00
|
|
|
|
2022-02-10 17:11:10 -08:00
|
|
|
// case common.DirMove:
|
|
|
|
// // move the old directory (probably should copy instead) to the new one
|
|
|
|
// newDocumentRoot := config.GetSystemCriticalConfig().DocumentRoot
|
2022-08-14 14:27:58 -07:00
|
|
|
// log.Println("Old board path:", path.Join(m.config.DocumentRoot, dir))
|
|
|
|
// log.Println("Old board path:", path.Join(newDocumentRoot, dir))
|
2022-02-10 17:11:10 -08:00
|
|
|
// if err = os.Rename(
|
|
|
|
// path.Join(m.config.DocumentRoot, dir),
|
|
|
|
// path.Join(newDocumentRoot, dir),
|
|
|
|
// ); err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
2022-08-14 14:27:58 -07:00
|
|
|
// log.Printf("/%s/ directory/files successfully moved")
|
2022-02-10 17:11:10 -08:00
|
|
|
// }
|
2022-02-09 10:32:36 -08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|