Format problematic DB macros
This commit is contained in:
parent
de9c58d216
commit
c49573dc11
2 changed files with 133 additions and 105 deletions
|
@ -20,8 +20,8 @@ use std::fs::create_dir_all;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
use util::{async_iife, ResultExt};
|
|
||||||
use util::channel::ReleaseChannel;
|
use util::channel::ReleaseChannel;
|
||||||
|
use util::{async_iife, ResultExt};
|
||||||
|
|
||||||
const CONNECTION_INITIALIZE_QUERY: &'static str = sql!(
|
const CONNECTION_INITIALIZE_QUERY: &'static str = sql!(
|
||||||
PRAGMA foreign_keys=TRUE;
|
PRAGMA foreign_keys=TRUE;
|
||||||
|
@ -49,7 +49,10 @@ lazy_static::lazy_static! {
|
||||||
/// This will retry a couple times if there are failures. If opening fails once, the db directory
|
/// 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.
|
/// 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.
|
/// In either case, static variables are set so that the user can be notified.
|
||||||
pub async fn open_db<M: Migrator + 'static>(db_dir: &Path, release_channel: &ReleaseChannel) -> ThreadSafeConnection<M> {
|
pub async fn open_db<M: Migrator + 'static>(
|
||||||
|
db_dir: &Path,
|
||||||
|
release_channel: &ReleaseChannel,
|
||||||
|
) -> ThreadSafeConnection<M> {
|
||||||
if *ZED_STATELESS {
|
if *ZED_STATELESS {
|
||||||
return open_fallback_db().await;
|
return open_fallback_db().await;
|
||||||
}
|
}
|
||||||
|
@ -237,7 +240,7 @@ macro_rules! define_connection {
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::{fs, thread};
|
use std::{fs, thread};
|
||||||
|
|
||||||
use sqlez::{domain::Domain, connection::Connection};
|
use sqlez::{connection::Connection, domain::Domain};
|
||||||
use sqlez_macros::sql;
|
use sqlez_macros::sql;
|
||||||
use tempdir::TempDir;
|
use tempdir::TempDir;
|
||||||
|
|
||||||
|
@ -255,9 +258,11 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn migrations() -> &'static [&'static str] {
|
fn migrations() -> &'static [&'static str] {
|
||||||
&[sql!(CREATE TABLE test(value);),
|
&[
|
||||||
|
sql!(CREATE TABLE test(value);),
|
||||||
// failure because test already exists
|
// failure because test already exists
|
||||||
sql!(CREATE TABLE test(value);)]
|
sql!(CREATE TABLE test(value);),
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -294,26 +299,40 @@ mod tests {
|
||||||
|
|
||||||
let tempdir = TempDir::new("DbTests").unwrap();
|
let tempdir = TempDir::new("DbTests").unwrap();
|
||||||
{
|
{
|
||||||
let corrupt_db = open_db::<CorruptedDB>(tempdir.path(), &util::channel::ReleaseChannel::Dev).await;
|
let corrupt_db =
|
||||||
|
open_db::<CorruptedDB>(tempdir.path(), &util::channel::ReleaseChannel::Dev).await;
|
||||||
assert!(corrupt_db.persistent());
|
assert!(corrupt_db.persistent());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
let good_db = open_db::<GoodDB>(tempdir.path(), &util::channel::ReleaseChannel::Dev).await;
|
let good_db = open_db::<GoodDB>(tempdir.path(), &util::channel::ReleaseChannel::Dev).await;
|
||||||
assert!(good_db.select_row::<usize>("SELECT * FROM test2").unwrap()().unwrap().is_none());
|
assert!(
|
||||||
|
good_db.select_row::<usize>("SELECT * FROM test2").unwrap()()
|
||||||
|
.unwrap()
|
||||||
|
.is_none()
|
||||||
|
);
|
||||||
|
|
||||||
let mut corrupted_backup_dir = fs::read_dir(
|
let mut corrupted_backup_dir = fs::read_dir(tempdir.path())
|
||||||
tempdir.path()
|
.unwrap()
|
||||||
).unwrap().find(|entry| {
|
.find(|entry| {
|
||||||
!entry.as_ref().unwrap().file_name().to_str().unwrap().starts_with("0")
|
!entry
|
||||||
}
|
.as_ref()
|
||||||
).unwrap().unwrap().path();
|
.unwrap()
|
||||||
|
.file_name()
|
||||||
|
.to_str()
|
||||||
|
.unwrap()
|
||||||
|
.starts_with("0")
|
||||||
|
})
|
||||||
|
.unwrap()
|
||||||
|
.unwrap()
|
||||||
|
.path();
|
||||||
corrupted_backup_dir.push(DB_FILE_NAME);
|
corrupted_backup_dir.push(DB_FILE_NAME);
|
||||||
|
|
||||||
dbg!(&corrupted_backup_dir);
|
dbg!(&corrupted_backup_dir);
|
||||||
|
|
||||||
let backup = Connection::open_file(&corrupted_backup_dir.to_string_lossy());
|
let backup = Connection::open_file(&corrupted_backup_dir.to_string_lossy());
|
||||||
assert!(backup.select_row::<usize>("SELECT * FROM test").unwrap()().unwrap().is_none());
|
assert!(backup.select_row::<usize>("SELECT * FROM test").unwrap()()
|
||||||
|
.unwrap()
|
||||||
|
.is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Test that DB exists but corrupted (causing recreate)
|
/// Test that DB exists but corrupted (causing recreate)
|
||||||
|
@ -346,7 +365,8 @@ mod tests {
|
||||||
let tempdir = TempDir::new("DbTests").unwrap();
|
let tempdir = TempDir::new("DbTests").unwrap();
|
||||||
{
|
{
|
||||||
// Setup the bad database
|
// Setup the bad database
|
||||||
let corrupt_db = open_db::<CorruptedDB>(tempdir.path(), &util::channel::ReleaseChannel::Dev).await;
|
let corrupt_db =
|
||||||
|
open_db::<CorruptedDB>(tempdir.path(), &util::channel::ReleaseChannel::Dev).await;
|
||||||
assert!(corrupt_db.persistent());
|
assert!(corrupt_db.persistent());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -355,12 +375,18 @@ mod tests {
|
||||||
for _ in 0..10 {
|
for _ in 0..10 {
|
||||||
let tmp_path = tempdir.path().to_path_buf();
|
let tmp_path = tempdir.path().to_path_buf();
|
||||||
let guard = thread::spawn(move || {
|
let guard = thread::spawn(move || {
|
||||||
let good_db = smol::block_on(open_db::<GoodDB>(tmp_path.as_path(), &util::channel::ReleaseChannel::Dev));
|
let good_db = smol::block_on(open_db::<GoodDB>(
|
||||||
assert!(good_db.select_row::<usize>("SELECT * FROM test2").unwrap()().unwrap().is_none());
|
tmp_path.as_path(),
|
||||||
|
&util::channel::ReleaseChannel::Dev,
|
||||||
|
));
|
||||||
|
assert!(
|
||||||
|
good_db.select_row::<usize>("SELECT * FROM test2").unwrap()()
|
||||||
|
.unwrap()
|
||||||
|
.is_none()
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
guards.push(guard);
|
guards.push(guard);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for guard in guards.into_iter() {
|
for guard in guards.into_iter() {
|
||||||
|
|
|
@ -119,7 +119,7 @@ impl WorkspaceDb {
|
||||||
.context("Getting center group")
|
.context("Getting center group")
|
||||||
.log_err()?,
|
.log_err()?,
|
||||||
dock_position,
|
dock_position,
|
||||||
left_sidebar_open
|
left_sidebar_open,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,7 +158,12 @@ impl WorkspaceDb {
|
||||||
dock_visible = ?4,
|
dock_visible = ?4,
|
||||||
dock_anchor = ?5,
|
dock_anchor = ?5,
|
||||||
timestamp = CURRENT_TIMESTAMP
|
timestamp = CURRENT_TIMESTAMP
|
||||||
))?((workspace.id, &workspace.location, workspace.left_sidebar_open, workspace.dock_position))
|
))?((
|
||||||
|
workspace.id,
|
||||||
|
&workspace.location,
|
||||||
|
workspace.left_sidebar_open,
|
||||||
|
workspace.dock_position,
|
||||||
|
))
|
||||||
.context("Updating workspace")?;
|
.context("Updating workspace")?;
|
||||||
|
|
||||||
// Save center pane group and dock pane
|
// Save center pane group and dock pane
|
||||||
|
@ -233,10 +238,16 @@ impl WorkspaceDb {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_center_pane_group(&self, workspace_id: WorkspaceId) -> Result<SerializedPaneGroup> {
|
fn get_center_pane_group(&self, workspace_id: WorkspaceId) -> Result<SerializedPaneGroup> {
|
||||||
Ok(self.get_pane_group(workspace_id, None)?
|
Ok(self
|
||||||
|
.get_pane_group(workspace_id, None)?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.next()
|
.next()
|
||||||
.unwrap_or_else(|| SerializedPaneGroup::Pane(SerializedPane { active: true, children: vec![] })))
|
.unwrap_or_else(|| {
|
||||||
|
SerializedPaneGroup::Pane(SerializedPane {
|
||||||
|
active: true,
|
||||||
|
children: vec![],
|
||||||
|
})
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_pane_group(
|
fn get_pane_group(
|
||||||
|
@ -296,7 +307,6 @@ impl WorkspaceDb {
|
||||||
.collect::<Result<_>>()
|
.collect::<Result<_>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn save_pane_group(
|
fn save_pane_group(
|
||||||
conn: &Connection,
|
conn: &Connection,
|
||||||
workspace_id: WorkspaceId,
|
workspace_id: WorkspaceId,
|
||||||
|
@ -311,12 +321,7 @@ impl WorkspaceDb {
|
||||||
INSERT INTO pane_groups(workspace_id, parent_group_id, position, axis)
|
INSERT INTO pane_groups(workspace_id, parent_group_id, position, axis)
|
||||||
VALUES (?, ?, ?, ?)
|
VALUES (?, ?, ?, ?)
|
||||||
RETURNING group_id
|
RETURNING group_id
|
||||||
))?((
|
))?((workspace_id, parent_id, position, *axis))?
|
||||||
workspace_id,
|
|
||||||
parent_id,
|
|
||||||
position,
|
|
||||||
*axis,
|
|
||||||
))?
|
|
||||||
.ok_or_else(|| anyhow!("Couldn't retrieve group_id from inserted pane_group"))?;
|
.ok_or_else(|| anyhow!("Couldn't retrieve group_id from inserted pane_group"))?;
|
||||||
|
|
||||||
for (position, group) in children.iter().enumerate() {
|
for (position, group) in children.iter().enumerate() {
|
||||||
|
@ -337,9 +342,7 @@ impl WorkspaceDb {
|
||||||
SELECT pane_id, active
|
SELECT pane_id, active
|
||||||
FROM panes
|
FROM panes
|
||||||
WHERE pane_id = (SELECT dock_pane FROM workspaces WHERE workspace_id = ?)
|
WHERE pane_id = (SELECT dock_pane FROM workspaces WHERE workspace_id = ?)
|
||||||
))?(
|
))?(workspace_id)?
|
||||||
workspace_id,
|
|
||||||
)?
|
|
||||||
.context("No dock pane for workspace")?;
|
.context("No dock pane for workspace")?;
|
||||||
|
|
||||||
Ok(SerializedPane::new(
|
Ok(SerializedPane::new(
|
||||||
|
@ -406,7 +409,6 @@ impl WorkspaceDb {
|
||||||
WHERE workspace_id = ?
|
WHERE workspace_id = ?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -495,7 +497,7 @@ mod tests {
|
||||||
dock_position: crate::dock::DockPosition::Shown(DockAnchor::Bottom),
|
dock_position: crate::dock::DockPosition::Shown(DockAnchor::Bottom),
|
||||||
center_group: Default::default(),
|
center_group: Default::default(),
|
||||||
dock_pane: Default::default(),
|
dock_pane: Default::default(),
|
||||||
left_sidebar_open: true
|
left_sidebar_open: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut workspace_2 = SerializedWorkspace {
|
let mut workspace_2 = SerializedWorkspace {
|
||||||
|
@ -504,7 +506,7 @@ mod tests {
|
||||||
dock_position: crate::dock::DockPosition::Hidden(DockAnchor::Expanded),
|
dock_position: crate::dock::DockPosition::Hidden(DockAnchor::Expanded),
|
||||||
center_group: Default::default(),
|
center_group: Default::default(),
|
||||||
dock_pane: Default::default(),
|
dock_pane: Default::default(),
|
||||||
left_sidebar_open: false
|
left_sidebar_open: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
db.save_workspace(workspace_1.clone()).await;
|
db.save_workspace(workspace_1.clone()).await;
|
||||||
|
@ -610,7 +612,7 @@ mod tests {
|
||||||
dock_position: DockPosition::Shown(DockAnchor::Bottom),
|
dock_position: DockPosition::Shown(DockAnchor::Bottom),
|
||||||
center_group,
|
center_group,
|
||||||
dock_pane,
|
dock_pane,
|
||||||
left_sidebar_open: true
|
left_sidebar_open: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
db.save_workspace(workspace.clone()).await;
|
db.save_workspace(workspace.clone()).await;
|
||||||
|
@ -683,7 +685,7 @@ mod tests {
|
||||||
dock_position: DockPosition::Shown(DockAnchor::Right),
|
dock_position: DockPosition::Shown(DockAnchor::Right),
|
||||||
center_group: Default::default(),
|
center_group: Default::default(),
|
||||||
dock_pane: Default::default(),
|
dock_pane: Default::default(),
|
||||||
left_sidebar_open: false
|
left_sidebar_open: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
db.save_workspace(workspace_3.clone()).await;
|
db.save_workspace(workspace_3.clone()).await;
|
||||||
|
@ -718,7 +720,7 @@ mod tests {
|
||||||
dock_position: crate::dock::DockPosition::Hidden(DockAnchor::Right),
|
dock_position: crate::dock::DockPosition::Hidden(DockAnchor::Right),
|
||||||
center_group: center_group.clone(),
|
center_group: center_group.clone(),
|
||||||
dock_pane,
|
dock_pane,
|
||||||
left_sidebar_open: true
|
left_sidebar_open: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue