88 lines
2.6 KiB
PHP
88 lines
2.6 KiB
PHP
<?php
|
|
// Auto-inicializa pequeños ajustes del esquema de Marketing cuando faltan columnas.
|
|
// Así evitamos errores al abrir la pantalla o guardar datos nuevos.
|
|
|
|
function marketing_apply_migration_if_missing(PDO $pdo, string $columnName, string $migrationFile): void
|
|
{
|
|
$checkColumnStmt = $pdo->prepare(
|
|
'SELECT COUNT(*) AS c '
|
|
. 'FROM information_schema.columns '
|
|
. 'WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?'
|
|
);
|
|
|
|
$checkColumnStmt->execute(['marketing_videos', $columnName]);
|
|
$row = $checkColumnStmt->fetch(PDO::FETCH_ASSOC);
|
|
$count = (int)($row['c'] ?? 0);
|
|
|
|
if ($count > 0) {
|
|
return;
|
|
}
|
|
|
|
if (!is_file($migrationFile)) {
|
|
throw new RuntimeException('No se encontró la migración: ' . basename($migrationFile));
|
|
}
|
|
|
|
$sql = file_get_contents($migrationFile);
|
|
if ($sql === false) {
|
|
throw new RuntimeException('No se pudo leer la migración: ' . basename($migrationFile));
|
|
}
|
|
|
|
$pdo->exec($sql);
|
|
}
|
|
|
|
function marketing_apply_migration_if_column_type_missing(PDO $pdo, string $columnName, string $needle, string $migrationFile): void
|
|
{
|
|
$checkColumnStmt = $pdo->prepare(
|
|
'SELECT COLUMN_TYPE AS column_type '
|
|
. 'FROM information_schema.columns '
|
|
. 'WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?'
|
|
);
|
|
|
|
$checkColumnStmt->execute(['marketing_videos', $columnName]);
|
|
$row = $checkColumnStmt->fetch(PDO::FETCH_ASSOC);
|
|
$columnType = (string)($row['column_type'] ?? '');
|
|
|
|
if ($columnType !== '' && stripos($columnType, $needle) !== false) {
|
|
return;
|
|
}
|
|
|
|
if (!is_file($migrationFile)) {
|
|
throw new RuntimeException('No se encontró la migración: ' . basename($migrationFile));
|
|
}
|
|
|
|
$sql = file_get_contents($migrationFile);
|
|
if ($sql === false) {
|
|
throw new RuntimeException('No se pudo leer la migración: ' . basename($migrationFile));
|
|
}
|
|
|
|
$pdo->exec($sql);
|
|
}
|
|
|
|
function marketing_ensure_schema(PDO $pdo): void
|
|
{
|
|
static $attempted = false;
|
|
if ($attempted) {
|
|
return;
|
|
}
|
|
$attempted = true;
|
|
|
|
marketing_apply_migration_if_missing(
|
|
$pdo,
|
|
'observacion',
|
|
__DIR__ . '/../db/migrations/086_add_observacion_to_marketing_videos.sql'
|
|
);
|
|
|
|
marketing_apply_migration_if_missing(
|
|
$pdo,
|
|
'resultados',
|
|
__DIR__ . '/../db/migrations/087_add_resultados_to_marketing_videos.sql'
|
|
);
|
|
|
|
marketing_apply_migration_if_column_type_missing(
|
|
$pdo,
|
|
'resultados',
|
|
'REDIMIENTO MEDIUM',
|
|
__DIR__ . '/../db/migrations/088_update_resultados_enum_to_include_redimiento_medium.sql'
|
|
);
|
|
}
|