mirror of
https://github.com/lobehub/lobehub.git
synced 2026-03-26 13:19:34 +07:00
👷 build: improve document db schema (#10892)
* add document db schema update * update migration sql * update migration sql
This commit is contained in:
@@ -270,6 +270,7 @@ table chat_groups_agents {
|
||||
table documents {
|
||||
id varchar(255) [pk, not null]
|
||||
title text
|
||||
description text
|
||||
content text
|
||||
file_type varchar(255) [not null]
|
||||
filename text
|
||||
@@ -280,6 +281,7 @@ table documents {
|
||||
source_type text [not null]
|
||||
source text [not null]
|
||||
file_id text
|
||||
knowledge_base_id text
|
||||
parent_id varchar(255)
|
||||
user_id text [not null]
|
||||
client_id text
|
||||
@@ -296,6 +298,7 @@ table documents {
|
||||
user_id [name: 'documents_user_id_idx']
|
||||
file_id [name: 'documents_file_id_idx']
|
||||
parent_id [name: 'documents_parent_id_idx']
|
||||
knowledge_base_id [name: 'documents_knowledge_base_id_idx']
|
||||
(client_id, user_id) [name: 'documents_client_id_user_id_unique', unique]
|
||||
(slug, user_id) [name: 'documents_slug_user_id_unique', unique]
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
ALTER TABLE "documents" ADD COLUMN IF NOT EXISTS "description" text;--> statement-breakpoint
|
||||
ALTER TABLE "documents" ADD COLUMN IF NOT EXISTS "knowledge_base_id" text;--> statement-breakpoint
|
||||
DO $$ BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'documents_knowledge_base_id_knowledge_bases_id_fk') THEN
|
||||
ALTER TABLE "documents" ADD CONSTRAINT "documents_knowledge_base_id_knowledge_bases_id_fk" FOREIGN KEY ("knowledge_base_id") REFERENCES "public"."knowledge_bases"("id") ON DELETE set null ON UPDATE no action;
|
||||
END IF;
|
||||
END $$;--> statement-breakpoint
|
||||
CREATE INDEX IF NOT EXISTS "documents_knowledge_base_id_idx" ON "documents" USING btree ("knowledge_base_id");
|
||||
9962
packages/database/migrations/meta/0066_snapshot.json
Normal file
9962
packages/database/migrations/meta/0066_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -462,7 +462,14 @@
|
||||
"when": 1766408202688,
|
||||
"tag": "0065_add_passkey",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 66,
|
||||
"version": "7",
|
||||
"when": 1766474494249,
|
||||
"tag": "0066_add_document_fields",
|
||||
"breakpoints": true
|
||||
}
|
||||
],
|
||||
"version": "6"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,7 +223,10 @@
|
||||
"hash": "9646161fa041354714f823d726af27247bcd6e60fa3be5698c0d69f337a5700b"
|
||||
},
|
||||
{
|
||||
"sql": ["DROP TABLE \"user_budgets\";", "\nDROP TABLE \"user_subscriptions\";"],
|
||||
"sql": [
|
||||
"DROP TABLE \"user_budgets\";",
|
||||
"\nDROP TABLE \"user_subscriptions\";"
|
||||
],
|
||||
"bps": true,
|
||||
"folderMillis": 1729699958471,
|
||||
"hash": "7dad43a2a25d1aec82124a4e53f8d82f8505c3073f23606c1dc5d2a4598eacf9"
|
||||
@@ -295,7 +298,9 @@
|
||||
"hash": "845a692ceabbfc3caf252a97d3e19a213bc0c433df2689900135f9cfded2cf49"
|
||||
},
|
||||
{
|
||||
"sql": ["ALTER TABLE \"messages\" ADD COLUMN \"reasoning\" jsonb;"],
|
||||
"sql": [
|
||||
"ALTER TABLE \"messages\" ADD COLUMN \"reasoning\" jsonb;"
|
||||
],
|
||||
"bps": true,
|
||||
"folderMillis": 1737609172353,
|
||||
"hash": "2cb36ae4fcdd7b7064767e04bfbb36ae34518ff4bb1b39006f2dd394d1893868"
|
||||
@@ -510,7 +515,9 @@
|
||||
"hash": "a7ccf007fd185ff922823148d1eae6fafe652fc98d2fd2793f84a84f29e93cd1"
|
||||
},
|
||||
{
|
||||
"sql": ["ALTER TABLE \"ai_providers\" ADD COLUMN \"config\" jsonb;"],
|
||||
"sql": [
|
||||
"ALTER TABLE \"ai_providers\" ADD COLUMN \"config\" jsonb;"
|
||||
],
|
||||
"bps": true,
|
||||
"folderMillis": 1749309388370,
|
||||
"hash": "39cea379f08ee4cb944875c0b67f7791387b508c2d47958bb4cd501ed1ef33eb"
|
||||
@@ -628,7 +635,9 @@
|
||||
"hash": "1ba9b1f74ea13348da98d6fcdad7867ab4316ed565bf75d84d160c526cdac14b"
|
||||
},
|
||||
{
|
||||
"sql": ["ALTER TABLE \"agents\" ADD COLUMN IF NOT EXISTS \"virtual\" boolean DEFAULT false;"],
|
||||
"sql": [
|
||||
"ALTER TABLE \"agents\" ADD COLUMN IF NOT EXISTS \"virtual\" boolean DEFAULT false;"
|
||||
],
|
||||
"bps": true,
|
||||
"folderMillis": 1759116400580,
|
||||
"hash": "433ddae88e785f2db734e49a4c115eee93e60afe389f7919d66e5ba9aa159a37"
|
||||
@@ -678,13 +687,17 @@
|
||||
"hash": "4bdc6505797d7a33b622498c138cfd47f637239f6905e1c484cd01d9d5f21d6b"
|
||||
},
|
||||
{
|
||||
"sql": ["ALTER TABLE \"user_settings\" ADD COLUMN IF NOT EXISTS \"image\" jsonb;"],
|
||||
"sql": [
|
||||
"ALTER TABLE \"user_settings\" ADD COLUMN IF NOT EXISTS \"image\" jsonb;"
|
||||
],
|
||||
"bps": true,
|
||||
"folderMillis": 1760108430562,
|
||||
"hash": "ce09b301abb80f6563abc2f526bdd20b4f69bae430f09ba2179b9e3bfec43067"
|
||||
},
|
||||
{
|
||||
"sql": ["ALTER TABLE \"documents\" ADD COLUMN IF NOT EXISTS \"editor_data\" jsonb;"],
|
||||
"sql": [
|
||||
"ALTER TABLE \"documents\" ADD COLUMN IF NOT EXISTS \"editor_data\" jsonb;"
|
||||
],
|
||||
"bps": true,
|
||||
"folderMillis": 1761554153406,
|
||||
"hash": "bf2f21293e90e11cf60a784cf3ec219eafa95f7545d7d2f9d1449c0b0949599a"
|
||||
@@ -764,13 +777,17 @@
|
||||
"hash": "923ccbdf46c32be9a981dabd348e6923b4a365444241e9b8cc174bf5b914cbc5"
|
||||
},
|
||||
{
|
||||
"sql": ["ALTER TABLE \"agents\" ADD COLUMN IF NOT EXISTS \"market_identifier\" text;\n"],
|
||||
"sql": [
|
||||
"ALTER TABLE \"agents\" ADD COLUMN IF NOT EXISTS \"market_identifier\" text;\n"
|
||||
],
|
||||
"bps": true,
|
||||
"folderMillis": 1762870034882,
|
||||
"hash": "4178aacb4b8892b7fd15d29209bbf9b1d1f9d7c406ba796f27542c0bcd919680"
|
||||
},
|
||||
{
|
||||
"sql": ["ALTER TABLE \"message_plugins\" ADD COLUMN IF NOT EXISTS \"intervention\" jsonb;\n"],
|
||||
"sql": [
|
||||
"ALTER TABLE \"message_plugins\" ADD COLUMN IF NOT EXISTS \"intervention\" jsonb;\n"
|
||||
],
|
||||
"bps": true,
|
||||
"folderMillis": 1762911968658,
|
||||
"hash": "552a032cc0e595277232e70b5f9338658585bafe9481ae8346a5f322b673a68b"
|
||||
@@ -799,7 +816,9 @@
|
||||
"hash": "f823b521f4d25e5dc5ab238b372727d2d2d7f0aed27b5eabc8a9608ce4e50568"
|
||||
},
|
||||
{
|
||||
"sql": ["ALTER TABLE \"agents\" ADD COLUMN IF NOT EXISTS \"editor_data\" jsonb;"],
|
||||
"sql": [
|
||||
"ALTER TABLE \"agents\" ADD COLUMN IF NOT EXISTS \"editor_data\" jsonb;"
|
||||
],
|
||||
"bps": true,
|
||||
"folderMillis": 1764215503726,
|
||||
"hash": "4188893a9083b3c7baebdbad0dd3f9d9400ede7584ca2394f5c64305dc9ec7b0"
|
||||
@@ -840,7 +859,9 @@
|
||||
"hash": "2c103eee82bdf329944fb622dd9c2b9f20df80eb54f23eb9254d2285de413099"
|
||||
},
|
||||
{
|
||||
"sql": ["ALTER TABLE \"user_settings\" ADD COLUMN IF NOT EXISTS \"market\" jsonb;"],
|
||||
"sql": [
|
||||
"ALTER TABLE \"user_settings\" ADD COLUMN IF NOT EXISTS \"market\" jsonb;"
|
||||
],
|
||||
"bps": true,
|
||||
"folderMillis": 1764335703306,
|
||||
"hash": "28c0d738c0b1fdf5fd871363be1a1477b4accbabdc140fe8dc6e9b339aae2c89"
|
||||
@@ -1037,12 +1058,23 @@
|
||||
{
|
||||
"sql": [
|
||||
"CREATE TABLE IF NOT EXISTS \"passkey\" (\n\t\"aaguid\" text,\n\t\"backedUp\" boolean,\n\t\"counter\" integer,\n\t\"createdAt\" timestamp DEFAULT now(),\n\t\"credentialID\" text NOT NULL,\n\t\"deviceType\" text,\n\t\"id\" text PRIMARY KEY NOT NULL,\n\t\"name\" text,\n\t\"publicKey\" text NOT NULL,\n\t\"transports\" text,\n\t\"userId\" text NOT NULL\n);\n",
|
||||
"\nALTER TABLE \"passkey\" ADD CONSTRAINT \"passkey_userId_users_id_fk\" FOREIGN KEY (\"userId\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;",
|
||||
"\nCREATE UNIQUE INDEX IF NOT EXISTS \"passkey_credential_id_unique\" ON \"passkey\" USING btree (\"credentialID\");",
|
||||
"\nDO $$ BEGIN\n IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'passkey_userId_users_id_fk') THEN\n ALTER TABLE \"passkey\" ADD CONSTRAINT \"passkey_userId_users_id_fk\" FOREIGN KEY (\"userId\") REFERENCES \"public\".\"users\"(\"id\") ON DELETE cascade ON UPDATE no action;\n END IF;\nEND $$;\n",
|
||||
"\t\nCREATE UNIQUE INDEX IF NOT EXISTS \"passkey_credential_id_unique\" ON \"passkey\" USING btree (\"credentialID\");",
|
||||
"\nCREATE INDEX IF NOT EXISTS \"passkey_user_id_idx\" ON \"passkey\" USING btree (\"userId\");"
|
||||
],
|
||||
"bps": true,
|
||||
"folderMillis": 1766408202688,
|
||||
"hash": "6ca80d3a8c326e30e2a778515ff80e801726894e5b57da29c00f81e1983e4172"
|
||||
"hash": "1ae24d25f4b0ee7f38353b6d7fc6be4ae1171c105eb329d8aa97888abc85373b"
|
||||
},
|
||||
{
|
||||
"sql": [
|
||||
"ALTER TABLE \"documents\" ADD COLUMN IF NOT EXISTS \"description\" text;",
|
||||
"\nALTER TABLE \"documents\" ADD COLUMN IF NOT EXISTS \"knowledge_base_id\" text;",
|
||||
"\nDO $$ BEGIN\n IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'documents_knowledge_base_id_knowledge_bases_id_fk') THEN\n ALTER TABLE \"documents\" ADD CONSTRAINT \"documents_knowledge_base_id_knowledge_bases_id_fk\" FOREIGN KEY (\"knowledge_base_id\") REFERENCES \"public\".\"knowledge_bases\"(\"id\") ON DELETE set null ON UPDATE no action;\n END IF;\nEND $$;",
|
||||
"\nCREATE INDEX IF NOT EXISTS \"documents_knowledge_base_id_idx\" ON \"documents\" USING btree (\"knowledge_base_id\");\n"
|
||||
],
|
||||
"bps": true,
|
||||
"folderMillis": 1766474494249,
|
||||
"hash": "42d46c25f68aa3d6ab46b7c15734d984a9a8452e8058e98de79f4d675a38e28f"
|
||||
}
|
||||
]
|
||||
]
|
||||
@@ -18,7 +18,7 @@ import { createInsertSchema } from 'drizzle-zod';
|
||||
import { LobeDocumentPage } from '@/types/document';
|
||||
import { FileSource } from '@/types/files';
|
||||
|
||||
import { idGenerator } from '../utils/idGenerator';
|
||||
import { idGenerator, randomSlug } from '../utils/idGenerator';
|
||||
import { accessedAt, createdAt, timestamps } from './_helpers';
|
||||
import { asyncTasks } from './asyncTask';
|
||||
import { users } from './user';
|
||||
@@ -55,6 +55,7 @@ export const documents = pgTable(
|
||||
|
||||
// Basic information
|
||||
title: text('title'),
|
||||
description: text('description'),
|
||||
content: text('content'),
|
||||
|
||||
// Special type: custom/folder
|
||||
@@ -72,13 +73,17 @@ export const documents = pgTable(
|
||||
pages: jsonb('pages').$type<LobeDocumentPage[]>(),
|
||||
|
||||
// Source type
|
||||
sourceType: text('source_type', { enum: ['file', 'web', 'api'] }).notNull(),
|
||||
sourceType: text('source_type', { enum: ['file', 'web', 'api', 'topic'] }).notNull(),
|
||||
source: text('source').notNull(), // File path or web URL
|
||||
|
||||
// Associated file (optional)
|
||||
// forward reference needs AnyPgColumn to avoid circular type inference
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
fileId: text('file_id').references((): AnyPgColumn => files.id, { onDelete: 'set null' }),
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
knowledgeBaseId: text('knowledge_base_id').references(() => knowledgeBases.id, {
|
||||
onDelete: 'set null',
|
||||
}),
|
||||
|
||||
// Parent document (for folder hierarchy structure)
|
||||
parentId: varchar('parent_id', { length: 255 }).references((): AnyPgColumn => documents.id, {
|
||||
@@ -93,7 +98,7 @@ export const documents = pgTable(
|
||||
|
||||
editorData: jsonb('editor_data').$type<Record<string, any>>(),
|
||||
|
||||
slug: varchar('slug', { length: 255 }),
|
||||
slug: varchar('slug', { length: 255 }).$defaultFn(() => randomSlug(3)),
|
||||
|
||||
// Timestamps
|
||||
...timestamps,
|
||||
@@ -105,6 +110,7 @@ export const documents = pgTable(
|
||||
index('documents_user_id_idx').on(table.userId),
|
||||
index('documents_file_id_idx').on(table.fileId),
|
||||
index('documents_parent_id_idx').on(table.parentId),
|
||||
index('documents_knowledge_base_id_idx').on(table.knowledgeBaseId),
|
||||
uniqueIndex('documents_client_id_user_id_unique').on(table.clientId, table.userId),
|
||||
uniqueIndex('documents_slug_user_id_unique')
|
||||
.on(table.slug, table.userId)
|
||||
|
||||
Reference in New Issue
Block a user