Files
bookorbit/server/src/scripts/migrate.ts
T
neon d182d913c6 fix(books): support accent-insensitive search
Install the PostgreSQL unaccent extension and use a custom immutable wrapper function to perform accent-insensitive ILIKE comparisons when searching for books, authors, series, and narrators. Add database indexes on the unaccented values to maintain query performance.

Closes #640
2026-07-11 17:47:29 +00:00

50 lines
1.3 KiB
TypeScript

import { existsSync } from 'fs';
import { join } from 'path';
import { drizzle } from 'drizzle-orm/node-postgres';
import { migrate } from 'drizzle-orm/node-postgres/migrator';
import { Pool } from 'pg';
import { installPostgresExtensions } from './postgres-extensions';
function resolveMigrationsFolder(): string {
const candidates = [
join(__dirname, '..', '..', 'migrations'),
join(__dirname, '..', 'db', 'migrations'),
join(process.cwd(), 'migrations'),
join(process.cwd(), 'src', 'db', 'migrations'),
];
const match = candidates.find((path) => existsSync(path));
if (!match) {
throw new Error(`Unable to locate migrations folder. Checked: ${candidates.join(', ')}`);
}
return match;
}
async function runMigrations() {
const connectionString = process.env.DATABASE_URL;
if (!connectionString) {
throw new Error('DATABASE_URL is required');
}
const pool = new Pool({
connectionString,
max: 3,
idleTimeoutMillis: 10_000,
connectionTimeoutMillis: 5_000,
});
try {
await installPostgresExtensions(pool);
const migrationsFolder = resolveMigrationsFolder();
await migrate(drizzle(pool), { migrationsFolder });
console.log(`Migrations applied successfully from ${migrationsFolder}`);
} finally {
await pool.end();
}
}
void runMigrations();