Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Clean up project repositories / repository statuses too (#33803)
Conrad Irwin2a8121fbfc50
diff --git a/crates/collab/migrations.sqlite/20221109000000_test_schema.sql b/crates/collab/migrations.sqlite/20221109000000_test_schema.sqlindex 63c8ab8ef4c..91cf4d7af05 100644--- a/crates/collab/migrations.sqlite/20221109000000_test_schema.sql+++ b/crates/collab/migrations.sqlite/20221109000000_test_schema.sql@@ -107,7 +107,7 @@ CREATE INDEX "index_worktree_entries_on_project_id" ON "worktree_entries" ("projCREATE INDEX "index_worktree_entries_on_project_id_and_worktree_id" ON "worktree_entries" ("project_id", "worktree_id");CREATE TABLE "project_repositories" (- "project_id" INTEGER NOT NULL,+ "project_id" INTEGER NOT NULL REFERENCES projects (id) ON DELETE CASCADE,"abs_path" VARCHAR,"id" INTEGER NOT NULL,"entry_ids" VARCHAR,@@ -124,7 +124,7 @@ CREATE TABLE "project_repositories" (CREATE INDEX "index_project_repositories_on_project_id" ON "project_repositories" ("project_id");CREATE TABLE "project_repository_statuses" (- "project_id" INTEGER NOT NULL,+ "project_id" INTEGER NOT NULL REFERENCES projects (id) ON DELETE CASCADE,"repository_id" INTEGER NOT NULL,"repo_path" VARCHAR NOT NULL,"status" INT8 NOT NULL,diff --git a/crates/collab/migrations/20250702185129_add_cascading_delete_to_repository_entries.sql b/crates/collab/migrations/20250702185129_add_cascading_delete_to_repository_entries.sqlnew file mode 100644index 00000000000..6d898c48119--- /dev/null+++ b/crates/collab/migrations/20250702185129_add_cascading_delete_to_repository_entries.sql@@ -0,0 +1,25 @@+DELETE FROM project_repositories+WHERE project_id NOT IN (SELECT id FROM projects);++ALTER TABLE project_repositories+ ADD CONSTRAINT fk_project_repositories_project_id+ FOREIGN KEY (project_id)+ REFERENCES projects (id)+ ON DELETE CASCADE+ NOT VALID;++ALTER TABLE project_repositories+ VALIDATE CONSTRAINT fk_project_repositories_project_id;++DELETE FROM project_repository_statuses+WHERE project_id NOT IN (SELECT id FROM projects);++ALTER TABLE project_repository_statuses+ ADD CONSTRAINT fk_project_repository_statuses_project_id+ FOREIGN KEY (project_id)+ REFERENCES projects (id)+ ON DELETE CASCADE+ NOT VALID;++ALTER TABLE project_repository_statuses+ VALIDATE CONSTRAINT fk_project_repository_statuses_project_id;diff --git a/crates/collab/src/db/queries/servers.rs b/crates/collab/src/db/queries/servers.rsindex 73deaaffb68..da6ff77cf0c 100644--- a/crates/collab/src/db/queries/servers.rs+++ b/crates/collab/src/db/queries/servers.rs@@ -142,6 +142,50 @@ impl Database {}}+ loop {+ let delete_query = Query::delete()+ .from_table(project_repository_statuses::Entity)+ .and_where(+ Expr::tuple([Expr::col((+ project_repository_statuses::Entity,+ project_repository_statuses::Column::ProjectId,+ ))+ .into()])+ .in_subquery(+ Query::select()+ .columns([(+ project_repository_statuses::Entity,+ project_repository_statuses::Column::ProjectId,+ )])+ .from(project_repository_statuses::Entity)+ .inner_join(+ project::Entity,+ Expr::col((project::Entity, project::Column::Id)).equals((+ project_repository_statuses::Entity,+ project_repository_statuses::Column::ProjectId,+ )),+ )+ .and_where(project::Column::HostConnectionServerId.ne(server_id))+ .limit(10000)+ .to_owned(),+ ),+ )+ .to_owned();++ let statement = Statement::from_sql_and_values(+ tx.get_database_backend(),+ delete_query+ .to_string(sea_orm::sea_query::PostgresQueryBuilder)+ .as_str(),+ vec![],+ );++ let result = tx.execute(statement).await?;+ if result.rows_affected() == 0 {+ break;+ }+ }+Ok(())}).await