-
-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Assign users to tasks #2237
base: development
Are you sure you want to change the base?
Assign users to tasks #2237
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
-- Begin transaction | ||
BEGIN; | ||
|
||
-- Create task_assignments table only if it does not exist | ||
DO $$ | ||
BEGIN | ||
IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'task_assignments') THEN | ||
CREATE TABLE public.task_assignments ( | ||
project_id INTEGER NOT NULL, | ||
task_id INTEGER NOT NULL, | ||
user_id INTEGER NOT NULL, | ||
assigned_at TIMESTAMPTZ NOT NULL DEFAULT now() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need a new table for this? I thought we would just have an ASSIGN event in the task events table? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding new things to the database should always be a last resort There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we rely solely on the task_events table, we'd either have multiple event records per user (causing redundancy and data bloat) or a separate field for assignees that stores a list of user IDs resulting in slower querying and an empty column for non-assignment events. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point - we need to reference both the assigner (eg manager) and the assigned (mapper) in one event. Instead, how about we add a field to the tasks table The task event will have the user id for the assigner (plus a comment containing the username of who it was assigned to), while the assigned_to field will have the user id of the assigned. Do you think that could be good? (the task unlocking logic may have to be tweaked though to remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are to assign multiple users to a task, the field we add to the tasks table would need to be a list of integers (since the table can only have one row for each task ID). We would need to append or extend to the assigned_to field, which would be a list of user IDs. This is not the best approach. It would be better to store it in a new task assignments table for faster querying, simple insert and delete for assignments and referential integrity with foreign keys. The assignments would also be automatically deleted without any new query code if the user, project or task were to be deleted. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fully agree - if we need to assign multiple users then this is the way to go π Assigning multiple users doesn't really fit into our model though. A task can only be locked by one user. The credit for mapping a task will only go to one user. I thought we always discussed team mapping as an informal process, not recorded. Anyway, if this was discussed in my absence and agreed on, I will defer to your judgement π |
||
); | ||
ALTER TABLE public.task_assignments OWNER TO fmtm; | ||
END IF; | ||
END$$; | ||
|
||
-- Add primary key constraint if it does not exist | ||
DO $$ | ||
BEGIN | ||
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_name = 'task_assignments' AND constraint_name = 'task_assignments_pkey') THEN | ||
ALTER TABLE ONLY public.task_assignments | ||
ADD CONSTRAINT task_assignments_pkey PRIMARY KEY (task_id, user_id); | ||
END IF; | ||
END$$; | ||
|
||
-- Add foreign key constraints if they do not exist | ||
DO $$ | ||
BEGIN | ||
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_name = 'task_assignments' AND constraint_name = 'task_assignments_user_id_fkey') THEN | ||
ALTER TABLE ONLY public.task_assignments | ||
ADD CONSTRAINT task_assignments_user_id_fkey FOREIGN KEY (user_id) | ||
REFERENCES public.users (id) ON DELETE CASCADE; | ||
END IF; | ||
|
||
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_name = 'task_assignments' AND constraint_name = 'task_assignments_project_id_fkey') THEN | ||
ALTER TABLE public.task_assignments | ||
ADD CONSTRAINT task_assignments_project_id_fkey FOREIGN KEY (project_id) | ||
REFERENCES public.projects (id) ON DELETE CASCADE; | ||
END IF; | ||
END$$; | ||
|
||
-- Commit transaction | ||
COMMIT; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -398,6 +398,14 @@ CREATE TABLE public.geometrylog ( | |
); | ||
ALTER TABLE public.geometrylog OWNER TO fmtm; | ||
|
||
CREATE TABLE public.task_assignments ( | ||
project_id INTEGER NOT NULL, | ||
task_id INTEGER NOT NULL, | ||
user_id INTEGER NOT NULL, | ||
assigned_at TIMESTAMPTZ NOT NULL DEFAULT now() | ||
); | ||
ALTER TABLE public.task_assignments OWNER TO fmtm; | ||
|
||
-- nextval for primary keys (autoincrement) | ||
|
||
ALTER TABLE ONLY public.organisations ALTER COLUMN id SET DEFAULT nextval( | ||
|
@@ -467,6 +475,9 @@ ADD CONSTRAINT xlsforms_title_key UNIQUE (title); | |
ALTER TABLE ONLY public.geometrylog | ||
ADD CONSTRAINT geometrylog_pkey PRIMARY KEY (id); | ||
|
||
ALTER TABLE ONLY public.task_assignments | ||
ADD CONSTRAINT task_assignments_pkey PRIMARY KEY (task_id, user_id); | ||
|
||
-- Indexing | ||
|
||
CREATE INDEX idx_projects_outline ON public.projects USING gist (outline); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably need an index for faster lookup. Choosing the column depends what you will query on most frequently. |
||
|
@@ -567,6 +578,13 @@ ADD CONSTRAINT user_roles_user_id_fkey FOREIGN KEY ( | |
user_id | ||
) REFERENCES public.users (id); | ||
|
||
ALTER TABLE ONLY public.task_assignments | ||
ADD CONSTRAINT task_assignments_user_id_fkey FOREIGN KEY (user_id) | ||
REFERENCES public.users (id) ON DELETE CASCADE; | ||
|
||
ALTER TABLE public.task_assignments | ||
ADD CONSTRAINT task_assignments_project_id_fkey FOREIGN KEY (project_id) | ||
REFERENCES public.projects (id) ON DELETE CASCADE; | ||
-- Triggers | ||
|
||
CREATE OR REPLACE FUNCTION public.set_task_state() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a simpler syntax in postgres for this:
create table if not exists task_assignments
Checking the information schema is only needed when this is not possible, such as with enums