From 226ff767d7ee95ba3567c1e5abfaa707996b5419 Mon Sep 17 00:00:00 2001 From: Hao Wu Date: Fri, 7 Apr 2023 15:36:57 +0800 Subject: [PATCH 1/4] soft delete msg --- api/sqlc/queries/chat_message.sql | 33 +++++++++------ api/sqlc/schema.sql | 9 +++- api/sqlc_queries/chat_message.sql.go | 62 ++++++++++++++++++---------- api/sqlc_queries/models.go | 1 + 4 files changed, 70 insertions(+), 35 deletions(-) diff --git a/api/sqlc/queries/chat_message.sql b/api/sqlc/queries/chat_message.sql index 97d14075..fa3d50a4 100644 --- a/api/sqlc/queries/chat_message.sql +++ b/api/sqlc/queries/chat_message.sql @@ -1,11 +1,13 @@ -- name: GetAllChatMessages :many -SELECT * FROM chat_message ORDER BY id; +SELECT * FROM chat_message +WHERE is_deleted = false +ORDER BY id; -- name: GetChatMessagesBySessionUUID :many SELECT cm.* FROM chat_message cm INNER JOIN chat_session cs ON cm.chat_session_uuid = cs.uuid -WHERE cs.active = true and cs.uuid = $1 +WHERE cm.is_deleted = false and cs.active = true and cs.uuid = $1 ORDER BY cm.id OFFSET $2 LIMIT $3; @@ -15,14 +17,15 @@ LIMIT $3; SELECT cm.* FROM chat_message cm INNER JOIN chat_session cs ON cm.chat_session_uuid = cs.uuid -WHERE cs.active = true and cs.uuid = $1 +WHERE cm.is_deleted = false and cs.active = true and cs.uuid = $1 ORDER BY cm.id OFFSET $2 LIMIT $1; -- name: GetChatMessageByID :one -SELECT * FROM chat_message WHERE id = $1; +SELECT * FROM chat_message +WHERE is_deleted = false and id = $1; -- name: CreateChatMessage :one @@ -36,12 +39,15 @@ WHERE id = $1 RETURNING *; -- name: DeleteChatMessage :exec -DELETE FROM chat_message WHERE id = $1; +UPDATE chat_message set is_deleted = true, updated_at = now() +WHERE id = $1; --- by uuid + +---- UUID ---- -- name: GetChatMessageByUUID :one -SELECT * FROM chat_message WHERE uuid = $1; +SELECT * FROM chat_message +WHERE is_deleted = false and uuid = $1; -- name: CreateChatMessageByUUID :one INSERT INTO chat_message (uuid, chat_session_uuid, role, content, score, user_id, created_by, updated_by, raw) @@ -54,7 +60,8 @@ WHERE uuid = $1 RETURNING *; -- name: DeleteChatMessageByUUID :exec -DELETE FROM chat_message WHERE uuid = $1; +UPDATE chat_message SET is_deleted = true, updated_at = now() +WHERE uuid = $1; -- name: HasChatMessagePermission :one @@ -62,7 +69,7 @@ SELECT COUNT(*) > 0 as has_permission FROM chat_message cm INNER JOIN chat_session cs ON cm.chat_session_uuid = cs.uuid INNER JOIN auth_user au ON cs.user_id = au.id -WHERE cm.id = $1 AND (cs.user_id = $2 OR au.is_superuser) and cs.active = true; +WHERE cm.is_deleted = false and cm.id = $1 AND (cs.user_id = $2 OR au.is_superuser) and cs.active = true; -- name: GetLatestMessagesBySessionUUID :many @@ -72,7 +79,7 @@ Where chat_message.id in ( SELECT chat_message.id FROM chat_message - WHERE chat_message.chat_session_uuid = $1 + WHERE chat_message.chat_session_uuid = $1 and chat_message.is_deleted = false ORDER BY created_at DESC LIMIT $2 ) @@ -82,7 +89,7 @@ ORDER BY created_at; -- name: GetFirstMessageBySessionUUID :one SELECT * FROM chat_message -WHERE chat_session_uuid = $1 +WHERE chat_session_uuid = $1 and is_deleted = false ORDER BY created_at LIMIT 1; @@ -94,6 +101,7 @@ WHERE chat_message.id in ( FROM chat_message cm WHERE cm.chat_session_uuid = $3 AND cm.id < (SELECT id FROM chat_message WHERE chat_message.uuid = $1) + AND cm.is_deleted = false ORDER BY cm.created_at DESC LIMIT $2 ) @@ -107,7 +115,8 @@ WHERE uuid = $1 ; -- name: DeleteChatMessagesBySesionUUID :exec -DELETE FROM chat_message +UPDATE chat_message +SET is_deleted = true, updated_at = now() WHERE chat_session_uuid = $1; diff --git a/api/sqlc/schema.sql b/api/sqlc/schema.sql index 11dd9750..43745e5a 100644 --- a/api/sqlc/schema.sql +++ b/api/sqlc/schema.sql @@ -51,8 +51,8 @@ CREATE TABLE IF NOT EXISTS chat_message ( updated_at timestamp DEFAULT now() Not NULL, created_by integer NOT NULL, updated_by integer NOT NULL, + is_deleted BOOLEAN NOT NULL DEFAULT false, raw jsonb default '{}' NOT NULL - ); -- alter table chat_message add column chat_session_uuid character varying(255) NOT NULL DEFAULT ''; @@ -94,8 +94,13 @@ CREATE TABLE IF NOT EXISTS user_active_chat_session ( -- ALTER TABLE chat_prompt RENAME COLUMN topic TO session_uuid; +-- chat_session ALTER TABLE chat_session ADD COLUMN IF NOT EXISTS temperature float DEFAULT 1.0 NOT NULL; ALTER TABLE chat_session ADD COLUMN IF NOT EXISTS top_p float DEFAULT 1.0 NOT NULL; ALTER TABLE chat_session ADD COLUMN IF NOT EXISTS max_tokens int DEFAULT 512 NOT NULL; ALTER TABLE chat_session ADD COLUMN IF NOT EXISTS debug boolean DEFAULT false NOT NULL; -ALTER TABlE chat_session ADD COLUMN IF NOT EXISTS model character varying(255) NOT NULL DEFAULT 'gpt-3.5-turbo'; \ No newline at end of file +ALTER TABlE chat_session ADD COLUMN IF NOT EXISTS model character varying(255) NOT NULL DEFAULT 'gpt-3.5-turbo'; + + +-- chat_messages +ALTER TABLE chat_message ADD COLUMN IF NOT EXISTS is_deleted BOOLEAN NOT NULL DEFAULT false; diff --git a/api/sqlc_queries/chat_message.sql.go b/api/sqlc_queries/chat_message.sql.go index c8835e7d..abaea5f9 100644 --- a/api/sqlc_queries/chat_message.sql.go +++ b/api/sqlc_queries/chat_message.sql.go @@ -13,7 +13,7 @@ import ( const createChatMessage = `-- name: CreateChatMessage :one INSERT INTO chat_message (chat_session_uuid, uuid, role, content, score, user_id, created_by, updated_by, raw) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) -RETURNING id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, raw +RETURNING id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, is_deleted, raw ` type CreateChatMessageParams struct { @@ -53,6 +53,7 @@ func (q *Queries) CreateChatMessage(ctx context.Context, arg CreateChatMessagePa &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, &i.Raw, ) return i, err @@ -61,7 +62,7 @@ func (q *Queries) CreateChatMessage(ctx context.Context, arg CreateChatMessagePa const createChatMessageByUUID = `-- name: CreateChatMessageByUUID :one INSERT INTO chat_message (uuid, chat_session_uuid, role, content, score, user_id, created_by, updated_by, raw) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) -RETURNING id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, raw +RETURNING id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, is_deleted, raw ` type CreateChatMessageByUUIDParams struct { @@ -101,13 +102,15 @@ func (q *Queries) CreateChatMessageByUUID(ctx context.Context, arg CreateChatMes &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, &i.Raw, ) return i, err } const deleteChatMessage = `-- name: DeleteChatMessage :exec -DELETE FROM chat_message WHERE id = $1 +UPDATE chat_message set is_deleted = true, updated_at = now() +WHERE id = $1 ` func (q *Queries) DeleteChatMessage(ctx context.Context, id int32) error { @@ -116,7 +119,8 @@ func (q *Queries) DeleteChatMessage(ctx context.Context, id int32) error { } const deleteChatMessageByUUID = `-- name: DeleteChatMessageByUUID :exec -DELETE FROM chat_message WHERE uuid = $1 +UPDATE chat_message SET is_deleted = true, updated_at = now() +WHERE uuid = $1 ` func (q *Queries) DeleteChatMessageByUUID(ctx context.Context, uuid string) error { @@ -125,7 +129,8 @@ func (q *Queries) DeleteChatMessageByUUID(ctx context.Context, uuid string) erro } const deleteChatMessagesBySesionUUID = `-- name: DeleteChatMessagesBySesionUUID :exec -DELETE FROM chat_message +UPDATE chat_message +SET is_deleted = true, updated_at = now() WHERE chat_session_uuid = $1 ` @@ -135,7 +140,9 @@ func (q *Queries) DeleteChatMessagesBySesionUUID(ctx context.Context, chatSessio } const getAllChatMessages = `-- name: GetAllChatMessages :many -SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, raw FROM chat_message ORDER BY id +SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, is_deleted, raw FROM chat_message +WHERE is_deleted = false +ORDER BY id ` func (q *Queries) GetAllChatMessages(ctx context.Context) ([]ChatMessage, error) { @@ -159,6 +166,7 @@ func (q *Queries) GetAllChatMessages(ctx context.Context) ([]ChatMessage, error) &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, &i.Raw, ); err != nil { return nil, err @@ -175,7 +183,8 @@ func (q *Queries) GetAllChatMessages(ctx context.Context) ([]ChatMessage, error) } const getChatMessageByID = `-- name: GetChatMessageByID :one -SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, raw FROM chat_message WHERE id = $1 +SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, is_deleted, raw FROM chat_message +WHERE is_deleted = false and id = $1 ` func (q *Queries) GetChatMessageByID(ctx context.Context, id int32) (ChatMessage, error) { @@ -193,16 +202,17 @@ func (q *Queries) GetChatMessageByID(ctx context.Context, id int32) (ChatMessage &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, &i.Raw, ) return i, err } const getChatMessageBySessionUUID = `-- name: GetChatMessageBySessionUUID :one -SELECT cm.id, cm.uuid, cm.chat_session_uuid, cm.role, cm.content, cm.score, cm.user_id, cm.created_at, cm.updated_at, cm.created_by, cm.updated_by, cm.raw +SELECT cm.id, cm.uuid, cm.chat_session_uuid, cm.role, cm.content, cm.score, cm.user_id, cm.created_at, cm.updated_at, cm.created_by, cm.updated_by, cm.is_deleted, cm.raw FROM chat_message cm INNER JOIN chat_session cs ON cm.chat_session_uuid = cs.uuid -WHERE cs.active = true and cs.uuid = $1 +WHERE cm.is_deleted = false and cs.active = true and cs.uuid = $1 ORDER BY cm.id OFFSET $2 LIMIT $1 @@ -228,6 +238,7 @@ func (q *Queries) GetChatMessageBySessionUUID(ctx context.Context, arg GetChatMe &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, &i.Raw, ) return i, err @@ -235,10 +246,11 @@ func (q *Queries) GetChatMessageBySessionUUID(ctx context.Context, arg GetChatMe const getChatMessageByUUID = `-- name: GetChatMessageByUUID :one -SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, raw FROM chat_message WHERE uuid = $1 +SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, is_deleted, raw FROM chat_message +WHERE is_deleted = false and uuid = $1 ` -// by uuid +// -- UUID ---- func (q *Queries) GetChatMessageByUUID(ctx context.Context, uuid string) (ChatMessage, error) { row := q.db.QueryRowContext(ctx, getChatMessageByUUID, uuid) var i ChatMessage @@ -254,16 +266,17 @@ func (q *Queries) GetChatMessageByUUID(ctx context.Context, uuid string) (ChatMe &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, &i.Raw, ) return i, err } const getChatMessagesBySessionUUID = `-- name: GetChatMessagesBySessionUUID :many -SELECT cm.id, cm.uuid, cm.chat_session_uuid, cm.role, cm.content, cm.score, cm.user_id, cm.created_at, cm.updated_at, cm.created_by, cm.updated_by, cm.raw +SELECT cm.id, cm.uuid, cm.chat_session_uuid, cm.role, cm.content, cm.score, cm.user_id, cm.created_at, cm.updated_at, cm.created_by, cm.updated_by, cm.is_deleted, cm.raw FROM chat_message cm INNER JOIN chat_session cs ON cm.chat_session_uuid = cs.uuid -WHERE cs.active = true and cs.uuid = $1 +WHERE cm.is_deleted = false and cs.active = true and cs.uuid = $1 ORDER BY cm.id OFFSET $2 LIMIT $3 @@ -296,6 +309,7 @@ func (q *Queries) GetChatMessagesBySessionUUID(ctx context.Context, arg GetChatM &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, &i.Raw, ); err != nil { return nil, err @@ -327,9 +341,9 @@ func (q *Queries) GetChatMessagesCount(ctx context.Context, userID int32) (int64 } const getFirstMessageBySessionUUID = `-- name: GetFirstMessageBySessionUUID :one -SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, raw +SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, is_deleted, raw FROM chat_message -WHERE chat_session_uuid = $1 +WHERE chat_session_uuid = $1 and is_deleted = false ORDER BY created_at LIMIT 1 ` @@ -349,19 +363,21 @@ func (q *Queries) GetFirstMessageBySessionUUID(ctx context.Context, chatSessionU &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, &i.Raw, ) return i, err } const getLastNChatMessages = `-- name: GetLastNChatMessages :many -SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, raw +SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, is_deleted, raw FROM chat_message WHERE chat_message.id in ( SELECT id FROM chat_message cm WHERE cm.chat_session_uuid = $3 AND cm.id < (SELECT id FROM chat_message WHERE chat_message.uuid = $1) + AND cm.is_deleted = false ORDER BY cm.created_at DESC LIMIT $2 ) @@ -395,6 +411,7 @@ func (q *Queries) GetLastNChatMessages(ctx context.Context, arg GetLastNChatMess &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, &i.Raw, ); err != nil { return nil, err @@ -411,13 +428,13 @@ func (q *Queries) GetLastNChatMessages(ctx context.Context, arg GetLastNChatMess } const getLatestMessagesBySessionUUID = `-- name: GetLatestMessagesBySessionUUID :many -SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, raw +SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, is_deleted, raw FROM chat_message Where chat_message.id in ( SELECT chat_message.id FROM chat_message - WHERE chat_message.chat_session_uuid = $1 + WHERE chat_message.chat_session_uuid = $1 and chat_message.is_deleted = false ORDER BY created_at DESC LIMIT $2 ) @@ -450,6 +467,7 @@ func (q *Queries) GetLatestMessagesBySessionUUID(ctx context.Context, arg GetLat &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, &i.Raw, ); err != nil { return nil, err @@ -470,7 +488,7 @@ SELECT COUNT(*) > 0 as has_permission FROM chat_message cm INNER JOIN chat_session cs ON cm.chat_session_uuid = cs.uuid INNER JOIN auth_user au ON cs.user_id = au.id -WHERE cm.id = $1 AND (cs.user_id = $2 OR au.is_superuser) and cs.active = true +WHERE cm.is_deleted = false and cm.id = $1 AND (cs.user_id = $2 OR au.is_superuser) and cs.active = true ` type HasChatMessagePermissionParams struct { @@ -488,7 +506,7 @@ func (q *Queries) HasChatMessagePermission(ctx context.Context, arg HasChatMessa const updateChatMessage = `-- name: UpdateChatMessage :one UPDATE chat_message SET role = $2, content = $3, score = $4, user_id = $5, updated_by = $6, updated_at = now() WHERE id = $1 -RETURNING id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, raw +RETURNING id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, is_deleted, raw ` type UpdateChatMessageParams struct { @@ -522,6 +540,7 @@ func (q *Queries) UpdateChatMessage(ctx context.Context, arg UpdateChatMessagePa &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, &i.Raw, ) return i, err @@ -530,7 +549,7 @@ func (q *Queries) UpdateChatMessage(ctx context.Context, arg UpdateChatMessagePa const updateChatMessageByUUID = `-- name: UpdateChatMessageByUUID :one UPDATE chat_message SET content = $2, updated_at = now() WHERE uuid = $1 -RETURNING id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, raw +RETURNING id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, is_deleted, raw ` type UpdateChatMessageByUUIDParams struct { @@ -553,6 +572,7 @@ func (q *Queries) UpdateChatMessageByUUID(ctx context.Context, arg UpdateChatMes &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, &i.Raw, ) return i, err diff --git a/api/sqlc_queries/models.go b/api/sqlc_queries/models.go index 9db81cbf..8cc7a2a9 100644 --- a/api/sqlc_queries/models.go +++ b/api/sqlc_queries/models.go @@ -43,6 +43,7 @@ type ChatMessage struct { UpdatedAt time.Time CreatedBy int32 UpdatedBy int32 + IsDeleted bool Raw json.RawMessage } From 8c8fd2c2622315fba7342fff23fbc2a795444480 Mon Sep 17 00:00:00 2001 From: Hao Wu Date: Fri, 7 Apr 2023 15:47:48 +0800 Subject: [PATCH 2/4] update --- e2e/lib/db/chat_message/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/lib/db/chat_message/index.ts b/e2e/lib/db/chat_message/index.ts index 31d94ba9..ba906823 100644 --- a/e2e/lib/db/chat_message/index.ts +++ b/e2e/lib/db/chat_message/index.ts @@ -1,6 +1,6 @@ export async function selectChatMessagesBySessionUUID(pool, sessionId: number) { const query = { - text: 'SELECT id, uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by FROM chat_message WHERE chat_session_uuid = $1 order by id', + text: 'SELECT id, uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by FROM chat_message WHERE chat_session_uuid = $1 and is_deleted = false order by id', values: [sessionId], }; From 8afd586812aa2307d02c0e99feb9544a8675837c Mon Sep 17 00:00:00 2001 From: Hao Wu Date: Fri, 7 Apr 2023 15:54:03 +0800 Subject: [PATCH 3/4] update --- api/sqlc/queries/chat_prompt.sql | 27 ++++++++++------ api/sqlc/schema.sql | 6 +++- api/sqlc_queries/chat_prompt.sql.go | 50 +++++++++++++++++++---------- api/sqlc_queries/models.go | 1 + 4 files changed, 56 insertions(+), 28 deletions(-) diff --git a/api/sqlc/queries/chat_prompt.sql b/api/sqlc/queries/chat_prompt.sql index 40d01010..dd784cce 100644 --- a/api/sqlc/queries/chat_prompt.sql +++ b/api/sqlc/queries/chat_prompt.sql @@ -1,8 +1,11 @@ -- name: GetAllChatPrompts :many -SELECT * FROM chat_prompt ORDER BY id; +SELECT * FROM chat_prompt +WHERE is_deleted = false +ORDER BY id; -- name: GetChatPromptByID :one -SELECT * FROM chat_prompt WHERE id = $1; +SELECT * FROM chat_prompt +WHERE is_deleted = false and id = $1; -- name: CreateChatPrompt :one INSERT INTO chat_prompt (uuid, chat_session_uuid, role, content, user_id, created_by, updated_by) @@ -16,35 +19,37 @@ RETURNING *; -- name: UpdateChatPromptByUUID :one UPDATE chat_prompt SET content = $2, updated_at = now() -WHERE uuid = $1 +WHERE uuid = $1 and is_deleted = false RETURNING *; -- name: DeleteChatPrompt :exec -DELETE FROM chat_prompt WHERE id = $1; +UPDATE chat_prompt +SET is_deleted = true, updated_at = now() +WHERE id = $1; -- name: GetChatPromptsByUserID :many SELECT * FROM chat_prompt -WHERE user_id = $1 +WHERE user_id = $1 and is_deleted = false ORDER BY id; -- name: GetChatPromptsBysession_uuid :many SELECT * FROM chat_prompt -WHERE chat_session_uuid = $1 +WHERE chat_session_uuid = $1 and is_deleted = false ORDER BY id; -- name: GetChatPromptsBySessionUUID :many SELECT * FROM chat_prompt -WHERE chat_session_uuid = $1 +WHERE chat_session_uuid = $1 and is_deleted = false ORDER BY id; -- name: GetOneChatPromptBySessionUUID :one SELECT * FROM chat_prompt -WHERE chat_session_uuid = $1 +WHERE chat_session_uuid = $1 and is_deleted = false ORDER BY id LIMIT 1; @@ -55,8 +60,10 @@ LIMIT 1; SELECT COUNT(*) > 0 as has_permission FROM chat_prompt cp INNER JOIN auth_user au ON cp.user_id = au.id -WHERE cp.id = $1 AND (cp.user_id = $2 OR au.is_superuser); +WHERE cp.id = $1 AND (cp.user_id = $2 OR au.is_superuser) AND cp.is_deleted = false; -- name: DeleteChatPromptByUUID :exec -DELETE FROM chat_prompt WHERE uuid = $1; \ No newline at end of file +UPDATE chat_prompt +SET is_deleted = true, updated_at = now() +WHERE uuid = $1; \ No newline at end of file diff --git a/api/sqlc/schema.sql b/api/sqlc/schema.sql index 43745e5a..8e700aff 100644 --- a/api/sqlc/schema.sql +++ b/api/sqlc/schema.sql @@ -68,7 +68,8 @@ CREATE TABLE IF NOT EXISTS chat_prompt ( created_at timestamp DEFAULT now() NOT NULL , updated_at timestamp DEFAULT now() NOT NULL, created_by integer NOT NULL, - updated_by integer NOT NULL + updated_by integer NOT NULL, + is_deleted BOOLEAN NOT NULL DEFAULT false -- raw jsonb default '{}' NOT NULL ); @@ -104,3 +105,6 @@ ALTER TABlE chat_session ADD COLUMN IF NOT EXISTS model character varying(255) -- chat_messages ALTER TABLE chat_message ADD COLUMN IF NOT EXISTS is_deleted BOOLEAN NOT NULL DEFAULT false; + +-- chat prompt +ALTER TABLE chat_prompt ADD COLUMN IF NOT EXISTS is_deleted BOOLEAN NOT NULL DEFAULT false; diff --git a/api/sqlc_queries/chat_prompt.sql.go b/api/sqlc_queries/chat_prompt.sql.go index 65db4161..c778702f 100644 --- a/api/sqlc_queries/chat_prompt.sql.go +++ b/api/sqlc_queries/chat_prompt.sql.go @@ -12,7 +12,7 @@ import ( const createChatPrompt = `-- name: CreateChatPrompt :one INSERT INTO chat_prompt (uuid, chat_session_uuid, role, content, user_id, created_by, updated_by) VALUES ($1, $2, $3, $4, $5, $6, $7) -RETURNING id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by +RETURNING id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, is_deleted ` type CreateChatPromptParams struct { @@ -48,12 +48,15 @@ func (q *Queries) CreateChatPrompt(ctx context.Context, arg CreateChatPromptPara &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, ) return i, err } const deleteChatPrompt = `-- name: DeleteChatPrompt :exec -DELETE FROM chat_prompt WHERE id = $1 +UPDATE chat_prompt +SET is_deleted = true, updated_at = now() +WHERE id = $1 ` func (q *Queries) DeleteChatPrompt(ctx context.Context, id int32) error { @@ -62,7 +65,9 @@ func (q *Queries) DeleteChatPrompt(ctx context.Context, id int32) error { } const deleteChatPromptByUUID = `-- name: DeleteChatPromptByUUID :exec -DELETE FROM chat_prompt WHERE uuid = $1 +UPDATE chat_prompt +SET is_deleted = true, updated_at = now() +WHERE uuid = $1 ` func (q *Queries) DeleteChatPromptByUUID(ctx context.Context, uuid string) error { @@ -71,7 +76,9 @@ func (q *Queries) DeleteChatPromptByUUID(ctx context.Context, uuid string) error } const getAllChatPrompts = `-- name: GetAllChatPrompts :many -SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by FROM chat_prompt ORDER BY id +SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, is_deleted FROM chat_prompt +WHERE is_deleted = false +ORDER BY id ` func (q *Queries) GetAllChatPrompts(ctx context.Context) ([]ChatPrompt, error) { @@ -95,6 +102,7 @@ func (q *Queries) GetAllChatPrompts(ctx context.Context) ([]ChatPrompt, error) { &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, ); err != nil { return nil, err } @@ -110,7 +118,8 @@ func (q *Queries) GetAllChatPrompts(ctx context.Context) ([]ChatPrompt, error) { } const getChatPromptByID = `-- name: GetChatPromptByID :one -SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by FROM chat_prompt WHERE id = $1 +SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, is_deleted FROM chat_prompt +WHERE is_deleted = false and id = $1 ` func (q *Queries) GetChatPromptByID(ctx context.Context, id int32) (ChatPrompt, error) { @@ -128,14 +137,15 @@ func (q *Queries) GetChatPromptByID(ctx context.Context, id int32) (ChatPrompt, &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, ) return i, err } const getChatPromptsBySessionUUID = `-- name: GetChatPromptsBySessionUUID :many -SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by +SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, is_deleted FROM chat_prompt -WHERE chat_session_uuid = $1 +WHERE chat_session_uuid = $1 and is_deleted = false ORDER BY id ` @@ -160,6 +170,7 @@ func (q *Queries) GetChatPromptsBySessionUUID(ctx context.Context, chatSessionUu &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, ); err != nil { return nil, err } @@ -175,9 +186,9 @@ func (q *Queries) GetChatPromptsBySessionUUID(ctx context.Context, chatSessionUu } const getChatPromptsByUserID = `-- name: GetChatPromptsByUserID :many -SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by +SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, is_deleted FROM chat_prompt -WHERE user_id = $1 +WHERE user_id = $1 and is_deleted = false ORDER BY id ` @@ -202,6 +213,7 @@ func (q *Queries) GetChatPromptsByUserID(ctx context.Context, userID int32) ([]C &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, ); err != nil { return nil, err } @@ -217,9 +229,9 @@ func (q *Queries) GetChatPromptsByUserID(ctx context.Context, userID int32) ([]C } const getChatPromptsBysession_uuid = `-- name: GetChatPromptsBysession_uuid :many -SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by +SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, is_deleted FROM chat_prompt -WHERE chat_session_uuid = $1 +WHERE chat_session_uuid = $1 and is_deleted = false ORDER BY id ` @@ -244,6 +256,7 @@ func (q *Queries) GetChatPromptsBysession_uuid(ctx context.Context, chatSessionU &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, ); err != nil { return nil, err } @@ -259,9 +272,9 @@ func (q *Queries) GetChatPromptsBysession_uuid(ctx context.Context, chatSessionU } const getOneChatPromptBySessionUUID = `-- name: GetOneChatPromptBySessionUUID :one -SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by +SELECT id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, is_deleted FROM chat_prompt -WHERE chat_session_uuid = $1 +WHERE chat_session_uuid = $1 and is_deleted = false ORDER BY id LIMIT 1 ` @@ -281,6 +294,7 @@ func (q *Queries) GetOneChatPromptBySessionUUID(ctx context.Context, chatSession &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, ) return i, err } @@ -289,7 +303,7 @@ const hasChatPromptPermission = `-- name: HasChatPromptPermission :one SELECT COUNT(*) > 0 as has_permission FROM chat_prompt cp INNER JOIN auth_user au ON cp.user_id = au.id -WHERE cp.id = $1 AND (cp.user_id = $2 OR au.is_superuser) +WHERE cp.id = $1 AND (cp.user_id = $2 OR au.is_superuser) AND cp.is_deleted = false ` type HasChatPromptPermissionParams struct { @@ -307,7 +321,7 @@ func (q *Queries) HasChatPromptPermission(ctx context.Context, arg HasChatPrompt const updateChatPrompt = `-- name: UpdateChatPrompt :one UPDATE chat_prompt SET chat_session_uuid = $2, role = $3, content = $4, score = $5, user_id = $6, updated_at = now(), updated_by = $7 WHERE id = $1 -RETURNING id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by +RETURNING id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, is_deleted ` type UpdateChatPromptParams struct { @@ -343,14 +357,15 @@ func (q *Queries) UpdateChatPrompt(ctx context.Context, arg UpdateChatPromptPara &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, ) return i, err } const updateChatPromptByUUID = `-- name: UpdateChatPromptByUUID :one UPDATE chat_prompt SET content = $2, updated_at = now() -WHERE uuid = $1 -RETURNING id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by +WHERE uuid = $1 and is_deleted = false +RETURNING id, uuid, chat_session_uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by, is_deleted ` type UpdateChatPromptByUUIDParams struct { @@ -373,6 +388,7 @@ func (q *Queries) UpdateChatPromptByUUID(ctx context.Context, arg UpdateChatProm &i.UpdatedAt, &i.CreatedBy, &i.UpdatedBy, + &i.IsDeleted, ) return i, err } diff --git a/api/sqlc_queries/models.go b/api/sqlc_queries/models.go index 8cc7a2a9..d73c7b3c 100644 --- a/api/sqlc_queries/models.go +++ b/api/sqlc_queries/models.go @@ -59,6 +59,7 @@ type ChatPrompt struct { UpdatedAt time.Time CreatedBy int32 UpdatedBy int32 + IsDeleted bool } type ChatSession struct { From 10d39850dbf9ac2f5ed465613e6ae3200bbadd3b Mon Sep 17 00:00:00 2001 From: Hao Wu Date: Fri, 7 Apr 2023 15:54:37 +0800 Subject: [PATCH 4/4] update --- e2e/lib/db/chat_prompt/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/lib/db/chat_prompt/index.ts b/e2e/lib/db/chat_prompt/index.ts index 71cddbeb..00283b18 100644 --- a/e2e/lib/db/chat_prompt/index.ts +++ b/e2e/lib/db/chat_prompt/index.ts @@ -1,6 +1,6 @@ export async function selectChatPromptsBySessionUUID(pool, sessionUUID: string) { const query = { - text: 'SELECT id, uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by FROM chat_prompt WHERE chat_session_uuid = $1 order by id', + text: 'SELECT id, uuid, role, content, score, user_id, created_at, updated_at, created_by, updated_by FROM chat_prompt WHERE chat_session_uuid = $1 and is_deleted = false order by id', values: [sessionUUID], };