UNPKG

streamby-core

Version:

StreamBy middleware framework for media storage management

72 lines (71 loc) 3.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setupStreambyPg = setupStreambyPg; const node_fs_1 = require("node:fs"); const node_path_1 = require("node:path"); async function setupStreambyPg({ pool, schema = 'streamby', reset = false, allowResetInProd = false, // New parameter }) { const client = await pool.connect(); const lockKey = `streamby:setup:${schema}`; const lockHash = `hashtext('${lockKey}')`; const result = { didCreateSchema: false, didCreateTables: false, didReset: false, errors: [], }; try { // Production safety check if (process.env.NODE_ENV === 'production' && reset && !allowResetInProd) { const errorMessage = `Attempted to reset schema '${schema}' in production environment without explicit 'allowResetInProd' flag. Aborting to prevent data loss.`; result.errors.push(errorMessage); throw new Error(errorMessage); } const lockResult = await client.query(`SELECT pg_try_advisory_lock(${lockHash})::boolean AS locked;`); if (!lockResult.rows[0].locked) { const errorMessage = `Another setup process for schema '${schema}' is already running. Could not acquire advisory lock.`; result.errors.push(errorMessage); throw new Error(errorMessage); } await client.query('BEGIN;'); let fullSql = ''; // Read and append reset SQL if reset is true if (reset) { console.log(`[StreamByPgSetup] Resetting schema "${schema}"...`); const resetSqlPath = (0, node_path_1.join)(process.cwd(), 'src/sql/reset.sql'); const resetSql = await node_fs_1.promises.readFile(resetSqlPath, 'utf8'); fullSql += resetSql; result.didReset = true; result.didCreateSchema = true; // Schema is recreated during reset } else { console.log(`[StreamByPgSetup] Ensuring schema "${schema}" exists...`); // If not resetting, the CREATE SCHEMA IF NOT EXISTS in setup.sql will handle it. // We can assume it will attempt to create if not exists. result.didCreateSchema = true; } // Read and append setup SQL console.log(`[StreamByPgSetup] Creating/updating tables and indexes in schema "${schema}"...`); const setupSqlPath = (0, node_path_1.join)(process.cwd(), 'src/sql/setup.sql'); const setupSql = await node_fs_1.promises.readFile(setupSqlPath, 'utf8'); fullSql += setupSql; result.didCreateTables = true; // Assuming if setup.sql runs, tables are handled // Replace {{SCHEMA}} token with the actual schema name, ensuring proper quoting const finalSql = fullSql.replace(/{{SCHEMA}}/g, `"${schema}"`); await client.query(finalSql); await client.query('COMMIT;'); console.log(`[StreamByPgSetup] Schema setup for "${schema}" completed successfully.`); } catch (error) { await client.query('ROLLBACK;'); const errorMessage = error instanceof Error ? error.message : String(error); result.errors.push(errorMessage); console.error(`[StreamByPgSetup] Error during schema setup for "${schema}":`, error); throw error; // Re-throw the error after logging and capturing } finally { await client.query(`SELECT pg_advisory_unlock(${lockHash});`); client.release(); } return result; }