Don't allow unsupported database versions without flag

This commit is contained in:
Tulir Asokan 2019-05-28 21:29:43 +03:00
parent 1d43aeca2f
commit 063ec7873d
2 changed files with 10 additions and 1 deletions

View file

@ -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)

View file

@ -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)
}