Files
Inventory/scripts/check-prod-audit-dates.php
Matthieu 38777b7de0 fix(custom-fields) : prevent data loss on ModelType save + restoration scripts
Backend: match existing CustomField by name as fallback when ID is not provided,
preventing deletion and recreation of field definitions (which cascade-deletes values).

Includes restoration/migration scripts for prod:
- restore-custom-field-values.php: restores piece values from audit logs
- migrate-orphaned-custom-fields.php: migrates values from orphaned CFs
- fix-prod-all.php: combined fix (migrate + restore + cleanup)
- fix-prod-recreate-and-migrate.php: full fix (recreate missing CFs + migrate + restore)
- check-prod-*.php: diagnostic scripts

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 20:24:37 +01:00

47 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__.'/../vendor/autoload.php';
use Doctrine\DBAL\DriverManager;
$conn = DriverManager::getConnection([
'driver' => 'pdo_pgsql',
'host' => 'localhost',
'port' => 5432,
'dbname' => 'inventory',
'user' => 'ferme_user',
'password' => 'fermerecette',
]);
echo "--- Audit logs with customField deletions (to:null) ---\n";
$rows = $conn->fetchAllAssociative("
SELECT al.entityid, al.entitytype, al.diff::text as diff, al.createdat
FROM audit_logs al
WHERE al.diff::text LIKE '%customField%'
AND al.diff::text LIKE '%\"to\":null%'
ORDER BY al.createdat DESC
LIMIT 20
");
echo sprintf("Found %d entries\n\n", count($rows));
foreach ($rows as $r) {
echo sprintf("[%s] %s %s: %s\n", $r['createdat'], $r['entitytype'], $r['entityid'], substr($r['diff'], 0, 120));
}
echo "\n--- Orphaned CFValues (pointing to CFs with no ModelType) ---\n";
$rows = $conn->fetchAllAssociative("
SELECT COUNT(*) as cnt,
CASE WHEN cfv.pieceid IS NOT NULL THEN 'piece'
WHEN cfv.composantid IS NOT NULL THEN 'composant'
WHEN cfv.productid IS NOT NULL THEN 'product'
ELSE 'unknown' END as entity_type
FROM custom_field_values cfv
JOIN custom_fields cf ON cf.id = cfv.customfieldid
WHERE cf.typecomposantid IS NULL AND cf.typepieceid IS NULL AND cf.typeproductid IS NULL
GROUP BY entity_type
");
foreach ($rows as $r) {
echo sprintf(" %s: %d orphaned values\n", $r['entity_type'], $r['cnt']);
}