commented tasks module database migration

This commit is contained in:
2026-05-07 14:07:39 +02:00
parent 08e7060b1b
commit 0b97dd4095

View File

@@ -5,8 +5,8 @@
-- ---------------------------------------------------------------------
-- Block 1: Tasks Table
-- Stores community Tasks with Geometry, Moderation and Completion.
-- Status Flow: pending rejected | open → completed verified
-- Stores Tasks with Geometry, Moderation and Completion.
-- Status Flow from pending to rejected or approved to completed to verified
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS tasks (
task_id SERIAL PRIMARY KEY,
@@ -21,17 +21,17 @@ CREATE TABLE IF NOT EXISTS tasks (
browser_id VARCHAR(36),
photo_path VARCHAR(255),
status VARCHAR(20) NOT NULL DEFAULT 'pending'
CHECK (status IN ('pending', 'rejected', 'open', 'completed', 'verified')),
CHECK (status IN ('pending', 'rejected', 'approved', 'completed', 'verified')),
address VARCHAR(255),
-- Completion Fields (NULL until completed)
-- Completion Fields empty before completed
completed_by_name VARCHAR(100),
completed_by_browser VARCHAR(36),
completion_photo VARCHAR(255),
completion_comment TEXT,
completed_at TIMESTAMP,
-- Counters (updated via Triggers)
-- Counters updated via Triggers
likes_count INTEGER NOT NULL DEFAULT 0,
dislikes_count INTEGER NOT NULL DEFAULT 0,
comment_count INTEGER NOT NULL DEFAULT 0,
@@ -46,8 +46,8 @@ CREATE INDEX idx_tasks_category ON tasks(category);
-- ---------------------------------------------------------------------
-- Block 2: User Points Table
-- One Entry per verified Task Completion. Leaderboard via SUM/GROUP BY.
-- Block 2: Citizen Points Table
-- One Row per Completion. Leaderboard via SUM and GROUP BY.
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS user_points (
points_id SERIAL PRIMARY KEY,
@@ -63,8 +63,8 @@ CREATE INDEX idx_user_points_user ON user_points(user_name);
-- ---------------------------------------------------------------------
-- Block 3: Extends Votes Table for Tasks
-- Either contribution_id OR task_id is set, not both.
-- Block 3: Adapts Votes Table for Tasks
-- Either contribution_id OR task_id
-- ---------------------------------------------------------------------
ALTER TABLE votes
ADD COLUMN task_id INTEGER REFERENCES tasks(task_id) ON DELETE CASCADE;
@@ -78,8 +78,8 @@ ALTER TABLE votes
-- ---------------------------------------------------------------------
-- Block 4: Extends Comments Table for Tasks
-- Either contribution_id OR task_id is set, not both.
-- Block 4: Adapts Comments Table for Tasks
-- Either contribution_id OR task_id
-- ---------------------------------------------------------------------
ALTER TABLE comments
ADD COLUMN task_id INTEGER REFERENCES tasks(task_id) ON DELETE CASCADE;
@@ -88,7 +88,7 @@ CREATE INDEX idx_comments_task ON comments(task_id);
-- ---------------------------------------------------------------------
-- Block 5: Trigger — updated_at Timestamp for Tasks
-- Block 5: Trigger Updated Timestamp for Tasks
-- ---------------------------------------------------------------------
CREATE TRIGGER set_tasks_updated_at
BEFORE UPDATE ON tasks
@@ -97,8 +97,8 @@ CREATE TRIGGER set_tasks_updated_at
-- ---------------------------------------------------------------------
-- Block 6: Trigger Vote Counts for Tasks
-- Mirrors the Pattern from Contributions.
-- Block 6: Trigger Vote Counts for Tasks
-- Mirrors Pattern from Contributions.
-- ---------------------------------------------------------------------
CREATE OR REPLACE FUNCTION update_task_vote_counts()
RETURNS TRIGGER AS $$
@@ -131,8 +131,8 @@ CREATE TRIGGER trigger_update_task_vote_counts
-- ---------------------------------------------------------------------
-- Block 7: Trigger Comment Count for Tasks
-- Only counts approved Comments. Mirrors Contribution Pattern.
-- Block 7: Trigger Comment Count for Tasks
-- Mirrors Pattern from Contributions.
-- ---------------------------------------------------------------------
CREATE OR REPLACE FUNCTION update_task_comment_count()
RETURNS TRIGGER AS $$
@@ -169,7 +169,7 @@ CREATE TRIGGER trigger_update_task_comment_count
-- ---------------------------------------------------------------------
-- Block 8: Views for QGIS (optional)
-- Block 8: Views for QGIS
-- ---------------------------------------------------------------------
CREATE OR REPLACE VIEW tasks_points AS
SELECT * FROM tasks WHERE geom_type = 'point';