Converted to using rusqlite

This commit is contained in:
Mikayla Maki 2022-10-13 18:24:00 -07:00 committed by K Simmons
parent aa8fa4a6d5
commit dbea3cf20c
8 changed files with 91 additions and 172 deletions

149
Cargo.lock generated
View file

@ -244,21 +244,6 @@ dependencies = [
"futures-lite", "futures-lite",
] ]
[[package]]
name = "async-global-executor"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0da5b41ee986eed3f524c380e6d64965aea573882a8907682ad100f7859305ca"
dependencies = [
"async-channel",
"async-executor",
"async-io",
"async-lock",
"blocking",
"futures-lite",
"once_cell",
]
[[package]] [[package]]
name = "async-io" name = "async-io"
version = "1.9.0" version = "1.9.0"
@ -338,33 +323,6 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "async-std"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62565bb4402e926b29953c785397c6dc0391b7b446e45008b0049eb43cec6f5d"
dependencies = [
"async-channel",
"async-global-executor",
"async-io",
"async-lock",
"async-process",
"crossbeam-utils 0.8.12",
"futures-channel",
"futures-core",
"futures-io",
"futures-lite",
"gloo-timers",
"kv-log-macro",
"log",
"memchr",
"once_cell",
"pin-project-lite 0.2.9",
"pin-utils",
"slab",
"wasm-bindgen-futures",
]
[[package]] [[package]]
name = "async-stream" name = "async-stream"
version = "0.3.3" version = "0.3.3"
@ -1645,8 +1603,10 @@ dependencies = [
"async-trait", "async-trait",
"collections", "collections",
"gpui", "gpui",
"lazy_static",
"parking_lot 0.11.2", "parking_lot 0.11.2",
"sqlx", "rusqlite",
"rusqlite_migration",
"tempdir", "tempdir",
] ]
@ -1868,9 +1828,6 @@ name = "either"
version = "1.8.0" version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "encoding_rs" name = "encoding_rs"
@ -1974,6 +1931,12 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
[[package]]
name = "fallible-streaming-iterator"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
[[package]] [[package]]
name = "fastrand" name = "fastrand"
version = "1.8.0" version = "1.8.0"
@ -2041,18 +2004,6 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7bad48618fdb549078c333a7a8528acb57af271d0433bdecd523eb620628364e" checksum = "7bad48618fdb549078c333a7a8528acb57af271d0433bdecd523eb620628364e"
[[package]]
name = "flume"
version = "0.10.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1657b4441c3403d9f7b3409e47575237dac27b1b5726df654a6ecbf92f0f7577"
dependencies = [
"futures-core",
"futures-sink",
"pin-project",
"spin 0.9.4",
]
[[package]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.7" version = "1.0.7"
@ -2307,17 +2258,6 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "futures-rustls"
version = "0.22.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2411eed028cdf8c8034eaf21f9915f956b6c3abec4d4c7949ee67f0721127bd"
dependencies = [
"futures-io",
"rustls 0.20.6",
"webpki 0.22.0",
]
[[package]] [[package]]
name = "futures-sink" name = "futures-sink"
version = "0.3.24" version = "0.3.24"
@ -2473,18 +2413,6 @@ dependencies = [
"regex", "regex",
] ]
[[package]]
name = "gloo-timers"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fb7d06c1c8cc2a29bee7ec961009a0b2caa0793ee4900c2ffb348734ba1c8f9"
dependencies = [
"futures-channel",
"futures-core",
"js-sys",
"wasm-bindgen",
]
[[package]] [[package]]
name = "go_to_line" name = "go_to_line"
version = "0.1.0" version = "0.1.0"
@ -3093,15 +3021,6 @@ dependencies = [
"arrayvec 0.7.2", "arrayvec 0.7.2",
] ]
[[package]]
name = "kv-log-macro"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f"
dependencies = [
"log",
]
[[package]] [[package]]
name = "language" name = "language"
version = "0.1.0" version = "0.1.0"
@ -3154,7 +3073,7 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
dependencies = [ dependencies = [
"spin 0.5.2", "spin",
] ]
[[package]] [[package]]
@ -3229,9 +3148,9 @@ dependencies = [
[[package]] [[package]]
name = "libsqlite3-sys" name = "libsqlite3-sys"
version = "0.24.2" version = "0.25.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "898745e570c7d0453cc1fbc4a701eb6c662ed54e8fec8b7d14be137ebeeb9d14" checksum = "9f0455f2c1bc9a7caa792907026e469c1d91761fb0ea37cbb16427c77280cf35"
dependencies = [ dependencies = [
"cc", "cc",
"pkg-config", "pkg-config",
@ -4825,7 +4744,7 @@ dependencies = [
"cc", "cc",
"libc", "libc",
"once_cell", "once_cell",
"spin 0.5.2", "spin",
"untrusted", "untrusted",
"web-sys", "web-sys",
"winapi 0.3.9", "winapi 0.3.9",
@ -4932,6 +4851,31 @@ dependencies = [
"zeroize", "zeroize",
] ]
[[package]]
name = "rusqlite"
version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01e213bc3ecb39ac32e81e51ebe31fd888a940515173e3a18a35f8c6e896422a"
dependencies = [
"bitflags",
"fallible-iterator",
"fallible-streaming-iterator",
"hashlink",
"libsqlite3-sys",
"serde_json",
"smallvec",
]
[[package]]
name = "rusqlite_migration"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eda44233be97aea786691f9f6f7ef230bcf905061f4012e90f4f39e6dcf31163"
dependencies = [
"log",
"rusqlite",
]
[[package]] [[package]]
name = "rust-embed" name = "rust-embed"
version = "6.4.1" version = "6.4.1"
@ -5639,15 +5583,6 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]]
name = "spin"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f6002a767bff9e83f8eeecf883ecb8011875a21ae8da43bffb817a57e78cc09"
dependencies = [
"lock_api",
]
[[package]] [[package]]
name = "spsc-buffer" name = "spsc-buffer"
version = "0.1.1" version = "0.1.1"
@ -5693,10 +5628,8 @@ dependencies = [
"dotenvy", "dotenvy",
"either", "either",
"event-listener", "event-listener",
"flume",
"futures-channel", "futures-channel",
"futures-core", "futures-core",
"futures-executor",
"futures-intrusive", "futures-intrusive",
"futures-util", "futures-util",
"hashlink", "hashlink",
@ -5706,7 +5639,6 @@ dependencies = [
"indexmap", "indexmap",
"itoa", "itoa",
"libc", "libc",
"libsqlite3-sys",
"log", "log",
"md-5", "md-5",
"memchr", "memchr",
@ -5742,12 +5674,9 @@ dependencies = [
"dotenvy", "dotenvy",
"either", "either",
"heck 0.4.0", "heck 0.4.0",
"hex",
"once_cell", "once_cell",
"proc-macro2", "proc-macro2",
"quote", "quote",
"serde",
"serde_json",
"sha2 0.10.6", "sha2 0.10.6",
"sqlx-core", "sqlx-core",
"sqlx-rt", "sqlx-rt",
@ -5761,8 +5690,6 @@ version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24c5b2d25fa654cc5f841750b8e1cdedbe21189bf9a9382ee90bfa9dd3562396" checksum = "24c5b2d25fa654cc5f841750b8e1cdedbe21189bf9a9382ee90bfa9dd3562396"
dependencies = [ dependencies = [
"async-std",
"futures-rustls",
"once_cell", "once_cell",
"tokio", "tokio",
"tokio-rustls", "tokio-rustls",

View file

@ -135,25 +135,21 @@ impl Telemetry {
Some(self.state.lock().log_file.as_ref()?.path().to_path_buf()) Some(self.state.lock().log_file.as_ref()?.path().to_path_buf())
} }
pub fn start(self: &Arc<Self>, db: Arc<Db>) { pub fn start(self: &Arc<Self>, db: Arc<Mutex<Db>>) {
let this = self.clone(); let this = self.clone();
self.executor self.executor
.spawn( .spawn(
async move { async move {
let device_id = if let Some(device_id) = db let db = db.lock();
.read(["device_id"])? let device_id = if let Ok(device_id) = db.read_kvp("device_id") {
.into_iter()
.flatten()
.next()
.and_then(|bytes| String::from_utf8(bytes).ok())
{
device_id device_id
} else { } else {
let device_id = Uuid::new_v4().to_string(); let device_id = Uuid::new_v4().to_string();
db.write([("device_id", device_id.as_bytes())])?; db.write_kvp("device_id", &device_id)?;
device_id device_id
}; };
drop(db);
let device_id = Some(Arc::from(device_id)); let device_id = Some(Arc::from(device_id));
let mut state = this.state.lock(); let mut state = this.state.lock();
state.device_id = device_id.clone(); state.device_id = device_id.clone();

View file

@ -15,7 +15,9 @@ collections = { path = "../collections" }
anyhow = "1.0.57" anyhow = "1.0.57"
async-trait = "0.1" async-trait = "0.1"
parking_lot = "0.11.1" parking_lot = "0.11.1"
sqlx = { version = "0.6.2", features = ["sqlite", "macros", "offline", "json", "runtime-async-std-rustls"] } rusqlite = { version = "0.28.0", features = ["bundled", "serde_json"] }
rusqlite_migration = "1.0.0"
lazy_static = "1.4.0"
[dev-dependencies] [dev-dependencies]
gpui = { path = "../gpui", features = ["test-support"] } gpui = { path = "../gpui", features = ["test-support"] }

View file

@ -1,4 +0,0 @@
CREATE TABLE kv_store(
key TEXT PRIMARY KEY,
value TEXT NOT NULL
) STRICT;

View file

@ -1,3 +0,0 @@
{
"db": "SQLite"
}

View file

@ -1,16 +1,17 @@
mod kvp; mod kvp;
mod migrations;
use anyhow::Result; use anyhow::Result;
use sqlx::sqlite::SqliteConnectOptions; use migrations::MIGRATIONS;
use sqlx::{Pool, Sqlite}; use parking_lot::Mutex;
use rusqlite::Connection;
use std::path::Path; use std::path::Path;
use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
pub use kvp::*; pub use kvp::*;
pub struct Db { pub struct Db {
pool: Pool<Sqlite>, connecion: Connection,
in_memory: bool, in_memory: bool,
} }
@ -21,35 +22,26 @@ pub struct Db {
impl Db { impl Db {
/// Open or create a database at the given file path. Falls back to in memory database if the /// Open or create a database at the given file path. Falls back to in memory database if the
/// database at the given path is corrupted /// database at the given path is corrupted
pub async fn open(path: &Path) -> Arc<Self> { pub fn open(path: &Path) -> Result<Arc<Mutex<Self>>> {
let options = SqliteConnectOptions::from_str(&format!( let conn = Connection::open(path)?;
"sqlite://{}",
path.to_string_lossy().to_string()
))
.expect("database path should always be well formed")
.create_if_missing(true);
Self::initialize(options, false) Self::initialize(conn, false).or_else(|_| Self::open_in_memory())
.await
.unwrap_or(Self::open_in_memory().await)
} }
/// Open a in memory database for testing and as a fallback. /// Open a in memory database for testing and as a fallback.
pub async fn open_in_memory() -> Arc<Self> { pub fn open_in_memory() -> Result<Arc<Mutex<Self>>> {
let options = SqliteConnectOptions::from_str(":memory:") let conn = Connection::open_in_memory()?;
.expect("Should always be able to create in memory database options");
Self::initialize(options, true) Self::initialize(conn, true)
.await
.expect("Should always be able to create an in memory database")
} }
async fn initialize(options: SqliteConnectOptions, in_memory: bool) -> Result<Arc<Self>> { fn initialize(mut conn: Connection, in_memory: bool) -> Result<Arc<Mutex<Self>>> {
let pool = Pool::<Sqlite>::connect_with(options).await?; MIGRATIONS.to_latest(&mut conn)?;
sqlx::migrate!().run(&pool).await?; Ok(Arc::new(Mutex::new(Self {
connecion: conn,
Ok(Arc::new(Self { pool, in_memory })) in_memory,
})))
} }
} }
@ -61,7 +53,7 @@ mod tests {
#[gpui::test] #[gpui::test]
fn test_db() { fn test_db() {
let dir = TempDir::new("db-test").unwrap(); let dir = TempDir::new("db-test").unwrap();
let fake_db = Db::open_fake(); let fake_db = Db::open_in_memory().unwrap();
let real_db = Db::open(&dir.path().join("test.db")).unwrap(); let real_db = Db::open(&dir.path().join("test.db")).unwrap();
for db in [&real_db, &fake_db] { for db in [&real_db, &fake_db] {

View file

@ -3,31 +3,30 @@ use anyhow::Result;
use super::Db; use super::Db;
impl Db { impl Db {
pub async fn read_kvp(&self, key: &str) -> Result<String> { pub fn read_kvp(&self, key: &str) -> Result<String> {
let value = sqlx::query!("SELECT value FROM kv_store WHERE key = ?", key) let mut stmt = self
.fetch_one(&self.pool) .connecion
.await .prepare_cached("SELECT value FROM kv_store WHERE key = (?)")?;
.map(|res| res.value)?;
Ok(value) Ok(stmt.query_row([key], |row| row.get(0))?)
} }
pub async fn delete_kvp(&self, key: &str) -> Result<()> { pub fn delete_kvp(&self, key: &str) -> Result<()> {
sqlx::query!("DELETE FROM kv_store WHERE key = ?", key) let mut stmt = self
.execute(&self.pool) .connecion
.await?; .prepare_cached("SELECT value FROM kv_store WHERE key = (?)")?;
stmt.execute([key])?;
Ok(()) Ok(())
} }
pub async fn write_kvp(&self, key: &str, value: &str) -> Result<()> { pub fn write_kvp(&self, key: &str, value: &str) -> Result<()> {
sqlx::query!( let mut stmt = self
"INSERT OR REPLACE INTO kv_store(key, value) VALUES (?, ?)", .connecion
key, .prepare_cached("INSERT OR REPLACE INTO kv_store(key, value) VALUES ((?), (?))")?;
value
) stmt.execute([key, value])?;
.execute(&self.pool)
.await?;
Ok(()) Ok(())
} }

View file

@ -0,0 +1,10 @@
use rusqlite_migration::{Migrations, M};
lazy_static::lazy_static! {
pub static ref MIGRATIONS: Migrations<'static> = Migrations::new(vec![M::up(
"CREATE TABLE kv_store(
key TEXT PRIMARY KEY,
value TEXT NOT NULL
) STRICT;",
)]);
}