SPB Git forge

spb/rareindex

Public
54commits 1branches 0releases
7.1 MBsize
maindefault branch
10 days agolast push
TypeScript 61.9% HTML 37.2% SQL 0.7%
1.3 KB · 34 lines typescript
Raw Blame History
1import { migrate } from 'drizzle-orm/postgres-js/migrator';2import { fileURLToPath } from 'node:url';3import path from 'node:path';4import { getDb, getSql, closeDb } from './client.js';56/**7 * Runs SQL migrations from ./migrations. Extensions are created first (idempotent) because8 * drizzle-kit does not manage them; `vector` is optional and only enabled when available.9 */10export async function runMigrations(): Promise<void> {11  const sql = getSql({ max: 1 });12  await sql`CREATE EXTENSION IF NOT EXISTS pg_trgm`;13  await sql`CREATE EXTENSION IF NOT EXISTS unaccent`;14  const [vec] = await sql`SELECT 1 AS ok FROM pg_available_extensions WHERE name = 'vector'`;15  if (vec) await sql`CREATE EXTENSION IF NOT EXISTS vector`;16  else console.warn('[migrate] pgvector not available: embedding columns will fail — install pgvector');17  const here = path.dirname(fileURLToPath(import.meta.url));18  await migrate(getDb({ max: 1 }), { migrationsFolder: path.resolve(here, '../migrations') });19}2021const isMain = process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url);22if (isMain) {23  runMigrations()24    .then(async () => {25      console.log('[migrate] done');26      await closeDb();27    })28    .catch(async (err) => {29      console.error('[migrate] failed', err);30      await closeDb();31      process.exit(1);32    });33}34