Skip to content

Commit

Permalink
♻️ refactor: move schema and migration folder (lobehub#4874)
Browse files Browse the repository at this point in the history
* move schema folder

* fix tests

* fix vercel build
  • Loading branch information
arvinxx authored Dec 3, 2024
1 parent b7b583d commit 9aa16d4
Show file tree
Hide file tree
Showing 92 changed files with 62 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.database
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ COPY --from=builder /deps/node_modules/pg /app/node_modules/pg
COPY --from=builder /deps/node_modules/drizzle-orm /app/node_modules/drizzle-orm

# Copy database migrations
COPY --from=builder /app/src/database/server/migrations /app/migrations
COPY --from=builder /app/src/database/migrations /app/migrations
COPY --from=builder /app/scripts/migrateServerDB/docker.cjs /app/docker.cjs
COPY --from=builder /app/scripts/migrateServerDB/errorHint.js /app/errorHint.js

Expand Down
4 changes: 2 additions & 2 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export default {
url: connectionString,
},
dialect: 'postgresql',
out: './src/database/server/migrations',
out: './src/database/migrations',

schema: './src/database/server/schemas/lobechat',
schema: './src/database/schemas',
strict: true,
} satisfies Config;
2 changes: 1 addition & 1 deletion scripts/migrateServerDB/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dotenv.config();

const runMigrations = async () => {
await migrator.migrate(serverDB, {
migrationsFolder: join(__dirname, '../../src/database/server/migrations'),
migrationsFolder: join(__dirname, '../../src/database/migrations'),
});
console.log('✅ database migration pass.');
// eslint-disable-next-line unicorn/no-process-exit
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createInsertSchema } from 'drizzle-zod';

import { LobeAgentChatConfig, LobeAgentTTSConfig } from '@/types/agent';

import { idGenerator, randomSlug } from '../../utils/idGenerator';
import { idGenerator, randomSlug } from '@/database/utils/idGenerator';
import { timestamps } from './_helpers';
import { files, knowledgeBases } from './file';
import { users } from './user';
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from 'drizzle-orm/pg-core';
import { createInsertSchema } from 'drizzle-zod';

import { idGenerator } from '../../utils/idGenerator';
import { idGenerator } from '@/database/utils/idGenerator';
import { accessedAt, createdAt, timestamps } from './_helpers';
import { asyncTasks } from './asyncTask';
import { chunks } from './rag';
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from 'drizzle-orm/pg-core';
import { createSelectSchema } from 'drizzle-zod';

import { idGenerator } from '../../utils/idGenerator';
import { idGenerator } from '@/database/utils/idGenerator';
import { timestamps } from './_helpers';
import { agents } from './agent';
import { files } from './file';
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { boolean, integer, pgTable, text, unique, uniqueIndex, varchar } from 'drizzle-orm/pg-core';
import { createInsertSchema } from 'drizzle-zod';

import { idGenerator, randomSlug } from '../../utils/idGenerator';
import { idGenerator, randomSlug } from '@/database/utils/idGenerator';
import { timestamps } from './_helpers';
import { users } from './user';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { boolean, jsonb, pgTable, text, unique } from 'drizzle-orm/pg-core';
import { createInsertSchema } from 'drizzle-zod';

import { idGenerator } from '../../utils/idGenerator';
import { idGenerator } from '@/database/utils/idGenerator';
import { timestamps, timestamptz } from './_helpers';
import { sessions } from './session';
import { users } from './user';
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/database/server/core/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ws from 'ws';
import { serverDBEnv } from '@/config/db';
import { isServerMode } from '@/const/version';

import * as schema from '../schemas/lobechat';
import * as schema from '../../schemas';

export const getDBInstance = (): NeonDatabase<typeof schema> => {
if (!isServerMode) return {} as any;
Expand Down
4 changes: 2 additions & 2 deletions src/database/server/core/dbForTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ws from 'ws';

import { serverDBEnv } from '@/config/db';

import * as schema from '../schemas/lobechat';
import * as schema from '../../schemas';

export const getTestDBInstance = async () => {
let connectionString = serverDBEnv.DATABASE_TEST_URL;
Expand All @@ -24,7 +24,7 @@ export const getTestDBInstance = async () => {
const db = nodeDrizzle(client, { schema });

await nodeMigrator.migrate(db, {
migrationsFolder: join(__dirname, '../migrations'),
migrationsFolder: join(__dirname, '../../migrations'),
});

return db;
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/__tests__/_test_template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { getTestDBInstance } from '@/database/server/core/dbForTest';

import { sessionGroups, users } from '../../schemas/lobechat';
import { sessionGroups, users } from '../../../schemas';
import { SessionGroupModel } from '../sessionGroup';

let serverDB = await getTestDBInstance();
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/__tests__/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
knowledgeBases,
sessions,
users,
} from '../../schemas/lobechat';
} from '../../../schemas';
import { AgentModel } from '../agent';

let serverDB = await getTestDBInstance();
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/__tests__/asyncTask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { getTestDBInstance } from '@/database/server/core/dbForTest';
import { AsyncTaskStatus, AsyncTaskType } from '@/types/asyncTask';

import { asyncTasks, users } from '../../schemas/lobechat';
import { asyncTasks, users } from '../../../schemas';
import { ASYNC_TASK_TIMEOUT, AsyncTaskModel } from '../asyncTask';

let serverDB = await getTestDBInstance();
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/__tests__/chunk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
files,
unstructuredChunks,
users,
} from '../../schemas/lobechat';
} from '../../../schemas';
import { ChunkModel } from '../chunk';
import { codeEmbedding, designThinkingQuery, designThinkingQuery2 } from './fixtures/embedding';

Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/__tests__/file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
knowledgeBaseFiles,
knowledgeBases,
users,
} from '../../schemas/lobechat';
} from '../../../schemas';
import { FileModel } from '../file';

