mirror of
https://github.com/hoernschen/dendrite.git
synced 2024-12-27 07:28:27 +00:00
Add SQLite migration (up only)
This commit is contained in:
parent
027a957f45
commit
30a93f5330
5 changed files with 166 additions and 1 deletions
|
@ -24,6 +24,7 @@ import (
|
|||
|
||||
func LoadFromGoose() {
|
||||
goose.AddMigration(UpAddForgottenColumn, DownAddForgottenColumn)
|
||||
goose.AddMigration(UpStateBlocksRefactor, DownStateBlocksRefactor)
|
||||
}
|
||||
|
||||
func LoadAddForgottenColumn(m *sqlutil.Migrations) {
|
||||
|
|
|
@ -0,0 +1,156 @@
|
|||
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package deltas
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/matrix-org/dendrite/internal"
|
||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||
"github.com/matrix-org/dendrite/roomserver/types"
|
||||
"github.com/matrix-org/util"
|
||||
)
|
||||
|
||||
func LoadStateBlocksRefactor(m *sqlutil.Migrations) {
|
||||
m.AddMigration(UpStateBlocksRefactor, DownStateBlocksRefactor)
|
||||
}
|
||||
|
||||
func UpStateBlocksRefactor(tx *sql.Tx) error {
|
||||
if _, err := tx.Exec(`ALTER TABLE roomserver_state_block RENAME TO _roomserver_state_block;`); err != nil {
|
||||
return fmt.Errorf("tx.Exec: %w", err)
|
||||
}
|
||||
if _, err := tx.Exec(`ALTER TABLE roomserver_state_snapshots RENAME TO _roomserver_state_snapshots;`); err != nil {
|
||||
return fmt.Errorf("tx.Exec: %w", err)
|
||||
}
|
||||
_, err := tx.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS roomserver_state_block (
|
||||
state_block_nid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
event_nids TEXT NOT NULL DEFAULT '[]',
|
||||
UNIQUE (event_nids)
|
||||
);
|
||||
`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("tx.Exec: %w", err)
|
||||
}
|
||||
_, err = tx.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS roomserver_state_snapshots (
|
||||
state_snapshot_nid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
room_nid INTEGER NOT NULL,
|
||||
state_block_nids TEXT NOT NULL DEFAULT '[]',
|
||||
UNIQUE (room_nid, state_block_nids)
|
||||
);
|
||||
`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("tx.Exec: %w", err)
|
||||
}
|
||||
snapshotrows, err := tx.Query(`SELECT state_snapshot_nid, room_nid, state_block_nids FROM _roomserver_state_snapshots;`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("tx.Query: %w", err)
|
||||
}
|
||||
defer internal.CloseAndLogIfError(context.TODO(), snapshotrows, "rows.close() failed")
|
||||
for snapshotrows.Next() {
|
||||
var snapshot types.StateSnapshotNID
|
||||
var room types.RoomNID
|
||||
var jsonblocks string
|
||||
var blocks []types.StateBlockNID
|
||||
if err = snapshotrows.Scan(&snapshot, &room, &jsonblocks); err != nil {
|
||||
return fmt.Errorf("rows.Scan: %w", err)
|
||||
}
|
||||
if err = json.Unmarshal([]byte(jsonblocks), &blocks); err != nil {
|
||||
return fmt.Errorf("json.Unmarshal: %w", err)
|
||||
}
|
||||
|
||||
var newblocks []types.StateBlockNID
|
||||
for _, block := range blocks {
|
||||
if err = func() error {
|
||||
blockrows, berr := tx.Query(`SELECT event_nid FROM _roomserver_state_block WHERE state_block_nid = $1`, block)
|
||||
if berr != nil {
|
||||
return fmt.Errorf("tx.Query (event nids from old block): %w", berr)
|
||||
}
|
||||
defer internal.CloseAndLogIfError(context.TODO(), blockrows, "rows.close() failed")
|
||||
events := types.EventNIDs{}
|
||||
for blockrows.Next() {
|
||||
var event types.EventNID
|
||||
if err = blockrows.Scan(&event); err != nil {
|
||||
return fmt.Errorf("rows.Scan: %w", err)
|
||||
}
|
||||
events = append(events, event)
|
||||
}
|
||||
events = events[:util.SortAndUnique(events)]
|
||||
eventjson, eerr := json.Marshal(events)
|
||||
if eerr != nil {
|
||||
return fmt.Errorf("json.Marshal: %w", eerr)
|
||||
}
|
||||
|
||||
var blocknid types.StateBlockNID
|
||||
err = tx.QueryRow(`
|
||||
INSERT INTO roomserver_state_block (event_nids)
|
||||
VALUES ($1)
|
||||
ON CONFLICT (event_nids) DO UPDATE SET event_nids=$1
|
||||
RETURNING state_block_nid
|
||||
`, eventjson).Scan(&blocknid)
|
||||
if err != nil {
|
||||
return fmt.Errorf("tx.QueryRow.Scan (insert new block): %w", err)
|
||||
}
|
||||
newblocks = append(newblocks, blocknid)
|
||||
return nil
|
||||
}(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
newblocksjson, jerr := json.Marshal(newblocks)
|
||||
if jerr != nil {
|
||||
return fmt.Errorf("json.Marshal (new blocks): %w", jerr)
|
||||
}
|
||||
var newsnapshot types.StateSnapshotNID
|
||||
err = tx.QueryRow(`
|
||||
INSERT INTO roomserver_state_snapshots (room_nid, state_block_nids)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT (room_nid, state_block_nids) DO UPDATE SET room_nid=$3
|
||||
RETURNING state_snapshot_nid
|
||||
`, room, newblocksjson, room).Scan(&newsnapshot)
|
||||
if err != nil {
|
||||
return fmt.Errorf("tx.QueryRow.Scan (insert new snapshot): %w", err)
|
||||
}
|
||||
|
||||
_, err = tx.Exec(`UPDATE roomserver_events SET state_snapshot_nid=$1 WHERE state_snapshot_nid=$2`, newsnapshot, snapshot)
|
||||
if err != nil {
|
||||
return fmt.Errorf("tx.Exec (update events): %w", err)
|
||||
}
|
||||
_, err = tx.Exec(`UPDATE roomserver_rooms SET state_snapshot_nid=$1 WHERE state_snapshot_nid=$2`, newsnapshot, snapshot)
|
||||
if err != nil {
|
||||
return fmt.Errorf("tx.Exec (update rooms): %w", err)
|
||||
}
|
||||
|
||||
fmt.Println("Rewrote snapshot", snapshot, "to", newsnapshot)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err = tx.Exec(`DROP TABLE _roomserver_state_snapshots;`); err != nil {
|
||||
return fmt.Errorf("tx.Exec (delete old snapshot table): %w", err)
|
||||
}
|
||||
if _, err = tx.Exec(`DROP TABLE _roomserver_state_block;`); err != nil {
|
||||
return fmt.Errorf("tx.Exec (delete old block table): %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func DownStateBlocksRefactor(tx *sql.Tx) error {
|
||||
panic("DOWN!")
|
||||
}
|
|
@ -35,7 +35,7 @@ import (
|
|||
const stateDataSchema = `
|
||||
CREATE TABLE IF NOT EXISTS roomserver_state_block (
|
||||
state_block_nid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
event_nids TEXT NOT NULL,
|
||||
event_nids TEXT NOT NULL DEFAULT '[]',
|
||||
UNIQUE (event_nids)
|
||||
);
|
||||
`
|
||||
|
|
|
@ -61,6 +61,7 @@ func Open(dbProperties *config.DatabaseOptions, cache caching.RoomServerCaches)
|
|||
}
|
||||
m := sqlutil.NewMigrations()
|
||||
deltas.LoadAddForgottenColumn(m)
|
||||
deltas.LoadStateBlocksRefactor(m)
|
||||
if err := m.RunDeltas(db, dbProperties); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -40,6 +40,13 @@ type StateSnapshotNID int64
|
|||
// These blocks of state data are combined to form the actual state.
|
||||
type StateBlockNID int64
|
||||
|
||||
// EventNIDs is used to sort and dedupe event NIDs.
|
||||
type EventNIDs []EventNID
|
||||
|
||||
func (a EventNIDs) Len() int { return len(a) }
|
||||
func (a EventNIDs) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a EventNIDs) Less(i, j int) bool { return a[i] < a[j] }
|
||||
|
||||
// StateBlockNIDs is used to sort and dedupe state block NIDs.
|
||||
type StateBlockNIDs []StateBlockNID
|
||||
|
||||
|
|
Loading…
Reference in a new issue