@@ -32,8 +32,8 @@ use ruma::{
32
32
GlobalAccountDataEventType , RoomAccountDataEventType , StateEventType ,
33
33
} ,
34
34
serde:: Raw ,
35
- CanonicalJsonObject , EventId , OwnedEventId , OwnedRoomId , OwnedTransactionId , OwnedUserId ,
36
- RoomId , RoomVersionId , TransactionId , UserId ,
35
+ CanonicalJsonObject , EventId , MilliSecondsSinceUnixEpoch , OwnedEventId , OwnedRoomId ,
36
+ OwnedTransactionId , OwnedUserId , RoomId , RoomVersionId , TransactionId , UInt , UserId ,
37
37
} ;
38
38
use rusqlite:: { OptionalExtension , Transaction } ;
39
39
use serde:: { de:: DeserializeOwned , Deserialize , Serialize } ;
@@ -69,7 +69,7 @@ mod keys {
69
69
/// This is used to figure whether the sqlite database requires a migration.
70
70
/// Every new SQL migration should imply a bump of this number, and changes in
71
71
/// the [`SqliteStateStore::run_migrations`] function..
72
- const DATABASE_VERSION : u8 = 10 ;
72
+ const DATABASE_VERSION : u8 = 11 ;
73
73
74
74
/// A sqlite based cryptostore.
75
75
#[ derive( Clone ) ]
@@ -318,6 +318,17 @@ impl SqliteStateStore {
318
318
. await ?;
319
319
}
320
320
321
+ if from < 11 && to >= 11 {
322
+ conn. with_transaction ( move |txn| {
323
+ // Run the migration.
324
+ txn. execute_batch ( include_str ! (
325
+ "../migrations/state_store/010_send_queue_enqueue_time.sql"
326
+ ) ) ?;
327
+ txn. set_db_version ( 11 )
328
+ } )
329
+ . await ?;
330
+ }
331
+
321
332
Ok ( ( ) )
322
333
}
323
334
@@ -1753,7 +1764,6 @@ impl StateStore for SqliteStateStore {
1753
1764
let room_id_value = self . serialize_value ( & room_id. to_owned ( ) ) ?;
1754
1765
1755
1766
let content = self . serialize_json ( & content) ?;
1756
-
1757
1767
// The transaction id is used both as a key (in remove/update) and a value (as
1758
1768
// it's useful for the callers), so we keep it as is, and neither hash
1759
1769
// it (with encode_key) or encrypt it (through serialize_value). After
@@ -1824,26 +1834,28 @@ impl StateStore for SqliteStateStore {
1824
1834
// Note: ROWID is always present and is an auto-incremented integer counter. We
1825
1835
// want to maintain the insertion order, so we can sort using it.
1826
1836
// Note 2: transaction_id is not encoded, see why in `save_send_queue_event`.
1827
- let res: Vec < ( String , Vec < u8 > , Option < Vec < u8 > > , usize ) > = self
1837
+ let res: Vec < ( String , Vec < u8 > , Option < Vec < u8 > > , usize , u64 ) > = self
1828
1838
. acquire ( )
1829
1839
. await ?
1830
1840
. prepare (
1831
- "SELECT transaction_id, content, wedge_reason, priority FROM send_queue_events WHERE room_id = ? ORDER BY priority DESC, ROWID" ,
1841
+ "SELECT transaction_id, content, wedge_reason, priority, enqueue_time FROM send_queue_events WHERE room_id = ? ORDER BY priority DESC, ROWID" ,
1832
1842
|mut stmt| {
1833
1843
stmt. query ( ( room_id, ) ) ?
1834
- . mapped ( |row| Ok ( ( row. get ( 0 ) ?, row. get ( 1 ) ?, row. get ( 2 ) ?, row. get ( 3 ) ?) ) )
1844
+ . mapped ( |row| Ok ( ( row. get ( 0 ) ?, row. get ( 1 ) ?, row. get ( 2 ) ?, row. get ( 3 ) ?, row . get ( 4 ) ? ) ) )
1835
1845
. collect ( )
1836
1846
} ,
1837
1847
)
1838
1848
. await ?;
1839
1849
1840
1850
let mut requests = Vec :: with_capacity ( res. len ( ) ) ;
1841
1851
for entry in res {
1852
+ let enqueue_time = MilliSecondsSinceUnixEpoch ( UInt :: new ( entry. 4 ) . unwrap ( ) ) ;
1842
1853
requests. push ( QueuedRequest {
1843
1854
transaction_id : entry. 0 . into ( ) ,
1844
1855
kind : self . deserialize_json ( & entry. 1 ) ?,
1845
1856
error : entry. 2 . map ( |v| self . deserialize_value ( & v) ) . transpose ( ) ?,
1846
1857
priority : entry. 3 ,
1858
+ enqueue_time : Some ( enqueue_time) ,
1847
1859
} ) ;
1848
1860
}
1849
1861
@@ -1909,7 +1921,6 @@ impl StateStore for SqliteStateStore {
1909
1921
// See comment in `save_send_queue_event`.
1910
1922
let parent_txn_id = parent_txn_id. to_string ( ) ;
1911
1923
let own_txn_id = own_txn_id. to_string ( ) ;
1912
-
1913
1924
self . acquire ( )
1914
1925
. await ?
1915
1926
. with_transaction ( move |txn| {
@@ -2011,26 +2022,28 @@ impl StateStore for SqliteStateStore {
2011
2022
let room_id = self . encode_key ( keys:: DEPENDENTS_SEND_QUEUE , room_id) ;
2012
2023
2013
2024
// Note: transaction_id is not encoded, see why in `save_send_queue_event`.
2014
- let res: Vec < ( String , String , Option < Vec < u8 > > , Vec < u8 > ) > = self
2025
+ let res: Vec < ( String , String , Option < Vec < u8 > > , Vec < u8 > , u64 ) > = self
2015
2026
. acquire ( )
2016
2027
. await ?
2017
2028
. prepare (
2018
- "SELECT own_transaction_id, parent_transaction_id, parent_key, content FROM dependent_send_queue_events WHERE room_id = ? ORDER BY ROWID" ,
2029
+ "SELECT own_transaction_id, parent_transaction_id, parent_key, content, enqueue_time FROM dependent_send_queue_events WHERE room_id = ? ORDER BY ROWID" ,
2019
2030
|mut stmt| {
2020
2031
stmt. query ( ( room_id, ) ) ?
2021
- . mapped ( |row| Ok ( ( row. get ( 0 ) ?, row. get ( 1 ) ?, row. get ( 2 ) ?, row. get ( 3 ) ?) ) )
2032
+ . mapped ( |row| Ok ( ( row. get ( 0 ) ?, row. get ( 1 ) ?, row. get ( 2 ) ?, row. get ( 3 ) ?, row . get ( 4 ) ? ) ) )
2022
2033
. collect ( )
2023
2034
} ,
2024
2035
)
2025
2036
. await ?;
2026
2037
2027
2038
let mut dependent_events = Vec :: with_capacity ( res. len ( ) ) ;
2028
2039
for entry in res {
2040
+ let enqueue_time = MilliSecondsSinceUnixEpoch ( UInt :: new ( entry. 4 ) . unwrap ( ) ) ;
2029
2041
dependent_events. push ( DependentQueuedRequest {
2030
2042
own_transaction_id : entry. 0 . into ( ) ,
2031
2043
parent_transaction_id : entry. 1 . into ( ) ,
2032
2044
parent_key : entry. 2 . map ( |bytes| self . deserialize_value ( & bytes) ) . transpose ( ) ?,
2033
2045
kind : self . deserialize_json ( & entry. 3 ) ?,
2046
+ enqueue_time : Some ( enqueue_time) ,
2034
2047
} ) ;
2035
2048
}
2036
2049
@@ -2427,7 +2440,6 @@ mod migration_tests {
2427
2440
let room_id_value = this. serialize_value ( & room_id. to_owned ( ) ) ?;
2428
2441
2429
2442
let content = this. serialize_json ( & content) ?;
2430
-
2431
2443
txn. prepare_cached ( "INSERT INTO send_queue_events (room_id, room_id_val, transaction_id, content, wedged) VALUES (?, ?, ?, ?, ?)" ) ?
2432
2444
. execute ( ( room_id_key, room_id_value, transaction_id. to_string ( ) , content, is_wedged) ) ?;
2433
2445
0 commit comments