Simplify the SerializableItem::cleanup implementation (#29567)

Release Notes:

- N/A

---------

Co-authored-by: Julia Ryan <juliaryan3.14@gmail.com>
This commit is contained in:
Mikayla Maki 2025-04-28 15:15:24 -07:00 committed by GitHub
parent 17703310ae
commit 1d7c86bf0d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 120 additions and 200 deletions

View file

@ -11,7 +11,7 @@ use std::{
use anyhow::{Context, Result, anyhow, bail};
use client::DevServerProjectId;
use db::{define_connection, query, sqlez::connection::Connection, sqlez_macros::sql};
use gpui::{Axis, Bounds, WindowBounds, WindowId, point, size};
use gpui::{Axis, Bounds, Task, WindowBounds, WindowId, point, size};
use itertools::Itertools;
use project::debugger::breakpoint_store::{BreakpointState, SourceBreakpoint};
@ -21,16 +21,17 @@ use remote::ssh_session::SshProjectId;
use sqlez::{
bindable::{Bind, Column, StaticColumnCount},
statement::{SqlType, Statement},
thread_safe_connection::ThreadSafeConnection,
};
use ui::px;
use ui::{App, px};
use util::{ResultExt, maybe};
use uuid::Uuid;
use crate::WorkspaceId;
use model::{
GroupId, LocalPaths, PaneId, SerializedItem, SerializedPane, SerializedPaneGroup,
GroupId, ItemId, LocalPaths, PaneId, SerializedItem, SerializedPane, SerializedPaneGroup,
SerializedSshProject, SerializedWorkspace,
};
@ -1422,6 +1423,37 @@ impl WorkspaceDb {
}
}
pub fn delete_unloaded_items(
alive_items: Vec<ItemId>,
workspace_id: WorkspaceId,
table: &'static str,
db: &ThreadSafeConnection,
cx: &mut App,
) -> Task<Result<()>> {
let db = db.clone();
cx.spawn(async move |_| {
let placeholders = alive_items
.iter()
.map(|_| "?")
.collect::<Vec<&str>>()
.join(", ");
let query = format!(
"DELETE FROM {table} WHERE workspace_id = ? AND item_id NOT IN ({placeholders})"
);
db.write(move |conn| {
let mut statement = Statement::prepare(conn, query)?;
let mut next_index = statement.bind(&workspace_id, 1)?;
for id in alive_items {
next_index = statement.bind(&id, next_index)?;
}
statement.exec()
})
.await
})
}
#[cfg(test)]
mod tests {
use std::thread;
@ -1430,14 +1462,13 @@ mod tests {
use super::*;
use crate::persistence::model::SerializedWorkspace;
use crate::persistence::model::{SerializedItem, SerializedPane, SerializedPaneGroup};
use db::open_test_db;
use gpui;
#[gpui::test]
async fn test_breakpoints() {
env_logger::try_init().ok();
let db = WorkspaceDb(open_test_db("test_breakpoints").await);
let db = WorkspaceDb::open_test_db("test_breakpoints").await;
let id = db.next_id().await.unwrap();
let path = Path::new("/tmp/test.rs");
@ -1622,7 +1653,7 @@ mod tests {
async fn test_remove_last_breakpoint() {
env_logger::try_init().ok();
let db = WorkspaceDb(open_test_db("test_remove_last_breakpoint").await);
let db = WorkspaceDb::open_test_db("test_remove_last_breakpoint").await;
let id = db.next_id().await.unwrap();
let singular_path = Path::new("/tmp/test_remove_last_breakpoint.rs");
@ -1709,7 +1740,7 @@ mod tests {
async fn test_next_id_stability() {
env_logger::try_init().ok();
let db = WorkspaceDb(open_test_db("test_next_id_stability").await);
let db = WorkspaceDb::open_test_db("test_next_id_stability").await;
db.write(|conn| {
conn.migrate(
@ -1757,7 +1788,7 @@ mod tests {
async fn test_workspace_id_stability() {
env_logger::try_init().ok();
let db = WorkspaceDb(open_test_db("test_workspace_id_stability").await);
let db = WorkspaceDb::open_test_db("test_workspace_id_stability").await;
db.write(|conn| {
conn.migrate(
@ -1851,7 +1882,7 @@ mod tests {
async fn test_full_workspace_serialization() {
env_logger::try_init().ok();
let db = WorkspaceDb(open_test_db("test_full_workspace_serialization").await);
let db = WorkspaceDb::open_test_db("test_full_workspace_serialization").await;
// -----------------
// | 1,2 | 5,6 |
@ -1926,7 +1957,7 @@ mod tests {
async fn test_workspace_assignment() {
env_logger::try_init().ok();
let db = WorkspaceDb(open_test_db("test_basic_functionality").await);
let db = WorkspaceDb::open_test_db("test_basic_functionality").await;
let workspace_1 = SerializedWorkspace {
id: WorkspaceId(1),
@ -2022,7 +2053,7 @@ mod tests {
async fn test_session_workspaces() {
env_logger::try_init().ok();
let db = WorkspaceDb(open_test_db("test_serializing_workspaces_session_id").await);
let db = WorkspaceDb::open_test_db("test_serializing_workspaces_session_id").await;
let workspace_1 = SerializedWorkspace {
id: WorkspaceId(1),
@ -2175,7 +2206,7 @@ mod tests {
let dir4 = tempfile::TempDir::with_prefix("dir4").unwrap();
let db =
WorkspaceDb(open_test_db("test_serializing_workspaces_last_session_workspaces").await);
WorkspaceDb::open_test_db("test_serializing_workspaces_last_session_workspaces").await;
let workspaces = [
(1, vec![dir1.path()], vec![0], 9),
@ -2264,9 +2295,10 @@ mod tests {
#[gpui::test]
async fn test_last_session_workspace_locations_ssh_projects() {
let db = WorkspaceDb(
open_test_db("test_serializing_workspaces_last_session_workspaces_ssh_projects").await,
);
let db = WorkspaceDb::open_test_db(
"test_serializing_workspaces_last_session_workspaces_ssh_projects",
)
.await;
let ssh_projects = [
("host-1", "my-user-1"),
@ -2340,7 +2372,7 @@ mod tests {
#[gpui::test]
async fn test_get_or_create_ssh_project() {
let db = WorkspaceDb(open_test_db("test_get_or_create_ssh_project").await);
let db = WorkspaceDb::open_test_db("test_get_or_create_ssh_project").await;
let (host, port, paths, user) = (
"example.com".to_string(),
@ -2386,7 +2418,7 @@ mod tests {
#[gpui::test]
async fn test_get_or_create_ssh_project_with_null_user() {
let db = WorkspaceDb(open_test_db("test_get_or_create_ssh_project_with_null_user").await);
let db = WorkspaceDb::open_test_db("test_get_or_create_ssh_project_with_null_user").await;
let (host, port, paths, user) = (
"example.com".to_string(),
@ -2415,7 +2447,7 @@ mod tests {
#[gpui::test]
async fn test_get_ssh_projects() {
let db = WorkspaceDb(open_test_db("test_get_ssh_projects").await);
let db = WorkspaceDb::open_test_db("test_get_ssh_projects").await;
let projects = vec![
(
@ -2458,7 +2490,7 @@ mod tests {
async fn test_simple_split() {
env_logger::try_init().ok();
let db = WorkspaceDb(open_test_db("simple_split").await);
let db = WorkspaceDb::open_test_db("simple_split").await;
// -----------------
// | 1,2 | 5,6 |
@ -2513,7 +2545,7 @@ mod tests {
async fn test_cleanup_panes() {
env_logger::try_init().ok();
let db = WorkspaceDb(open_test_db("test_cleanup_panes").await);
let db = WorkspaceDb::open_test_db("test_cleanup_panes").await;
let center_pane = group(
Axis::Horizontal,

View file

@ -62,7 +62,7 @@ use persistence::{
model::{SerializedSshProject, SerializedWorkspace},
};
pub use persistence::{
DB as WORKSPACE_DB, WorkspaceDb,
DB as WORKSPACE_DB, WorkspaceDb, delete_unloaded_items,
model::{ItemId, LocalPaths, SerializedWorkspaceLocation},
};
use postage::stream::Stream;