Skip to content

Commit

Permalink
♻️ refactor: update sql and types (#4979)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx authored Dec 11, 2024
1 parent 74fd0dc commit 8243f01
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/database/migrations/0006_add_knowledge_base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ CREATE TABLE IF NOT EXISTS "unstructured_chunks" (
"file_id" varchar
);
--> statement-breakpoint
ALTER TABLE "files_to_messages" RENAME TO "messages_files";
ALTER TABLE "files_to_messages" RENAME TO "messages_files";--> statement-breakpoint
DROP TABLE "files_to_agents";--> statement-breakpoint
ALTER TABLE "files" ADD COLUMN "file_hash" varchar(64);--> statement-breakpoint
ALTER TABLE "files" ADD COLUMN "chunk_task_id" uuid;--> statement-breakpoint
Expand Down
4 changes: 4 additions & 0 deletions src/database/migrations/0007_fix_embedding_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ CREATE TEMP TABLE embeddings_temp AS
SELECT DISTINCT ON (chunk_id) *
FROM embeddings
ORDER BY chunk_id, random();
--> statement-breakpoint

-- step 2: delete all rows from the original table
DELETE FROM embeddings;
--> statement-breakpoint

-- step 3: insert the rows we want to keep back into the original table
INSERT INTO embeddings
SELECT * FROM embeddings_temp;
--> statement-breakpoint

-- step 4: drop the temporary table
DROP TABLE embeddings_temp;
--> statement-breakpoint

-- step 5: now it's safe to add the unique constraint
ALTER TABLE "embeddings" ADD CONSTRAINT "embeddings_chunk_id_unique" UNIQUE("chunk_id");
2 changes: 1 addition & 1 deletion src/database/server/models/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Column, count, inArray, sql } from 'drizzle-orm';
import { and, desc, eq, exists, isNull, like, or } from 'drizzle-orm/expressions';

import { LobeChatDatabase } from '@/database/type';
import { idGenerator } from '@/database/utils/idGenerator';

import { NewMessage, TopicItem, messages, topics } from '../../schemas';
import { idGenerator } from '@/database/utils/idGenerator';

export interface CreateTopicParams {
favorite?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/services/import/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class ClientService {
}

if (sessionGroups.length > 0) {
const res = await SessionGroupModel.batchCreate(sessionGroups);
const res = await SessionGroupModel.batchCreate(sessionGroups as any);
sessionGroupResult = this.mapImportResult(res);
}

Expand Down
12 changes: 10 additions & 2 deletions src/types/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
MessageRoleType,
} from '@/types/message';
import { MetaData } from '@/types/meta';
import { SessionGroupId, SessionGroupItem } from '@/types/session';
import { SessionGroupId } from '@/types/session';
import { ChatTopic } from '@/types/topic';

interface ImportSession {
Expand Down Expand Up @@ -64,9 +64,17 @@ interface ImportMessage {
updatedAt: number;
}

interface ImportSessionGroup {
createdAt: number;
id: string;
name: string;
sort?: number | null;
updatedAt: number;
}

export interface ImporterEntryData {
messages?: ImportMessage[];
sessionGroups?: SessionGroupItem[];
sessionGroups?: ImportSessionGroup[];
sessions?: ImportSession[];
topics?: ChatTopic[];
version: number;
Expand Down

0 comments on commit 8243f01

Please sign in to comment.