2022-10-27 20:58:54 +00:00
|
|
|
pub mod kvp;
|
2022-12-02 02:31:05 +00:00
|
|
|
pub mod query;
|
2022-10-13 23:31:26 +00:00
|
|
|
|
2022-11-21 06:41:10 +00:00
|
|
|
// Re-export
|
|
|
|
pub use anyhow;
|
2022-12-01 22:16:38 +00:00
|
|
|
use anyhow::Context;
|
2023-03-01 06:11:58 +00:00
|
|
|
use gpui::MutableAppContext;
|
2022-11-19 23:14:13 +00:00
|
|
|
pub use indoc::indoc;
|
|
|
|
pub use lazy_static;
|
2022-12-02 02:31:05 +00:00
|
|
|
use parking_lot::{Mutex, RwLock};
|
2022-11-30 18:54:01 +00:00
|
|
|
pub use smol;
|
2022-11-19 23:14:13 +00:00
|
|
|
pub use sqlez;
|
2022-11-29 01:42:18 +00:00
|
|
|
pub use sqlez_macros;
|
2022-12-02 02:31:05 +00:00
|
|
|
pub use util::channel::{RELEASE_CHANNEL, RELEASE_CHANNEL_NAME};
|
|
|
|
pub use util::paths::DB_DIR;
|
2022-10-18 18:43:18 +00:00
|
|
|
|
2022-11-21 06:41:10 +00:00
|
|
|
use sqlez::domain::Migrator;
|
2022-11-01 20:15:58 +00:00
|
|
|
use sqlez::thread_safe_connection::ThreadSafeConnection;
|
2022-11-30 18:54:01 +00:00
|
|
|
use sqlez_macros::sql;
|
2022-12-04 00:26:37 +00:00
|
|
|
use std::fs::create_dir_all;
|
2023-03-01 06:11:58 +00:00
|
|
|
use std::future::Future;
|
2022-12-02 02:31:05 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2022-11-21 21:42:26 +00:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2022-12-01 22:16:38 +00:00
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
2022-12-02 02:31:05 +00:00
|
|
|
use util::channel::ReleaseChannel;
|
2022-12-19 16:11:10 +00:00
|
|
|
use util::{async_iife, ResultExt};
|
2022-12-01 22:16:38 +00:00
|
|
|
|
2022-11-30 18:54:01 +00:00
|
|
|
const CONNECTION_INITIALIZE_QUERY: &'static str = sql!(
|
2022-11-10 23:29:29 +00:00
|
|
|
PRAGMA foreign_keys=TRUE;
|
2022-11-30 18:54:01 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const DB_INITIALIZE_QUERY: &'static str = sql!(
|
|
|
|
PRAGMA journal_mode=WAL;
|
2022-12-04 00:46:35 +00:00
|
|
|
PRAGMA busy_timeout=1;
|
|
|
|
PRAGMA case_sensitive_like=TRUE;
|
|
|
|
PRAGMA synchronous=NORMAL;
|
2022-11-30 18:54:01 +00:00
|
|
|
);
|
2022-11-10 23:29:29 +00:00
|
|
|
|
2022-12-01 22:16:38 +00:00
|
|
|
const FALLBACK_DB_NAME: &'static str = "FALLBACK_MEMORY_DB";
|
|
|
|
|
2022-12-02 22:30:26 +00:00
|
|
|
const DB_FILE_NAME: &'static str = "db.sqlite";
|
|
|
|
|
2022-11-21 21:42:26 +00:00
|
|
|
lazy_static::lazy_static! {
|
2023-03-01 03:20:21 +00:00
|
|
|
// !!!!!!! CHANGE BACK TO DEFAULT FALSE BEFORE SHIPPING
|
2023-03-09 03:03:44 +00:00
|
|
|
static ref ZED_STATELESS: bool = std::env::var("ZED_STATELESS").map_or(false, |v| !v.is_empty());
|
2022-12-02 02:31:05 +00:00
|
|
|
static ref DB_FILE_OPERATIONS: Mutex<()> = Mutex::new(());
|
|
|
|
pub static ref BACKUP_DB_PATH: RwLock<Option<PathBuf>> = RwLock::new(None);
|
2022-12-19 16:11:10 +00:00
|
|
|
pub static ref ALL_FILE_DB_FAILED: AtomicBool = AtomicBool::new(false);
|
2022-11-21 21:42:26 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 21:18:44 +00:00
|
|
|
/// Open or create a database at the given directory path.
|
2022-12-02 02:31:05 +00:00
|
|
|
/// This will retry a couple times if there are failures. If opening fails once, the db directory
|
|
|
|
/// is moved to a backup folder and a new one is created. If that fails, a shared in memory db is created.
|
|
|
|
/// In either case, static variables are set so that the user can be notified.
|
2022-12-19 16:11:10 +00:00
|
|
|
pub async fn open_db<M: Migrator + 'static>(
|
|
|
|
db_dir: &Path,
|
|
|
|
release_channel: &ReleaseChannel,
|
|
|
|
) -> ThreadSafeConnection<M> {
|
2022-12-17 20:03:51 +00:00
|
|
|
if *ZED_STATELESS {
|
|
|
|
return open_fallback_db().await;
|
|
|
|
}
|
|
|
|
|
2022-12-02 22:30:26 +00:00
|
|
|
let release_channel_name = release_channel.dev_name();
|
|
|
|
let main_db_dir = db_dir.join(Path::new(&format!("0-{}", release_channel_name)));
|
2022-11-17 00:35:56 +00:00
|
|
|
|
2022-12-01 22:16:38 +00:00
|
|
|
let connection = async_iife!({
|
2022-12-02 02:31:05 +00:00
|
|
|
// Note: This still has a race condition where 1 set of migrations succeeds
|
|
|
|
// (e.g. (Workspace, Editor)) and another fails (e.g. (Workspace, Terminal))
|
2023-03-09 03:03:44 +00:00
|
|
|
// This will cause the first connection to have the database taken out
|
2022-12-02 02:31:05 +00:00
|
|
|
// from under it. This *should* be fine though. The second dabatase failure will
|
|
|
|
// cause errors in the log and so should be observed by developers while writing
|
|
|
|
// soon-to-be good migrations. If user databases are corrupted, we toss them out
|
2023-03-09 03:03:44 +00:00
|
|
|
// and try again from a blank. As long as running all migrations from start to end
|
2022-12-02 20:43:02 +00:00
|
|
|
// on a blank database is ok, this race condition will never be triggered.
|
2022-12-02 02:31:05 +00:00
|
|
|
//
|
|
|
|
// Basically: Don't ever push invalid migrations to stable or everyone will have
|
|
|
|
// a bad time.
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-01 22:16:38 +00:00
|
|
|
// If no db folder, create one at 0-{channel}
|
2022-12-02 02:31:05 +00:00
|
|
|
create_dir_all(&main_db_dir).context("Could not create db directory")?;
|
2022-12-02 22:30:26 +00:00
|
|
|
let db_path = main_db_dir.join(Path::new(DB_FILE_NAME));
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-02 02:31:05 +00:00
|
|
|
// Optimistically open databases in parallel
|
|
|
|
if !DB_FILE_OPERATIONS.is_locked() {
|
|
|
|
// Try building a connection
|
|
|
|
if let Some(connection) = open_main_db(&db_path).await {
|
|
|
|
return Ok(connection)
|
|
|
|
};
|
2022-12-01 22:16:38 +00:00
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2023-03-09 03:03:44 +00:00
|
|
|
// Take a lock in the failure case so that we move the db once per process instead
|
2022-12-02 02:31:05 +00:00
|
|
|
// of potentially multiple times from different threads. This shouldn't happen in the
|
|
|
|
// normal path
|
|
|
|
let _lock = DB_FILE_OPERATIONS.lock();
|
|
|
|
if let Some(connection) = open_main_db(&db_path).await {
|
|
|
|
return Ok(connection)
|
|
|
|
};
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-01 22:16:38 +00:00
|
|
|
let backup_timestamp = SystemTime::now()
|
|
|
|
.duration_since(UNIX_EPOCH)
|
2022-12-02 02:31:05 +00:00
|
|
|
.expect("System clock is set before the unix timestamp, Zed does not support this region of spacetime")
|
2022-12-01 22:16:38 +00:00
|
|
|
.as_millis();
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-01 22:16:38 +00:00
|
|
|
// If failed, move 0-{channel} to {current unix timestamp}-{channel}
|
2022-12-02 02:31:05 +00:00
|
|
|
let backup_db_dir = db_dir.join(Path::new(&format!(
|
|
|
|
"{}-{}",
|
2022-12-01 22:16:38 +00:00
|
|
|
backup_timestamp,
|
2022-12-02 22:30:26 +00:00
|
|
|
release_channel_name,
|
2022-12-01 22:16:38 +00:00
|
|
|
)));
|
|
|
|
|
2022-12-02 02:31:05 +00:00
|
|
|
std::fs::rename(&main_db_dir, &backup_db_dir)
|
2022-12-01 22:16:38 +00:00
|
|
|
.context("Failed clean up corrupted database, panicking.")?;
|
|
|
|
|
2022-12-02 02:31:05 +00:00
|
|
|
// Set a static ref with the failed timestamp and error so we can notify the user
|
|
|
|
{
|
|
|
|
let mut guard = BACKUP_DB_PATH.write();
|
|
|
|
*guard = Some(backup_db_dir);
|
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-01 22:16:38 +00:00
|
|
|
// Create a new 0-{channel}
|
2022-12-02 02:31:05 +00:00
|
|
|
create_dir_all(&main_db_dir).context("Should be able to create the database directory")?;
|
2022-12-02 22:30:26 +00:00
|
|
|
let db_path = main_db_dir.join(Path::new(DB_FILE_NAME));
|
2022-12-01 22:16:38 +00:00
|
|
|
|
|
|
|
// Try again
|
2022-12-02 02:31:05 +00:00
|
|
|
open_main_db(&db_path).await.context("Could not newly created db")
|
2022-12-01 22:16:38 +00:00
|
|
|
}).await.log_err();
|
|
|
|
|
2022-12-02 02:31:05 +00:00
|
|
|
if let Some(connection) = connection {
|
2022-12-01 22:16:38 +00:00
|
|
|
return connection;
|
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-02 02:31:05 +00:00
|
|
|
// Set another static ref so that we can escalate the notification
|
|
|
|
ALL_FILE_DB_FAILED.store(true, Ordering::Release);
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-01 22:16:38 +00:00
|
|
|
// If still failed, create an in memory db with a known name
|
|
|
|
open_fallback_db().await
|
|
|
|
}
|
2022-11-14 21:18:44 +00:00
|
|
|
|
2022-12-02 02:31:05 +00:00
|
|
|
async fn open_main_db<M: Migrator>(db_path: &PathBuf) -> Option<ThreadSafeConnection<M>> {
|
2022-12-02 20:43:02 +00:00
|
|
|
log::info!("Opening main db");
|
2022-12-02 02:31:05 +00:00
|
|
|
ThreadSafeConnection::<M>::builder(db_path.to_string_lossy().as_ref(), true)
|
|
|
|
.with_db_initialization_query(DB_INITIALIZE_QUERY)
|
|
|
|
.with_connection_initialize_query(CONNECTION_INITIALIZE_QUERY)
|
|
|
|
.build()
|
|
|
|
.await
|
|
|
|
.log_err()
|
|
|
|
}
|
|
|
|
|
2022-12-01 22:16:38 +00:00
|
|
|
async fn open_fallback_db<M: Migrator>() -> ThreadSafeConnection<M> {
|
2022-12-02 20:43:02 +00:00
|
|
|
log::info!("Opening fallback db");
|
2022-12-01 22:16:38 +00:00
|
|
|
ThreadSafeConnection::<M>::builder(FALLBACK_DB_NAME, false)
|
2022-11-30 18:54:01 +00:00
|
|
|
.with_db_initialization_query(DB_INITIALIZE_QUERY)
|
|
|
|
.with_connection_initialize_query(CONNECTION_INITIALIZE_QUERY)
|
|
|
|
.build()
|
|
|
|
.await
|
2022-12-01 22:16:38 +00:00
|
|
|
.expect(
|
|
|
|
"Fallback in memory database failed. Likely initialization queries or migrations have fundamental errors",
|
|
|
|
)
|
2022-10-18 22:58:05 +00:00
|
|
|
}
|
2022-10-13 22:43:42 +00:00
|
|
|
|
2022-12-01 00:19:46 +00:00
|
|
|
#[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;
|
|
|
|
|
2022-11-30 18:54:01 +00:00
|
|
|
ThreadSafeConnection::<M>::builder(db_name, false)
|
|
|
|
.with_db_initialization_query(DB_INITIALIZE_QUERY)
|
|
|
|
.with_connection_initialize_query(CONNECTION_INITIALIZE_QUERY)
|
2022-11-30 20:34:42 +00:00
|
|
|
// Serialize queued writes via a mutex and run them synchronously
|
2022-12-01 00:19:46 +00:00
|
|
|
.with_write_queue_constructor(locking_queue())
|
2022-11-30 18:54:01 +00:00
|
|
|
.build()
|
|
|
|
.await
|
2022-12-01 22:16:38 +00:00
|
|
|
.unwrap()
|
2022-11-14 21:18:44 +00:00
|
|
|
}
|
2022-11-04 20:22:35 +00:00
|
|
|
|
2022-11-17 00:35:56 +00:00
|
|
|
/// Implements a basic DB wrapper for a given domain
|
|
|
|
#[macro_export]
|
2022-12-02 02:31:05 +00:00
|
|
|
macro_rules! define_connection {
|
|
|
|
(pub static ref $id:ident: $t:ident<()> = $migrations:expr;) => {
|
|
|
|
pub struct $t($crate::sqlez::thread_safe_connection::ThreadSafeConnection<$t>);
|
2022-11-17 00:35:56 +00:00
|
|
|
|
|
|
|
impl ::std::ops::Deref for $t {
|
2022-12-02 02:31:05 +00:00
|
|
|
type Target = $crate::sqlez::thread_safe_connection::ThreadSafeConnection<$t>;
|
2022-11-17 00:35:56 +00:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-02 02:31:05 +00:00
|
|
|
impl $crate::sqlez::domain::Domain for $t {
|
|
|
|
fn name() -> &'static str {
|
|
|
|
stringify!($t)
|
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-02 02:31:05 +00:00
|
|
|
fn migrations() -> &'static [&'static str] {
|
|
|
|
$migrations
|
2022-12-19 16:11:10 +00:00
|
|
|
}
|
2022-12-02 02:31:05 +00:00
|
|
|
}
|
2022-11-17 00:35:56 +00:00
|
|
|
|
2022-12-01 00:19:46 +00:00
|
|
|
#[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! {
|
2022-12-04 00:26:37 +00:00
|
|
|
pub static ref $id: $t = $t($crate::smol::block_on($crate::open_db(&$crate::DB_DIR, &$crate::RELEASE_CHANNEL)));
|
2022-11-21 18:38:16 +00:00
|
|
|
}
|
2022-11-21 06:41:10 +00:00
|
|
|
};
|
2022-12-02 02:31:05 +00:00
|
|
|
(pub static ref $id:ident: $t:ident<$($d:ty),+> = $migrations:expr;) => {
|
|
|
|
pub struct $t($crate::sqlez::thread_safe_connection::ThreadSafeConnection<( $($d),+, $t )>);
|
2022-11-29 01:42:18 +00:00
|
|
|
|
2022-12-02 02:31:05 +00:00
|
|
|
impl ::std::ops::Deref for $t {
|
|
|
|
type Target = $crate::sqlez::thread_safe_connection::ThreadSafeConnection<($($d),+, $t)>;
|
2022-11-29 01:42:18 +00:00
|
|
|
|
2022-12-02 02:31:05 +00:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
2022-11-23 09:53:58 +00:00
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-02 02:31:05 +00:00
|
|
|
impl $crate::sqlez::domain::Domain for $t {
|
|
|
|
fn name() -> &'static str {
|
|
|
|
stringify!($t)
|
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-02 02:31:05 +00:00
|
|
|
fn migrations() -> &'static [&'static str] {
|
|
|
|
$migrations
|
2022-12-19 16:11:10 +00:00
|
|
|
}
|
2022-11-23 09:53:58 +00:00
|
|
|
}
|
2022-11-24 08:02:07 +00:00
|
|
|
|
2022-12-02 02:31:05 +00:00
|
|
|
#[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))));
|
2022-11-24 08:02:07 +00:00
|
|
|
}
|
2022-11-29 01:42:18 +00:00
|
|
|
|
2022-12-02 02:31:05 +00:00
|
|
|
#[cfg(not(any(test, feature = "test-support")))]
|
|
|
|
$crate::lazy_static::lazy_static! {
|
2022-12-04 00:26:37 +00:00
|
|
|
pub static ref $id: $t = $t($crate::smol::block_on($crate::open_db(&$crate::DB_DIR, &$crate::RELEASE_CHANNEL)));
|
2022-11-23 09:53:58 +00:00
|
|
|
}
|
|
|
|
};
|
2022-12-02 02:31:05 +00:00
|
|
|
}
|
2022-11-21 18:52:19 +00:00
|
|
|
|
2023-03-01 06:11:58 +00:00
|
|
|
pub fn write_and_log<F>(cx: &mut MutableAppContext, db_write: impl FnOnce() -> F + Send + 'static)
|
|
|
|
where
|
|
|
|
F: Future<Output = anyhow::Result<()>> + Send,
|
|
|
|
{
|
|
|
|
cx.background()
|
|
|
|
.spawn(async move { db_write().await.log_err() })
|
|
|
|
.detach()
|
|
|
|
}
|
|
|
|
|
2022-12-02 02:31:05 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-12-04 00:26:37 +00:00
|
|
|
use std::{fs, thread};
|
2022-11-29 01:42:18 +00:00
|
|
|
|
2022-12-19 16:11:10 +00:00
|
|
|
use sqlez::{connection::Connection, domain::Domain};
|
2022-12-02 02:31:05 +00:00
|
|
|
use sqlez_macros::sql;
|
|
|
|
use tempdir::TempDir;
|
2022-11-23 09:53:58 +00:00
|
|
|
|
2022-12-02 22:30:26 +00:00
|
|
|
use crate::{open_db, DB_FILE_NAME};
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-02 02:31:05 +00:00
|
|
|
// Test bad migration panics
|
2022-12-02 22:30:26 +00:00
|
|
|
#[gpui::test]
|
|
|
|
#[should_panic]
|
|
|
|
async fn test_bad_migration_panics() {
|
|
|
|
enum BadDB {}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-02 22:30:26 +00:00
|
|
|
impl Domain for BadDB {
|
|
|
|
fn name() -> &'static str {
|
|
|
|
"db_tests"
|
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-02 22:30:26 +00:00
|
|
|
fn migrations() -> &'static [&'static str] {
|
2022-12-19 16:11:10 +00:00
|
|
|
&[
|
|
|
|
sql!(CREATE TABLE test(value);),
|
2022-12-02 22:30:26 +00:00
|
|
|
// failure because test already exists
|
2022-12-19 16:11:10 +00:00
|
|
|
sql!(CREATE TABLE test(value);),
|
|
|
|
]
|
2022-12-02 22:30:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-02 22:30:26 +00:00
|
|
|
let tempdir = TempDir::new("DbTests").unwrap();
|
2022-12-04 00:26:37 +00:00
|
|
|
let _bad_db = open_db::<BadDB>(tempdir.path(), &util::channel::ReleaseChannel::Dev).await;
|
2022-12-02 02:31:05 +00:00
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-02 02:31:05 +00:00
|
|
|
/// Test that DB exists but corrupted (causing recreate)
|
2022-12-02 22:30:26 +00:00
|
|
|
#[gpui::test]
|
|
|
|
async fn test_db_corruption() {
|
|
|
|
enum CorruptedDB {}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-02 22:30:26 +00:00
|
|
|
impl Domain for CorruptedDB {
|
|
|
|
fn name() -> &'static str {
|
|
|
|
"db_tests"
|
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-02 22:30:26 +00:00
|
|
|
fn migrations() -> &'static [&'static str] {
|
|
|
|
&[sql!(CREATE TABLE test(value);)]
|
|
|
|
}
|
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-02 22:30:26 +00:00
|
|
|
enum GoodDB {}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-02 22:30:26 +00:00
|
|
|
impl Domain for GoodDB {
|
|
|
|
fn name() -> &'static str {
|
|
|
|
"db_tests" //Notice same name
|
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-02 22:30:26 +00:00
|
|
|
fn migrations() -> &'static [&'static str] {
|
|
|
|
&[sql!(CREATE TABLE test2(value);)] //But different migration
|
|
|
|
}
|
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-02 22:30:26 +00:00
|
|
|
let tempdir = TempDir::new("DbTests").unwrap();
|
|
|
|
{
|
2022-12-19 16:11:10 +00:00
|
|
|
let corrupt_db =
|
|
|
|
open_db::<CorruptedDB>(tempdir.path(), &util::channel::ReleaseChannel::Dev).await;
|
2022-12-02 22:30:26 +00:00
|
|
|
assert!(corrupt_db.persistent());
|
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-04 00:26:37 +00:00
|
|
|
let good_db = open_db::<GoodDB>(tempdir.path(), &util::channel::ReleaseChannel::Dev).await;
|
2022-12-19 16:11:10 +00:00
|
|
|
assert!(
|
|
|
|
good_db.select_row::<usize>("SELECT * FROM test2").unwrap()()
|
|
|
|
.unwrap()
|
|
|
|
.is_none()
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut corrupted_backup_dir = fs::read_dir(tempdir.path())
|
|
|
|
.unwrap()
|
|
|
|
.find(|entry| {
|
|
|
|
!entry
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.file_name()
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.starts_with("0")
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
.unwrap()
|
|
|
|
.path();
|
2022-12-02 22:30:26 +00:00
|
|
|
corrupted_backup_dir.push(DB_FILE_NAME);
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-02 22:30:26 +00:00
|
|
|
let backup = Connection::open_file(&corrupted_backup_dir.to_string_lossy());
|
2022-12-19 16:11:10 +00:00
|
|
|
assert!(backup.select_row::<usize>("SELECT * FROM test").unwrap()()
|
|
|
|
.unwrap()
|
|
|
|
.is_none());
|
2022-12-02 02:31:05 +00:00
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-04 00:26:37 +00:00
|
|
|
/// Test that DB exists but corrupted (causing recreate)
|
|
|
|
#[gpui::test]
|
|
|
|
async fn test_simultaneous_db_corruption() {
|
|
|
|
enum CorruptedDB {}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-04 00:26:37 +00:00
|
|
|
impl Domain for CorruptedDB {
|
|
|
|
fn name() -> &'static str {
|
|
|
|
"db_tests"
|
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-04 00:26:37 +00:00
|
|
|
fn migrations() -> &'static [&'static str] {
|
|
|
|
&[sql!(CREATE TABLE test(value);)]
|
|
|
|
}
|
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-04 00:26:37 +00:00
|
|
|
enum GoodDB {}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-04 00:26:37 +00:00
|
|
|
impl Domain for GoodDB {
|
|
|
|
fn name() -> &'static str {
|
|
|
|
"db_tests" //Notice same name
|
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-04 00:26:37 +00:00
|
|
|
fn migrations() -> &'static [&'static str] {
|
|
|
|
&[sql!(CREATE TABLE test2(value);)] //But different migration
|
|
|
|
}
|
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-04 00:26:37 +00:00
|
|
|
let tempdir = TempDir::new("DbTests").unwrap();
|
|
|
|
{
|
2022-12-04 00:46:35 +00:00
|
|
|
// Setup the bad database
|
2022-12-19 16:11:10 +00:00
|
|
|
let corrupt_db =
|
|
|
|
open_db::<CorruptedDB>(tempdir.path(), &util::channel::ReleaseChannel::Dev).await;
|
2022-12-04 00:26:37 +00:00
|
|
|
assert!(corrupt_db.persistent());
|
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-04 00:46:35 +00:00
|
|
|
// Try to connect to it a bunch of times at once
|
2022-12-04 00:26:37 +00:00
|
|
|
let mut guards = vec![];
|
|
|
|
for _ in 0..10 {
|
|
|
|
let tmp_path = tempdir.path().to_path_buf();
|
|
|
|
let guard = thread::spawn(move || {
|
2022-12-19 16:11:10 +00:00
|
|
|
let good_db = smol::block_on(open_db::<GoodDB>(
|
|
|
|
tmp_path.as_path(),
|
|
|
|
&util::channel::ReleaseChannel::Dev,
|
|
|
|
));
|
|
|
|
assert!(
|
|
|
|
good_db.select_row::<usize>("SELECT * FROM test2").unwrap()()
|
|
|
|
.unwrap()
|
|
|
|
.is_none()
|
|
|
|
);
|
2022-12-04 00:26:37 +00:00
|
|
|
});
|
2022-12-19 16:11:10 +00:00
|
|
|
|
2022-12-04 00:26:37 +00:00
|
|
|
guards.push(guard);
|
|
|
|
}
|
2022-12-19 16:11:10 +00:00
|
|
|
|
|
|
|
for guard in guards.into_iter() {
|
|
|
|
assert!(guard.join().is_ok());
|
|
|
|
}
|
2022-12-04 00:26:37 +00:00
|
|
|
}
|
2022-11-21 06:41:10 +00:00
|
|
|
}
|