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

Commitdfd9a09

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

File tree

3 files changed

+566
-0
lines changed

3 files changed

+566
-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: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
-- Safe-guard, do not create tasks for workspaces that are already tasks.
73+
NOT EXISTS (
74+
SELECT1
75+
FROM tasks t
76+
WHEREt.workspace_id=w.id
77+
);
78+
79+
-- Step 2: Populate task_workspace_apps table with agent and app information
80+
INSERT INTO task_workspace_apps (
81+
task_id,
82+
workspace_build_number,
83+
workspace_agent_id,
84+
workspace_app_id
85+
)
86+
SELECT
87+
t.idAS task_id,
88+
latest_build.build_numberAS workspace_build_number,
89+
sidebar_app.agent_idAS workspace_agent_id,
90+
sidebar_app.idAS workspace_app_id
91+
FROM tasks t
92+
INNER JOIN LATERAL (
93+
-- Find the latest build for this tasks workspace.
94+
SELECT
95+
wb.build_number,
96+
wb.ai_task_sidebar_app_id
97+
FROM workspace_builds wb
98+
WHEREwb.workspace_id=t.workspace_id
99+
ORDER BYwb.build_numberDESC
100+
LIMIT1
101+
) latest_buildON true
102+
-- Get the sidebar app (optional, can be NULL).
103+
LEFT JOIN workspace_apps sidebar_app
104+
ONsidebar_app.id=latest_build.ai_task_sidebar_app_id
105+
WHERE
106+
-- Safe-guard, do not create for existing tasks.
107+
NOT EXISTS (
108+
SELECT1
109+
FROM task_workspace_apps twa
110+
WHEREtwa.task_id=t.id
111+
);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp