Skip to content

Commit 62c21e4

Browse files
reivilibresandhose
authored andcommitted
Add stubs for migrating devices, access tokens and refresh tokens
1 parent 0f3b2d2 commit 62c21e4

File tree

2 files changed

+82
-7
lines changed

2 files changed

+82
-7
lines changed

crates/syn2mas/src/migration.rs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ struct UsersMigrated {
6868
user_localparts_to_uuid: HashMap<CompactString, Uuid>,
6969
}
7070

71+
struct DevicesMigrated {
72+
/// Lookup table from `(user_id, device_id)` pairs to the
73+
/// UUID of the `compat_session` in MAS
74+
devices_to_uuid: HashMap<(CompactString, CompactString), Uuid>,
75+
}
76+
7177
/// Performs a migration from Synapse's database to MAS' database.
7278
///
7379
/// # Panics
@@ -96,7 +102,7 @@ pub async fn migrate(
96102
counts
97103
.users
98104
.try_into()
99-
.expect("More than usize::MAX users — wow!"),
105+
.expect("More than usize::MAX users — unable to handle this many!"),
100106
server_name,
101107
rng,
102108
)
@@ -121,6 +127,29 @@ pub async fn migrate(
121127
)
122128
.await?;
123129

130+
let migrated_devices = migrate_devices(
131+
synapse,
132+
mas,
133+
counts
134+
.devices
135+
.try_into()
136+
.expect("More than usize::MAX devices — unable to handle this many!"),
137+
server_name,
138+
rng,
139+
&migrated_users.user_localparts_to_uuid,
140+
)
141+
.await?;
142+
143+
migrate_access_and_refresh_tokens(
144+
synapse,
145+
mas,
146+
server_name,
147+
rng,
148+
&migrated_users.user_localparts_to_uuid,
149+
&migrated_devices.devices_to_uuid,
150+
)
151+
.await?;
152+
124153
Ok(())
125154
}
126155

@@ -312,6 +341,36 @@ async fn migrate_external_ids(
312341

313342
Ok(())
314343
}
344+
#[tracing::instrument(skip_all, level = Level::INFO)]
345+
async fn migrate_devices(
346+
synapse: &mut SynapseReader<'_>,
347+
mas: &mut MasWriter<'_>,
348+
device_count_hint: usize,
349+
server_name: &str,
350+
rng: &mut impl RngCore,
351+
user_localparts_to_uuid: &HashMap<CompactString, Uuid>,
352+
) -> Result<DevicesMigrated, Error> {
353+
// TODO is 1:1 enough capacity for a HashMap?
354+
let mut devices_to_uuid = HashMap::with_capacity(device_count_hint);
355+
356+
todo!();
357+
358+
Ok(DevicesMigrated { devices_to_uuid })
359+
}
360+
361+
#[tracing::instrument(skip_all, level = Level::INFO)]
362+
async fn migrate_access_and_refresh_tokens(
363+
synapse: &mut SynapseReader<'_>,
364+
mas: &mut MasWriter<'_>,
365+
server_name: &str,
366+
rng: &mut impl RngCore,
367+
user_localparts_to_uuid: &HashMap<CompactString, Uuid>,
368+
devices: &HashMap<(CompactString, CompactString), Uuid>,
369+
) -> Result<(), Error> {
370+
todo!();
371+
372+
Ok(())
373+
}
315374

316375
fn transform_user(
317376
user: &SynapseUser,

crates/syn2mas/src/synapse_reader/mod.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,21 @@ pub struct SynapseRefreshToken {
235235
/// underneath our feet. It's still not a good idea to run Synapse at the same
236236
/// time as the migration.
237237
// TODO not complete!
238-
const TABLES_TO_LOCK: &[&str] = &["users", "user_threepids", "user_external_ids"];
238+
const TABLES_TO_LOCK: &[&str] = &[
239+
"users",
240+
"user_threepids",
241+
"user_external_ids",
242+
"devices",
243+
"access_tokens",
244+
"refresh_tokens",
245+
];
239246

240247
/// Number of migratable rows in various Synapse tables.
241248
/// Used to estimate progress.
242249
#[derive(Clone, Debug)]
243250
pub struct SynapseRowCounts {
244251
pub users: i64,
252+
pub devices: i64,
245253
}
246254

247255
pub struct SynapseReader<'c> {
@@ -312,19 +320,27 @@ impl<'conn> SynapseReader<'conn> {
312320
///
313321
/// - An underlying database error
314322
pub async fn count_rows(&mut self) -> Result<SynapseRowCounts, Error> {
315-
let users = sqlx::query(
323+
let users: i64 = sqlx::query_scalar(
316324
"
317325
SELECT COUNT(1) FROM users
318326
WHERE appservice_id IS NULL AND is_guest = 0
319327
",
320328
)
321329
.fetch_one(&mut *self.txn)
322330
.await
323-
.into_database("counting Synapse users")?
324-
.try_get::<i64, _>(0)
325-
.into_database("couldn't decode count of Synapse users table")?;
331+
.into_database("counting Synapse users")?;
326332

327-
Ok(SynapseRowCounts { users })
333+
let devices = sqlx::query_scalar(
334+
"
335+
SELECT COUNT(1) FROM devices
336+
WHERE NOT hidden
337+
",
338+
)
339+
.fetch_one(&mut *self.txn)
340+
.await
341+
.into_database("counting Synapse devices")?;
342+
343+
Ok(SynapseRowCounts { users, devices })
328344
}
329345

330346
/// Reads Synapse users, excluding application service users (which do not

0 commit comments

Comments
 (0)