Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit193163e

Browse files
committed
chore(coderd/database/migrations): migrate tasks to new data model
Updatescoder/internal#976
1 parent4a0514f commit193163e

File tree

3 files changed

+529
-0
lines changed

3 files changed

+529
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- No-op: This migration is not reversible as it transforms existing data into
2+
-- a new schema. Rolling back would require deleting tasks and potentially
3+
-- losing data.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
-- Migrate existing task workspaces to the new tasks data model. This migration
2+
-- identifies workspaces that were created as tasks (has_ai_task = true) and
3+
-- populates the tasks and task_workspace_apps tables with their data.
4+
5+
-- Step 1: Create tasks from workspaces with has_ai_task TRUE in their latest build.
6+
INSERT INTO tasks (
7+
id,
8+
organization_id,
9+
owner_id,
10+
name,
11+
workspace_id,
12+
template_version_id,
13+
template_parameters,
14+
prompt,
15+
created_at,
16+
deleted_at
17+
)
18+
SELECT
19+
gen_random_uuid()AS id,
20+
w.organization_id,
21+
w.owner_id,
22+
w.name,
23+
w.idAS workspace_id,
24+
latest_task_build.template_version_id,
25+
COALESCE(params.template_parameters,'{}'::jsonb)AS template_parameters,
26+
COALESCE(ai_prompt.value,'')AS prompt,
27+
w.created_at,
28+
CASE WHENw.deleted= true THENw.deleting_at ELSENULL ENDAS deleted_at
29+
FROM workspaces w
30+
INNER JOIN LATERAL (
31+
-- Find the latest build for this workspace that has has_ai_task = true.
32+
SELECT
33+
wb.template_version_id
34+
FROM workspace_builds wb
35+
WHEREwb.workspace_id=w.id
36+
ANDwb.has_ai_task= true
37+
ORDER BYwb.build_numberDESC
38+
LIMIT1
39+
) latest_task_buildON true
40+
LEFT JOIN LATERAL (
41+
-- Find the latest build that has a non-empty AI Prompt parameter.
42+
SELECT
43+
wb.id
44+
FROM workspace_builds wb
45+
WHEREwb.workspace_id=w.id
46+
AND EXISTS (
47+
SELECT1
48+
FROM workspace_build_parameters wbp
49+
WHEREwbp.workspace_build_id=wb.id
50+
ANDwbp.name='AI Prompt'
51+
ANDwbp.value!=''
52+
)
53+
ORDER BYwb.build_numberDESC
54+
LIMIT1
55+
) latest_prompt_buildON true
56+
LEFT JOIN LATERAL (
57+
-- Extract the AI Prompt parameter value from the prompt build.
58+
SELECTwbp.value
59+
FROM workspace_build_parameters wbp
60+
WHEREwbp.workspace_build_id=latest_prompt_build.id
61+
ANDwbp.name='AI Prompt'
62+
LIMIT1
63+
) ai_promptON true
64+
LEFT JOIN LATERAL (
65+
-- Aggregate all other parameters (excluding AI Prompt) from the prompt build.
66+
SELECT jsonb_object_agg(wbp.name,wbp.value)AS template_parameters
67+
FROM workspace_build_parameters wbp
68+
WHEREwbp.workspace_build_id=latest_prompt_build.id
69+
ANDwbp.name!='AI Prompt'
70+
) paramsON true
71+
WHERE
72+
-- Skip deleted workspaces because of duplicate name.
73+
w.deleted= false
74+
-- Safe-guard, do not create tasks for workspaces that are already tasks.
75+
AND NOT EXISTS (
76+
SELECT1
77+
FROM tasks t
78+
WHEREt.workspace_id=w.id
79+
);
80+
81+
-- Step 2: Populate task_workspace_apps table with build/agent/app information.
82+
INSERT INTO task_workspace_apps (
83+
task_id,
84+
workspace_build_number,
85+
workspace_agent_id,
86+
workspace_app_id
87+
)
88+
SELECT
89+
t.idAS task_id,
90+
latest_build.build_numberAS workspace_build_number,
91+
sidebar_app.agent_idAS workspace_agent_id,
92+
sidebar_app.idAS workspace_app_id
93+
FROM tasks t
94+
INNER JOIN LATERAL (
95+
-- Find the latest build for this tasks workspace.
96+
SELECT
97+
wb.build_number,
98+
wb.ai_task_sidebar_app_id
99+
FROM workspace_builds wb
100+
WHEREwb.workspace_id=t.workspace_id
101+
ORDER BYwb.build_numberDESC
102+
LIMIT1
103+
) latest_buildON true
104+
-- Get the sidebar app (optional, can be NULL).
105+
LEFT JOIN workspace_apps sidebar_app
106+
ONsidebar_app.id=latest_build.ai_task_sidebar_app_id
107+
WHERE
108+
-- Safe-guard, do not create for existing tasks.
109+
NOT EXISTS (
110+
SELECT1
111+
FROM task_workspace_apps twa
112+
WHEREtwa.task_id=t.id
113+
);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp