diff --git a/scripts/migrateServerDB/errorHint.js b/scripts/migrateServerDB/errorHint.js index facc82dede86..6b55bd447b5a 100644 --- a/scripts/migrateServerDB/errorHint.js +++ b/scripts/migrateServerDB/errorHint.js @@ -12,6 +12,25 @@ docker run -p 5432:5432 -d --name pg -e POSTGRES_PASSWORD=mysecretpassword pgvec if you have any other question, please open issue here: https://github.com/lobehub/lobe-chat/issues `; +const DB_FAIL_INIT_HINT = `------------------------------------------------------------------------------------------ +⚠️ Database migrate failed due to not find the db instance. + +1) You might not switch to server db mode, please set the env blow and try again: + +\`\`\` +NEXT_PUBLIC_SERVICE_MODE=server +\`\`\` + +2) if you are using docker postgres image, you may need to set DATABASE_DRIVER to node + +\`\`\` +DATABASE_DRIVER=node +\`\`\` + +if you have any other question, please open issue here: https://github.com/lobehub/lobe-chat/issues +`; + module.exports = { + DB_FAIL_INIT_HINT, PGVECTOR_HINT, }; diff --git a/scripts/migrateServerDB/index.ts b/scripts/migrateServerDB/index.ts index ccc7ed562866..34b2e8582738 100644 --- a/scripts/migrateServerDB/index.ts +++ b/scripts/migrateServerDB/index.ts @@ -1,18 +1,24 @@ import * as dotenv from 'dotenv'; -import * as migrator from 'drizzle-orm/neon-serverless/migrator'; +import { migrate as neonMigrate } from 'drizzle-orm/neon-serverless/migrator'; +import { migrate as nodeMigrate } from 'drizzle-orm/node-postgres/migrator'; import { join } from 'node:path'; import { serverDB } from '../../src/database/server/core/db'; -import { PGVECTOR_HINT } from './errorHint'; +import { DB_FAIL_INIT_HINT, 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 migrationsFolder = join(__dirname, '../../src/database/migrations'); + const runMigrations = async () => { - await migrator.migrate(serverDB, { - migrationsFolder: join(__dirname, '../../src/database/migrations'), - }); + if (process.env.DATABASE_DRIVER === 'node') { + await nodeMigrate(serverDB, { migrationsFolder }); + } else { + await neonMigrate(serverDB, { migrationsFolder }); + } + console.log('✅ database migration pass.'); // eslint-disable-next-line unicorn/no-process-exit process.exit(0); @@ -26,8 +32,12 @@ if (connectionString) { runMigrations().catch((err) => { console.error('❌ Database migrate failed:', err); - if ((err.message as string).includes('extension "vector" is not available')) { + const errMsg = err.message as string; + + if (errMsg.includes('extension "vector" is not available')) { console.info(PGVECTOR_HINT); + } else if (errMsg.includes(`Cannot read properties of undefined (reading 'migrate')`)) { + console.info(DB_FAIL_INIT_HINT); } // eslint-disable-next-line unicorn/no-process-exit