-
Notifications
You must be signed in to change notification settings - Fork 934
refactor(desktop): extract db helpers from workspaces router #681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bf4a1df
refactor(desktop): extract db helpers from workspaces router
Kitenite e7e0563
refactor(desktop): split workspaces router into domain-specific modules
Kitenite 1f515fd
Merge remote-tracking branch 'origin' into refactor-workspace-file
Kitenite 7d1ea21
Merge remote-tracking branch 'origin' into refactor-workspace-file
Kitenite f56b400
Merge remote-tracking branch 'origin' into refactor-workspace-file
Kitenite c24246f
lock
Kitenite 33e88ad
Merge remote-tracking branch 'origin' into refactor-workspace-file
Kitenite 5beaa70
fix potential race condition
Kitenite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
apps/desktop/src/lib/trpc/routers/workspaces/procedures/branch.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { projects, workspaces } from "@superset/local-db"; | ||
| import { eq } from "drizzle-orm"; | ||
| import { localDb } from "main/lib/local-db"; | ||
| import { terminalManager } from "main/lib/terminal"; | ||
| import { z } from "zod"; | ||
| import { publicProcedure, router } from "../../.."; | ||
| import { | ||
| getBranchWorkspace, | ||
| getWorkspace, | ||
| setLastActiveWorkspace, | ||
| touchWorkspace, | ||
| } from "../utils/db-helpers"; | ||
| import { listBranches, safeCheckoutBranch } from "../utils/git"; | ||
|
|
||
| export const createBranchProcedures = () => { | ||
| return router({ | ||
| getBranches: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| projectId: z.string(), | ||
| fetch: z.boolean().optional(), // Whether to fetch remote refs (default: false, avoids UI stalls) | ||
| }), | ||
| ) | ||
| .query(async ({ input }) => { | ||
| const project = localDb | ||
| .select() | ||
| .from(projects) | ||
| .where(eq(projects.id, input.projectId)) | ||
| .get(); | ||
| if (!project) { | ||
| throw new Error(`Project ${input.projectId} not found`); | ||
| } | ||
|
|
||
| const branches = await listBranches(project.mainRepoPath, { | ||
| fetch: input.fetch, | ||
| }); | ||
|
|
||
| // Get branches that are in use by worktrees, with their workspace IDs | ||
| const projectWorkspaces = localDb | ||
| .select() | ||
| .from(workspaces) | ||
| .where(eq(workspaces.projectId, input.projectId)) | ||
| .all(); | ||
| const worktreeBranchMap: Record<string, string> = {}; | ||
| for (const ws of projectWorkspaces) { | ||
| if (ws.type === "worktree" && ws.branch) { | ||
| worktreeBranchMap[ws.branch] = ws.id; | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| ...branches, | ||
| inUse: Object.keys(worktreeBranchMap), | ||
| inUseWorkspaces: worktreeBranchMap, // branch -> workspaceId | ||
| }; | ||
| }), | ||
|
|
||
| // Switch an existing branch workspace to a different branch | ||
| switchBranchWorkspace: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| projectId: z.string(), | ||
| branch: z.string(), | ||
| }), | ||
| ) | ||
| .mutation(async ({ input }) => { | ||
| const project = localDb | ||
| .select() | ||
| .from(projects) | ||
| .where(eq(projects.id, input.projectId)) | ||
| .get(); | ||
| if (!project) { | ||
| throw new Error(`Project ${input.projectId} not found`); | ||
| } | ||
|
|
||
| const workspace = getBranchWorkspace(input.projectId); | ||
| if (!workspace) { | ||
| throw new Error("No branch workspace found for this project"); | ||
| } | ||
|
|
||
| // Checkout the new branch with safety checks (terminals continue running on the new branch) | ||
| await safeCheckoutBranch(project.mainRepoPath, input.branch); | ||
|
|
||
| // Send newline to terminals so their prompts refresh with new branch | ||
| terminalManager.refreshPromptsForWorkspace(workspace.id); | ||
|
|
||
| // Update the workspace - name is always the branch for branch workspaces | ||
| touchWorkspace(workspace.id, { | ||
| branch: input.branch, | ||
| name: input.branch, | ||
| }); | ||
| setLastActiveWorkspace(workspace.id); | ||
|
|
||
| const updatedWorkspace = getWorkspace(workspace.id); | ||
| if (!updatedWorkspace) { | ||
| throw new Error(`Workspace ${workspace.id} not found after update`); | ||
| } | ||
|
|
||
| return { | ||
| workspace: updatedWorkspace, | ||
| worktreePath: project.mainRepoPath, | ||
| }; | ||
| }), | ||
| }); | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.