1UPDATE thread_comment SET type='cause' WHERE type != 'comment';
2
3DROP VIEW thread_view;
4
5CREATE VIEW thread_view
6AS
7SELECT thread.id, thread.original_poster, thread.coordinator, thread.closed_by,
8       thread.private, thread.lock, thread.type,
9       thread.create_date, thread.last_activity_date, thread.last_modification_date, thread.close_date,
10       thread.title, thread.content, thread.content_html,
11       thread.task_count, thread.task_count_closed, thread.task_sum_cost,
12       label.id AS label_id,
13       label.name AS label_name,
14       (SELECT MAX(priority) FROM task_view WHERE task_view.thread_id = thread.id) AS priority,
15       CASE WHEN thread.state = 'proposal' THEN 0
16            WHEN thread.state = 'opened' AND thread.task_count = 0 THEN 1
17            WHEN thread.state = 'opened' THEN 2
18            WHEN thread.state = 'closed' THEN 3
19            WHEN thread.state = 'rejected' THEN 4 END AS sort,
20       CASE WHEN thread.state = 'opened' AND thread.task_count > 0 AND thread.task_count = thread.task_count_closed THEN 'done'
21            ELSE thread.state END AS state,
22       (SELECT COUNT(*) FROM thread_comment WHERE type = 'cause' AND thread_id=thread.id) AS cause_count
23FROM thread
24         LEFT JOIN thread_label ON thread.id = thread_label.thread_id
25         LEFT JOIN label ON label.id = thread_label.label_id;