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
42.13k stars 9.49k forks source link

[Bug] ❌ Database migrate failed: TypeError: Cannot read properties of undefined (reading 'migrate') #4152

Open e1100x opened 1 week ago

e1100x commented 1 week ago

📦 Environment

Vercel

📌 Version

lobechat 1.19.34,Github Codespaces

💻 Operating System

Ubuntu

🌐 Browser

Chrome

🐛 Bug Description

使用codespace直接运行。 bun install ---- ok bun build ---- ok

但在codespace中修改为server版,使用vercel的Postgres(全新初始化)。仅做如下配置(尝试了serverless和node),始终报错,一直无法build成功,提示❌ Database migrate failed

# Specify the service mode as server if you want to use the server database
NEXT_PUBLIC_SERVICE_MODE=server

# Postgres database URL
DATABASE_URL=postgres://default:BChG1Yzu9oTx@ep-restless-cell-a49pem4x.us-east-1.aws.neon.tech:5432/verceldb?sslmode=require
# DATABASE_DRIVER=node

# use `openssl rand -base64 32` to generate a key for the encryption of the database
# we use this key to encrypt the user api key
KEY_VAULTS_SECRET=jgwsK28dspyVQoIf8/M3IIHl1h6LYYceSYNXeLpy6uk=

APP_URL=https://www.google.com
截屏2024-09-26 下午2 29 08

📷 Recurrence Steps

  1. 点击主页的 image
  2. bun install
  3. 修改.env
# Specify the service mode as server if you want to use the server database
NEXT_PUBLIC_SERVICE_MODE=server

# Postgres database URL
DATABASE_URL=postgres://default:BChG1Yzu9oTx@ep-restless-cell-a49pem4x.us-east-1.aws.neon.tech:5432/verceldb?sslmode=require
# DATABASE_DRIVER=node

# use `openssl rand -base64 32` to generate a key for the encryption of the database
# we use this key to encrypt the user api key
KEY_VAULTS_SECRET=jgwsK28dspyVQoIf8/M3IIHl1h6LYYceSYNXeLpy6uk=

APP_URL=https://www.google.com
  1. bun build
  2. 报错:❌ Database migrate failed

🚦 Expected Behavior

No response

📝 Additional Information

本地使用database版本docker已经部署成功, 但这个版本始终不能编译成功

lobehubbot commented 1 week ago

👀 @e1100x

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。我们会尽快调查此事,并尽快回复您。 请确保您已经提供了尽可能多的背景信息。

dosubot[bot] commented 1 week ago

Hey @e1100x! 👋 I'm here to help you with bug fixes, answer your questions, and guide you on becoming a contributor. Let's squash some bugs together!

I found a similar issue that was resolved. The error "TypeError: Cannot read properties of undefined (reading 'migrate')" during database migration can be resolved by ensuring the MIGRATION_DB environment variable is set correctly and the migrateServerDB script is properly defined and accessible.

Here are the steps to resolve the issue:

  1. Check Environment Variable: Ensure that the MIGRATION_DB environment variable is set correctly in your environment. You can set it in your .env file or directly in your GitHub Codespaces environment.

  2. Verify Script Path: Ensure that the ./scripts/migrateServerDB/index.ts file exists and is correctly implemented. The error might be due to the script not being found or having issues within the script itself.

  3. Run Migration Command: Execute the migration command manually to see if there are any additional errors or logs that can help diagnose the issue:

    MIGRATION_DB=1 tsx ./scripts/migrateServerDB/index.ts

Here is the exact implementation of the migrateServerDB/index.ts script in the LobeChat codebase:

import * as dotenv from 'dotenv';
import * as migrator from 'drizzle-orm/neon-serverless/migrator';
import { join } from 'node:path';

import { serverDB } from '../../src/database/server/core/db';
import { PGVECTOR_HINT } from './errorHint';

// Read the `.env` file if it exists, or a file specified by the
// dotenv_config_path parameter that's passed to Node.js
dotenv.config();

const runMigrations = async () => {
  await migrator.migrate(serverDB, {
    migrationsFolder: join(__dirname, '../../src/database/server/migrations'),
  });
  console.log('✅ database migration pass.');
  // eslint-disable-next-line unicorn/no-process-exit
  process.exit(0);
};

let connectionString = process.env.DATABASE_URL;

// only migrate database if the connection string is available
if (connectionString) {
  // eslint-disable-next-line unicorn/prefer-top-level-await
  runMigrations().catch((err) => {
    console.error('❌ Database migrate failed:', err);

    if ((err.message as string).includes('extension "vector" is not available')) {
      console.info(PGVECTOR_HINT);
    }

    // eslint-disable-next-line unicorn/no-process-exit
    process.exit(1);
  });
} else {
  console.log('🟢 not find database env, migration skipped');
}

