Skip to content

Commit 89b8cec

Browse files
committed
feat(android): migrate unknown hostname
1 parent 9a8802a commit 89b8cec

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

aw-datastore/src/datastore.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,4 +966,40 @@ impl DatastoreInstance {
966966
},
967967
}
968968
}
969+
970+
/// Migrates all buckets whose hostname is "unknown" or "Unknown" to `new_hostname`.
971+
/// Events are left untouched; only the bucket metadata is updated.
972+
/// Returns the number of buckets that were updated.
973+
pub fn migrate_hostname(
974+
&mut self,
975+
conn: &Connection,
976+
new_hostname: &str,
977+
) -> Result<usize, DatastoreError> {
978+
info!(
979+
"Migrating hostname from 'unknown'/'Unknown' to '{}'",
980+
new_hostname
981+
);
982+
983+
let updated = match conn.execute(
984+
"UPDATE buckets SET hostname = ?1 WHERE hostname = 'unknown' OR hostname = 'Unknown'",
985+
[new_hostname],
986+
) {
987+
Ok(n) => n,
988+
Err(err) => {
989+
return Err(DatastoreError::InternalError(format!(
990+
"Failed to migrate hostname: {err}"
991+
)))
992+
}
993+
};
994+
995+
if updated > 0 {
996+
info!("Migrated hostname for {} bucket(s)", updated);
997+
// Refresh the in-memory cache so callers see the new hostnames immediately.
998+
self.get_stored_buckets(conn)?;
999+
} else {
1000+
info!("No buckets with hostname 'unknown'/'Unknown' found; nothing to migrate");
1001+
}
1002+
1003+
Ok(updated)
1004+
}
9691005
}

aw-datastore/src/worker.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub enum Command {
7777
GetKeyValue(String),
7878
SetKeyValue(String, String),
7979
DeleteKeyValue(String),
80+
MigrateHostname(String),
8081
Close(),
8182
}
8283

@@ -305,6 +306,17 @@ impl DatastoreWorker {
305306
Ok(()) => Ok(Response::Empty()),
306307
Err(e) => Err(e),
307308
},
309+
Command::MigrateHostname(new_hostname) => {
310+
match ds.migrate_hostname(tx, &new_hostname) {
311+
Ok(count) => {
312+
if count > 0 {
313+
self.commit = true;
314+
}
315+
Ok(Response::Count(count as i64))
316+
}
317+
Err(e) => Err(e),
318+
}
319+
}
308320
Command::Close() => {
309321
self.quit = true;
310322
Ok(Response::Empty())
@@ -529,6 +541,22 @@ impl Datastore {
529541
_unwrap_response(receiver)
530542
}
531543

544+
/// Migrates all buckets whose hostname is "unknown" or "Unknown" to `new_hostname`.
545+
/// Returns the number of buckets updated.
546+
pub fn migrate_hostname(&self, new_hostname: &str) -> Result<usize, DatastoreError> {
547+
let cmd = Command::MigrateHostname(new_hostname.to_string());
548+
let receiver = self.requester.request(cmd).unwrap();
549+
match receiver.collect().unwrap() {
550+
Ok(r) => match r {
551+
Response::Count(n) => Ok(n as usize),
552+
_ => Err(DatastoreError::InternalError(
553+
"Unexpected response to MigrateHostname command".to_string(),
554+
)),
555+
},
556+
Err(e) => Err(e),
557+
}
558+
}
559+
532560
// Should block until worker has stopped
533561
pub fn close(&self) {
534562
info!("Sending close request to database");

aw-server/src/android/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,24 @@ pub mod android {
250250
}
251251
}
252252

253+
#[no_mangle]
254+
pub unsafe extern "C" fn Java_net_activitywatch_android_RustInterface_migrateHostname(
255+
env: JNIEnv,
256+
_: JClass,
257+
hostname: JString,
258+
) -> jstring {
259+
let hostname = jstring_to_string(&env, hostname);
260+
if hostname.is_empty() {
261+
return create_error_object(&env, "hostname must not be empty".to_string());
262+
}
263+
match openDatastore().migrate_hostname(&hostname) {
264+
Ok(count) => {
265+
string_to_jstring(&env, format!("Migrated hostname for {} bucket(s)", count))
266+
}
267+
Err(e) => create_error_object(&env, format!("Failed to migrate hostname: {:?}", e)),
268+
}
269+
}
270+
253271
#[no_mangle]
254272
pub unsafe extern "C" fn Java_net_activitywatch_android_RustInterface_query(
255273
env: JNIEnv,

0 commit comments

Comments
 (0)