mirror of
https://github.com/lobehub/lobehub.git
synced 2026-03-28 13:39:28 +07:00
54 lines
1.8 KiB
TypeScript
54 lines
1.8 KiB
TypeScript
import * as dotenv from 'dotenv';
|
|
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';
|
|
|
|
// @ts-ignore tsgo handle esm import cjs and compatibility issues
|
|
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, '../../packages/database/migrations');
|
|
|
|
const isDesktop = process.env.NEXT_PUBLIC_IS_DESKTOP_APP === '1';
|
|
|
|
const runMigrations = async () => {
|
|
const { serverDB } = await import('../../packages/database/src/server');
|
|
|
|
const time = Date.now();
|
|
if (process.env.DATABASE_DRIVER === 'node') {
|
|
await nodeMigrate(serverDB, { migrationsFolder });
|
|
} else {
|
|
await neonMigrate(serverDB, { migrationsFolder });
|
|
}
|
|
|
|
console.log('✅ database migration pass. use: %s ms', Date.now() - time);
|
|
// 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 (!isDesktop && connectionString) {
|
|
// eslint-disable-next-line unicorn/prefer-top-level-await
|
|
runMigrations().catch((err) => {
|
|
console.error('❌ Database migrate failed:', err);
|
|
|
|
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
|
|
process.exit(1);
|
|
});
|
|
} else {
|
|
console.log('🟢 not find database env or in desktop mode, migration skipped');
|
|
}
|