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