/
aaronair
/
Test_task_rt_solution
Обзор
Документация
Войти
/
aaronair
/
Test_task_rt_solution
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
backend/src/db/migrate.ts
38 строк
1 KB
Ilikedrinkpivo
first_commit
28 май 2026, 14:51
28 май 2026, 14:51
b4498fc
Код
Авторство
О чём код?
import { readdir, readFile } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; import postgres from "postgres"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); export async function runMigrations(databaseUrl: string) { const sql = postgres(databaseUrl, { max: 1 }); const migrationsDir = path.join(__dirname, "migrations"); await sql` CREATE TABLE IF NOT EXISTS drizzle_migrations ( id SERIAL PRIMARY KEY, hash TEXT NOT NULL UNIQUE, created_at TIMESTAMPTZ DEFAULT now() ) `; const files = (await readdir(migrationsDir)) .filter((f) => f.endsWith(".sql")) .sort(); for (const file of files) { const hash = file; const existing = await sql` SELECT 1 FROM drizzle_migrations WHERE hash = ${hash} `; if (existing.length > 0) continue; const content = await readFile(path.join(migrationsDir, file), "utf-8"); await sql.unsafe(content); await sql`INSERT INTO drizzle_migrations (hash) VALUES (${hash})`; console.log(`Applied migration: ${file}`); } await sql.end(); }