From 063ec7873d1e98b702ac11447532224647edc662 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 28 May 2019 21:29:43 +0300 Subject: [PATCH] Don't allow unsupported database versions without flag --- database/upgrades/upgrades.go | 6 ++++++ main.go | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/database/upgrades/upgrades.go b/database/upgrades/upgrades.go index 5366198..fe96e23 100644 --- a/database/upgrades/upgrades.go +++ b/database/upgrades/upgrades.go @@ -26,6 +26,8 @@ const NumberOfUpgrades = 6 var upgrades [NumberOfUpgrades]upgrade +var UnsupportedDatabaseVersion = fmt.Errorf("unsupported database version") + func getVersion(dialect Dialect, db *sql.DB) (int, error) { _, err := db.Exec("CREATE TABLE IF NOT EXISTS version (version INTEGER)") if err != nil { @@ -65,6 +67,10 @@ func Run(log log.Logger, dialectName string, db *sql.DB) error { return err } + if version > NumberOfUpgrades { + return UnsupportedDatabaseVersion + } + log.Infofln("Database currently on v%d, latest: v%d", version, NumberOfUpgrades) for i, upgrade := range upgrades[version:] { log.Infofln("Upgrading database to v%d: %s", version+i+1, upgrade.message) diff --git a/main.go b/main.go index 666a721..2c6189e 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,9 @@ import ( flag "maunium.net/go/mauflag" log "maunium.net/go/maulogger/v2" + "maunium.net/go/mautrix-appservice" + "maunium.net/go/mautrix-whatsapp/database/upgrades" "maunium.net/go/mautrix-whatsapp/config" "maunium.net/go/mautrix-whatsapp/database" @@ -36,6 +38,7 @@ var configPath = flag.MakeFull("c", "config", "The path to your config file.", " //var baseConfigPath = flag.MakeFull("b", "base-config", "The path to the example config file.", "example-config.yaml").String() var registrationPath = flag.MakeFull("r", "registration", "The path where to save the appservice registration.", "registration.yaml").String() var generateRegistration = flag.MakeFull("g", "generate-registration", "Generate registration and quit.", "false").Bool() +var ignoreUnsupportedDatabase = flag.Make().LongKey("ignore-unsupported-database").Usage("Run even if database is too new").Default("false").Bool() var wantHelp, _ = flag.MakeHelpFlag() func (bridge *Bridge) GenerateRegistration() { @@ -136,7 +139,7 @@ func (bridge *Bridge) Init() { bridge.Log.Debugln("Initializing database") bridge.DB, err = database.New(bridge.Config.AppService.Database.Type, bridge.Config.AppService.Database.URI) - if err != nil { + if err != nil && (err != upgrades.UnsupportedDatabaseVersion || !*ignoreUnsupportedDatabase) { bridge.Log.Fatalln("Failed to initialize database:", err) os.Exit(14) }