95 lines
2.6 KiB
JavaScript
95 lines
2.6 KiB
JavaScript
|
|
#!/usr/bin/env node
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Script para crear la base de datos y ejecutar migraciones
|
|||
|
|
*
|
|||
|
|
* Uso: node scripts/db-create.js
|
|||
|
|
*
|
|||
|
|
* IMPORTANTE: Requiere PostgreSQL instalado y configurado
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
import { readFile } from 'fs/promises';
|
|||
|
|
import { join, dirname } from 'path';
|
|||
|
|
import { fileURLToPath } from 'url';
|
|||
|
|
import pg from 'pg';
|
|||
|
|
import dotenv from 'dotenv';
|
|||
|
|
|
|||
|
|
dotenv.config();
|
|||
|
|
|
|||
|
|
const { Client } = pg;
|
|||
|
|
const __filename = fileURLToPath(import.meta.url);
|
|||
|
|
const __dirname = dirname(__filename);
|
|||
|
|
const projectRoot = join(__dirname, '..', '..');
|
|||
|
|
|
|||
|
|
async function main() {
|
|||
|
|
const client = new Client({
|
|||
|
|
host: process.env.DB_HOST || 'localhost',
|
|||
|
|
port: parseInt(process.env.DB_PORT || '5432', 10),
|
|||
|
|
database: 'postgres', // Conectar a postgres para crear la BD
|
|||
|
|
user: process.env.DB_USER || 'postgres',
|
|||
|
|
password: process.env.DB_PASSWORD || '',
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
await client.connect();
|
|||
|
|
console.log('✅ Conectado a PostgreSQL');
|
|||
|
|
|
|||
|
|
// Crear base de datos si no existe
|
|||
|
|
const dbName = process.env.DB_NAME || 'emerges_tes';
|
|||
|
|
const dbExists = await client.query(
|
|||
|
|
`SELECT 1 FROM pg_database WHERE datname = $1`,
|
|||
|
|
[dbName]
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
if (dbExists.rows.length === 0) {
|
|||
|
|
await client.query(`CREATE DATABASE ${dbName}`);
|
|||
|
|
console.log(`✅ Base de datos '${dbName}' creada`);
|
|||
|
|
} else {
|
|||
|
|
console.log(`ℹ️ Base de datos '${dbName}' ya existe`);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
await client.end();
|
|||
|
|
|
|||
|
|
// Conectar a la base de datos creada
|
|||
|
|
const dbClient = new Client({
|
|||
|
|
host: process.env.DB_HOST || 'localhost',
|
|||
|
|
port: parseInt(process.env.DB_PORT || '5432', 10),
|
|||
|
|
database: dbName,
|
|||
|
|
user: process.env.DB_USER || 'postgres',
|
|||
|
|
password: process.env.DB_PASSWORD || '',
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
await dbClient.connect();
|
|||
|
|
console.log(`✅ Conectado a base de datos '${dbName}'`);
|
|||
|
|
|
|||
|
|
// Ejecutar migraciones
|
|||
|
|
console.log('\n📦 Ejecutando migraciones...\n');
|
|||
|
|
|
|||
|
|
// Migración 001: Crear esquema
|
|||
|
|
const migration001 = await readFile(
|
|||
|
|
join(projectRoot, 'database', 'migrations', '001_create_schema.sql'),
|
|||
|
|
'utf-8'
|
|||
|
|
);
|
|||
|
|
await dbClient.query(migration001);
|
|||
|
|
console.log('✅ Migración 001: Esquema creado');
|
|||
|
|
|
|||
|
|
// Migración 002: Funciones y triggers
|
|||
|
|
const migration002 = await readFile(
|
|||
|
|
join(projectRoot, 'database', 'migrations', '002_create_functions.sql'),
|
|||
|
|
'utf-8'
|
|||
|
|
);
|
|||
|
|
await dbClient.query(migration002);
|
|||
|
|
console.log('✅ Migración 002: Funciones y triggers creados');
|
|||
|
|
|
|||
|
|
await dbClient.end();
|
|||
|
|
|
|||
|
|
console.log('\n🎉 Base de datos configurada correctamente\n');
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('❌ Error:', error.message);
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
main();
|
|||
|
|
|