建立、診斷與遷移
初始化資料表與結構、產生測試資料、執行診斷,並追蹤背景遷移工作。
POST
路由範本
/private/module/custom_tables/{scope}/tables/bootstrap用途
一次建立一張資料表,並依序走一般新增流程寫入最多 200 筆種子資料。
適用時機
新功能初始化時,結構與少量初始資料需要一起建立時使用。
實際掛載路徑
- 聊天室
/private/module/custom_tables/chatroom/{chatroom_id}/tables/bootstrap - 部門
/private/module/custom_tables/department/{department_id}/tables/bootstrap - 公司
/private/module/custom_tables/company/tables/bootstrap
範圍
| 範圍 | 是否提供 | 驗證與權限 |
|---|---|---|
| 聊天室 | 可使用 | ChatRoomAccessRequired |
| 部門 | 可使用 | DepartmentAccessRequired |
| 公司 | 可使用 | CompanyAccessRequired |
請求結構
TableBootstrapRequest| 欄位 | 型別 | 必填 | 說明 |
|---|---|---|---|
created_by_ai | boolean | 否 | Mark seeded records AI-authored |
rows | object[] | 否 | Seed record data dicts (display-name OR internal keys), inserted synchronously in order; max 200 (use bulk-insert for more) |
table | CustomTableCreate | 是 | Table definition (same as POST .../tables) |
回應結構
TableBootstrapResponse| 欄位 | 型別 | 必填 | 說明 |
|---|---|---|---|
records | CustomTableRecordResponse[] | 否 | The seeded records in insertion order |
table | CustomTableResponse | 是 | The created table (same as POST .../tables) |
範例
建立庫存資料表與第一筆庫存
請求
{
"pathParams": {
"chatroom_id": "11111111-1111-4111-8111-111111111111"
},
"body": {
"table": {
"name": "庫存",
"description": "目前庫存量",
"schema_definition": {
"columns": [
{
"name": "品項",
"type": "string",
"required": true
},
{
"name": "數量",
"type": "integer"
}
]
}
},
"rows": [
{
"品項": "筆記本",
"數量": 20
}
],
"created_by_ai": false
}
}回應201
{
"table": {
"id": "22222222-2222-4222-8222-222222222222",
"name": "庫存",
"description": "目前庫存量",
"schema_definition": {
"columns": [
{
"target_table_id": null,
"cardinality": null,
"link_field": null,
"aggregation": null,
"target_column": null,
"direction": null,
"source_table_id": null,
"match": null,
"filter": null,
"expression": null,
"options": null,
"max_count": null,
"max_file_bytes": null,
"allowed_mime_types": null,
"restricted": null,
"name": "id",
"type": "string",
"required": true,
"default_value": null,
"max_length": null,
"description": "Auto-generated unique identifier"
},
{
"target_table_id": null,
"cardinality": null,
"link_field": null,
"aggregation": null,
"target_column": null,
"direction": null,
"source_table_id": null,
"match": null,
"filter": null,
"expression": null,
"options": null,
"max_count": null,
"max_file_bytes": null,
"allowed_mime_types": null,
"restricted": null,
"name": "品項",
"type": "string",
"required": true,
"default_value": null,
"max_length": null,
"description": null
},
{
"target_table_id": null,
"cardinality": null,
"link_field": null,
"aggregation": null,
"target_column": null,
"direction": null,
"source_table_id": null,
"match": null,
"filter": null,
"expression": null,
"options": null,
"max_count": null,
"max_file_bytes": null,
"allowed_mime_types": null,
"restricted": null,
"name": "數量",
"type": "integer",
"required": false,
"default_value": null,
"max_length": null,
"description": null
}
]
},
"settings": {
"default_permissions": {
"can_read": "all",
"can_insert": false,
"can_edit": "none"
},
"column_mapping": {
"品項": "col_a1111111_1111_4111_8111_111111111111",
"數量": "col_b2222222_2222_4222_8222_222222222222"
}
},
"chatroom_id": "11111111-1111-4111-8111-111111111111",
"department_id": null,
"company_id": null,
"record_count": 0,
"created_at": "2026-07-19T02:20:00",
"updated_at": "2026-07-19T02:25:00"
},
"records": [
{
"id": "33333333-3333-4333-8333-333333333333",
"table_id": "22222222-2222-4222-8222-222222222222",
"data": {
"id": "77777777-7777-4777-8777-777777777777",
"品項": "筆記本",
"數量": 20
},
"created_by_ai": false,
"created_by": {
"id": "99999999-9999-4999-8999-999999999999",
"created_at": "2026-01-01 08:00:00",
"expired_at": "2027-01-01 08:00:00",
"photo_url": "https://example.com/avatar.png",
"enabled": true,
"is_verified": true,
"username": "api-user",
"email": "api-user@example.com",
"nickname": "API User",
"department_id": "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa",
"department_name": "營運部"
},
"created_by_client": null,
"is_deleted": false,
"sort_order": 1,
"version": 1,
"expanded": null,
"incoming_links": null,
"can_edit": true,
"pending_approval": null,
"created_at": "2026-07-19T02:20:00",
"updated_at": "2026-07-19T02:25:00"
}
]
}錯誤
| 狀態 | 發生條件 | 錯誤本文 |
|---|---|---|
| 400 | 某筆種子資料無法通過一般新增驗證。 | |
| 400 | 在 chatroom 或 company scope 的 bootstrap 上送了 `settings.default_permissions.audience: "company"`。detail 就是這一整句,而且整個呼叫在資料表列寫入之前就失敗——沒有表,也沒有種子資料列。這種分享只有在部門 scope 的表上才有意義,在那裡它取代了逐部門建立授權列。 | |
| 400 | `audience` 帶了 `"scope"` 與 `"company"` 以外的值——該值會以 `repr` 形式回拋,所以 `"everyone"` 會呈現成 `'everyone'`。與 default-permissions 的 PATCH 不同,這條通道收的是 raw settings dict 而非 Pydantic `Literal`,因此錯誤的值會一路走到文法閘門,回 `400` 而不是一般的 `422` 驗證 body。 | |
| 422 | 資料表規格不合法,或 rows 超過 200 筆。 | |
POST
路由範本
/private/module/custom_tables/{scope}/tables/bootstrap-schema用途
以請求內 alias 表示跨表參照,依相依順序建立最多 20 張資料表。
適用時機
要一次初始化一組有 link、rollup 或 lookup 相依關係的資料表時使用。
實際掛載路徑
- 聊天室
/private/module/custom_tables/chatroom/{chatroom_id}/tables/bootstrap-schema - 部門
/private/module/custom_tables/department/{department_id}/tables/bootstrap-schema - 公司
/private/module/custom_tables/company/tables/bootstrap-schema
範圍
| 範圍 | 是否提供 | 驗證與權限 |
|---|---|---|
| 聊天室 | 可使用 | ChatRoomAccessRequired |
| 部門 | 可使用 | DepartmentAccessRequired |
| 公司 | 可使用 | CompanyAccessRequired |
請求結構
SchemaBootstrapRequest| 欄位 | 型別 | 必填 | 說明 |
|---|---|---|---|
tables | SchemaBootstrapTable[] | 是 | Table specs (max 20); aliases must be unique within the request |
回應結構
SchemaBootstrapResponse| 欄位 | 型別 | 必填 | 說明 |
|---|---|---|---|
tables | SchemaBootstrapResultEntry[] | 否 | 未指定 |
範例
以 alias 建立客戶與訂單資料表
請求
{
"pathParams": {
"chatroom_id": "11111111-1111-4111-8111-111111111111"
},
"body": {
"tables": [
{
"alias": "orders",
"name": "訂單",
"columns": [
{
"name": "編號",
"type": "string",
"required": true
},
{
"name": "客戶",
"type": "link",
"target_table_alias": "customers",
"cardinality": "one"
}
]
},
{
"alias": "customers",
"name": "客戶",
"columns": [
{
"name": "名稱",
"type": "string",
"required": true
}
]
}
]
}
}回應201
{
"tables": [
{
"alias": "orders",
"table": {
"id": "55555555-5555-4555-8555-555555555555",
"name": "訂單",
"description": null,
"schema_definition": {
"columns": [
{
"target_table_id": null,
"cardinality": null,
"link_field": null,
"aggregation": null,
"target_column": null,
"direction": null,
"source_table_id": null,
"match": null,
"filter": null,
"expression": null,
"options": null,
"max_count": null,
"max_file_bytes": null,
"allowed_mime_types": null,
"restricted": null,
"name": "id",
"type": "string",
"required": true,
"default_value": null,
"max_length": null,
"description": "Auto-generated unique identifier"
},
{
"target_table_id": null,
"cardinality": null,
"link_field": null,
"aggregation": null,
"target_column": null,
"direction": null,
"source_table_id": null,
"match": null,
"filter": null,
"expression": null,
"options": null,
"max_count": null,
"max_file_bytes": null,
"allowed_mime_types": null,
"restricted": null,
"name": "編號",
"type": "string",
"required": true,
"default_value": null,
"max_length": null,
"description": null
},
{
"target_table_id": "44444444-4444-4444-8444-444444444444",
"cardinality": "one",
"link_field": null,
"aggregation": null,
"target_column": null,
"direction": null,
"source_table_id": null,
"match": null,
"filter": null,
"expression": null,
"options": null,
"max_count": null,
"max_file_bytes": null,
"allowed_mime_types": null,
"restricted": null,
"name": "客戶",
"type": "link",
"required": false,
"default_value": null,
"max_length": null,
"description": null
}
]
},
"settings": {
"default_permissions": {
"can_read": "all",
"can_insert": false,
"can_edit": "none"
},
"column_mapping": {
"編號": "col_d4444444_4444_4444_8444_444444444444",
"客戶": "col_e5555555_5555_4555_8555_555555555555"
}
},
"chatroom_id": "11111111-1111-4111-8111-111111111111",
"department_id": null,
"company_id": null,
"record_count": 0,
"created_at": "2026-07-19T02:20:00",
"updated_at": "2026-07-19T02:25:00"
}
},
{
"alias": "customers",
"table": {
"id": "44444444-4444-4444-8444-444444444444",
"name": "客戶",
"description": null,
"schema_definition": {
"columns": [
{
"target_table_id": null,
"cardinality": null,
"link_field": null,
"aggregation": null,
"target_column": null,
"direction": null,
"source_table_id": null,
"match": null,
"filter": null,
"expression": null,
"options": null,
"max_count": null,
"max_file_bytes": null,
"allowed_mime_types": null,
"restricted": null,
"name": "id",
"type": "string",
"required": true,
"default_value": null,
"max_length": null,
"description": "Auto-generated unique identifier"
},
{
"target_table_id": null,
"cardinality": null,
"link_field": null,
"aggregation": null,
"target_column": null,
"direction": null,
"source_table_id": null,
"match": null,
"filter": null,
"expression": null,
"options": null,
"max_count": null,
"max_file_bytes": null,
"allowed_mime_types": null,
"restricted": null,
"name": "名稱",
"type": "string",
"required": true,
"default_value": null,
"max_length": null,
"description": null
}
]
},
"settings": {
"default_permissions": {
"can_read": "all",
"can_insert": false,
"can_edit": "none"
},
"column_mapping": {
"名稱": "col_c3333333_3333_4333_8333_333333333333"
}
},
"chatroom_id": "11111111-1111-4111-8111-111111111111",
"department_id": null,
"company_id": null,
"record_count": 0,
"created_at": "2026-07-19T02:20:00",
"updated_at": "2026-07-19T02:25:00"
}
}
]
}錯誤
| 狀態 | 發生條件 | 錯誤本文 |
|---|---|---|
| 400 | alias 重複,或欄位參照請求中不存在的 alias。 | |
| 400 | alias 之間形成循環相依,無法先建立任何一張目標表。 | |
POST
路由範本
/private/module/custom_tables/{scope}/tables/{table_id}/test-data用途
依欄位型別產生隨機資料列,用於開發、效能與遷移測試。
適用時機
只在可丟棄或明確隔離的測試資料表需要快速造資料時使用。
實際掛載路徑
- 聊天室
/private/module/custom_tables/chatroom/{chatroom_id}/tables/{table_id}/test-data - 部門
/private/module/custom_tables/department/{department_id}/tables/{table_id}/test-data - 公司
/private/module/custom_tables/company/tables/{table_id}/test-data
範圍
| 範圍 | 是否提供 | 驗證與權限 |
|---|---|---|
| 聊天室 | 可使用 | CustomTableModeratorRequired |
| 部門 | 可使用 | CustomTableModeratorRequired |
| 公司 | 可使用 | CustomTableModeratorRequired |
參數
| 欄位 | 位置 | 型別 | 必填 | 說明 |
|---|---|---|---|---|
table_id | path | uuid | 是 | 要插入隨機測試資料的自訂資料表 UUID。 |
請求結構
TestDataInsertRequest| 欄位 | 型別 | 必填 | 說明 |
|---|---|---|---|
record_count | integer | 是 | Number of test records to insert |
回應結構
TestDataInsertResponse| 欄位 | 型別 | 必填 | 說明 |
|---|---|---|---|
background_processing | boolean | 是 | Whether the insertion is running in background |
inserted_count | integer | null | 否 | Number of records inserted (for synchronous operations) |
message | string | 是 | Status message |
migration_id | string | null | 否 | Migration ID for tracking background operations |
requested_records | integer | null | 否 | Number of records requested for background processing |
total_requested | integer | null | 否 | Total number of records requested |
範例
同步產生一筆測試資料
請求
{
"pathParams": {
"chatroom_id": "11111111-1111-4111-8111-111111111111",
"table_id": "22222222-2222-4222-8222-222222222222"
},
"body": {
"record_count": 1
}
}回應200
{
"background_processing": false,
"inserted_count": 1,
"total_requested": 1,
"requested_records": null,
"migration_id": null,
"message": "Successfully inserted 1 test records"
}錯誤
| 狀態 | 發生條件 | 錯誤本文 |
|---|---|---|
| 400 | 資料表結構無法產生相容資料,或插入流程失敗。 | |
| 423 | 資料表已被鎖定(detail 使用 Table is locked for operation: <op>;test-data 會取得 test_data_insertion)。 | |
POST
路由範本
/private/module/custom_tables/{scope}/tables/{table_id}/diagnosis用途
驗證所有資料列、找出結構與完整性問題,並提供效能建議。
適用時機
大量匯入、結構遷移後,或懷疑既有資料不符合目前結構時使用。
實際掛載路徑
- 聊天室
/private/module/custom_tables/chatroom/{chatroom_id}/tables/{table_id}/diagnosis - 部門
/private/module/custom_tables/department/{department_id}/tables/{table_id}/diagnosis - 公司
/private/module/custom_tables/company/tables/{table_id}/diagnosis
範圍
| 範圍 | 是否提供 | 驗證與權限 |
|---|---|---|
| 聊天室 | 可使用 | CustomTableModeratorRequired |
| 部門 | 可使用 | CustomTableModeratorRequired |
| 公司 | 可使用 | CustomTableAccessRequired |
參數
| 欄位 | 位置 | 型別 | 必填 | 說明 |
|---|---|---|---|---|
table_id | path | uuid | 是 | 要執行診斷的自訂資料表 UUID。 |
回應結構
TableDiagnosisResponse| 欄位 | 型別 | 必填 | 說明 |
|---|---|---|---|
background_processing | boolean | 是 | Whether the diagnosis is running in background |
diagnosis_results | object | null | 否 | Complete diagnosis results (for synchronous operations) |
estimated_records | integer | null | 否 | Estimated number of records to process |
message | string | 是 | Status message |
migration_id | string | null | 否 | Migration ID for tracking background operations |
範例
同步診斷小型資料表
請求
{
"pathParams": {
"chatroom_id": "11111111-1111-4111-8111-111111111111",
"table_id": "22222222-2222-4222-8222-222222222222"
}
}回應200
{
"background_processing": false,
"migration_id": "88888888-8888-4888-8888-888888888888",
"estimated_records": null,
"diagnosis_results": {
"total_records": 1,
"valid_records": 1,
"invalid_records": 0,
"validation_errors": [],
"schema_issues": [],
"data_integrity_issues": [],
"performance_recommendations": []
},
"message": "Diagnosis completed"
}錯誤
| 狀態 | 發生條件 | 錯誤本文 |
|---|---|---|
| 400 | 診斷流程無法讀取或驗證目前資料表。 | |
| 423 | 資料表已被鎖定(detail 使用 Table is locked for operation: <op>;diagnosis 會取得 diagnosis)。 | |
GET
路由範本
/private/module/custom_tables/{scope}/tables/{table_id}/migration/status用途
回傳指定資料表目前是否被遷移鎖定、鎖定資訊與快速資料列數。
適用時機
在可能衝突的結構或資料寫入前,顯示資料表是否暫時忙碌時使用。
實際掛載路徑
- 聊天室
/private/module/custom_tables/chatroom/{chatroom_id}/tables/{table_id}/migration/status - 部門
/private/module/custom_tables/department/{department_id}/tables/{table_id}/migration/status - 公司
/private/module/custom_tables/company/tables/{table_id}/migration/status
範圍
| 範圍 | 是否提供 | 驗證與權限 |
|---|---|---|
| 聊天室 | 可使用 | ChatRoomJoinedRequired |
| 部門 | 可使用 | CustomTableAccessRequired |
| 公司 | 可使用 | CustomTableAccessRequired |
參數
| 欄位 | 位置 | 型別 | 必填 | 說明 |
|---|---|---|---|---|
table_id | path | uuid | 是 | 要檢查鎖定狀態的自訂資料表 UUID。 |
回應結構
TableMigrationStatusResponse| 欄位 | 型別 | 必填 | 說明 |
|---|---|---|---|
is_locked | boolean | 是 | Whether the table is currently locked |
lock_info | object | 是 | Lock information if table is locked |
record_count | integer | 是 | Current number of records in the table |
table_id | string | 是 | Table ID |
範例
確認資料表目前未被鎖定
請求
{
"pathParams": {
"chatroom_id": "11111111-1111-4111-8111-111111111111",
"table_id": "22222222-2222-4222-8222-222222222222"
}
}回應200
{
"table_id": "22222222-2222-4222-8222-222222222222",
"is_locked": false,
"lock_info": {},
"record_count": 1
}錯誤
| 狀態 | 發生條件 | 錯誤本文 |
|---|---|---|
| 404 | 資料表不存在,或不屬於指定範圍。 | |
GET
路由範本
/private/module/custom_tables/{scopeName}/migrations/{migration_id}/status用途
回傳背景遷移或診斷工作的狀態、進度、處理筆數與錯誤。
適用時機
端點回傳 migration_id 後,用來輪詢直到 completed 或 failed。
實際掛載路徑
- 聊天室
/private/module/custom_tables/chatroom/migrations/{migration_id}/status - 部門
/private/module/custom_tables/department/migrations/{migration_id}/status - 公司
/private/module/custom_tables/company/migrations/{migration_id}/status
範圍
| 範圍 | 是否提供 | 驗證與權限 |
|---|---|---|
| 聊天室 | 可使用 | get_current_user |
| 部門 | 可使用 | get_current_user |
| 公司 | 可使用 | get_current_user |
參數
| 欄位 | 位置 | 型別 | 必填 | 說明 |
|---|---|---|---|---|
migration_id | path | uuid | 是 | 全域唯一的背景遷移 UUID。 |
回應結構
MigrationStatusResponse| 欄位 | 型別 | 必填 | 說明 |
|---|---|---|---|
created_at | string | 是 | Migration creation timestamp |
error_message | string | null | 否 | Error message if migration failed |
migrated_records | integer | 是 | Number of records processed |
migration_id | string | 是 | Migration ID |
operation | string | 是 | Migration operation type |
progress | integer | 是 | Progress percentage (0-100) |
status | string | 是 | Current migration status |
table_id | string | 是 | Table ID |
total_records | integer | 是 | Total records to process |
updated_at | string | 是 | Last update timestamp |
範例
取得已完成診斷工作的進度
請求
{
"pathParams": {
"migration_id": "88888888-8888-4888-8888-888888888888"
}
}回應200
{
"migration_id": "88888888-8888-4888-8888-888888888888",
"table_id": "22222222-2222-4222-8222-222222222222",
"operation": "diagnosis",
"status": "completed",
"progress": 100,
"total_records": 5001,
"migrated_records": 5001,
"error_message": null,
"created_at": "2026-07-19T02:20:00",
"updated_at": "2026-07-19T02:25:00"
}錯誤
| 狀態 | 發生條件 | 錯誤本文 |
|---|---|---|
| 404 | 找不到此遷移,或無法解析其所屬資料表。 | |
GET
路由範本
/private/module/custom_tables/{scopeName}/migrations/{migration_id}/diagnosis-results用途
依診斷工作的 migration_id 回傳完整驗證、結構、完整性與效能結果。
適用時機
背景 diagnosis 已完成,且需要取得詳細問題清單時使用。
實際掛載路徑
- 聊天室
/private/module/custom_tables/chatroom/migrations/{migration_id}/diagnosis-results - 部門
/private/module/custom_tables/department/migrations/{migration_id}/diagnosis-results - 公司
/private/module/custom_tables/company/migrations/{migration_id}/diagnosis-results
範圍
| 範圍 | 是否提供 | 驗證與權限 |
|---|---|---|
| 聊天室 | 可使用 | get_current_user |
| 部門 | 可使用 | get_current_user |
| 公司 | 可使用 | get_current_user |
參數
| 欄位 | 位置 | 型別 | 必填 | 說明 |
|---|---|---|---|---|
migration_id | path | uuid | 是 | 背景診斷工作的全域唯一 UUID。 |
回應結構
TableDiagnosisResponse| 欄位 | 型別 | 必填 | 說明 |
|---|---|---|---|
background_processing | boolean | 是 | Whether the diagnosis is running in background |
diagnosis_results | object | null | 否 | Complete diagnosis results (for synchronous operations) |
estimated_records | integer | null | 否 | Estimated number of records to process |
message | string | 是 | Status message |
migration_id | string | null | 否 | Migration ID for tracking background operations |
範例
取得已完成的背景診斷結果
請求
{
"pathParams": {
"migration_id": "88888888-8888-4888-8888-888888888888"
}
}回應200
{
"background_processing": false,
"migration_id": null,
"estimated_records": null,
"diagnosis_results": {
"total_records": 1,
"valid_records": 1,
"invalid_records": 0,
"validation_errors": [],
"schema_issues": [],
"data_integrity_issues": [],
"performance_recommendations": []
},
"message": "Background diagnosis completed"
}錯誤
| 狀態 | 發生條件 | 錯誤本文 |
|---|---|---|
| 404 | 診斷尚未產生結果、已過期,或 migration_id 不存在。 | |
Last updated on