mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-04 01:24:20 +03:00
Get rusqlite more shippable
This commit is contained in:
parent
6019e4c37b
commit
b06366ebb7
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -1603,6 +1603,7 @@ dependencies = [
|
|||||||
"collections",
|
"collections",
|
||||||
"gpui",
|
"gpui",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
|
"log",
|
||||||
"parking_lot 0.11.2",
|
"parking_lot 0.11.2",
|
||||||
"rusqlite",
|
"rusqlite",
|
||||||
"rusqlite_migration",
|
"rusqlite_migration",
|
||||||
|
@ -283,9 +283,9 @@ impl AutoUpdater {
|
|||||||
let db = self.db.clone();
|
let db = self.db.clone();
|
||||||
cx.background().spawn(async move {
|
cx.background().spawn(async move {
|
||||||
if should_show {
|
if should_show {
|
||||||
db.write([(SHOULD_SHOW_UPDATE_NOTIFICATION_KEY, "")])?;
|
db.write_kvp(SHOULD_SHOW_UPDATE_NOTIFICATION_KEY, "")?;
|
||||||
} else {
|
} else {
|
||||||
db.delete([(SHOULD_SHOW_UPDATE_NOTIFICATION_KEY)])?;
|
db.delete_kvp(SHOULD_SHOW_UPDATE_NOTIFICATION_KEY)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
@ -293,8 +293,7 @@ impl AutoUpdater {
|
|||||||
|
|
||||||
fn should_show_update_notification(&self, cx: &AppContext) -> Task<Result<bool>> {
|
fn should_show_update_notification(&self, cx: &AppContext) -> Task<Result<bool>> {
|
||||||
let db = self.db.clone();
|
let db = self.db.clone();
|
||||||
cx.background().spawn(async move {
|
cx.background()
|
||||||
Ok(db.read([(SHOULD_SHOW_UPDATE_NOTIFICATION_KEY)])?[0].is_some())
|
.spawn(async move { Ok(db.read_kvp(SHOULD_SHOW_UPDATE_NOTIFICATION_KEY)?.is_some()) })
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,10 +14,11 @@ test-support = []
|
|||||||
collections = { path = "../collections" }
|
collections = { path = "../collections" }
|
||||||
anyhow = "1.0.57"
|
anyhow = "1.0.57"
|
||||||
async-trait = "0.1"
|
async-trait = "0.1"
|
||||||
|
lazy_static = "1.4.0"
|
||||||
|
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
|
||||||
parking_lot = "0.11.1"
|
parking_lot = "0.11.1"
|
||||||
rusqlite = { version = "0.28.0", features = ["bundled", "serde_json"] }
|
rusqlite = { version = "0.28.0", features = ["bundled", "serde_json"] }
|
||||||
rusqlite_migration = "1.0.0"
|
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"] }
|
||||||
|
@ -1,19 +1,22 @@
|
|||||||
mod items;
|
|
||||||
mod kvp;
|
mod kvp;
|
||||||
mod migrations;
|
mod migrations;
|
||||||
|
|
||||||
use anyhow::Result;
|
|
||||||
use migrations::MIGRATIONS;
|
|
||||||
use parking_lot::Mutex;
|
|
||||||
use rusqlite::Connection;
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub use kvp::*;
|
use anyhow::Result;
|
||||||
|
use log::error;
|
||||||
|
use parking_lot::Mutex;
|
||||||
|
use rusqlite::Connection;
|
||||||
|
|
||||||
pub struct Db {
|
use migrations::MIGRATIONS;
|
||||||
|
|
||||||
|
pub enum Db {
|
||||||
|
Real {
|
||||||
connection: Mutex<Connection>,
|
connection: Mutex<Connection>,
|
||||||
in_memory: bool,
|
in_memory: bool,
|
||||||
|
},
|
||||||
|
Null,
|
||||||
}
|
}
|
||||||
|
|
||||||
// To make a migration:
|
// To make a migration:
|
||||||
@ -23,25 +26,43 @@ 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 fn open(path: &Path) -> Result<Arc<Self>> {
|
pub fn open(path: &Path) -> Arc<Self> {
|
||||||
let conn = Connection::open(path)?;
|
Connection::open(path)
|
||||||
|
.map_err(Into::into)
|
||||||
Self::initialize(conn, false).or_else(|_| Self::open_in_memory())
|
.and_then(|connection| Self::initialize(connection, false))
|
||||||
|
.unwrap_or_else(|e| {
|
||||||
|
error!(
|
||||||
|
"Connecting to db failed. Falling back to in memory db. {}",
|
||||||
|
e
|
||||||
|
);
|
||||||
|
Self::open_in_memory()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Open a in memory database for testing and as a fallback.
|
/// Open a in memory database for testing and as a fallback.
|
||||||
pub fn open_in_memory() -> Result<Arc<Self>> {
|
pub fn open_in_memory() -> Arc<Self> {
|
||||||
let conn = Connection::open_in_memory()?;
|
Connection::open_in_memory()
|
||||||
|
.map_err(Into::into)
|
||||||
Self::initialize(conn, true)
|
.and_then(|connection| Self::initialize(connection, true))
|
||||||
|
.unwrap_or_else(|e| {
|
||||||
|
error!("Connecting to in memory db failed. Reverting to null db. {}");
|
||||||
|
Arc::new(Self::Null)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn initialize(mut conn: Connection, in_memory: bool) -> Result<Arc<Self>> {
|
fn initialize(mut conn: Connection, in_memory: bool) -> Result<Arc<Self>> {
|
||||||
MIGRATIONS.to_latest(&mut conn)?;
|
MIGRATIONS.to_latest(&mut conn)?;
|
||||||
|
|
||||||
Ok(Arc::new(Self {
|
Ok(Arc::new(Self::Real {
|
||||||
connection: Mutex::new(conn),
|
connection: Mutex::new(conn),
|
||||||
in_memory,
|
in_memory,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn persisting(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
Db::Real { in_memory, .. } => *in_memory,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@ fn main() {
|
|||||||
let db = app.background().spawn(async move {
|
let db = app.background().spawn(async move {
|
||||||
project::Db::open(&*zed::paths::DB)
|
project::Db::open(&*zed::paths::DB)
|
||||||
.log_err()
|
.log_err()
|
||||||
.unwrap_or_else(project::Db::null)
|
.unwrap_or_else(project::Db::open_in_memory())
|
||||||
});
|
});
|
||||||
|
|
||||||
load_embedded_fonts(&app);
|
load_embedded_fonts(&app);
|
||||||
|
@ -6,7 +6,7 @@ lazy_static::lazy_static! {
|
|||||||
pub static ref LOGS_DIR: PathBuf = HOME.join("Library/Logs/Zed");
|
pub static ref LOGS_DIR: PathBuf = HOME.join("Library/Logs/Zed");
|
||||||
pub static ref LANGUAGES_DIR: PathBuf = HOME.join("Library/Application Support/Zed/languages");
|
pub static ref LANGUAGES_DIR: PathBuf = HOME.join("Library/Application Support/Zed/languages");
|
||||||
pub static ref DB_DIR: PathBuf = HOME.join("Library/Application Support/Zed/db");
|
pub static ref DB_DIR: PathBuf = HOME.join("Library/Application Support/Zed/db");
|
||||||
pub static ref DB: PathBuf = DB_DIR.join("zed.db");
|
pub static ref DB: PathBuf = DB_DIR.join("zed.sqlite");
|
||||||
pub static ref SETTINGS: PathBuf = CONFIG_DIR.join("settings.json");
|
pub static ref SETTINGS: PathBuf = CONFIG_DIR.join("settings.json");
|
||||||
pub static ref KEYMAP: PathBuf = CONFIG_DIR.join("keymap.json");
|
pub static ref KEYMAP: PathBuf = CONFIG_DIR.join("keymap.json");
|
||||||
pub static ref LAST_USERNAME: PathBuf = CONFIG_DIR.join("last-username.txt");
|
pub static ref LAST_USERNAME: PathBuf = CONFIG_DIR.join("last-username.txt");
|
||||||
|
Loading…
Reference in New Issue
Block a user