let serverDB = await getTestDBInstance();
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/__tests__/knowledgeBase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
knowledgeBaseFiles,
knowledgeBases,
users,
} from '../../schemas/lobechat';
} from '../../../schemas';
import { KnowledgeBaseModel } from '../knowledgeBase';

let serverDB = await getTestDBInstance();
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/__tests__/message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
sessions,
topics,
users,
} from '../../schemas/lobechat';
} from '../../../schemas';
import { MessageModel } from '../message';

let serverDB = await getTestDBInstance();
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/__tests__/nextauth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
nextauthSessions,
nextauthVerificationTokens,
users,
} from '@/database/server/schemas/lobechat';
} from '@/database/schemas';
import { LobeNextAuthDbAdapter } from '@/libs/next-auth/adapter';

let serverDB = await getTestDBInstance();
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { getTestDBInstance } from '@/database/server/core/dbForTest';

import { NewInstalledPlugin, installedPlugins, users } from '../../schemas/lobechat';
import { NewInstalledPlugin, installedPlugins, users } from '../../../schemas';
import { PluginModel } from '../plugin';

let serverDB = await getTestDBInstance();
Expand Down
4 changes: 2 additions & 2 deletions src/database/server/models/__tests__/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
sessions,
topics,
users,
} from '../../schemas/lobechat';
import { idGenerator } from '../../utils/idGenerator';
} from '../../../schemas';
import { idGenerator } from '@/database/utils/idGenerator';
import { SessionModel } from '../session';

let serverDB = await getTestDBInstance();
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/__tests__/sessionGroup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { getTestDBInstance } from '@/database/server/core/dbForTest';

import { sessionGroups, users } from '../../schemas/lobechat';
import { sessionGroups, users } from '../../../schemas';
import { SessionGroupModel } from '../sessionGroup';

let serverDB = await getTestDBInstance();
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/__tests__/topic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { getTestDBInstance } from '@/database/server/core/dbForTest';

import { messages, sessions, topics, users } from '../../schemas/lobechat';
import { messages, sessions, topics, users } from '../../../schemas';
import { CreateTopicParams, TopicModel } from '../topic';

let serverDB = await getTestDBInstance();
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/__tests__/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { KeyVaultsGateKeeper } from '@/server/modules/KeyVaultsEncrypt';
import { UserGuide, UserPreference } from '@/types/user';
import { UserSettings } from '@/types/user/settings';

import { userSettings, users } from '../../schemas/lobechat';
import { userSettings, users } from '../../../schemas';
import { SessionModel } from '../session';
import { UserModel } from '../user';

Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/_template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { and, desc } from 'drizzle-orm/expressions';

import { serverDB } from '@/database/server';

import { NewSessionGroup, SessionGroupItem, sessionGroups } from '../schemas/lobechat';
import { NewSessionGroup, SessionGroupItem, sessionGroups } from '../../schemas';

export class TemplateModel {
private userId: string;
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
agentsToSessions,
files,
knowledgeBases,
} from '@/database/server/schemas/lobechat';
} from '@/database/schemas';

export class AgentModel {
private userId: string;
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/asyncTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
AsyncTaskType,
} from '@/types/asyncTask';

import { AsyncTaskSelectItem, NewAsyncTaskItem, asyncTasks } from '../schemas/lobechat';
import { AsyncTaskSelectItem, NewAsyncTaskItem, asyncTasks } from '../../schemas';

// set timeout to about 5 minutes, and give 2s padding time
export const ASYNC_TASK_TIMEOUT = 298 * 1000;
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
fileChunks,
files,
unstructuredChunks,
} from '../schemas/lobechat';
} from '../../schemas';

export class ChunkModel {
private userId: string;
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/embedding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { and } from 'drizzle-orm/expressions';

import { serverDB } from '@/database/server';

import { NewEmbeddingsItem, embeddings } from '../schemas/lobechat';
import { NewEmbeddingsItem, embeddings } from '../../schemas';

export class EmbeddingModel {
private userId: string;
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
files,
globalFiles,
knowledgeBaseFiles,
} from '../schemas/lobechat';
} from '../../schemas';

