lobehub / lobe-chat

🤯 Lobe Chat - an open-source, modern-design AI chat framework. Supports Multi AI Providers( OpenAI / Claude 3 / Gemini / Ollama / Azure / DeepSeek), Knowledge Base (file upload / knowledge management / RAG ), Multi-Modals (Vision/TTS) and plugin system. One-click FREE deployment of your private ChatGPT/ Claude application.
https://chat-preview.lobehub.com
Other
44.46k stars 9.98k forks source link

[Bug] 使用服务端数据库部署报错 #3237

Closed xudongyss closed 3 months ago

xudongyss commented 3 months ago

📦 Environment

Other

📌 Version

1.4.3

💻 Operating System

Other Linux

🌐 Browser

Chrome

🐛 Bug Description

bun run db:migrate $ MIGRATION_DB=1 tsx scripts/migrateServerDB/index.ts ❌ Database migrate failed: TypeError: Cannot read properties of undefined (reading 'migrate') at Object.migrate (/www/wwwroot/lobe-chat-test/node_modules/src/neon-serverless/migrator.ts:10:19) at runMigrations (/www/wwwroot/lobe-chat-test/scripts/migrateServerDB/index.ts:12:18) at (/www/wwwroot/lobe-chat-test/scripts/migrateServerDB/index.ts:25:3) at Object. (/www/wwwroot/lobe-chat-test/scripts/migrateServerDB/index.ts:32:1) at Module._compile (node:internal/modules/cjs/loader:1358:14) at Object.transformer (/www/wwwroot/lobe-chat-test/node_modules/tsx/dist/register-CCR7NebB.cjs:3:1101) at Module.load (node:internal/modules/cjs/loader:1208:32) at Module._load (node:internal/modules/cjs/loader:1024:12) at cjsLoader (node:internal/modules/esm/translators:348:17) at ModuleWrap. (node:internal/modules/esm/translators:297:7) error: script "db:migrate" exited with code 1 error: script "build-migrate-db" exited with code 1 error: script "postbuild" exited with code 1

📷 Recurrence Steps

No response

🚦 Expected Behavior

No response

📝 Additional Information

No response

lobehubbot commented 3 months ago

👀 @xudongyss

Thank you for raising an issue. We will investigate into the matter and get back to you as soon as possible. Please make sure you have given us as much context as possible.\ 非常感谢您提交 issue。我们会尽快调查此事,并尽快回复您。 请确保您已经提供了尽可能多的背景信息。

arvinxx commented 3 months ago

非 Vercel 平台部署的问题暂不处理,请耐心等待 Docker 版

lobehubbot commented 3 months ago

✅ @xudongyss

This issue is closed, If you have any questions, you can comment and reply.\ 此问题已经关闭。如果您有任何问题,可以留言并回复。

lobehubbot commented 3 months ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


The issue of non-Vercel platform deployment will not be dealt with yet. Please be patient and wait for the Docker version.

xudongyss commented 3 months ago

非 Vercel 平台部署的问题暂不处理,请耐心等待 Docker 版

服务端数据非要基于Vercel 平台?在自己的服务器上部署不行?

lobehubbot commented 3 months ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


The issue of non-Vercel platform deployment will not be dealt with yet. Please be patient and wait for the Docker version.

Does the server data have to be based on the Vercel platform? Can't you deploy it on your own server?

arvinxx commented 3 months ago

@xudongyss https://github.com/lobehub/lobe-chat/discussions/3222

xudongyss commented 3 months ago

@xudongyss #3222

我没用 Docker 部署,直接用的 bun run build,然后 bun start,但在 .env 配置了数据库之后,Clerk 也配了,bun run build 就报错

lobehubbot commented 3 months ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@xudongyss #3222

I did not use Docker for deployment. I used bun run build directly, and then bun start. However, after .env configured the database and Clerk, bun run build reported an error.

arvinxx commented 3 months ago

@xudongyss 你用的 pg 是普通模式还是 serverless 的 ?

lobehubbot commented 3 months ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@xudongyss Are you using pg in normal mode or serverless?

arvinxx commented 3 months ago

目前 db 的 migration 脚本并没有针对普通 pg 做兼容。如果你用的是普通 pg ,试试把这一行:

https://github.com/lobehub/lobe-chat/blob/2b54caeda18497cee3cd07a3ad29945c881247f6/scripts/migrateServerDB/index.ts#L2

改成这个,然后再试试:

import * as migrator from 'drizzle-orm/node-postgres/migrator'; 

后面做 Docker 镜像版的时候我才会做这部分兼容的动作。

xudongyss commented 3 months ago

目前 db 的 migration 脚本并没有针对普通 pg 做兼容。如果你用的是普通 pg ,试试把这一行:

