mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-08-19 16:46:23 -07:00
Move database schema updating to gochan-migration
This commit is contained in:
parent
1da4330780
commit
17c28e5ebe
8 changed files with 260 additions and 119 deletions
|
|
@ -3,8 +3,10 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/gochan-org/gochan/cmd/gochan-migration/internal/common"
|
||||
"github.com/gochan-org/gochan/cmd/gochan-migration/internal/gcupdate"
|
||||
"github.com/gochan-org/gochan/cmd/gochan-migration/internal/pre2021"
|
||||
"github.com/gochan-org/gochan/pkg/config"
|
||||
|
||||
|
|
@ -13,7 +15,7 @@ import (
|
|||
|
||||
const (
|
||||
banner = `Welcome to the gochan database migration tool for gochan %s!
|
||||
This migration tool is currently very unstable, and will likely go through
|
||||
This migration tool is currently 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.
|
||||
|
||||
|
|
@ -28,14 +30,17 @@ for the threads and board pages`
|
|||
)
|
||||
|
||||
var (
|
||||
versionStr string
|
||||
versionStr string
|
||||
dbVersionStr string
|
||||
)
|
||||
|
||||
func main() {
|
||||
var options common.MigrationOptions
|
||||
var dirAction string
|
||||
var updateDB bool
|
||||
|
||||
log.SetFlags(0)
|
||||
flag.BoolVar(&updateDB, "updatedb", false, "If this is set, gochan-migrate will check, and if needed, update gochan's database schema")
|
||||
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)
|
||||
|
|
@ -43,10 +48,12 @@ func main() {
|
|||
|
||||
config.InitConfig(versionStr)
|
||||
|
||||
if options.ChanType == "" || options.OldChanConfig == "" {
|
||||
if !updateDB && (options.ChanType == "" || options.OldChanConfig == "") {
|
||||
flag.PrintDefaults()
|
||||
log.Fatal("Missing required oldchan value")
|
||||
return
|
||||
} else if updateDB {
|
||||
options.ChanType = "gcupdate"
|
||||
}
|
||||
switch dirAction {
|
||||
case "":
|
||||
|
|
@ -64,6 +71,8 @@ func main() {
|
|||
log.Printf(banner, versionStr)
|
||||
var migrator common.DBMigrator
|
||||
switch options.ChanType {
|
||||
case "gcupdate":
|
||||
migrator = &gcupdate.GCDatabaseUpdater{}
|
||||
case "pre2021":
|
||||
migrator = &pre2021.Pre2021Migrator{}
|
||||
case "kusabax":
|
||||
|
|
@ -77,18 +86,20 @@ func main() {
|
|||
return
|
||||
}
|
||||
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())
|
||||
var err error
|
||||
if !updateDB {
|
||||
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 = 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 {
|
||||
log.Fatalf("Unable to initialize %s migrator: %s\n",
|
||||
|
|
@ -102,7 +113,8 @@ func main() {
|
|||
log.Fatalln("Error migrating database:", err.Error())
|
||||
}
|
||||
if migrated {
|
||||
log.Fatalf("Database is already migrated")
|
||||
log.Println("Database is already migrated")
|
||||
os.Exit(0)
|
||||
}
|
||||
log.Println(migrateCompleteTxt)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue