@@ -193,13 +193,21 @@ pub struct SynapseThreepid {
193
193
pub added_at : MillisecondsTimestamp ,
194
194
}
195
195
196
+ /// Row of the `user_external_ids` table in Synapse.
197
+ #[ derive( Clone , Debug , FromRow , PartialEq , Eq , PartialOrd , Ord ) ]
198
+ pub struct SynapseExternalId {
199
+ pub user_id : FullUserId ,
200
+ pub auth_provider : String ,
201
+ pub external_id : String ,
202
+ }
203
+
196
204
/// List of Synapse tables that we should acquire an `EXCLUSIVE` lock on.
197
205
///
198
206
/// This is a safety measure against other processes changing the data
199
207
/// underneath our feet. It's still not a good idea to run Synapse at the same
200
208
/// time as the migration.
201
209
// TODO not complete!
202
- const TABLES_TO_LOCK : & [ & str ] = & [ "users" ] ;
210
+ const TABLES_TO_LOCK : & [ & str ] = & [ "users" , "user_threepids" , "user_external_ids" ] ;
203
211
204
212
/// Number of migratable rows in various Synapse tables.
205
213
/// Used to estimate progress.
@@ -319,6 +327,21 @@ impl<'conn> SynapseReader<'conn> {
319
327
. fetch ( & mut * self . txn )
320
328
. map_err ( |err| err. into_database ( "reading Synapse threepids" ) )
321
329
}
330
+
331
+ /// Read associations between Synapse users and external identity providers
332
+ pub fn read_user_external_ids (
333
+ & mut self ,
334
+ ) -> impl Stream < Item = Result < SynapseExternalId , Error > > + ' _ {
335
+ sqlx:: query_as (
336
+ "
337
+ SELECT
338
+ user_id, auth_provider, external_id
339
+ FROM user_external_ids
340
+ " ,
341
+ )
342
+ . fetch ( & mut * self . txn )
343
+ . map_err ( |err| err. into_database ( "reading Synapse user external IDs" ) )
344
+ }
322
345
}
323
346
324
347
#[ cfg( test) ]
@@ -330,7 +353,7 @@ mod test {
330
353
use sqlx:: { migrate:: Migrator , PgPool } ;
331
354
332
355
use crate :: {
333
- synapse_reader:: { SynapseThreepid , SynapseUser } ,
356
+ synapse_reader:: { SynapseExternalId , SynapseThreepid , SynapseUser } ,
334
357
SynapseReader ,
335
358
} ;
336
359
@@ -368,4 +391,20 @@ mod test {
368
391
369
392
assert_debug_snapshot ! ( threepids) ;
370
393
}
394
+
395
+ #[ sqlx:: test( migrator = "MIGRATOR" , fixtures( "user_alice" , "external_ids_alice" ) ) ]
396
+ async fn test_read_external_ids ( pool : PgPool ) {
397
+ let mut conn = pool. acquire ( ) . await . expect ( "failed to get connection" ) ;
398
+ let mut reader = SynapseReader :: new ( & mut conn, false )
399
+ . await
400
+ . expect ( "failed to make SynapseReader" ) ;
401
+
402
+ let external_ids: BTreeSet < SynapseExternalId > = reader
403
+ . read_user_external_ids ( )
404
+ . try_collect ( )
405
+ . await
406
+ . expect ( "failed to read Synapse external user IDs" ) ;
407
+
408
+ assert_debug_snapshot ! ( external_ids) ;
409
+ }
371
410
}
0 commit comments