Каркас monorepo: scheduler, data_collector, khbuild_assistant_prep, khb_common; миграции; CI/CD
Some checks failed
ci / test (push) Failing after 20s
ci / deploy (push) Has been skipped

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-11 09:44:50 +03:00
commit ebb1cb7950
101 changed files with 4971 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
import { readdirSync, readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import pg from "pg";
export type Pool = pg.Pool;
export type Queryable = Pick<pg.Pool, "query">;
export function createPool(url: string, max = 5): Pool {
return new pg.Pool({ connectionString: url, max });
}
/** Выполняет fn в транзакции. */
export async function withTx<T>(pool: Pool, fn: (c: pg.PoolClient) => Promise<T>): Promise<T> {
const c = await pool.connect();
try {
await c.query("begin");
const r = await fn(c);
await c.query("commit");
return r;
} catch (e) {
await c.query("rollback").catch(() => undefined);
throw e;
} finally {
c.release();
}
}
/** Применяет *.sql из папки по порядку имён; журнал - таблица schema_migrations. Возвращает применённые. */
export async function migrate(pool: Pool, dir: string = migrationsDir): Promise<string[]> {
await pool.query(
"create table if not exists schema_migrations (name text primary key, applied_at timestamptz not null default now())",
);
const applied = new Set((await pool.query("select name from schema_migrations")).rows.map((r) => r.name as string));
const done: string[] = [];
for (const file of readdirSync(dir).filter((f) => f.endsWith(".sql")).sort()) {
if (applied.has(file)) continue;
const sql = readFileSync(join(dir, file), "utf8");
await withTx(pool, async (c) => {
await c.query(sql);
await c.query("insert into schema_migrations (name) values ($1)", [file]);
});
done.push(file);
}
return done;
}
/** Папка migrations пакета khb_common (работает и из src, и из dist). */
export const migrationsDir = join(dirname(fileURLToPath(import.meta.url)), "..", "..", "migrations");