Skip to content

refactor(workspace): 移除 data_residency 字段(后端) - #98

Open
Postroggy wants to merge 6 commits into
superduck-ai:mainfrom
Postroggy:refactor/remove-workspace-geo-be
Open

refactor(workspace): 移除 data_residency 字段(后端)#98
Postroggy wants to merge 6 commits into
superduck-ai:mainfrom
Postroggy:refactor/remove-workspace-geo-be

Conversation

@Postroggy

@Postroggy Postroggy commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

问题背景

#93 中维护者反馈:工作区的 geo / data_residency 功能是之前对齐原版 Anthropic 工作区 API 时遗留的,本项目不再需要,应整体移除。

为避免单 PR 范围过大,拆分为两个 PR 共同完成 #93:本 PR 为后端部分,前端部分另开 PR。

方案

彻底移除 workspaces.data_residency 列及所有相关处理逻辑,覆盖 admin / console / db / platform 四层:

  • 新增 migration 00018_drop_workspace_data_residency.sqldrop column if exists data_residency(幂等,Down 可恢复默认值)
  • platform.ConsoleWorkspace / db.AdminWorkspace 移除 DataResidencyDataResidencySettings 字段
  • 删除 console / admin 请求与响应中的 data_residency 字段,以及 normalize / format / decode / encode 等辅助函数与类型别名
  • 同步调整三处测试,不再创建或断言 residency

兼容性取舍

移除该字段会偏离 Anthropic workspace API 的 data_residency 合同。这是有意的:本项目不提供多地域推理,保留该字段只会产生无意义的默认值噪音。前端 PR 将同步移除 UI 与类型。

改动文件

  • internal/db/migrations/00018_drop_workspace_data_residency.sql(新增)
  • internal/admin/domain_workspace.gointernal/admin/dto.gointernal/admin/service.go
  • internal/db/admin.gointernal/db/console_api_keys.go
  • internal/platform/console.gointernal/platformapi/console_api_keys.gointernal/platformapi/platform_aliases.go
  • tests/admin_api_test.gotests/platform_email_login_api_test.gotests/platform_console_backend_api_test.go

验证方式

  • go build ./...
  • golangci-lint run(仓库 .golangci.yml):0 issues
  • go test ./internal/admin/ ./internal/db/ ./tests/ -count=1:通过
  • just dead-code / just complexity / just duplicates:均通过

Refs #93

Summary by CodeRabbit

  • Changes

    • Removed workspace data residency fields from workspace creation, updates, responses, and console APIs.
    • Workspace records no longer store or return residency configuration.
    • Workspace creation now validates organization identifiers, trims name and color values, and applies color fallbacks consistently.
    • Existing workspace tags, roles, external keys, and archive protections remain supported.
  • Documentation

    • Added migration and rollout guidance covering removal of workspace residency fields and rollback behavior.

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 939193bb-f2b4-4415-ba09-d64c9745559d

📥 Commits

Reviewing files that changed from the base of the PR and between 731c2fd and 886c0e5.

📒 Files selected for processing (11)
  • docs/design/be/workspace-data-residency-removal.md
  • internal/admin/domain_workspace.go
  • internal/admin/dto.go
  • internal/admin/service.go
  • internal/db/admin_workspaces.go
  • internal/db/console_api_keys.go
  • internal/db/console_workspaces_sqlx_test.go
  • internal/db/migrations/00031_drop_workspace_data_residency.sql
  • internal/db/migrations/00047_drop_workspace_data_residency_after_uuid_rebuild.sql
  • internal/db/schema.go
  • internal/platform/console.go
💤 Files with no reviewable changes (2)
  • internal/db/schema.go
  • internal/admin/domain_workspace.go
🚧 Files skipped from review as they are similar to previous changes (3)
  • internal/admin/dto.go
  • internal/admin/service.go
  • internal/db/console_api_keys.go

📝 Walkthrough

Walkthrough

This change removes workspace data residency from admin and console contracts, services, persistence, API formatting, and tests. Reversible migrations drop the database column and restore it during rollback.

Changes

Workspace data residency removal

