Address some issues with the sqlez_macros

This commit is contained in:
Kay Simmons 2022-11-30 16:19:46 -08:00 committed by Mikayla Maki
parent 1b225fa37c
commit f68e8d4664
10 changed files with 183 additions and 174 deletions

View file

@ -4,7 +4,6 @@ pub mod kvp;
pub use anyhow;
pub use indoc::indoc;
pub use lazy_static;
use parking_lot::Mutex;
pub use smol;
pub use sqlez;
pub use sqlez_macros;
@ -34,7 +33,7 @@ lazy_static::lazy_static! {
}
/// Open or create a database at the given directory path.
pub async fn open_file_db<M: Migrator>() -> ThreadSafeConnection<M> {
pub async fn open_db<M: Migrator>() -> ThreadSafeConnection<M> {
// Use 0 for now. Will implement incrementing and clearing of old db files soon TM
let current_db_dir = (*DB_DIR).join(Path::new(&format!("0-{}", *RELEASE_CHANNEL_NAME)));
@ -56,18 +55,15 @@ pub async fn open_file_db<M: Migrator>() -> ThreadSafeConnection<M> {
.await
}
pub async fn open_memory_db<M: Migrator>(db_name: &str) -> ThreadSafeConnection<M> {
#[cfg(any(test, feature = "test-support"))]
pub async fn open_test_db<M: Migrator>(db_name: &str) -> ThreadSafeConnection<M> {
use sqlez::thread_safe_connection::locking_queue;
ThreadSafeConnection::<M>::builder(db_name, false)
.with_db_initialization_query(DB_INITIALIZE_QUERY)
.with_connection_initialize_query(CONNECTION_INITIALIZE_QUERY)
// Serialize queued writes via a mutex and run them synchronously
.with_write_queue_constructor(Box::new(|connection| {
let connection = Mutex::new(connection);
Box::new(move |queued_write| {
let connection = connection.lock();
queued_write(&connection)
})
}))
.with_write_queue_constructor(locking_queue())
.build()
.await
}
@ -76,22 +72,24 @@ pub async fn open_memory_db<M: Migrator>(db_name: &str) -> ThreadSafeConnection<
#[macro_export]
macro_rules! connection {
($id:ident: $t:ident<$d:ty>) => {
pub struct $t(::db::sqlez::thread_safe_connection::ThreadSafeConnection<$d>);
pub struct $t($crate::sqlez::thread_safe_connection::ThreadSafeConnection<$d>);
impl ::std::ops::Deref for $t {
type Target = ::db::sqlez::thread_safe_connection::ThreadSafeConnection<$d>;
type Target = $crate::sqlez::thread_safe_connection::ThreadSafeConnection<$d>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
::db::lazy_static::lazy_static! {
pub static ref $id: $t = $t(if cfg!(any(test, feature = "test-support")) {
$crate::smol::block_on(::db::open_memory_db(stringify!($id)))
} else {
$crate::smol::block_on(::db::open_file_db())
});
#[cfg(any(test, feature = "test-support"))]
$crate::lazy_static::lazy_static! {
pub static ref $id: $t = $t($crate::smol::block_on($crate::open_test_db(stringify!($id))));
}
#[cfg(not(any(test, feature = "test-support")))]
$crate::lazy_static::lazy_static! {
pub static ref $id: $t = $t($crate::smol::block_on($crate::open_db()));
}
};
}

View file

@ -1,25 +1,9 @@
use sqlez::{domain::Domain, thread_safe_connection::ThreadSafeConnection};
use sqlez::domain::Domain;
use sqlez_macros::sql;
use crate::{open_file_db, open_memory_db, query};
use crate::{connection, query};
pub struct KeyValueStore(ThreadSafeConnection<KeyValueStore>);
impl std::ops::Deref for KeyValueStore {
type Target = ThreadSafeConnection<KeyValueStore>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
lazy_static::lazy_static! {
pub static ref KEY_VALUE_STORE: KeyValueStore = KeyValueStore(if cfg!(any(test, feature = "test-support")) {
smol::block_on(open_memory_db("KEY_VALUE_STORE"))
} else {
smol::block_on(open_file_db())
});
}
connection!(KEY_VALUE_STORE: KeyValueStore<KeyValueStore>);
impl Domain for KeyValueStore {
fn name() -> &'static str {
@ -27,8 +11,10 @@ impl Domain for KeyValueStore {
}
fn migrations() -> &'static [&'static str] {
// Legacy migrations using rusqlite may have already created kv_store during alpha,
// migrations must be infallible so this must have 'IF NOT EXISTS'
&[sql!(
CREATE TABLE kv_store(
CREATE TABLE IF NOT EXISTS kv_store(
key TEXT PRIMARY KEY,
value TEXT NOT NULL
) STRICT;
@ -62,7 +48,7 @@ mod tests {
#[gpui::test]
async fn test_kvp() {
let db = KeyValueStore(crate::open_memory_db("test_kvp").await);
let db = KeyValueStore(crate::open_test_db("test_kvp").await);
assert_eq!(db.read_kvp("key-1").unwrap(), None);