1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-09-04 10:06:24 -07:00

Add stuff for migrating boards to gochan-migration

This commit is contained in:
Eggbertx 2022-02-09 10:32:36 -08:00
parent 3491a7b7b8
commit 64123e8e6b
3 changed files with 173 additions and 27 deletions

View file

@ -0,0 +1,145 @@
package pre2021
import (
"os"
"path"
"github.com/gochan-org/gochan/pkg/config"
"github.com/gochan-org/gochan/pkg/gclog"
"github.com/gochan-org/gochan/pkg/gcsql"
)
func (m *Pre2021Migrator) MigrateBoards() error {
// get all boards from new db
boards, err := gcsql.GetAllBoards()
if err != nil {
return err
}
// get boards from old db
rows, err := m.db.QuerySQL(`SELECT
dir,
type,
upload_type,
title,
subtitle,
description,
section,
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
FROM DBPREFIXboards`)
if err != nil {
return err
}
for rows.Next() {
var dir string
var board_type int
var upload_type int
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
if err = rows.Scan(&dir,
&board_type,
&upload_type,
&title,
&subtitle,
&description,
&section,
&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
for _, board := range boards {
if board.Dir == dir {
gclog.Printf(gclog.LStdLog, "Board /%s/ already exists in new db, moving on\n", dir)
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{
Dir: dir,
Type: board_type, // ??
UploadType: upload_type, // ??
Title: title,
Subtitle: subtitle,
Description: description,
Section: section,
MaxFilesize: max_file_size,
MaxPages: max_pages,
DefaultStyle: default_style,
Locked: locked,
Anonymous: anonymous,
ForcedAnon: forced_anon,
MaxAge: max_age,
AutosageAfter: autosage_after,
NoImagesAfter: no_images_after,
MaxMessageLength: max_message_length,
EmbedsAllowed: embeds_allowed,
RedirectToThread: redirect_to_thread,
RequireFile: require_file,
EnableCatalog: enable_catalog,
}); err != nil {
return err
}
gclog.Printf(gclog.LStdLog, "/%s/ successfully migrated in the database")
// move the old directory (probably should copy instead) to the new one
newDocumentRoot := config.GetSystemCriticalConfig().DocumentRoot
gclog.Println(gclog.LStdLog, "Old board path:", path.Join(m.config.DocumentRoot, dir))
gclog.Println(gclog.LStdLog, "Old board path:", path.Join(newDocumentRoot, dir))
if err = os.Rename(
path.Join(m.config.DocumentRoot, dir),
path.Join(newDocumentRoot, dir),
); err != nil {
return err
}
gclog.Printf(gclog.LStdLog, "/%s/ directory/files successfully moved")
}
return nil
}

View file

@ -19,12 +19,13 @@ const (
)
type Pre2021Config struct {
DBtype string
DBhost string
DBname string
DBusername string
DBpassword string
DBprefix string
DBtype string
DBhost string
DBname string
DBusername string
DBpassword string
DBprefix string
DocumentRoot string
}
type Pre2021Migrator struct {
@ -43,8 +44,8 @@ func (m *Pre2021Migrator) readConfig() error {
func (m *Pre2021Migrator) Init(options common.MigrationOptions) error {
m.options = options
err := m.readConfig()
if err != nil {
var err error
if err = m.readConfig(); err != nil {
return err
}
m.db, err = gcsql.Open(
@ -74,10 +75,6 @@ func (m *Pre2021Migrator) MigrateDB() error {
return nil
}
func (m *Pre2021Migrator) MigrateBoards() error {
return nil
}
func (m *Pre2021Migrator) MigratePosts() error {
return nil
}

View file

@ -2,15 +2,13 @@ package main
import (
"flag"
"fmt"
"os"
"github.com/gochan-org/gochan/cmd/gochan-migration/internal/common"
"github.com/gochan-org/gochan/cmd/gochan-migration/internal/pre2021"
"github.com/gochan-org/gochan/pkg/config"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
"github.com/gochan-org/gochan/pkg/gclog"
"github.com/gochan-org/gochan/pkg/gcsql"
)
const (
@ -20,17 +18,13 @@ several changes before it can be considered "stable", so make sure you check
the README and/or the -h command line flag before you use it.
`
fatalLogFlags = gclog.LFatal | gclog.LErrorLog | gclog.LStdLog
)
var (
versionStr string
)
func fatalPrintln(args ...interface{}) {
fmt.Println(args...)
os.Exit(1)
}
func main() {
var options common.MigrationOptions
@ -42,12 +36,12 @@ func main() {
if options.ChanType == "" || options.OldChanConfig == "" {
flag.PrintDefaults()
fmt.Println("Missing required database connection info")
gclog.Println(fatalLogFlags, "Missing required database connection info")
os.Exit(1)
return
}
fmt.Printf(banner, versionStr)
gclog.Printf(gclog.LAccessLog, banner, versionStr)
var migrator common.DBMigrator
switch options.ChanType {
case "pre2021":
@ -57,20 +51,30 @@ func main() {
case "tinyboard":
fallthrough
default:
fmt.Printf(
gclog.Printf(fatalLogFlags,
"Unsupported chan type %q, Currently only pre2021 database migration is supported\n",
options.ChanType)
os.Exit(1)
}
config.InitConfig(versionStr)
systemCritical := config.GetSystemCriticalConfig()
gcsql.ConnectToDB(
systemCritical.DBhost, systemCritical.DBtype, systemCritical.DBname,
systemCritical.DBusername, systemCritical.DBpassword, systemCritical.DBprefix)
gcsql.CheckAndInitializeDatabase(systemCritical.DBtype)
defer gcsql.Close()
err := migrator.Init(options)
if err != nil {
fmt.Printf("Unable to initialize %s migrator: %s\n", options.ChanType, err.Error())
gclog.Printf(fatalLogFlags,
"Unable to initialize %s migrator: %s\n", options.ChanType, err.Error())
os.Exit(1)
}
defer migrator.Close()
if err = migrator.MigrateDB(); err != nil {
fmt.Println("Error migrating database: ", err.Error())
gclog.Printf(fatalLogFlags, "Error migrating database: ", err.Error())
os.Exit(1)
}
fmt.Println("Database migration successful!")
gclog.Println(gclog.LStdLog, "Database migration successful!")
}