forked from MirrorHub/mautrix-whatsapp
Don't allow unsupported database versions without flag
This commit is contained in:
parent
1d43aeca2f
commit
063ec7873d
2 changed files with 10 additions and 1 deletions
|
@ -26,6 +26,8 @@ const NumberOfUpgrades = 6
|
||||||
|
|
||||||
var upgrades [NumberOfUpgrades]upgrade
|
var upgrades [NumberOfUpgrades]upgrade
|
||||||
|
|
||||||
|
var UnsupportedDatabaseVersion = fmt.Errorf("unsupported database version")
|
||||||
|
|
||||||
func getVersion(dialect Dialect, db *sql.DB) (int, error) {
|
func getVersion(dialect Dialect, db *sql.DB) (int, error) {
|
||||||
_, err := db.Exec("CREATE TABLE IF NOT EXISTS version (version INTEGER)")
|
_, err := db.Exec("CREATE TABLE IF NOT EXISTS version (version INTEGER)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -65,6 +67,10 @@ func Run(log log.Logger, dialectName string, db *sql.DB) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if version > NumberOfUpgrades {
|
||||||
|
return UnsupportedDatabaseVersion
|
||||||
|
}
|
||||||
|
|
||||||
log.Infofln("Database currently on v%d, latest: v%d", version, NumberOfUpgrades)
|
log.Infofln("Database currently on v%d, latest: v%d", version, NumberOfUpgrades)
|
||||||
for i, upgrade := range upgrades[version:] {
|
for i, upgrade := range upgrades[version:] {
|
||||||
log.Infofln("Upgrading database to v%d: %s", version+i+1, upgrade.message)
|
log.Infofln("Upgrading database to v%d: %s", version+i+1, upgrade.message)
|
||||||
|
|
5
main.go
5
main.go
|
@ -25,7 +25,9 @@ import (
|
||||||
|
|
||||||
flag "maunium.net/go/mauflag"
|
flag "maunium.net/go/mauflag"
|
||||||
log "maunium.net/go/maulogger/v2"
|
log "maunium.net/go/maulogger/v2"
|
||||||
|
|
||||||
"maunium.net/go/mautrix-appservice"
|
"maunium.net/go/mautrix-appservice"
|
||||||
|
"maunium.net/go/mautrix-whatsapp/database/upgrades"
|
||||||
|
|
||||||
"maunium.net/go/mautrix-whatsapp/config"
|
"maunium.net/go/mautrix-whatsapp/config"
|
||||||
"maunium.net/go/mautrix-whatsapp/database"
|
"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 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 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 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()
|
var wantHelp, _ = flag.MakeHelpFlag()
|
||||||
|
|
||||||
func (bridge *Bridge) GenerateRegistration() {
|
func (bridge *Bridge) GenerateRegistration() {
|
||||||
|
@ -136,7 +139,7 @@ func (bridge *Bridge) Init() {
|
||||||
|
|
||||||
bridge.Log.Debugln("Initializing database")
|
bridge.Log.Debugln("Initializing database")
|
||||||
bridge.DB, err = database.New(bridge.Config.AppService.Database.Type, bridge.Config.AppService.Database.URI)
|
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)
|
bridge.Log.Fatalln("Failed to initialize database:", err)
|
||||||
os.Exit(14)
|
os.Exit(14)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue