Skip to content

Commit 7019ad0

Browse files
committed
Add SynapseReader support and test for external IDs
1 parent c016199 commit 7019ad0

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
INSERT INTO user_external_ids
2+
(
3+
user_id,
4+
auth_provider,
5+
external_id
6+
)
7+
VALUES
8+
(
9+
'@alice:example.com',
10+
'oidc-raasu',
11+
'871.syn30'
12+
);

crates/syn2mas/src/synapse_reader/mod.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,21 @@ pub struct SynapseThreepid {
193193
pub added_at: MillisecondsTimestamp,
194194
}
195195

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+
196204
/// List of Synapse tables that we should acquire an `EXCLUSIVE` lock on.
197205
///
198206
/// This is a safety measure against other processes changing the data
199207
/// underneath our feet. It's still not a good idea to run Synapse at the same
200208
/// time as the migration.
201209
// TODO not complete!
202-
const TABLES_TO_LOCK: &[&str] = &["users"];
210+
const TABLES_TO_LOCK: &[&str] = &["users", "user_threepids", "user_external_ids"];
203211

204212
/// Number of migratable rows in various Synapse tables.
205213
/// Used to estimate progress.
@@ -319,6 +327,21 @@ impl<'conn> SynapseReader<'conn> {
319327
.fetch(&mut *self.txn)
320328
.map_err(|err| err.into_database("reading Synapse threepids"))
321329
}
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+
}
322345
}
323346

324347
#[cfg(test)]
@@ -330,7 +353,7 @@ mod test {
330353
use sqlx::{migrate::Migrator, PgPool};
331354

332355
use crate::{
333-
synapse_reader::{SynapseThreepid, SynapseUser},
356+
synapse_reader::{SynapseExternalId, SynapseThreepid, SynapseUser},
334357
SynapseReader,
335358
};
336359

@@ -368,4 +391,20 @@ mod test {
368391

369392
assert_debug_snapshot!(threepids);
370393
}
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+
}
371410
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
source: crates/syn2mas/src/synapse_reader/mod.rs
3+
expression: external_ids
4+
---
5+
{
6+
SynapseExternalId {
7+
user_id: FullUserId(
8+
"@alice:example.com",
9+
),
10+
auth_provider: "oidc-raasu",
11+
external_id: "871.syn30",
12+
},
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- Copyright 2025 New Vector Ltd.
2+
--
3+
-- SPDX-License-Identifier: AGPL-3.0-only
4+
-- Please see LICENSE in the repository root for full details.
5+
6+
-- Brings in the `user_external_ids` table from Synapse
7+
8+
CREATE TABLE user_external_ids (
9+
auth_provider text NOT NULL,
10+
external_id text NOT NULL,
11+
user_id text NOT NULL
12+
);

0 commit comments

Comments
 (0)