export class FileModel {
private readonly userId: string;
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/knowledgeBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { and, desc } from 'drizzle-orm/expressions';
import { serverDB } from '@/database/server';
import { KnowledgeBaseItem } from '@/types/knowledgeBase';

import { NewKnowledgeBase, knowledgeBaseFiles, knowledgeBases } from '../schemas/lobechat';
import { NewKnowledgeBase, knowledgeBaseFiles, knowledgeBases } from '../../schemas';

export class KnowledgeBaseModel {
private userId: string;
Expand Down
4 changes: 2 additions & 2 deletions src/database/server/models/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { count } from 'drizzle-orm';
import { and, asc, desc, eq, gte, inArray, isNull, like, lt } from 'drizzle-orm/expressions';

import { serverDB } from '@/database/server/core/db';
import { idGenerator } from '@/database/server/utils/idGenerator';
import { idGenerator } from '@/database/utils/idGenerator';
import { getFullFileUrl } from '@/server/utils/files';
import {
ChatFileItem,
Expand All @@ -28,7 +28,7 @@ import {
messageTranslates,
messages,
messagesFiles,
} from '../schemas/lobechat';
} from '../../schemas';

export interface QueryMessageParams {
current?: number;
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { and, desc, eq } from 'drizzle-orm/expressions';

import { serverDB } from '@/database/server';

import { InstalledPluginItem, NewInstalledPlugin, installedPlugins } from '../schemas/lobechat';
import { InstalledPluginItem, NewInstalledPlugin, installedPlugins } from '../../schemas';

export class PluginModel {
private userId: string;
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/ragEval/dataset.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { and, desc, eq } from 'drizzle-orm';

import { serverDB } from '@/database/server';
import { NewEvalDatasetsItem, evalDatasets } from '@/database/server/schemas/lobechat';
import { NewEvalDatasetsItem, evalDatasets } from '@/database/schemas';
import { RAGEvalDataSetItem } from '@/types/eval';

export class EvalDatasetModel {
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/ragEval/datasetRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
NewEvalDatasetRecordsItem,
evalDatasetRecords,
files,
} from '@/database/server/schemas/lobechat';
} from '@/database/schemas';
import { EvalDatasetRecordRefFile } from '@/types/eval';

export class EvalDatasetRecordModel {
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/ragEval/evaluation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
evalDatasets,
evalEvaluation,
evaluationRecords,
} from '@/database/server/schemas/lobechat';
} from '@/database/schemas';
import { EvalEvaluationStatus, RAGEvalEvaluationItem } from '@/types/eval';

export class EvalEvaluationModel {
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/ragEval/evaluationRecord.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { and, eq } from 'drizzle-orm';

import { serverDB } from '@/database/server';
import { NewEvaluationRecordsItem, evaluationRecords } from '@/database/server/schemas/lobechat';
import { NewEvaluationRecordsItem, evaluationRecords } from '@/database/schemas';

export class EvaluationRecordModel {
private userId: string;
Expand Down
4 changes: 2 additions & 2 deletions src/database/server/models/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
agentsToSessions,
sessionGroups,
sessions,
} from '../schemas/lobechat';
import { idGenerator } from '../utils/idGenerator';
} from '../../schemas';
import { idGenerator } from '@/database/utils/idGenerator';

export class SessionModel {
private userId: string;
Expand Down
4 changes: 2 additions & 2 deletions src/database/server/models/sessionGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { eq } from 'drizzle-orm';
import { and, asc, desc } from 'drizzle-orm/expressions';

import { serverDB } from '@/database/server';
import { idGenerator } from '@/database/server/utils/idGenerator';
import { idGenerator } from '@/database/utils/idGenerator';

import { SessionGroupItem, sessionGroups } from '../schemas/lobechat';
import { SessionGroupItem, sessionGroups } from '../../schemas';

export class SessionGroupModel {
private userId: string;
Expand Down
2 changes: 1 addition & 1 deletion src/database/server/models/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { and, desc } from 'drizzle-orm/expressions';
import { serverDB } from '@/database/server';
import { CreateThreadParams, ThreadStatus } from '@/types/topic';

import { ThreadItem, threads } from '../schemas/lobechat';
import { ThreadItem, threads } from '../../schemas';

const queryColumns = {
createdAt: threads.createdAt,
Expand Down
4 changes: 2 additions & 2 deletions src/database/server/models/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { and, desc, eq, exists, isNull, like, or } from 'drizzle-orm/expressions

import { serverDB } from '@/database/server/core/db';

import { NewMessage, TopicItem, messages, topics } from '../schemas/lobechat';
import { idGenerator } from '../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/database/server/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { UserGuide, UserPreference } from '@/types/user';
import { UserKeyVaults, UserSettings } from '@/types/user/settings';
import { merge } from '@/utils/merge';

import { NewUser, UserItem, userSettings, users } from '../schemas/lobechat';
import { NewUser, UserItem, userSettings, users } from '../../schemas';
import { SessionModel } from './session';

export class UserNotFoundError extends TRPCError {
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 9aa16d4

Please sign in to comment.