mirror of
https://github.com/bookorbit/bookorbit.git
synced 2026-11-03 00:11:37 -06:00
d182d913c6
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
50 lines
1.3 KiB
TypeScript
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();
|