Layer / File(s) Summary
Workspace contract updates
internal/admin/dto.go, internal/platform/console.go, internal/platformapi/platform_aliases.go, internal/platformapi/console_api_keys.go
Workspace request, response, and model contracts no longer expose data residency fields or types.
Admin workspace flow
internal/admin/domain_workspace.go, internal/admin/service.go, internal/db/admin_workspaces.go
Admin creation, update, mapping, and database queries no longer parse, validate, store, or return data residency.
Console workspace flow
internal/db/console_api_keys.go, internal/platformapi/console_api_keys.go
Console workspace creation, listing, row conversion, and response formatting no longer handle data residency.
Schema migration and validation
internal/db/migrations/*, tests/*workspace*, tests/platform_email_login_api_test.go, docs/design/be/workspace-data-residency-removal.md
Migrations remove the residency column and restore it on downgrade. Tests and design documentation reflect the updated behavior.

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related PRs

Suggested reviewers: arthur-zhang

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确概括了后端移除工作区 data_residency 字段及相关逻辑这一主要变更。
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
tests/platform_console_backend_api_test.go (1)

824-832: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Assert that data_residency is absent from the response.

This test should lock in the contract being removed.

Proposed test assertion
 	if created["type"] != "workspace" || created["name"] != workspaceName || created["display_color"] != "`#1A8961`" || created["color"] != "`#1A8961`" {
 		t.Fatalf("created workspace = %#v, want source-compatible workspace shape", created)
 	}
+	if _, exists := created["data_residency"]; exists {
+		t.Fatalf("created workspace = %#v, data_residency must be omitted", created)
+	}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/platform_console_backend_api_test.go` around lines 824 - 832, Update
the workspace creation response assertions in the test around created and
workspaceID to verify that the created map does not contain the data_residency
field, preserving the existing source-compatible shape checks.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@tests/platform_console_backend_api_test.go`:
- Around line 824-832: Update the workspace creation response assertions in the
test around created and workspaceID to verify that the created map does not
contain the data_residency field, preserving the existing source-compatible
shape checks.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 27f43cfa-c042-44e6-8a92-0fa76ebd96af

📥 Commits

Reviewing files that changed from the base of the PR and between ef3cf6f and 731c2fd.

📒 Files selected for processing (12)
  • internal/admin/domain_workspace.go
  • internal/admin/dto.go
  • internal/admin/service.go
  • internal/db/admin.go
  • internal/db/console_api_keys.go
  • internal/db/migrations/00018_drop_workspace_data_residency.sql
  • internal/platform/console.go
  • internal/platformapi/console_api_keys.go
  • internal/platformapi/platform_aliases.go
  • tests/admin_api_test.go
  • tests/platform_console_backend_api_test.go
  • tests/platform_email_login_api_test.go
💤 Files with no reviewable changes (4)
  • internal/platformapi/platform_aliases.go
  • tests/admin_api_test.go
  • internal/admin/service.go
  • internal/admin/domain_workspace.go

@duckpr duckpr Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DuckPR reviewer: opencode
Model: anthropic/glm-5.2

✅ No new issues found.

Reviewed changes — 本 PR 从后端移除 workspaces.data_residency 列及其在 admin / console / db / platform 四层中的所有处理逻辑,并同步更新相关测试。

  • 新增 00018_drop_workspace_data_residency.sql migration,以 drop column if exists data_residency 删除列,并在 Down 中按原默认值恢复列。
  • 移除 admin 工作区 API 的 data_residency 字段,包括 createWorkspaceRequest / updateWorkspaceRequest / workspaceResponse 以及 internal/admin/domain_workspace.go 中的 encode / decode / validate 辅助函数。
  • 移除 console 工作区模型和 API 中的 data_residency,包括 platform.ConsoleWorkspaceConsoleWorkspaceDataResidency 以及 formatConsoleWorkspace / normalizeConsoleWorkspaceDataResidency
  • 更新 internal/db 层读写AdminWorkspace / CreateAdminWorkspace / UpdateAdminWorkspace / ArchiveAdminWorkspace / CreateConsoleWorkspace / ListConsoleWorkspaces 不再扫描或写入 data_residency
  • 调整三处集成测试,不再发送或断言 data_residency

Pullfrog  | View workflow run | Using anthropic/glm-5.2𝕏

@duckpr duckpr Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DuckPR reviewer: opencode
Model: anthropic/glm-5.2

✅ 无新问题。

Reviewed changes — 本次新增提交仅补充了设计文档,系统记录了移除 workspaces.data_residency 字段的完整设计决策、影响范围、兼容取舍、前后端协调与回滚方案。

  • 新增设计文档 docs/design/be/workspace-data-residency-removal.md,覆盖背景、数据模型与 migration、API 合同兼容取舍、前后端合并顺序、测试计划与回滚。

Pullfrog  | View workflow run | Using anthropic/glm-5.2𝕏

@Postroggy Postroggy changed the title 后端:移除工作区 data_residency / geo 字段 [Workspace] 移除 data_residency 字段(后端) Jul 18, 2026
@Postroggy

Copy link
Copy Markdown
Contributor Author

@Postroggy Postroggy changed the title [Workspace] 移除 data_residency 字段(后端) refactor(workspace): 移除 data_residency 字段(后端) Jul 19, 2026
Drop the workspaces.data_residency column and all residency handling
across admin, console, db, and platform layers. This field was a
leftover from aligning with the upstream Anthropic workspace API and
is no longer required.

- migration 00018 drops workspaces.data_residency
- remove residency fields/types from ConsoleWorkspace, AdminWorkspace
- strip data_residency from console and admin DTOs and responses
- update tests to stop asserting residency normalization
Record the decision, data-model/API impact, frontend/backend
coordination, and rollback path for removing the legacy
data_residency / geo field.
@Postroggy
Postroggy force-pushed the refactor/remove-workspace-geo-be branch from 8a54edc to dc6e07e Compare July 26, 2026 14:32

@duckpr duckpr Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DuckPR reviewer: opencode
Model: anthropic/glm-5.2

Important

合并 main 的 UUID 重构后,设计文档没有跟上:文档只描述了 00031,并断言「00031 会把列删掉,最终 schema 不含该列」,但这句在合并后已经不成立。详见下方 inline 评论。代码本身的两个 migration 都是必需且正确的。

Reviewed changes — 本次合并 origin/main 的 UUID 重构后,workspaces 表在 00036 被整体重建(建新表 → 拷贝 data_residency → drop 旧表 → 改名),导致 00031 删掉的列又回来了。新增的 00047_drop_workspace_data_residency_after_uuid_rebuild.sql 在重建之后再次删除该列,是正确且必需的;同时 schema.go legacy bootstrap 也已同步移除该列。

  • 新增 00047_drop_workspace_data_residency_after_uuid_rebuild.sql —— 在 00036_use_uuid_workspace_organization_reference.sql 重建 workspaces 表后再次执行 drop column if exists data_residency,确保最终 schema 不含该列(00036 在新表定义与 insert ... select 中都带回了该列)。
  • schema.go legacy bootstrap 同步移除 data_residency —— 从 create tablealter table add column 两处删除,与 migration 结果一致。
  • internal/db/admin.gointernal/db/admin_workspaces.go —— admin workspace 数据访问随 UUID 重构拆分为独立文件,AdminWorkspace 结构、SQL 与参数映射中不再含 data_residency
  • internal/admin/service.go —— 随 UUID 重构改用 principal.OrganizationUUIDCreateWorkspace / UpdateWorkspace / workspaceFromRecord 均不再触碰 residency。

Pullfrog  | Fix all ➔Fix 👍s ➔View workflow run | Using anthropic/glm-5.2𝕏

Comment on lines +51 to +52
- migration `00031` 使用 `drop column if exists`,对已应用旧迁移的库与全新库都安全。
- `internal/db/schema.go` 是 legacy bootstrap 文本 schema(仅 `migrateLegacyTextIDSchema` 使用),按项目规则不修改;其内部仍保留旧列定义,但 `Migrate()` 在 goose migration 之后执行,`00031` 会把列删掉,最终 schema 不含该列。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ 这段 migration 描述在合并 main 的 UUID 重构后已经不准确。文档只提到 00031,并断言「00031 会把列删掉,最终 schema 不含该列」——但 00036_use_uuid_workspace_organization_reference.sql 会整体重建 workspaces 表(建 workspaces_uuid_refs 时重新定义了 data_residency jsonb not null default ...,并在 insert ... select 中拷贝 w.data_residency,然后 drop 旧表、改名),因此 00031 删掉的列在 00036 之后会重新出现。真正完成最终删除的是本 PR 新增的 00047_drop_workspace_data_residency_after_uuid_rebuild.sql,而文档完全没有提及它。

按 AGENTS.md「修改 schema 变更…必须同步更新 docs/design/」的要求,需要把 00047 补进文档,并修正「00031 会把列删掉」这句不再成立的论断(说明 UUID 重建会带回该列、00047 负责重建后再次删除)。

Technical details
# 设计文档未同步 `00047` migration

## Affected sites
- `docs/design/be/workspace-data-residency-removal.md:51` — 只提及 `00031`,声称它让最终 schema 不含该列
- `docs/design/be/workspace-data-residency-removal.md:67` — 影响范围小节同样只列 `00031`
- `docs/design/be/workspace-data-residency-removal.md:119` — 回滚小节只描述 `00031` 的 Down
- `internal/db/migrations/00036_use_uuid_workspace_organization_reference.sql:31,47,97,113` — UUID 重建 workspaces 表时在新表定义与 copy 中带回 `data_residency`
- `internal/db/migrations/00047_drop_workspace_data_residency_after_uuid_rebuild.sql` — 文档未提及的第二次 drop

## Required outcome
- 文档「数据模型与 migration」小节需要说明存在两个 drop migration,以及 `00047` 存在的原因(`00036` 重建表时带回该列)
- 修正「`00031` 会把列删掉,最终 schema 不含该列」这句论断
- 回滚小节补充 `00047` 的 Down 行为(与 `00031` 一致,恢复带默认值的列)

## Suggested approach
最小改动:在「数据模型与 migration」小节增补一条 bullet 说明 `00036` 会重建 `workspaces` 表并带回 `data_residency`,因此需要 `00047` 在重建之后再次删除;并把「最终 schema 不含该列」的归因从 `00031` 改为 `00047`。回滚小节同理补充 `00047`

@duckpr duckpr Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DuckPR reviewer: opencode
Model: anthropic/glm-5.2

Important

设计文档仍未同步 00047 migration——详见下方「设计文档未更新 00047 migration」。

Reviewed changes — 本次新增提交(886c0e5)将 origin/main 的 Yourbatis mapper 重构合并进来;residency 移除相关的代码改动很小,主要是一个新增的 workspace 行映射单测。

  • 合并 main 的 Yourbatis 重构 —— internal/db/console_api_keys.go 的 API key 数据访问被改写为 Yourbatis mapper(来自 main 的 PR #200/#202/#203,非本 PR 作者改动);workspace SQL 路径仍保留 sqlx,data_residency 移除未受影响。
  • 新增 console_workspaces_sqlx_test.go —— 单测验证 consoleWorkspaceRow.workspace() 的 UUID / external ID / tags 映射,间接覆盖 residency 字段已不在行结构中。

⚠️ 设计文档未更新 00047 migration

上一轮 review(73beee2)已指出:docs/design/be/workspace-data-residency-removal.md 第 51-52 行仍断言「00031 会把列删掉,最终 schema 不含该列」,但合并 main 的 UUID 重构后这句已不成立——00036_use_uuid_workspace_organization_reference.sql 重建 workspaces 表时会重新带回 data_residency 列(新表定义与 insert ... select 都包含它),真正完成最终删除的是本 PR 的 00047。本次增量提交仍未修正该文档,且影响范围小节(第 44 行)与回滚小节(第 119 行)同样只提 00031

Technical details
# 设计文档未同步 `00047` migration

## Affected sites
- `docs/design/be/workspace-data-residency-removal.md:51` — 声称 `00031` 单独让最终 schema 不含该列
- `docs/design/be/workspace-data-residency-removal.md:44` — 影响范围小节只列 `00031`
- `docs/design/be/workspace-data-residency-removal.md:119` — 回滚小节只描述 `00031` 的 Down
- `internal/db/migrations/00036_use_uuid_workspace_organization_reference.sql:31,42,47,97,108,113` — UUID 重建时在新表定义与 copy 中带回 `data_residency`
- `internal/db/migrations/00047_drop_workspace_data_residency_after_uuid_rebuild.sql` — 文档未提及的第二次 drop

## Required outcome
- 「数据模型与 migration」小节说明存在两个 drop migration,以及 `00047` 存在的原因(`00036` 重建表时带回该列)
- 把「最终 schema 不含该列」的归因从 `00031` 改为 `00047`
- 回滚小节补充 `00047` 的 Down 行为(与 `00031` 一致,恢复带默认值的列)

## Suggested approach
最小改动:在「数据模型与 migration」小节增补一条 bullet 说明 `00036` 会重建 `workspaces` 表并带回 `data_residency`,因此需要 `00047` 在重建之后再次删除;并把「最终 schema 不含该列」的归因从 `00031` 改为 `00047`。影响范围与回滚小节同理补充 `00047`

Pullfrog  | Fix it ➔View workflow run | Using anthropic/glm-5.2𝕏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant