mirror of
https://github.com/tulir/mautrix-whatsapp
synced 2024-12-23 22:14:06 +01:00
Remove/fix some db upgrades
This commit is contained in:
parent
f3d6bbbc4a
commit
682c5bff38
4 changed files with 4 additions and 70 deletions
|
@ -1,61 +0,0 @@
|
|||
package upgrades
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func init() {
|
||||
var keys = []string{"imageMessage", "contactMessage", "locationMessage", "extendedTextMessage", "documentMessage", "audioMessage", "videoMessage"}
|
||||
upgrades[4] = upgrade{"Update message content to new protocol version. This may take a while.", func(tx *sql.Tx, ctx context) error {
|
||||
rows, err := ctx.db.Query("SELECT mxid, content FROM message")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for rows.Next() {
|
||||
var mxid string
|
||||
var rawContent []byte
|
||||
err = rows.Scan(&mxid, &rawContent)
|
||||
if err != nil {
|
||||
fmt.Println("Error scanning:", err)
|
||||
continue
|
||||
}
|
||||
var content map[string]interface{}
|
||||
err = json.Unmarshal(rawContent, &content)
|
||||
if err != nil {
|
||||
fmt.Printf("Error unmarshaling content of %s: %v\n", mxid, err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, key := range keys {
|
||||
val, ok := content[key].(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
ci, ok := val["contextInfo"].(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
qm, ok := ci["quotedMessage"].([]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
ci["quotedMessage"] = qm[0]
|
||||
goto save
|
||||
}
|
||||
continue
|
||||
|
||||
save:
|
||||
rawContent, err = json.Marshal(&content)
|
||||
if err != nil {
|
||||
fmt.Printf("Error marshaling updated content of %s: %v\n", mxid, err)
|
||||
}
|
||||
_, err = tx.Exec("UPDATE message SET content=$1 WHERE mxid=$2", rawContent, mxid)
|
||||
if err != nil {
|
||||
fmt.Printf("Error updating row of %s: %v\n", mxid, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}}
|
||||
}
|
|
@ -3,8 +3,6 @@ package upgrades
|
|||
import (
|
||||
"database/sql"
|
||||
"strings"
|
||||
|
||||
"maunium.net/go/mautrix/event"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
@ -24,12 +22,6 @@ func init() {
|
|||
user_id VARCHAR(255) PRIMARY KEY
|
||||
)`
|
||||
|
||||
type TempStateStore struct {
|
||||
Registrations map[string]bool `json:"registrations"`
|
||||
Members map[string]map[string]event.Membership `json:"memberships"`
|
||||
PowerLevels map[string]*event.PowerLevelsEventContent `json:"power_levels"`
|
||||
}
|
||||
|
||||
upgrades[9] = upgrade{"Move state store to main DB", func(tx *sql.Tx, ctx context) error {
|
||||
if ctx.dialect == Postgres {
|
||||
roomStateTable = strings.Replace(roomStateTable, "TEXT", "JSONB", 1)
|
||||
|
|
|
@ -48,7 +48,7 @@ func init() {
|
|||
}
|
||||
|
||||
// No need to copy auth data, users need to relogin anyway
|
||||
_, err = tx.Exec(`INSERT INTO "user" (mxid, management_room, last_connection) SELECT mxid, management_room, last_connection FROM old_user`)
|
||||
_, err = tx.Exec(`INSERT INTO "user" (mxid, management_room) SELECT mxid, management_room FROM old_user`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -100,6 +100,9 @@ func Run(log log.Logger, dialectName string, db *sql.DB) error {
|
|||
|
||||
log.Infofln("Database currently on v%d, latest: v%d", version, NumberOfUpgrades)
|
||||
for i, upgradeItem := range upgrades[version:] {
|
||||
if upgradeItem.fn == nil {
|
||||
continue
|
||||
}
|
||||
log.Infofln("Upgrading database to v%d: %s", version+i+1, upgradeItem.message)
|
||||
var tx *sql.Tx
|
||||
tx, err = db.Begin()
|
||||
|
|
Loading…
Reference in a new issue