From b6edc71ea531efd2d076800ce0f96f3b7a13942a Mon Sep 17 00:00:00 2001 From: julian-wasmeier-titanom Date: Mon, 20 May 2024 14:46:48 +0200 Subject: [PATCH] feat: add taskGroupAssignment table, rename userTaskGroup table --- backend/src/db/functions/task-group.ts | 4 +- .../migrations/0004_flippant_black_bird.sql | 19 + .../src/db/migrations/meta/0004_snapshot.json | 361 ++++++++++++++++++ backend/src/db/migrations/meta/_journal.json | 7 + backend/src/db/schema.ts | 13 +- 5 files changed, 401 insertions(+), 3 deletions(-) create mode 100644 backend/src/db/migrations/0004_flippant_black_bird.sql create mode 100644 backend/src/db/migrations/meta/0004_snapshot.json diff --git a/backend/src/db/functions/task-group.ts b/backend/src/db/functions/task-group.ts index 5ab314d..cd043fb 100644 --- a/backend/src/db/functions/task-group.ts +++ b/backend/src/db/functions/task-group.ts @@ -1,6 +1,6 @@ import { CreateTaskGroup } from 'src/task-group.controller'; import { db } from '..'; -import { taskGroupTable, userTaskGroupTable } from '../schema'; +import { taskGroupTable, taskGroupUserTable } from '../schema'; export async function dbGetTaskGroups() { return await db.select().from(taskGroupTable); @@ -26,7 +26,7 @@ export async function dbCreateTaskGroup({ const { taskGroupId } = res[0]; - await db.insert(userTaskGroupTable).values( + await db.insert(taskGroupUserTable).values( userIds.map((userId) => ({ taskGroupId, userId, diff --git a/backend/src/db/migrations/0004_flippant_black_bird.sql b/backend/src/db/migrations/0004_flippant_black_bird.sql new file mode 100644 index 0000000..50fc4af --- /dev/null +++ b/backend/src/db/migrations/0004_flippant_black_bird.sql @@ -0,0 +1,19 @@ +CREATE TABLE IF NOT EXISTS "task_group_assignment_table" ( + "id" serial PRIMARY KEY NOT NULL, + "task_group_id" integer NOT NULL, + "user_id" integer NOT NULL, + "created_at" timestamp DEFAULT now() NOT NULL +); +--> statement-breakpoint +ALTER TABLE "user_task_group" RENAME TO "task_group_user";--> statement-breakpoint +DO $$ BEGIN + ALTER TABLE "task_group_assignment_table" ADD CONSTRAINT "task_group_assignment_table_task_group_id_task_group_id_fk" FOREIGN KEY ("task_group_id") REFERENCES "public"."task_group"("id") ON DELETE no action ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; +--> statement-breakpoint +DO $$ BEGIN + ALTER TABLE "task_group_assignment_table" ADD CONSTRAINT "task_group_assignment_table_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action; +EXCEPTION + WHEN duplicate_object THEN null; +END $$; diff --git a/backend/src/db/migrations/meta/0004_snapshot.json b/backend/src/db/migrations/meta/0004_snapshot.json new file mode 100644 index 0000000..5170f78 --- /dev/null +++ b/backend/src/db/migrations/meta/0004_snapshot.json @@ -0,0 +1,361 @@ +{ + "id": "195761e6-c0c7-4afa-827e-dc8c0be2d9b3", + "prevId": "828bbbdd-14ff-4f0e-862a-97de9d3b154f", + "version": "6", + "dialect": "postgresql", + "tables": { + "public.assignment": { + "name": "assignment", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "task_id": { + "name": "task_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "state": { + "name": "state", + "type": "state", + "typeSchema": "public", + "primaryKey": false, + "notNull": false, + "default": "'pending'" + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "assignment_task_id_task_id_fk": { + "name": "assignment_task_id_task_id_fk", + "tableFrom": "assignment", + "tableTo": "task", + "columnsFrom": [ + "task_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "assignment_user_id_user_id_fk": { + "name": "assignment_user_id_user_id_fk", + "tableFrom": "assignment", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.task_group_assignment_table": { + "name": "task_group_assignment_table", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "task_group_id": { + "name": "task_group_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "task_group_assignment_table_task_group_id_task_group_id_fk": { + "name": "task_group_assignment_table_task_group_id_task_group_id_fk", + "tableFrom": "task_group_assignment_table", + "tableTo": "task_group", + "columnsFrom": [ + "task_group_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "task_group_assignment_table_user_id_user_id_fk": { + "name": "task_group_assignment_table_user_id_user_id_fk", + "tableFrom": "task_group_assignment_table", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.task_group": { + "name": "task_group", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "interval": { + "name": "interval", + "type": "interval", + "primaryKey": false, + "notNull": true + }, + "initial_start_date": { + "name": "initial_start_date", + "type": "timestamp", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.task_group_user": { + "name": "task_group_user", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "task_group_id": { + "name": "task_group_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "task_group_user_task_group_id_task_group_id_fk": { + "name": "task_group_user_task_group_id_task_group_id_fk", + "tableFrom": "task_group_user", + "tableTo": "task_group", + "columnsFrom": [ + "task_group_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "task_group_user_user_id_user_id_fk": { + "name": "task_group_user_user_id_user_id_fk", + "tableFrom": "task_group_user", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.task": { + "name": "task", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "task_group_id": { + "name": "task_group_id", + "type": "integer", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": { + "task_task_group_id_task_group_id_fk": { + "name": "task_task_group_id_task_group_id_fk", + "tableFrom": "task", + "tableTo": "task_group", + "columnsFrom": [ + "task_group_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.user": { + "name": "user", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "username": { + "name": "username", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "password": { + "name": "password", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + } + }, + "enums": { + "public.state": { + "name": "state", + "schema": "public", + "values": [ + "pending", + "completed" + ] + } + }, + "schemas": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/backend/src/db/migrations/meta/_journal.json b/backend/src/db/migrations/meta/_journal.json index 7ab7f8c..49a1acb 100644 --- a/backend/src/db/migrations/meta/_journal.json +++ b/backend/src/db/migrations/meta/_journal.json @@ -29,6 +29,13 @@ "when": 1716208018244, "tag": "0003_aspiring_the_hunter", "breakpoints": true + }, + { + "idx": 4, + "version": "6", + "when": 1716209187106, + "tag": "0004_flippant_black_bird", + "breakpoints": true } ] } \ No newline at end of file diff --git a/backend/src/db/schema.ts b/backend/src/db/schema.ts index d252229..7ed86b2 100644 --- a/backend/src/db/schema.ts +++ b/backend/src/db/schema.ts @@ -38,7 +38,18 @@ export const taskGroupTable = pgTable('task_group', { createdAt: timestamp('created_at').notNull().defaultNow(), }); -export const userTaskGroupTable = pgTable('user_task_group', { +export const taskGroupAssignmentTable = pgTable('task_group_assignment_table', { + id: serial('id').primaryKey(), + taskGroupId: integer('task_group_id') + .references(() => taskGroupTable.id) + .notNull(), + userId: integer('user_id') + .references(() => userTable.id) + .notNull(), + createdAt: timestamp('created_at').notNull().defaultNow(), +}); + +export const taskGroupUserTable = pgTable('task_group_user', { id: serial('id').primaryKey(), taskGroupId: integer('task_group_id') .references(() => taskGroupTable.id)