From 5b5cbc2efac8343fd6920fb026ae9eab54bda7b7 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sat, 30 Oct 2021 23:31:39 +0300 Subject: [PATCH] Remove legacy state store migration --- config/config.go | 2 - .../2019-08-25-move-state-store-to-db.go | 94 ------------------- main.go | 9 -- 3 files changed, 105 deletions(-) diff --git a/config/config.go b/config/config.go index c7bb73f..4ad808d 100644 --- a/config/config.go +++ b/config/config.go @@ -46,8 +46,6 @@ type Config struct { MaxIdleConns int `yaml:"max_idle_conns"` } `yaml:"database"` - StateStore string `yaml:"state_store_path,omitempty"` - Provisioning struct { Prefix string `yaml:"prefix"` SharedSecret string `yaml:"shared_secret"` diff --git a/database/upgrades/2019-08-25-move-state-store-to-db.go b/database/upgrades/2019-08-25-move-state-store-to-db.go index cbb6001..ea19934 100644 --- a/database/upgrades/2019-08-25-move-state-store-to-db.go +++ b/database/upgrades/2019-08-25-move-state-store-to-db.go @@ -2,93 +2,12 @@ package upgrades import ( "database/sql" - "encoding/json" - "fmt" - "io/ioutil" - "os" "strings" "maunium.net/go/mautrix/event" ) func init() { - migrateRegistrations := func(tx *sql.Tx, registrations map[string]bool) error { - if len(registrations) == 0 { - return nil - } - - executeBatch := func(tx *sql.Tx, valueStrings []string, values ...interface{}) error { - valueString := strings.Join(valueStrings, ",") - _, err := tx.Exec("INSERT INTO mx_registrations (user_id) VALUES "+valueString, values...) - return err - } - - batchSize := 100 - values := make([]interface{}, 0, batchSize) - valueStrings := make([]string, 0, batchSize) - i := 1 - for userID, registered := range registrations { - if i == batchSize { - err := executeBatch(tx, valueStrings, values...) - if err != nil { - return err - } - i = 1 - values = make([]interface{}, 0, batchSize) - valueStrings = make([]string, 0, batchSize) - } - if registered { - values = append(values, userID) - valueStrings = append(valueStrings, fmt.Sprintf("($%d)", i)) - i++ - } - } - return executeBatch(tx, valueStrings, values...) - } - - migrateMemberships := func(tx *sql.Tx, rooms map[string]map[string]event.Membership) error { - for roomID, members := range rooms { - if len(members) == 0 { - continue - } - var values []interface{} - var valueStrings []string - i := 1 - for userID, membership := range members { - values = append(values, roomID, userID, membership) - valueStrings = append(valueStrings, fmt.Sprintf("($%d, $%d, $%d)", i, i+1, i+2)) - i += 3 - } - valueString := strings.Join(valueStrings, ",") - _, err := tx.Exec("INSERT INTO mx_user_profile (room_id, user_id, membership) VALUES "+valueString, values...) - if err != nil { - return err - } - } - return nil - } - - migratePowerLevels := func(tx *sql.Tx, rooms map[string]*event.PowerLevelsEventContent) error { - if len(rooms) == 0 { - return nil - } - var values []interface{} - var valueStrings []string - i := 1 - for roomID, powerLevels := range rooms { - powerLevelBytes, err := json.Marshal(powerLevels) - if err != nil { - return err - } - values = append(values, roomID, powerLevelBytes) - valueStrings = append(valueStrings, fmt.Sprintf("($%d, $%d)", i, i+1)) - i += 2 - } - valueString := strings.Join(valueStrings, ",") - _, err := tx.Exec("INSERT INTO mx_room_state (room_id, power_levels) VALUES "+valueString, values...) - return err - } - userProfileTable := `CREATE TABLE mx_user_profile ( room_id VARCHAR(255), user_id VARCHAR(255), @@ -116,25 +35,12 @@ func init() { roomStateTable = strings.Replace(roomStateTable, "TEXT", "JSONB", 1) } - var store TempStateStore if _, err := tx.Exec(userProfileTable); err != nil { return err } else if _, err = tx.Exec(roomStateTable); err != nil { return err } else if _, err = tx.Exec(registrationsTable); err != nil { return err - } else if data, err := ioutil.ReadFile("mx-state.json"); err != nil { - ctx.log.Debugln("mx-state.json not found, not migrating state store") - } else if err = json.Unmarshal(data, &store); err != nil { - return err - } else if err = migrateRegistrations(tx, store.Registrations); err != nil { - return err - } else if err = migrateMemberships(tx, store.Members); err != nil { - return err - } else if err = migratePowerLevels(tx, store.PowerLevels); err != nil { - return err - } else if err = os.Rename("mx-state.json", "mx-state.json.bak"); err != nil { - return err } return nil }} diff --git a/main.go b/main.go index 5d0b81d..200a7c1 100644 --- a/main.go +++ b/main.go @@ -257,15 +257,6 @@ func (bridge *Bridge) Init() { os.Exit(14) } - if len(bridge.Config.AppService.StateStore) > 0 && bridge.Config.AppService.StateStore != "./mx-state.json" { - version, err := upgrades.GetVersion(bridge.DB.DB) - if version < 0 && err == nil { - bridge.Log.Fatalln("Non-standard state store path. Please move the state store to ./mx-state.json " + - "and update the config. The state store will be migrated into the db on the next launch.") - os.Exit(18) - } - } - bridge.Log.Debugln("Initializing state store") bridge.StateStore = database.NewSQLStateStore(bridge.DB) bridge.AS.StateStore = bridge.StateStore