https://github.com/lobehub/lobe-chat/blob/2b54caeda18497cee3cd07a3ad29945c881247f6/scripts/migrateServerDB/index.ts#L2

改成这个,然后再试试:

import * as migrator from 'drizzle-orm/node-postgres/migrator'; 

后面做 Docker 镜像版的时候我才会做这部分兼容的动作。

image 设置 DATABASE_DRIVER=node 这个参数没用是吧?

xudongyss commented 3 months ago
node-postgres

还是不行 Database migrate failed: TypeError: Cannot read properties of undefined (reading 'migrate') at migrate (/www/wwwroot/lobe-chat-test/node_modules/src/node-postgres/migrator.ts:10:19) at runMigrations (/www/wwwroot/lobe-chat-test/scripts/migrateServerDB/index.ts:12:9) at (/www/wwwroot/lobe-chat-test/scripts/migrateServerDB/index.ts:25:3) at Object. (/www/wwwroot/lobe-chat-test/scripts/migrateServerDB/index.ts:32:1) at Module._compile (node:internal/modules/cjs/loader:1358:14) at Object.transformer (/www/wwwroot/lobe-chat-test/node_modules/tsx/dist/register-CCR7NebB.cjs:3:1101) at Module.load (node:internal/modules/cjs/loader:1208:32) at Module._load (node:internal/modules/cjs/loader:1024:12) at cjsLoader (node:internal/modules/esm/translators:348:17) at ModuleWrap. (node:internal/modules/esm/translators:297:7) error: script "db:migrate" exited with code

arvinxx commented 3 months ago

那就得花时间研究下原因了。你先暂时搁置下吧。 Docker 版估计这两周应该就能出来了。

lobehubbot commented 3 months ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Then you have to take the time to study the reasons. Just put it aside for now. The Docker version is estimated to be available within two weeks.

anusoft commented 3 months ago

Modify below code and it should works when run migration on host

Commands:

export DATABASE_DRIVER="node"
export DATABASE_URL="postgres://lobechat:xxx@127.0.0.1:5432/lobechat"
export KEY_VAULTS_SECRET="xxx"

pnpm run db:migrate

Results:

> @lobehub/chat@1.7.2 db:migrate /root/repos/lobe-chat
> MIGRATION_DB=1 tsx scripts/migrateServerDB/index.ts

Not in production mode, returning empty object as DB instance
Database URL: postgres://lobechat:****@xxx:5432/lobechat
Initializing database connection for migration...
Using Node PostgreSQL driver
Starting database migration...
✅ Database migration successful.

scripts/migrateServerDB/index.ts

import * as dotenv from 'dotenv';
import { migrate } from 'drizzle-orm/node-postgres/migrator';
import { join } from 'node:path';
import { getDBInstance } from '../../src/database/server/core/db';

dotenv.config();

const runMigrations = async () => {
  try {
    console.log('Initializing database connection for migration...');
    const serverDB = getDBInstance(true); // Force server mode for migration

    if (!serverDB || typeof serverDB !== 'object') {
      throw new Error('Failed to initialize database connection');
    }

    console.log('Starting database migration...');
    await migrate(serverDB, {
      migrationsFolder: join(__dirname, '../../src/database/server/migrations'),
    });
    console.log('✅ Database migration successful.');
  } catch (error) {
    console.error('❌ Database migration failed:', error);
    console.error('Error stack:', error.stack);
    process.exit(1);
  }
};

const connectionString = process.env.DATABASE_URL;
if (connectionString) {
  console.log('Database URL:', connectionString.replace(/:[^:@]+@/, ':****@')); // Mask the password
  runMigrations();
} else {
  console.log('🟢 Database environment variable not found, migration skipped');
}

src/config/db.ts

import { createEnv } from '@t3-oss/env-nextjs';
import { z } from 'zod';

export const getServerDBConfig = () => {
  return createEnv({
    client: {
      NEXT_PUBLIC_ENABLED_SERVER_SERVICE: z.boolean(),
    },
    runtimeEnv: {
      DATABASE_DRIVER: process.env.DATABASE_DRIVER || 'neon',
      DATABASE_TEST_URL: process.env.DATABASE_TEST_URL,
      DATABASE_URL: process.env.DATABASE_URL,

      KEY_VAULTS_SECRET: process.env.KEY_VAULTS_SECRET,

      NEXT_PUBLIC_ENABLED_SERVER_SERVICE: process.env.NEXT_PUBLIC_SERVICE_MODE === 'server',
    },
    server: {
      DATABASE_DRIVER: z.enum(['neon', 'node']),
      DATABASE_TEST_URL: z.string().optional(),
      DATABASE_URL: z.string().optional(),

      KEY_VAULTS_SECRET: z.string().optional(),
    },
  });
};

export const serverDBEnv = getServerDBConfig();