|
| 1 | +DROP VIEW IF EXISTS template_version_with_user; |
| 2 | +DROP VIEW IF EXISTS workspace_build_with_user; |
| 3 | + |
| 4 | +-- Drop has_ai_task index and columns. |
| 5 | +DROP INDEX IF EXISTS idx_template_versions_has_ai_task; |
| 6 | +ALTER TABLE template_versions DROP COLUMN IF EXISTS has_ai_task; |
| 7 | +ALTER TABLE workspace_builds DROP COLUMN IF EXISTS has_ai_task; |
| 8 | + |
| 9 | +-- Remove task-related build reasons from build_reason enum. Automatic |
| 10 | +-- pauses stay system-attributed; manual pauses and resumes were created by |
| 11 | +-- authenticated user requests, so they keep user attribution as initiator. |
| 12 | +UPDATE workspace_builds |
| 13 | +SET reason = 'autostop' |
| 14 | +WHERE reason::text = 'task_auto_pause'; |
| 15 | + |
| 16 | +UPDATE workspace_builds |
| 17 | +SET reason = 'initiator' |
| 18 | +WHERE reason::text IN ('task_manual_pause', 'task_resume'); |
| 19 | + |
| 20 | +UPDATE workspace_build_orchestrations |
| 21 | +SET child_reason = 'autostop' |
| 22 | +WHERE child_reason::text = 'task_auto_pause'; |
| 23 | + |
| 24 | +UPDATE workspace_build_orchestrations |
| 25 | +SET child_reason = 'initiator' |
| 26 | +WHERE child_reason::text IN ('task_manual_pause', 'task_resume'); |
| 27 | + |
| 28 | +ALTER TYPE build_reason RENAME TO build_reason_old; |
| 29 | + |
| 30 | +CREATE TYPE build_reason AS ENUM ( |
| 31 | + 'initiator', |
| 32 | + 'autostart', |
| 33 | + 'autostop', |
| 34 | + 'dormancy', |
| 35 | + 'failedstop', |
| 36 | + 'autodelete', |
| 37 | + 'dashboard', |
| 38 | + 'cli', |
| 39 | + 'ssh_connection', |
| 40 | + 'vscode_connection', |
| 41 | + 'jetbrains_connection' |
| 42 | +); |
| 43 | + |
| 44 | +ALTER TABLE workspace_builds ALTER COLUMN reason DROP DEFAULT; |
| 45 | +ALTER TABLE workspace_builds ALTER COLUMN reason TYPE build_reason USING (reason::text::build_reason); |
| 46 | +ALTER TABLE workspace_builds ALTER COLUMN reason SET DEFAULT 'initiator'::build_reason; |
| 47 | + |
| 48 | +ALTER TABLE workspace_build_orchestrations ALTER COLUMN child_reason TYPE build_reason USING (child_reason::text::build_reason); |
| 49 | + |
| 50 | +DROP TYPE build_reason_old; |
| 51 | + |
| 52 | +-- Recreate views without task columns. |
| 53 | +CREATE VIEW template_version_with_user AS |
| 54 | + SELECT template_versions.id, |
| 55 | + template_versions.template_id, |
| 56 | + template_versions.organization_id, |
| 57 | + template_versions.created_at, |
| 58 | + template_versions.updated_at, |
| 59 | + template_versions.name, |
| 60 | + template_versions.readme, |
| 61 | + template_versions.job_id, |
| 62 | + template_versions.created_by, |
| 63 | + template_versions.external_auth_providers, |
| 64 | + template_versions.message, |
| 65 | + template_versions.archived, |
| 66 | + template_versions.source_example_id, |
| 67 | + template_versions.has_external_agent, |
| 68 | + COALESCE(visible_users.avatar_url, ''::text) AS created_by_avatar_url, |
| 69 | + COALESCE(visible_users.username, ''::text) AS created_by_username, |
| 70 | + COALESCE(visible_users.name, ''::text) AS created_by_name |
| 71 | + FROM (template_versions |
| 72 | + LEFT JOIN visible_users ON ((template_versions.created_by = visible_users.id))); |
| 73 | + |
| 74 | +COMMENT ON VIEW template_version_with_user IS 'Joins in the username + avatar url of the created by user.'; |
| 75 | + |
| 76 | +CREATE VIEW workspace_build_with_user AS |
| 77 | + SELECT workspace_builds.id, |
| 78 | + workspace_builds.created_at, |
| 79 | + workspace_builds.updated_at, |
| 80 | + workspace_builds.workspace_id, |
| 81 | + workspace_builds.template_version_id, |
| 82 | + workspace_builds.build_number, |
| 83 | + workspace_builds.transition, |
| 84 | + workspace_builds.initiator_id, |
| 85 | + workspace_builds.job_id, |
| 86 | + workspace_builds.deadline, |
| 87 | + workspace_builds.reason, |
| 88 | + workspace_builds.daily_cost, |
| 89 | + workspace_builds.max_deadline, |
| 90 | + workspace_builds.template_version_preset_id, |
| 91 | + workspace_builds.has_external_agent, |
| 92 | + workspace_builds.notified_autostop_deadline, |
| 93 | + COALESCE(visible_users.avatar_url, ''::text) AS initiator_by_avatar_url, |
| 94 | + COALESCE(visible_users.username, ''::text) AS initiator_by_username, |
| 95 | + COALESCE(visible_users.name, ''::text) AS initiator_by_name |
| 96 | + FROM (workspace_builds |
| 97 | + LEFT JOIN visible_users ON ((workspace_builds.initiator_id = visible_users.id))); |
| 98 | + |
| 99 | +COMMENT ON VIEW workspace_build_with_user IS 'Joins in the username + avatar url of the initiated by user.'; |
0 commit comments