2020-06-12 11:01:28 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2022-02-07 18:28:27 -08:00
|
|
|
"flag"
|
2022-08-14 14:27:58 -07:00
|
|
|
"log"
|
2023-04-07 14:34:28 -07:00
|
|
|
"os"
|
2024-08-10 20:01:33 -07:00
|
|
|
"strconv"
|
2020-06-16 13:31:47 -07:00
|
|
|
|
2021-06-24 16:41:26 -07:00
|
|
|
"github.com/gochan-org/gochan/cmd/gochan-migration/internal/common"
|
2023-04-07 14:34:28 -07:00
|
|
|
"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
|
|
|
|
2022-02-09 10:32:36 -08: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
|
|
|
`
|
2022-02-10 17:11:10 -08: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/
|
2023-01-04 15:33:50 -08:00
|
|
|
Then start the gochan server and go to http://yoursite/manage/rebuildall to generate the html files
|
2022-02-10 17:11:10 -08:00
|
|
|
for the threads and board pages`
|
|
|
|
|
|
|
|
|
|
allowedDirActions = "Valid values are noaction, copy, and move (defaults to noaction if unset)"
|
2020-06-12 11:01:28 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2024-08-10 20:01:33 -07:00
|
|
|
versionStr string
|
|
|
|
|
migrator common.DBMigrator
|
|
|
|
|
dbVersionStr string
|
2020-06-12 11:01:28 -07:00
|
|
|
)
|
|
|
|
|
|
2024-02-07 15:15:56 -08:00
|
|
|
func cleanup() int {
|
2024-02-06 12:30:18 -08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-12 11:01:28 -07:00
|
|
|
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
|
2023-04-07 14:34:28 -07:00
|
|
|
var updateDB bool
|
2021-12-18 18:49:22 -08:00
|
|
|
|
2022-08-14 14:27:58 -07:00
|
|
|
log.SetFlags(0)
|
2023-04-07 14:34:28 -07:00
|
|
|
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")
|
2023-03-29 09:35:46 -07:00
|
|
|
// 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()
|
2021-12-18 18:49:22 -08:00
|
|
|
|
2023-03-29 09:35:46 -07:00
|
|
|
config.InitConfig(versionStr)
|
|
|
|
|
|
2023-04-07 14:34:28 -07:00
|
|
|
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
|
2023-04-07 14:34:28 -07:00
|
|
|
} else if updateDB {
|
|
|
|
|
options.ChanType = "gcupdate"
|
2022-02-07 18:28:27 -08:00
|
|
|
}
|
2022-02-10 17:11:10 -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-02-10 17:11:10 -08:00
|
|
|
}
|
2021-12-18 18:49:22 -08:00
|
|
|
|
2022-08-14 14:27:58 -07:00
|
|
|
log.Printf(banner, versionStr)
|
2022-02-07 18:28:27 -08:00
|
|
|
switch options.ChanType {
|
2023-04-07 14:34:28 -07:00
|
|
|
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)
|
2022-02-10 17:11:10 -08:00
|
|
|
return
|
2022-02-07 18:28:27 -08:00
|
|
|
}
|
2022-02-09 10:32:36 -08:00
|
|
|
config.InitConfig(versionStr)
|
2023-04-07 14:34:28 -07:00
|
|
|
var err error
|
|
|
|
|
if !updateDB {
|
2024-05-01 17:32:19 -07:00
|
|
|
sqlCfg := config.GetSQLConfig()
|
|
|
|
|
err = gcsql.ConnectToDB(&sqlCfg)
|
2023-04-07 14:34:28 -07:00
|
|
|
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 {
|
2023-04-07 14:34:28 -07:00
|
|
|
log.Fatalf("Failed to initialize the database: %s", err.Error())
|
|
|
|
|
}
|
2022-12-13 12:44:16 -08:00
|
|
|
}
|
2022-02-09 10:32:36 -08:00
|
|
|
|
2022-09-03 14:51:37 -07:00
|
|
|
if err = migrator.Init(&options); err != nil {
|
2024-02-07 15:15:56 -08:00
|
|
|
cleanup()
|
2022-08-14 14:27:58 -07:00
|
|
|
log.Fatalf("Unable to initialize %s migrator: %s\n",
|
|
|
|
|
options.ChanType, err.Error())
|
2022-02-10 17:11:10 -08:00
|
|
|
return
|
2022-02-07 18:28:27 -08:00
|
|
|
}
|
2022-02-19 14:55:45 -08:00
|
|
|
var migrated bool
|
|
|
|
|
|
|
|
|
|
if migrated, err = migrator.MigrateDB(); err != nil {
|
2024-02-07 15:15:56 -08:00
|
|
|
cleanup()
|
2022-08-14 14:27:58 -07:00
|
|
|
log.Fatalln("Error migrating database:", err.Error())
|
2022-02-07 18:28:27 -08:00
|
|
|
}
|
2022-02-19 14:55:45 -08:00
|
|
|
if migrated {
|
2023-04-07 14:34:28 -07:00
|
|
|
log.Println("Database is already migrated")
|
2024-02-06 12:30:18 -08:00
|
|
|
return
|
2022-02-19 14:55:45 -08:00
|
|
|
}
|
2023-04-07 15:11:02 -07:00
|
|
|
if updateDB {
|
|
|
|
|
log.Println("Database schema updated successfully")
|
|
|
|
|
} else {
|
|
|
|
|
log.Println(migrateCompleteTxt)
|
|
|
|
|
}
|
2024-02-07 15:15:56 -08:00
|
|
|
os.Exit(cleanup())
|
2020-06-12 11:01:28 -07:00
|
|
|
}
|