2020-06-12 11:01:28 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2025-04-20 14:03:22 -07:00
|
|
|
"errors"
|
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"
|
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
|
|
|
)
|
|
|
|
|
2020-06-12 11:01:28 -07:00
|
|
|
var (
|
2025-04-22 17:03:52 -07:00
|
|
|
migrator common.DBMigrator
|
2020-06-12 11:01:28 -07:00
|
|
|
)
|
|
|
|
|
2024-10-13 13:43:08 -07:00
|
|
|
func cleanup() {
|
2024-02-06 12:30:18 -08:00
|
|
|
var err error
|
2024-10-13 13:43:08 -07:00
|
|
|
exitCode := 0
|
2024-02-06 12:30:18 -08:00
|
|
|
if migrator != nil {
|
|
|
|
if err = migrator.Close(); err != nil {
|
2024-10-13 13:43:08 -07:00
|
|
|
common.LogError().Err(err).Caller().Msg("Error closing migrator")
|
|
|
|
exitCode = 1
|
2024-02-06 12:30:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = gcsql.Close(); err != nil {
|
2024-10-13 13:43:08 -07:00
|
|
|
common.LogError().Err(err).Caller().Msg("Error closing SQL connection")
|
|
|
|
exitCode = 1
|
2024-02-06 12:30:18 -08:00
|
|
|
}
|
2024-10-13 13:43:08 -07:00
|
|
|
os.Exit(exitCode)
|
2024-02-06 12:30:18 -08:00
|
|
|
}
|
|
|
|
|
2020-06-12 11:01:28 -07:00
|
|
|
func main() {
|
2022-02-07 18:28:27 -08:00
|
|
|
var options common.MigrationOptions
|
2023-04-07 14:34:28 -07:00
|
|
|
var updateDB bool
|
2021-12-18 18:49:22 -08:00
|
|
|
|
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")
|
|
|
|
flag.Parse()
|
2021-12-18 18:49:22 -08:00
|
|
|
|
2025-04-22 17:03:52 -07:00
|
|
|
err := config.InitConfig()
|
2024-10-13 13:43:08 -07:00
|
|
|
if err != nil {
|
2025-03-03 17:16:48 -08:00
|
|
|
log.Fatalln("Unable to initialize configuration:", err.Error())
|
|
|
|
}
|
|
|
|
if err = common.InitMigrationLog(); err != nil {
|
2024-10-13 13:43:08 -07:00
|
|
|
log.Fatalln("Unable to initialize migration log:", err.Error())
|
|
|
|
}
|
|
|
|
fatalEv := common.LogFatal()
|
|
|
|
defer func() {
|
|
|
|
cleanup()
|
|
|
|
fatalEv.Discard()
|
|
|
|
}()
|
2023-03-29 09:35:46 -07:00
|
|
|
|
2024-12-29 17:49:05 -08:00
|
|
|
if !updateDB {
|
|
|
|
if options.ChanType == "" {
|
|
|
|
flag.PrintDefaults()
|
|
|
|
fatalEv.Msg("Missing required oldchan value")
|
|
|
|
} else if options.OldChanConfig == "" {
|
|
|
|
flag.PrintDefaults()
|
|
|
|
fatalEv.Msg("Missing required oldconfig value")
|
|
|
|
}
|
2023-04-07 14:34:28 -07:00
|
|
|
} else if updateDB {
|
|
|
|
options.ChanType = "gcupdate"
|
2022-02-07 18:28:27 -08:00
|
|
|
}
|
2024-10-13 13:43:08 -07:00
|
|
|
fatalEv.Str("chanType", options.ChanType)
|
2021-12-18 18:49:22 -08:00
|
|
|
|
2022-02-07 18:28:27 -08:00
|
|
|
switch options.ChanType {
|
2023-04-07 14:34:28 -07:00
|
|
|
case "gcupdate":
|
2025-04-22 17:03:52 -07:00
|
|
|
migrator = &gcupdate.GCDatabaseUpdater{}
|
2022-02-07 18:28:27 -08:00
|
|
|
case "pre2021":
|
|
|
|
migrator = &pre2021.Pre2021Migrator{}
|
|
|
|
case "kusabax":
|
|
|
|
fallthrough
|
|
|
|
case "tinyboard":
|
|
|
|
fallthrough
|
|
|
|
default:
|
2024-10-13 13:43:08 -07:00
|
|
|
fatalEv.Msg("Unsupported chan type, Currently only pre2021 database migration is supported")
|
2022-02-07 18:28:27 -08:00
|
|
|
}
|
2024-12-29 17:49:05 -08:00
|
|
|
migratingInPlace := migrator.IsMigratingInPlace()
|
|
|
|
common.LogInfo().
|
|
|
|
Str("oldChanType", options.ChanType).
|
|
|
|
Str("oldChanConfig", options.OldChanConfig).
|
|
|
|
Bool("migratingInPlace", migratingInPlace).
|
|
|
|
Msg("Starting database migration")
|
|
|
|
|
2025-04-22 17:03:52 -07:00
|
|
|
if err = config.InitConfig(); err != nil {
|
2025-03-03 17:16:48 -08:00
|
|
|
fatalEv.Err(err).Caller().Msg("Unable to reload configuration")
|
|
|
|
}
|
2025-01-25 13:01:52 -08:00
|
|
|
sqlCfg := config.GetSQLConfig()
|
|
|
|
if migratingInPlace && sqlCfg.DBtype == "sqlite3" && !updateDB {
|
|
|
|
common.LogWarning().
|
|
|
|
Str("dbType", sqlCfg.DBtype).
|
|
|
|
Bool("migrateInPlace", migratingInPlace).
|
|
|
|
Msg("SQLite has limitations with table column changes")
|
|
|
|
}
|
2024-12-29 17:49:05 -08:00
|
|
|
if !migratingInPlace {
|
2024-05-01 17:32:19 -07:00
|
|
|
err = gcsql.ConnectToDB(&sqlCfg)
|
2023-04-07 14:34:28 -07:00
|
|
|
if err != nil {
|
2024-10-13 13:43:08 -07:00
|
|
|
fatalEv.Err(err).Caller().Msg("Failed to connect to the database")
|
2023-04-07 14:34:28 -07:00
|
|
|
}
|
2025-06-27 17:01:57 -07:00
|
|
|
if err = gcsql.CheckAndInitializeDatabase(sqlCfg.DBtype, true); err != nil {
|
2024-10-13 13:43:08 -07:00
|
|
|
fatalEv.Err(err).Caller().Msg("Unable to initialize the database")
|
2023-04-07 14:34:28 -07:00
|
|
|
}
|
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-10-13 13:43:08 -07:00
|
|
|
fatalEv.Err(err).Caller().Msg("Unable to initialize migrator")
|
2022-02-07 18:28:27 -08:00
|
|
|
}
|
2022-02-19 14:55:45 -08:00
|
|
|
|
2024-10-13 13:43:08 -07:00
|
|
|
var migrated bool
|
2025-04-20 14:03:22 -07:00
|
|
|
migrated, err = migrator.MigrateDB()
|
|
|
|
if errors.Is(err, common.ErrNotInstalled) {
|
|
|
|
common.LogWarning().Msg(err.Error())
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
2025-04-20 14:48:56 -07:00
|
|
|
fatalEv.Msg("Unable to migrate database")
|
2022-02-07 18:28:27 -08:00
|
|
|
}
|
2022-02-19 14:55:45 -08:00
|
|
|
if migrated {
|
2025-01-10 16:44:19 -08:00
|
|
|
common.LogWarning().Msg("Database is already migrated")
|
2023-04-07 15:11:02 -07:00
|
|
|
} else {
|
2024-10-13 13:43:08 -07:00
|
|
|
common.LogInfo().Str("chanType", options.ChanType).Msg("Database migration complete")
|
2023-04-07 15:11:02 -07:00
|
|
|
}
|
2020-06-12 11:01:28 -07:00
|
|
|
}
|