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

144 lines
4 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"
"os"
2024-08-10 20:01:33 -07:00
"strconv"
2021-06-24 16:41:26 -07:00
"github.com/gochan-org/gochan/cmd/gochan-migration/internal/common"
"github.com/gochan-org/gochan/cmd/gochan-migration/internal/gcupdate"
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!
2024-08-10 20:01:33 -07:00
Make sure you check the README and/or the -h command line flag, and back up your current database before you use it.
2021-06-24 16:41:26 -07:00
`
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 (
2024-08-10 20:01:33 -07:00
versionStr string
migrator common.DBMigrator
dbVersionStr string
)
func cleanup() int {
returnVal := 0
var err error
if migrator != nil {
if err = migrator.Close(); err != nil {
returnVal = 1
log.Println("Error closing migrator:", err.Error())
}
}
if err = gcsql.Close(); err != nil {
returnVal = 1
log.Println("Error closing SQL connection:", err.Error())
}
return returnVal
}
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
var updateDB bool
2022-08-14 14:27:58 -07:00
log.SetFlags(0)
flag.BoolVar(&updateDB, "updatedb", false, "If this is set, gochan-migrate will check, and if needed, update gochan's database schema")
2022-02-07 18:28:27 -08:00
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()
config.InitConfig(versionStr)
if !updateDB && (options.ChanType == "" || options.OldChanConfig == "") {
2022-02-07 18:28:27 -08:00
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
} else if updateDB {
options.ChanType = "gcupdate"
2022-02-07 18:28:27 -08:00
}
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
switch options.ChanType {
case "gcupdate":
2024-08-10 20:01:33 -07:00
targetDBVer, err := strconv.Atoi(dbVersionStr)
if err != nil {
log.Fatalf("Invalid database version string %q, unable to parse to int", dbVersionStr)
}
migrator = &gcupdate.GCDatabaseUpdater{
TargetDBVer: targetDBVer,
}
2022-02-07 18:28:27 -08:00
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)
var err error
if !updateDB {
sqlCfg := config.GetSQLConfig()
err = gcsql.ConnectToDB(&sqlCfg)
if err != nil {
log.Fatalf("Failed to connect to the database: %s", err.Error())
}
2024-08-10 20:01:33 -07:00
if err = gcsql.CheckAndInitializeDatabase(sqlCfg.DBtype, dbVersionStr); err != nil {
log.Fatalf("Failed to initialize the database: %s", err.Error())
}
}
if err = migrator.Init(&options); err != nil {
cleanup()
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
}
var migrated bool
if migrated, err = migrator.MigrateDB(); err != nil {
cleanup()
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 {
log.Println("Database is already migrated")
return
}
if updateDB {
log.Println("Database schema updated successfully")
} else {
log.Println(migrateCompleteTxt)
}
os.Exit(cleanup())
}