2019-08-25 15:29:35 +02:00
|
|
|
package upgrades
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-08-25 16:25:19 +02:00
|
|
|
"os"
|
2019-08-25 15:29:35 +02:00
|
|
|
"strings"
|
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
"maunium.net/go/mautrix/event"
|
2019-08-25 15:29:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
migrateRegistrations := func(tx *sql.Tx, registrations map[string]bool) error {
|
2019-08-30 20:08:32 +02:00
|
|
|
if len(registrations) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-10-01 21:09:46 +02:00
|
|
|
|
|
|
|
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)
|
2019-08-25 15:29:35 +02:00
|
|
|
i := 1
|
|
|
|
for userID, registered := range registrations {
|
2019-10-01 21:09:46 +02:00
|
|
|
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)
|
|
|
|
}
|
2019-08-25 15:29:35 +02:00
|
|
|
if registered {
|
|
|
|
values = append(values, userID)
|
|
|
|
valueStrings = append(valueStrings, fmt.Sprintf("($%d)", i))
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
2019-10-01 21:09:46 +02:00
|
|
|
return executeBatch(tx, valueStrings, values...)
|
2019-08-25 15:29:35 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
migrateMemberships := func(tx *sql.Tx, rooms map[string]map[string]event.Membership) error {
|
2019-08-25 15:29:35 +02:00
|
|
|
for roomID, members := range rooms {
|
2019-08-30 20:08:32 +02:00
|
|
|
if len(members) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2019-08-27 14:30:22 +02:00
|
|
|
var values []interface{}
|
|
|
|
var valueStrings []string
|
|
|
|
i := 1
|
2019-08-25 15:29:35 +02:00
|
|
|
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
|
|
|
|
}
|
2019-08-27 14:30:22 +02:00
|
|
|
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
|
|
|
|
}
|
2019-08-25 15:29:35 +02:00
|
|
|
}
|
2019-08-27 14:30:22 +02:00
|
|
|
return nil
|
2019-08-25 15:29:35 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
migratePowerLevels := func(tx *sql.Tx, rooms map[string]*event.PowerLevelsEventContent) error {
|
2019-08-30 20:08:32 +02:00
|
|
|
if len(rooms) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-25 15:29:35 +02:00
|
|
|
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),
|
|
|
|
membership VARCHAR(15) NOT NULL,
|
|
|
|
PRIMARY KEY (room_id, user_id)
|
|
|
|
)`
|
|
|
|
|
|
|
|
roomStateTable := `CREATE TABLE mx_room_state (
|
|
|
|
room_id VARCHAR(255) PRIMARY KEY,
|
|
|
|
power_levels TEXT
|
|
|
|
)`
|
|
|
|
|
|
|
|
registrationsTable := `CREATE TABLE mx_registrations (
|
|
|
|
user_id VARCHAR(255) PRIMARY KEY
|
|
|
|
)`
|
|
|
|
|
2020-03-26 22:36:34 +01:00
|
|
|
type TempStateStore struct {
|
2020-05-08 21:32:22 +02:00
|
|
|
Registrations map[string]bool `json:"registrations"`
|
|
|
|
Members map[string]map[string]event.Membership `json:"memberships"`
|
|
|
|
PowerLevels map[string]*event.PowerLevelsEventContent `json:"power_levels"`
|
2020-03-26 22:36:34 +01:00
|
|
|
}
|
2019-08-25 15:29:35 +02:00
|
|
|
|
2020-03-26 22:36:34 +01:00
|
|
|
upgrades[9] = upgrade{"Move state store to main DB", func(tx *sql.Tx, ctx context) error {
|
2019-08-25 16:25:19 +02:00
|
|
|
if ctx.dialect == Postgres {
|
2019-08-25 15:29:35 +02:00
|
|
|
roomStateTable = strings.Replace(roomStateTable, "TEXT", "JSONB", 1)
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:36:34 +01:00
|
|
|
var store TempStateStore
|
2019-08-25 18:26:04 +02:00
|
|
|
if _, err := tx.Exec(userProfileTable); err != nil {
|
2019-08-25 15:29:35 +02:00
|
|
|
return err
|
|
|
|
} else if _, err = tx.Exec(roomStateTable); err != nil {
|
|
|
|
return err
|
|
|
|
} else if _, err = tx.Exec(registrationsTable); err != nil {
|
|
|
|
return err
|
2019-08-25 18:26:04 +02:00
|
|
|
} 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
|
2019-08-25 15:29:35 +02:00
|
|
|
} else if err = migrateRegistrations(tx, store.Registrations); err != nil {
|
|
|
|
return err
|
2019-11-10 20:22:11 +01:00
|
|
|
} else if err = migrateMemberships(tx, store.Members); err != nil {
|
2019-08-25 15:29:35 +02:00
|
|
|
return err
|
|
|
|
} else if err = migratePowerLevels(tx, store.PowerLevels); err != nil {
|
|
|
|
return err
|
2019-08-25 16:25:19 +02:00
|
|
|
} else if err = os.Rename("mx-state.json", "mx-state.json.bak"); err != nil {
|
|
|
|
return err
|
2019-08-25 15:29:35 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}}
|
|
|
|
}
|