Made dev tools not break everything about the db

Also improved multi statements to allow out of order parameter binding in statements
Ensured that all statements are run for maybe_row and single, and that of all statements only 1 of them returns only 1 row
Made bind and column calls add useful context to errors

Co-authored-by: kay@zed.dev
This commit is contained in:
Mikayla Maki 2022-11-21 13:42:26 -08:00
parent 2dc1130902
commit 3e0f9d27a7
13 changed files with 219 additions and 110 deletions

View file

@ -152,7 +152,7 @@ impl WorkspaceDb {
"})?((&workspace.location, workspace.id))
.context("clearing out old locations")?;
// Update or insert
// Upsert
self.exec_bound(indoc! {
"INSERT INTO
workspaces(workspace_id, workspace_location, dock_visible, dock_anchor, timestamp)
@ -190,8 +190,8 @@ impl WorkspaceDb {
.log_err();
}
sql_method! {
next_id() -> Result<Option<WorkspaceId>>:
sql_method!{
next_id() -> Result<WorkspaceId>:
"INSERT INTO workspaces DEFAULT VALUES RETURNING workspace_id"
}
@ -402,6 +402,10 @@ mod tests {
.unwrap();
let id = db.next_id().unwrap();
// Assert the empty row got inserted
assert_eq!(Some(id), db.select_row_bound::<WorkspaceId, WorkspaceId>
("SELECT workspace_id FROM workspaces WHERE workspace_id = ?").unwrap()
(id).unwrap());
db.exec_bound("INSERT INTO test_table(text, workspace_id) VALUES (?, ?)")
.unwrap()(("test-text-1", id))

View file

@ -3,7 +3,7 @@ use std::{
sync::Arc,
};
use anyhow::Result;
use anyhow::{Context, Result};
use async_recursion::async_recursion;
use gpui::{AsyncAppContext, Axis, ModelHandle, Task, ViewHandle};
@ -52,7 +52,7 @@ impl Column for WorkspaceLocation {
fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
let blob = statement.column_blob(start_index)?;
Ok((
WorkspaceLocation(bincode::deserialize(blob)?),
WorkspaceLocation(bincode::deserialize(blob).context("Bincode failed")?),
start_index + 1,
))
}

View file

@ -633,11 +633,11 @@ impl Workspace {
active_call = Some((call, subscriptions));
}
let id = if let Some(id) = serialized_workspace.as_ref().map(|ws| ws.id) {
id
} else {
DB.next_id().log_err().flatten().unwrap_or(0)
};
let database_id = serialized_workspace
.as_ref()
.map(|ws| ws.id)
.or_else(|| DB.next_id().log_err())
.unwrap_or(0);
let mut this = Workspace {
modal: None,
@ -666,7 +666,7 @@ impl Workspace {
last_leaders_by_pane: Default::default(),
window_edited: false,
active_call,
database_id: id,
database_id,
_observe_current_user,
};
this.project_remote_id_changed(project.read(cx).remote_id(), cx);