From f5519e4f64c7b3ff849f565970ffe1f642c1e7d3 Mon Sep 17 00:00:00 2001 From: Jiayuan Date: Mon, 30 Mar 2026 13:26:42 +0800 Subject: [PATCH] fix(handler): set issue_prefix when auto-creating workspace on first login ensureUserWorkspace in auth.go omitted the IssuePrefix field when creating a default workspace during first login. This caused the workspace's issue_prefix to be empty, producing identifiers like "-16" instead of "JIA-16". - Pass generateIssuePrefix(wsName) when creating the default workspace - Add fallback in getIssuePrefix to regenerate from workspace name if the stored prefix is empty - Add migration 024 to backfill empty prefixes on existing workspaces --- server/internal/handler/auth.go | 4 +++- server/internal/handler/handler.go | 7 ++++++- .../migrations/024_backfill_empty_issue_prefix.down.sql | 1 + server/migrations/024_backfill_empty_issue_prefix.up.sql | 8 ++++++++ 4 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 server/migrations/024_backfill_empty_issue_prefix.down.sql create mode 100644 server/migrations/024_backfill_empty_issue_prefix.up.sql diff --git a/server/internal/handler/auth.go b/server/internal/handler/auth.go index d0e4fedb..e073ef82 100644 --- a/server/internal/handler/auth.go +++ b/server/internal/handler/auth.go @@ -133,10 +133,12 @@ func (h *Handler) ensureUserWorkspace(ctx context.Context, user db.User) error { return nil } + wsName := defaultWorkspaceName(user) workspace, err := qtx.CreateWorkspace(ctx, db.CreateWorkspaceParams{ - Name: defaultWorkspaceName(user), + Name: wsName, Slug: defaultWorkspaceSlug(user), Description: pgtype.Text{}, + IssuePrefix: generateIssuePrefix(wsName), }) if err != nil { if isUniqueViolation(err) { diff --git a/server/internal/handler/handler.go b/server/internal/handler/handler.go index aaee0cd5..55da726d 100644 --- a/server/internal/handler/handler.go +++ b/server/internal/handler/handler.go @@ -250,12 +250,17 @@ func splitIdentifier(id string) *identifierParts { } // getIssuePrefix fetches the issue_prefix for a workspace. +// Falls back to generating a prefix from the workspace name if the stored +// prefix is empty (e.g. workspaces created before the prefix was introduced). func (h *Handler) getIssuePrefix(ctx context.Context, workspaceID pgtype.UUID) string { ws, err := h.Queries.GetWorkspace(ctx, workspaceID) if err != nil { return "" } - return ws.IssuePrefix + if ws.IssuePrefix != "" { + return ws.IssuePrefix + } + return generateIssuePrefix(ws.Name) } func (h *Handler) loadAgentForUser(w http.ResponseWriter, r *http.Request, agentID string) (db.Agent, bool) { diff --git a/server/migrations/024_backfill_empty_issue_prefix.down.sql b/server/migrations/024_backfill_empty_issue_prefix.down.sql new file mode 100644 index 00000000..ff6eaa35 --- /dev/null +++ b/server/migrations/024_backfill_empty_issue_prefix.down.sql @@ -0,0 +1 @@ +-- No-op: we cannot reliably determine which workspaces previously had empty prefixes. diff --git a/server/migrations/024_backfill_empty_issue_prefix.up.sql b/server/migrations/024_backfill_empty_issue_prefix.up.sql new file mode 100644 index 00000000..e6cc9c22 --- /dev/null +++ b/server/migrations/024_backfill_empty_issue_prefix.up.sql @@ -0,0 +1,8 @@ +-- Backfill workspaces that have an empty issue_prefix (e.g. auto-created +-- during first login before the prefix was wired up in ensureUserWorkspace). +UPDATE workspace SET issue_prefix = UPPER( + LEFT(REGEXP_REPLACE(name, '[^a-zA-Z]', '', 'g'), 3) +) WHERE issue_prefix = ''; + +-- Fallback for workspaces whose name has no alphabetic characters. +UPDATE workspace SET issue_prefix = 'WS' WHERE issue_prefix = '';