import { readdir, readFile } from 'fs/promises'; import path, { resolve } from 'path'; import { config } from 'dotenv'; import { omit } from 'lodash'; import { Pool, PoolClient } from 'pg'; interface SnapshotEntity { table: string; arrayFields?: string[]; omitFields?: string[]; updateExisting?: boolean; } export async function applySnapshot( snapshot: Record>, connection: PoolClient, ) { const entities: SnapshotEntity[] = [ { table: 'admin_settings' }, { table: 'users' }, { table: 'projects' }, { table: 'canonical_brand_data' }, { table: 'entity_audit_logs' }, { table: 'entity_completeness_tracking' }, { table: 'entity_facts' }, { table: 'entity_home_pages' }, { table: 'external_connectors' }, { table: 'external_profiles' }, { table: 'file' }, { table: 'json_ld_schemas' }, { table: 'migration_1772363000000_default_user_role_users' }, { table: 'nap_consistency_scores' }, { table: 'project_entities' }, { table: 'project_entity_data' }, { table: 'project_licenses' }, { table: 'project_members' }, { table: 'project_metrics' }, { table: 'project_products' }, { table: 'project_team_members' }, { table: 'roles' }, { table: 'sync_logs' }, { table: 'user_roles' }, { table: 'website_checks' }, { table: 'wikidata_edit_queue' }, { table: 'wikidata_entities' }, { table: 'subscription_plans' }, { table: 'brand_questions', arrayFields: ['questions'] }, { table: 'public_metric_requests', arrayFields: ['keywords', 'modelsUsed'] }, ]; await nuke(entities, connection); if (!entities) { console.log('no entities'); } if (!snapshot) { console.log('no snaphot'); } for (const updateTable of entities.map(genUpdateTable)) { await updateTable(snapshot, connection); } } function genUpdateTable(snapshotEntity: SnapshotEntity) { let processRow = (row: object) => row; const save = snapshotEntity.updateExisting ? update : insert; if ('omitFields' in snapshotEntity && snapshotEntity.omitFields?.length) { processRow = (row: object) => omit(row, snapshotEntity.omitFields!); } return async (snapshot: Record>, connection: PoolClient) => { const data = snapshot[snapshotEntity.table].map(processRow); const ids = snapshot[snapshotEntity.table] .map((row: any): number => row?.id) .filter(Boolean); const lastId = Math.max(...ids); if (!data) { return; } await save(snapshotEntity, data, connection); if (Number.isFinite(lastId)) { await connection.query( `ALTER SEQUENCE IF EXISTS ${snapshotEntity.table}_id_seq RESTART WITH ${lastId + 1}`, ); } }; } async function insert(entity: SnapshotEntity, rows: object[], connection: PoolClient) { if (!rows.length) { return; } const keys = Object.keys(rows[0]).map((column) => `"${column}"`); const values = rows.map((row) => serializeObjects(row, entity.arrayFields)); const placeholders = values.map(wrapReturnedInBrackets(genRowPlaceholders)); const query = `INSERT INTO ${entity.table} (${keys}) VALUES ${placeholders}`; return connection.query(query, values.flat()); } async function update(entity: SnapshotEntity, rows: object[], connection: PoolClient) { if (!rows.length) { return; } const keys = Object.keys(rows[0]).map((column) => `"${column}"`); const promises = rows.map((row: any) => { const placeholders = genRowPlaceholders(keys).map((val, idx) => keys[idx] + '=' + val); const values = serializeObjects(row, entity.arrayFields); return connection.query( `UPDATE ${entity.table} SET ${placeholders} WHERE id=${row.id};`, values, ); }); return Promise.all(promises); } function wrapReturnedInBrackets(fn: (...args: any[]) => any) { return (...args: any) => '(' + fn(...args) + ')'; } function genRowPlaceholders(row: any[], offsetByRows?: number) { const rowIdx = offsetByRows || 0; return row.map((_, colIdx) => row.length * rowIdx + colIdx + 1).map((idx) => '$' + idx); } function serializeObjects(row: any, leaveAsIs: string[] = []) { return Object.entries(row).map(([key, value]) => { if (leaveAsIs.includes(key)) { return value; } if (typeof value !== 'object' || value === null) { return value; } return JSON.stringify(value); }); } async function nuke(entities: SnapshotEntity[], connection: PoolClient) { const tables = new Set(entities.map(({ table }) => table)); return connection .query(`TRUNCATE TABLE ${[...tables].join(',')} RESTART IDENTITY CASCADE;`) .catch(console.error); } export async function readSnapshot(basePath: string) { const files = await readdir(basePath); const entries = await Promise.all( files.map(async (filename) => { const tableName = filename.split('.').slice(0, -1).join('.'); const text = await readFile(resolve(basePath, filename), { encoding: 'utf8' }); const rows = JSON.parse(text); const entry = [tableName, rows]; return entry; }), ); return Object.fromEntries(entries) as Record>; } const nodeEnv = process.env.NODE_ENV || 'production'; config({ path: path.resolve(process.cwd(), `.env.${nodeEnv}`) }); config({ path: path.resolve(process.cwd(), '.env') }); const pool = new Pool({ host: process.env.POSTGRES_HOST, port: +process.env.POSTGRES_PORT!, user: process.env.POSTGRES_USER, password: process.env.POSTGRES_PASSWORD, database: process.env.POSTGRES_DB, }); const snapshotDir = path.join(__dirname, '../db-snapshot'); async function main() { const db = await pool.connect(); console.log('ДБ подключена'); const snapshot = await readSnapshot(snapshotDir); await applySnapshot(snapshot, db); console.log('Снимок применён к базе данных'); db.release(); await pool.end(); } main();