From 7a7be4f0ac8689aa829e0ed35edc1e426a447226 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Mon, 13 Jan 2020 14:53:03 +0000 Subject: [PATCH] Fix state block schema a bit --- roomserver/storage/sqlite3/state_block_table.go | 7 +++++-- roomserver/storage/sqlite3/state_snapshot_table.go | 2 +- roomserver/storage/sqlite3/storage.go | 3 --- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/roomserver/storage/sqlite3/state_block_table.go b/roomserver/storage/sqlite3/state_block_table.go index 223c6b998..4f41eddd0 100644 --- a/roomserver/storage/sqlite3/state_block_table.go +++ b/roomserver/storage/sqlite3/state_block_table.go @@ -28,7 +28,7 @@ import ( const stateDataSchema = ` CREATE TABLE IF NOT EXISTS roomserver_state_block ( - state_block_nid INTEGER NOT NULL, + state_block_nid INTEGER PRIMARY KEY AUTOINCREMENT, event_type_nid INTEGER NOT NULL, event_state_key_nid INTEGER NOT NULL, event_nid INTEGER NOT NULL, @@ -41,7 +41,10 @@ const insertStateDataSQL = "" + " VALUES ($1, $2, $3, $4)" const selectNextStateBlockNIDSQL = ` - SELECT seq+1 FROM sqlite_sequence WHERE name = 'roomserver_state_block' + SELECT COALESCE(( + SELECT seq AS state_block_nid FROM sqlite_sequence + WHERE name = 'roomserver_state_block'), 0 + ) AS state_block_nid ` // Bulk state lookup by numeric state block ID. diff --git a/roomserver/storage/sqlite3/state_snapshot_table.go b/roomserver/storage/sqlite3/state_snapshot_table.go index a6467d115..0478e321a 100644 --- a/roomserver/storage/sqlite3/state_snapshot_table.go +++ b/roomserver/storage/sqlite3/state_snapshot_table.go @@ -88,7 +88,7 @@ func (s *stateSnapshotStatements) bulkSelectStateBlockNIDs( for i := range stateNIDs { nids[i] = int64(stateNIDs[i]) } - rows, err := s.bulkSelectStateBlockNIDsStmt.QueryContext(ctx, pq.Int64Array(nids)) + rows, err := s.bulkSelectStateBlockNIDsStmt.QueryContext(ctx, sqliteIn(pq.Int64Array(nids))) if err != nil { return nil, err } diff --git a/roomserver/storage/sqlite3/storage.go b/roomserver/storage/sqlite3/storage.go index 9cc33c8fb..5bff7e94f 100644 --- a/roomserver/storage/sqlite3/storage.go +++ b/roomserver/storage/sqlite3/storage.go @@ -18,7 +18,6 @@ package sqlite3 import ( "context" "database/sql" - "fmt" "net/url" "github.com/matrix-org/dendrite/roomserver/api" @@ -245,11 +244,9 @@ func (d *Database) AddState( if len(state) > 0 { stateBlockNID, err := d.statements.selectNextStateBlockNID(ctx) if err != nil { - fmt.Println("d.statements.selectNextStateBlockNID", err) return 0, err } if err = d.statements.bulkInsertStateData(ctx, stateBlockNID, state); err != nil { - fmt.Println("d.statements.bulkInsertStateData", err) return 0, err } stateBlockNIDs = append(stateBlockNIDs[:len(stateBlockNIDs):len(stateBlockNIDs)], stateBlockNID)