Skip to content

Commit

Permalink
Fix#1947 (#1960)
Browse files Browse the repository at this point in the history
* Fixes #1947

* Lint fix

* fallback from timestamp with timezone to timestamp

---------

Co-authored-by: Emrah TOY <[email protected]>
  • Loading branch information
emrahtoy and Emrah TOY authored Mar 19, 2024
1 parent ac02cde commit ec1bbc8
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"postpack": "shx rm -f oclif.manifest.json",
"prepack": "pnpm build && oclif manifest && oclif readme",
"typeorm": "typeorm-ts-node-commonjs",
"typeorm:migration-generate": "pnpm typeorm migration:generate -d ./src/utils/typeormDataSource.ts",
"watch": "tsc --watch",
"version": "oclif readme && git add README.md"
},
Expand Down
4 changes: 3 additions & 1 deletion packages/server/src/database/entities/Assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ export class Assistant implements IAssistant {
@Column({ type: 'text' })
details: string

@Column()
@Column({ type: 'uuid'})
credential: string

@Column({ nullable: true })
iconSrc?: string

@Column({ type: 'timestamp' })
@CreateDateColumn()
createdDate: Date

@Column({ type: 'timestamp' })
@UpdateDateColumn()
updatedDate: Date
}
2 changes: 2 additions & 0 deletions packages/server/src/database/entities/ChatFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ export class ChatFlow implements IChatFlow {
@Column({ nullable: true, type: 'text' })
speechToText?: string

@Column({type:'timestamp'})
@CreateDateColumn()
createdDate: Date

@Column({type:'timestamp'})
@UpdateDateColumn()
updatedDate: Date

Expand Down
7 changes: 4 additions & 3 deletions packages/server/src/database/entities/ChatMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class ChatMessage implements IChatMessage {
role: MessageType

@Index()
@Column()
@Column({ type: 'uuid' })
chatflowid: string

@Column({ type: 'text' })
Expand All @@ -32,15 +32,16 @@ export class ChatMessage implements IChatMessage {
@Column()
chatType: string

@Column()
@Column({ type: 'uuid' })
chatId: string

@Column({ nullable: true })
memoryType?: string

@Column({ nullable: true })
@Column({ type: 'uuid', nullable: true })
sessionId?: string

@Column({type:'timestamp'})
@CreateDateColumn()
createdDate: Date
}
7 changes: 4 additions & 3 deletions packages/server/src/database/entities/ChatMessageFeedback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ export class ChatMessageFeedback implements IChatMessageFeedback {
id: string

@Index()
@Column()
@Column({ type: 'uuid' })
chatflowid: string

@Index()
@Column()
@Column({ type: 'uuid' })
chatId: string

@Column()
@Column({ type: 'uuid' })
messageId: string

@Column({ nullable: true })
Expand All @@ -25,6 +25,7 @@ export class ChatMessageFeedback implements IChatMessageFeedback {
@Column({ nullable: true, type: 'text' })
content?: string

@Column({type:'timestamp'})
@CreateDateColumn()
createdDate: Date
}
2 changes: 2 additions & 0 deletions packages/server/src/database/entities/Credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export class Credential implements ICredential {
@Column({ type: 'text' })
encryptedData: string

@Column({type:'timestamp'})
@CreateDateColumn()
createdDate: Date

@Column({type:'timestamp'})
@UpdateDateColumn()
updatedDate: Date
}
2 changes: 2 additions & 0 deletions packages/server/src/database/entities/Tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ export class Tool implements ITool {
@Column({ nullable: true, type: 'text' })
func?: string

@Column({type:'timestamp'})
@CreateDateColumn()
createdDate: Date

@Column({type:'timestamp'})
@UpdateDateColumn()
updatedDate: Date
}
2 changes: 2 additions & 0 deletions packages/server/src/database/entities/Variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export class Variable implements IVariable {
@Column({ default: 'string', type: 'text' })
type: string

@Column({type:'timestamp'})
@CreateDateColumn()
createdDate: Date

@Column({type:'timestamp'})
@UpdateDateColumn()
updatedDate: Date
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { MigrationInterface, QueryRunner } from 'typeorm'

export class FieldTypes1710497452584 implements MigrationInterface {
name = 'FieldTypes1710497452584'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "chat_message" ALTER COLUMN "chatflowid" type uuid USING "chatflowid"::uuid`)
await queryRunner.query(`ALTER TABLE "chat_message" ALTER COLUMN "chatId" type uuid USING "chatId"::uuid`)
await queryRunner.query(`ALTER TABLE "chat_message" ALTER COLUMN "sessionId" type uuid USING "sessionId"::uuid`)

await queryRunner.query(`ALTER TABLE "assistant" ALTER COLUMN "credential" type uuid USING "credential"::uuid`)

await queryRunner.query(`ALTER TABLE "chat_message_feedback" ALTER COLUMN "chatflowid" type uuid USING "chatflowid"::uuid`)
await queryRunner.query(`ALTER TABLE "chat_message_feedback" ALTER COLUMN "chatId" type uuid USING "chatId"::uuid`)
await queryRunner.query(`ALTER TABLE "chat_message_feedback" ALTER COLUMN "messageId" type uuid USING "messageId"::uuid`)

await queryRunner.query(`ALTER TABLE "chat_message_feedback" ADD CONSTRAINT "UQ_6352078b5a294f2d22179ea7955" UNIQUE ("messageId")`)

await queryRunner.query(`CREATE INDEX IF NOT EXISTS "IDX_f56c36fe42894d57e5c664d229" ON "chat_message" ("chatflowid") `)
await queryRunner.query(`CREATE INDEX IF NOT EXISTS "IDX_f56c36fe42894d57e5c664d230" ON "chat_message_feedback" ("chatflowid") `)
await queryRunner.query(`CREATE INDEX IF NOT EXISTS "IDX_9acddcb7a2b51fe37669049fc6" ON "chat_message_feedback" ("chatId") `)
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX "public"."IDX_9acddcb7a2b51fe37669049fc6"`)
await queryRunner.query(`DROP INDEX "public"."IDX_f56c36fe42894d57e5c664d229"`)
await queryRunner.query(`DROP INDEX "public"."IDX_f56c36fe42894d57e5c664d230"`)

await queryRunner.query(`ALTER TABLE "chat_message_feedback" DROP CONSTRAINT "UQ_6352078b5a294f2d22179ea7955"`)

await queryRunner.query(`ALTER TABLE "chat_message" ALTER COLUMN "chatflowid" type varchar USING "chatflowid"::varchar`)
await queryRunner.query(`ALTER TABLE "chat_message" ALTER COLUMN "chatId" type varchar USING "chatId"::varchar`)
await queryRunner.query(`ALTER TABLE "chat_message" ALTER COLUMN "sessionId" type varchar USING "sessionId"::varchar`)

await queryRunner.query(`ALTER TABLE "assistant" ALTER COLUMN "credential" type varchar USING "credential"::varchar`)

await queryRunner.query(`ALTER TABLE "chat_message_feedback" ALTER COLUMN "chatflowid" type varchar USING "chatflowid"::varchar`)
await queryRunner.query(`ALTER TABLE "chat_message_feedback" ALTER COLUMN "chatId" type varchar USING "chatId"::varchar`)
await queryRunner.query(`ALTER TABLE "chat_message_feedback" ALTER COLUMN "messageId" type varchar USING "messageId"::varchar`)
}
}
4 changes: 3 additions & 1 deletion packages/server/src/database/migrations/postgres/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { AddFileUploadsToChatMessage1701788586491 } from './1701788586491-AddFil
import { AddVariableEntity1699325775451 } from './1702200925471-AddVariableEntity'
import { AddSpeechToText1706364937060 } from './1706364937060-AddSpeechToText'
import { AddFeedback1707213601923 } from './1707213601923-AddFeedback'
import { FieldTypes1710497452584 } from './1710497452584-FieldTypes'

export const postgresMigrations = [
Init1693891895163,
Expand All @@ -31,5 +32,6 @@ export const postgresMigrations = [
AddFileUploadsToChatMessage1701788586491,
AddVariableEntity1699325775451,
AddSpeechToText1706364937060,
AddFeedback1707213601923
AddFeedback1707213601923,
FieldTypes1710497452584
]
8 changes: 7 additions & 1 deletion packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1765,6 +1765,12 @@ export class App {
return date
}

const aMonthAgo = () => {
const date = new Date()
date.setMonth(new Date().getMonth() - 1)
return date
}

let fromDate
if (startDate) fromDate = setDateToStartOrEndOfDay(startDate, 'start')

Expand Down Expand Up @@ -1795,7 +1801,7 @@ export class App {

// set date range
query.andWhere('chat_message.createdDate BETWEEN :fromDate AND :toDate', {
fromDate: fromDate ?? new Date().setMonth(new Date().getMonth() - 1),
fromDate: fromDate ?? aMonthAgo(),
toDate: toDate ?? new Date()
})
// sort
Expand Down
4 changes: 4 additions & 0 deletions packages/server/src/utils/typeormDataSource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { DataSource } from 'typeorm'
import { getDataSource } from '../DataSource'

export const dataSource: DataSource = getDataSource()

0 comments on commit ec1bbc8

Please sign in to comment.