Ensure that you have the necessary environment variables set in Vercel as described in the LobeChat documentation. The typical format for the database connection URL is postgres://username:password@host:port/database. Here is an example of the environment variables you might need to set:

# Serverless Postgres DB Url
DATABASE_URL=postgres://username:password@host:port/database

# Specify service mode as server, otherwise it will not enter the server-side database
NEXT_PUBLIC_SERVICE_MODE=server

Make sure to also add the KEY_VAULTS_SECRET and APP_URL environment variables as described in the LobeChat documentation:

KEY_VAULTS_SECRET=jgwsK28dspyVQoIf8/M3IIHl1h6LYYceSYNXeLpy6uk=
APP_URL=https://your-project.vercel.app

By following these steps and ensuring all necessary environment variables are correctly set, you should be able to resolve the "TypeError: Cannot read properties of undefined (reading 'migrate')" error [1][2][3][4].

To continue talking to Dosu, mention @dosu.

e1100x commented 1 week ago

似乎不没有解决,而且不只是CodeSpaces上有这个问题,本地也遇到同样的问题,只是CodeSpaces上方便复现

lobehubbot commented 1 week ago

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


It seems that there is no solution, and this problem is not only on CodeSpaces, but also encounters the same problem locally, but it is easy to reproduce on CodeSpaces

sxjeru commented 1 week ago

Vercel 之外的环境应当使用 Docker 部署服务端数据库版本。 参考 #3237

lobehubbot commented 1 week ago

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


Environments outside of Vercel should use Docker to deploy the server-side database version. Reference #3237

e1100x commented 1 week ago

3237 里用的是本地Postgres,我用的是Vercel的

Docker版本我尝试过,很棒,已经部署成功,只是做二次开发的本地版本一直无法编译。试了一下Github 的Codespace环境也是一样的问题。也比较方便复现。所有用的Postgres都是Vercel的,描述中放的Vercel Postgres链接也是可用的,是我创建的全新的

DATABASE_URL=postgres://default:BChG1Yzu9oTx@ep-restless-cell-a49pem4x.us-east-1.aws.neon.tech:5432/verceldb?sslmode=require
lobehubbot commented 1 week ago

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


The Docker version has been deployed successfully, but the local version for secondary development has been unable to compile. I tried Github's Codespace environment and had the same problem. All Postgres used are Vercel. The Vercel Postgres link in the description is also available and is a brand new one created by me.

DATABASE_URL=postgres://default:BChG1Yzu9oTx@ep-restless-cell-a49pem4x.us-east-1.aws.neon.tech:5432/verceldb?sslmode=require
sxjeru commented 1 week ago

这应该与数据库无关,不论是 vercel 的 serverless 还是自建的,不过是 DATABASE_DRIVER 的区别。

lobehubbot commented 1 week ago

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


This should have nothing to do with the database, whether it is vercel or self-built, it is just the difference between DATABASE_DRIVER.

e1100x commented 1 week ago

主要是这个问题很容易复现,有什么修改的建议我可以尝试的吗?还是更推荐用docker版本

现在二次开发上确实有些瓶颈,望各位大神能给点建议

lobehubbot commented 1 week ago

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


The main reason is that this problem is easy to reproduce. Are there any suggestions for modifications that I can try? It is still more recommended to use the docker version

There are indeed some bottlenecks in secondary development now. I hope you can give me some suggestions.

cy948 commented 1 week ago

平常别在dev环境里面做生产构建不就行了,要测生产的构建直接docker。

lobehubbot commented 1 week ago

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


Normally, just don't do production builds in the dev environment. You need to test the production builds directly with docker.

e1100x commented 1 week ago

现在遇到的困难就是无论在哪个环境都build不了,都会复现 ❌ Database migrate failed,只能使用官方build好的docker。

.env是按照LobeChat的官方文档配置的(https://lobehub.com/zh/docs/self-hosting/server-database/vercel),不知道问题出在哪里,是不是被我漏了什么

lobehubbot commented 1 week ago

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


The difficulty we are encountering now is that it cannot be built in any environment and will reappear ❌ Database migrate failed. You can only use the officially built docker.

.env is configured according to the official documentation of LobeChat (https://lobehub.com/zh/docs/self-hosting/server-database/vercel). I don’t know where the problem lies. Did I miss something?

ycz87 commented 1 week ago

我是在vercel上部署的,一开始用的vercel上neon的Postgres,但使用体验相当不好,因为每次发送内容都要等待一段时间写入数据库,非常卡,所以想把NEXT_PUBLIC_SERVICE_MODE的server切换为client,编译后也得到同样的错误。

lobehubbot commented 1 week ago

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


I deployed it on vercel. I used neon's Postgres on vercel at the beginning, but the experience was quite bad, because every time I send content, I have to wait for a while to write to the database, which is very laggy, so I want to switch the server of NEXT_PUBLIC_SERVICE_MODE to client, I also get the same error after compiling.