Skip to content

Commit

Permalink
🔨 chore: improve migrate scripts (#4902)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx authored Dec 6, 2024
1 parent 45ec7b4 commit 871f5b9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
19 changes: 19 additions & 0 deletions scripts/migrateServerDB/errorHint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
22 changes: 16 additions & 6 deletions scripts/migrateServerDB/index.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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
Expand Down

0 comments on commit 871f5b9

Please sign in to comment.