1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-19 16:46:23 -07:00
gochan/cmd/gochan-migration/main.go

110 lines
3.2 KiB
Go
Raw Normal View History

package main
import (
2022-02-07 18:28:27 -08:00
"flag"
2022-08-14 14:27:58 -07:00
"log"
2021-06-24 16:41:26 -07:00
"github.com/gochan-org/gochan/cmd/gochan-migration/internal/common"
2022-02-07 18:28:27 -08:00
"github.com/gochan-org/gochan/cmd/gochan-migration/internal/pre2021"
"github.com/gochan-org/gochan/pkg/config"
2022-08-14 14:27:58 -07:00
"github.com/gochan-org/gochan/pkg/gcsql"
2021-06-24 16:41:26 -07:00
)
const (
banner = `Welcome to the gochan database migration tool for gochan %s!
This migration tool is currently very unstable, and will likely go through
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.
`
migrateCompleteTxt = `Database migration successful!
To migrate the uploads for each board, move or copy the uploads to /path/to/gochan/document/root/<boardname>/src/
Then copy the thumbnails to /path/to/gochan/documentroot/<boardname>/thumb/
Then start the gochan server and go to http://yoursite/manage/rebuildall to generate the html files
for the threads and board pages`
allowedDirActions = "Valid values are noaction, copy, and move (defaults to noaction if unset)"
)
var (
versionStr string
)
func main() {
2022-02-07 18:28:27 -08:00
var options common.MigrationOptions
2022-08-14 14:27:58 -07:00
var dirAction string
2022-08-14 14:27:58 -07:00
log.SetFlags(0)
2022-02-07 18:28:27 -08:00
config.InitConfig(versionStr)
flag.StringVar(&options.ChanType, "oldchan", "", "The imageboard we are migrating from (currently only pre2021 is supported, but more are coming")
flag.StringVar(&options.OldChanConfig, "oldconfig", "", "The path to the old chan's configuration file")
/* flag.StringVar(&dirAction, "diraction", "",
"Action taken on each board directory after it has been migrated. "+allowedDirActions) */
2022-02-07 18:28:27 -08:00
flag.Parse()
2022-02-07 18:28:27 -08:00
if options.ChanType == "" || options.OldChanConfig == "" {
flag.PrintDefaults()
2022-08-14 14:27:58 -07:00
log.Fatal("Missing required oldchan value")
2022-02-07 18:28:27 -08:00
return
}
switch dirAction {
case "":
fallthrough
case "noaction":
options.DirAction = common.DirNoAction
case "copy":
options.DirAction = common.DirCopy
case "move":
options.DirAction = common.DirMove
default:
2022-08-14 14:27:58 -07:00
log.Fatalln("Invalid diraction value. " + allowedDirActions)
}
2022-08-14 14:27:58 -07:00
log.Printf(banner, versionStr)
2022-02-07 18:28:27 -08:00
var migrator common.DBMigrator
switch options.ChanType {
case "pre2021":
migrator = &pre2021.Pre2021Migrator{}
case "kusabax":
fallthrough
case "tinyboard":
fallthrough
default:
2022-08-14 14:27:58 -07:00
log.Fatalf(
2022-02-07 18:28:27 -08:00
"Unsupported chan type %q, Currently only pre2021 database migration is supported\n",
options.ChanType)
return
2022-02-07 18:28:27 -08:00
}
config.InitConfig(versionStr)
systemCritical := config.GetSystemCriticalConfig()
err := gcsql.ConnectToDB(
systemCritical.DBhost, systemCritical.DBtype, systemCritical.DBname,
systemCritical.DBusername, systemCritical.DBpassword, systemCritical.DBprefix)
if err != nil {
log.Fatalf("Failed to connect to the database: %s", err.Error())
}
if err = gcsql.CheckAndInitializeDatabase(systemCritical.DBtype); err != nil {
log.Fatalf("Failed to initialize the database: %s", err.Error())
}
defer gcsql.Close()
if err = migrator.Init(&options); err != nil {
2022-08-14 14:27:58 -07:00
log.Fatalf("Unable to initialize %s migrator: %s\n",
options.ChanType, err.Error())
return
2022-02-07 18:28:27 -08:00
}
defer migrator.Close()
var migrated bool
if migrated, err = migrator.MigrateDB(); err != nil {
2022-08-14 14:27:58 -07:00
log.Fatalln("Error migrating database:", err.Error())
2022-02-07 18:28:27 -08:00
}
if migrated {
2022-08-14 14:27:58 -07:00
log.Fatalf("Database is already migrated")
}
2022-08-14 14:27:58 -07:00
log.Println(migrateCompleteTxt)
}