146 lines
5.0 KiB
PHP
146 lines
5.0 KiB
PHP
<?php
|
|
// Auto-inicializa el esquema de "Recursos Humanos" si faltan tablas o columnas.
|
|
// Esto evita que el usuario vea errores 500 cuando la base de datos del entorno
|
|
// no tiene ejecutadas las migraciones HR.
|
|
|
|
function hr_ensure_schema(PDO $pdo): void
|
|
{
|
|
static $attempted = false;
|
|
if ($attempted) {
|
|
return;
|
|
}
|
|
$attempted = true;
|
|
|
|
$requiredTables = [
|
|
'hr_reclutamiento',
|
|
'hr_colaboradores',
|
|
'hr_manuales',
|
|
'hr_evaluaciones',
|
|
];
|
|
|
|
// 1) Asegurar tablas requeridas
|
|
$missingTables = [];
|
|
$checkTableStmt = $pdo->prepare(
|
|
'SELECT COUNT(*) AS c '
|
|
. 'FROM information_schema.tables '
|
|
. 'WHERE table_schema = DATABASE() AND table_name = ?'
|
|
);
|
|
|
|
foreach ($requiredTables as $t) {
|
|
$checkTableStmt->execute([$t]);
|
|
$row = $checkTableStmt->fetch(PDO::FETCH_ASSOC);
|
|
$count = (int)($row['c'] ?? 0);
|
|
if ($count === 0) {
|
|
$missingTables[] = $t;
|
|
}
|
|
}
|
|
|
|
if (!empty($missingTables)) {
|
|
// Ejecutamos las migraciones HR en orden para que existan claves.
|
|
$migrationFiles = [
|
|
__DIR__ . '/../db/migrations/080_create_hr_reclutamiento_table.sql',
|
|
__DIR__ . '/../db/migrations/081_create_hr_colaboradores_table.sql',
|
|
__DIR__ . '/../db/migrations/082_create_hr_manuales_table.sql',
|
|
__DIR__ . '/../db/migrations/083_create_hr_evaluaciones_table.sql',
|
|
];
|
|
|
|
foreach ($migrationFiles as $file) {
|
|
if (!is_file($file)) {
|
|
throw new RuntimeException('No se encontró la migración: ' . basename($file));
|
|
}
|
|
$sql = file_get_contents($file);
|
|
if ($sql === false) {
|
|
throw new RuntimeException('No se pudo leer la migración: ' . basename($file));
|
|
}
|
|
$pdo->exec($sql);
|
|
}
|
|
}
|
|
|
|
// 2) Asegurar columnas necesarias en hr_reclutamiento
|
|
$requiredColumns = [
|
|
'hr_reclutamiento' => [
|
|
'edad_hijos_estudia_trabaja',
|
|
'organizacion_personal',
|
|
'claridad_expresarse',
|
|
'manejo_objeciones',
|
|
'experiencia_otros_trabajos',
|
|
'experiencia_relevante_ventas',
|
|
'empatia_trato',
|
|
],
|
|
];
|
|
|
|
$checkColStmt = $pdo->prepare(
|
|
'SELECT COUNT(*) AS c '
|
|
. 'FROM information_schema.columns '
|
|
. 'WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?'
|
|
);
|
|
|
|
$missingColumns = [];
|
|
foreach ($requiredColumns as $table => $cols) {
|
|
foreach ($cols as $col) {
|
|
$checkColStmt->execute([$table, $col]);
|
|
$row = $checkColStmt->fetch(PDO::FETCH_ASSOC);
|
|
$count = (int)($row['c'] ?? 0);
|
|
if ($count === 0) {
|
|
$missingColumns[] = $table . '.' . $col;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($missingColumns)) {
|
|
$file = __DIR__ . '/../db/migrations/084_add_hr_reclutamiento_indicadores_columns.sql';
|
|
if (!is_file($file)) {
|
|
throw new RuntimeException('No se encontró la migración: ' . basename($file));
|
|
}
|
|
$sql = file_get_contents($file);
|
|
if ($sql === false) {
|
|
throw new RuntimeException('No se pudo leer la migración: ' . basename($file));
|
|
}
|
|
$pdo->exec($sql);
|
|
}
|
|
|
|
// 3) Asegurar tipos correctos para columnas de indicadores (deben aceptar LETRAS)
|
|
// Si alguna columna quedó como INT en una BD antigua, evitará errores al guardar valores como "ESTUDIANTE".
|
|
$indicatorColumns = [
|
|
'organizacion_personal',
|
|
'claridad_expresarse',
|
|
'manejo_objeciones',
|
|
'experiencia_otros_trabajos',
|
|
'experiencia_relevante_ventas',
|
|
'empatia_trato',
|
|
];
|
|
|
|
$checkTypeStmt = $pdo->prepare(
|
|
'SELECT DATA_TYPE FROM information_schema.columns ' .
|
|
'WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?'
|
|
);
|
|
|
|
$needsIndicatorConversion = false;
|
|
foreach ($indicatorColumns as $col) {
|
|
$checkTypeStmt->execute(['hr_reclutamiento', $col]);
|
|
$row = $checkTypeStmt->fetch(PDO::FETCH_ASSOC);
|
|
$dataType = strtolower((string)($row['DATA_TYPE'] ?? ''));
|
|
|
|
// Si no es VARCHAR, la convertimos (cubre casos INT/NUMERIC en BD viejas).
|
|
if ($dataType !== 'varchar') {
|
|
// Si la columna no existe, lo manejamos con el bloque anterior de "missingColumns".
|
|
if ($dataType !== '') {
|
|
$needsIndicatorConversion = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($needsIndicatorConversion) {
|
|
$file = __DIR__ . '/../db/migrations/085_convert_hr_reclutamiento_indicadores_to_varchar.sql';
|
|
if (!is_file($file)) {
|
|
throw new RuntimeException('No se encontró la migración: ' . basename($file));
|
|
}
|
|
$sql = file_get_contents($file);
|
|
if ($sql === false) {
|
|
throw new RuntimeException('No se pudo leer la migración: ' . basename($file));
|
|
}
|
|
$pdo->exec($sql);
|
|
}
|
|
}
|