This PR adds `system_id` to telemetry, which is contained within a new `global` database (accessible by any release channel of Zed on a single system). This will help us get a more accurate understanding of user count, instead of relying on `installationd_id`, which is different per release channel. This doesn't solve the problem of a user with multiple machines, but it gets us closer. Release Notes: - N/A
93 lines
2.4 KiB
Rust
93 lines
2.4 KiB
Rust
use sqlez_macros::sql;
|
|
|
|
use crate::{define_connection, query};
|
|
|
|
define_connection!(pub static ref KEY_VALUE_STORE: KeyValueStore<()> =
|
|
&[sql!(
|
|
CREATE TABLE IF NOT EXISTS kv_store(
|
|
key TEXT PRIMARY KEY,
|
|
value TEXT NOT NULL
|
|
) STRICT;
|
|
)];
|
|
);
|
|
|
|
impl KeyValueStore {
|
|
query! {
|
|
pub fn read_kvp(key: &str) -> Result<Option<String>> {
|
|
SELECT value FROM kv_store WHERE key = (?)
|
|
}
|
|
}
|
|
|
|
query! {
|
|
pub async fn write_kvp(key: String, value: String) -> Result<()> {
|
|
INSERT OR REPLACE INTO kv_store(key, value) VALUES ((?), (?))
|
|
}
|
|
}
|
|
|
|
query! {
|
|
pub async fn delete_kvp(key: String) -> Result<()> {
|
|
DELETE FROM kv_store WHERE key = (?)
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use crate::kvp::KeyValueStore;
|
|
|
|
#[gpui::test]
|
|
async fn test_kvp() {
|
|
let db = KeyValueStore(crate::open_test_db("test_kvp").await);
|
|
|
|
assert_eq!(db.read_kvp("key-1").unwrap(), None);
|
|
|
|
db.write_kvp("key-1".to_string(), "one".to_string())
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(db.read_kvp("key-1").unwrap(), Some("one".to_string()));
|
|
|
|
db.write_kvp("key-1".to_string(), "one-2".to_string())
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(db.read_kvp("key-1").unwrap(), Some("one-2".to_string()));
|
|
|
|
db.write_kvp("key-2".to_string(), "two".to_string())
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(db.read_kvp("key-2").unwrap(), Some("two".to_string()));
|
|
|
|
db.delete_kvp("key-1".to_string()).await.unwrap();
|
|
assert_eq!(db.read_kvp("key-1").unwrap(), None);
|
|
}
|
|
}
|
|
|
|
define_connection!(pub static ref GLOBAL_KEY_VALUE_STORE: GlobalKeyValueStore<()> =
|
|
&[sql!(
|
|
CREATE TABLE IF NOT EXISTS kv_store(
|
|
key TEXT PRIMARY KEY,
|
|
value TEXT NOT NULL
|
|
) STRICT;
|
|
)];
|
|
global
|
|
);
|
|
|
|
impl GlobalKeyValueStore {
|
|
query! {
|
|
pub fn read_kvp(key: &str) -> Result<Option<String>> {
|
|
SELECT value FROM kv_store WHERE key = (?)
|
|
}
|
|
}
|
|
|
|
query! {
|
|
pub async fn write_kvp(key: String, value: String) -> Result<()> {
|
|
INSERT OR REPLACE INTO kv_store(key, value) VALUES ((?), (?))
|
|
}
|
|
}
|
|
|
|
query! {
|
|
pub async fn delete_kvp(key: String) -> Result<()> {
|
|
DELETE FROM kv_store WHERE key = (?)
|
|
}
|
|
}
|
|
}
|