Dock persistence working!
Co-Authored-By: Mikayla Maki <mikayla@zed.dev>
This commit is contained in:
parent
c1f7902309
commit
d20d21c6a2
29 changed files with 783 additions and 443 deletions
|
@ -1,6 +1,6 @@
|
|||
pub mod kvp;
|
||||
|
||||
use std::fs;
|
||||
use std::fs::create_dir_all;
|
||||
use std::path::Path;
|
||||
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
|
@ -8,24 +8,29 @@ use anyhow::Result;
|
|||
use indoc::indoc;
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
use sqlez::connection::Connection;
|
||||
use sqlez::domain::Domain;
|
||||
use sqlez::domain::{Domain, Migrator};
|
||||
use sqlez::thread_safe_connection::ThreadSafeConnection;
|
||||
use util::channel::RELEASE_CHANNEL_NAME;
|
||||
use util::paths::DB_DIR;
|
||||
|
||||
const INITIALIZE_QUERY: &'static str = indoc! {"
|
||||
PRAGMA journal_mode=WAL;
|
||||
PRAGMA synchronous=NORMAL;
|
||||
PRAGMA busy_timeout=1;
|
||||
PRAGMA foreign_keys=TRUE;
|
||||
PRAGMA case_sensitive_like=TRUE;
|
||||
"};
|
||||
|
||||
/// Open or create a database at the given directory path.
|
||||
pub fn open_file_db<D: Domain>() -> ThreadSafeConnection<D> {
|
||||
pub fn open_file_db<M: Migrator>() -> ThreadSafeConnection<M> {
|
||||
// Use 0 for now. Will implement incrementing and clearing of old db files soon TM
|
||||
let current_db_dir = (*util::paths::DB_DIR).join(Path::new(&format!(
|
||||
"0-{}",
|
||||
*util::channel::RELEASE_CHANNEL_NAME
|
||||
)));
|
||||
fs::create_dir_all(¤t_db_dir).expect("Should be able to create the database directory");
|
||||
let current_db_dir = (*DB_DIR).join(Path::new(&format!("0-{}", *RELEASE_CHANNEL_NAME)));
|
||||
|
||||
// if *RELEASE_CHANNEL == ReleaseChannel::Dev {
|
||||
// remove_dir_all(¤t_db_dir).ok();
|
||||
// }
|
||||
|
||||
create_dir_all(¤t_db_dir).expect("Should be able to create the database directory");
|
||||
let db_path = current_db_dir.join(Path::new("db.sqlite"));
|
||||
|
||||
ThreadSafeConnection::new(db_path.to_string_lossy().as_ref(), true)
|
||||
|
@ -44,3 +49,23 @@ pub fn write_db_to<D: Domain, P: AsRef<Path>>(
|
|||
let destination = Connection::open_file(dest.as_ref().to_string_lossy().as_ref());
|
||||
conn.backup_main(&destination)
|
||||
}
|
||||
|
||||
/// Implements a basic DB wrapper for a given domain
|
||||
#[macro_export]
|
||||
macro_rules! connection {
|
||||
($id:ident: $t:ident<$d:ty>) => {
|
||||
pub struct $t(::sqlez::thread_safe_connection::ThreadSafeConnection<$d>);
|
||||
|
||||
impl ::std::ops::Deref for $t {
|
||||
type Target = ::sqlez::thread_safe_connection::ThreadSafeConnection<$d>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref $id: $t = $t(::db::open_file_db());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,22 +1,9 @@
|
|||
use anyhow::Result;
|
||||
use indoc::indoc;
|
||||
|
||||
use sqlez::{
|
||||
connection::Connection, domain::Domain, migrations::Migration,
|
||||
thread_safe_connection::ThreadSafeConnection,
|
||||
};
|
||||
use sqlez::{domain::Domain, thread_safe_connection::ThreadSafeConnection};
|
||||
use std::ops::Deref;
|
||||
|
||||
pub(crate) const KVP_MIGRATION: Migration = Migration::new(
|
||||
"kvp",
|
||||
&[indoc! {"
|
||||
CREATE TABLE kv_store(
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL
|
||||
) STRICT;
|
||||
"}],
|
||||
);
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
pub static ref KEY_VALUE_STORE: KeyValueStore =
|
||||
KeyValueStore(crate::open_file_db());
|
||||
|
@ -26,8 +13,17 @@ lazy_static::lazy_static! {
|
|||
pub struct KeyValueStore(ThreadSafeConnection<KeyValueStore>);
|
||||
|
||||
impl Domain for KeyValueStore {
|
||||
fn migrate(conn: &Connection) -> anyhow::Result<()> {
|
||||
KVP_MIGRATION.run(conn)
|
||||
fn name() -> &'static str {
|
||||
"kvp"
|
||||
}
|
||||
|
||||
fn migrations() -> &'static [&'static str] {
|
||||
&[indoc! {"
|
||||
CREATE TABLE kv_store(
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL
|
||||
) STRICT;
|
||||
"}]
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue