Files
Inventory/scripts/check-prod-missing-piece-cfs.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

55 lines
1.9 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 "--- ModelTypes with orphaned piece values (CFs lost) ---\n\n";
$rows = $conn->fetchAllAssociative("
SELECT mt.id, mt.name, mt.category,
cf_orphan.name as lost_field,
COUNT(*) as affected_pieces,
COUNT(*) FILTER (WHERE cfv.value != '' AND cfv.value IS NOT NULL) as with_data
FROM custom_field_values cfv
JOIN custom_fields cf_orphan ON cf_orphan.id = cfv.customfieldid
JOIN pieces p ON p.id = cfv.pieceid
JOIN model_types mt ON mt.id = p.typepieceid
WHERE cf_orphan.typecomposantid IS NULL
AND cf_orphan.typepieceid IS NULL
AND cf_orphan.typeproductid IS NULL
GROUP BY mt.id, mt.name, mt.category, cf_orphan.name
ORDER BY mt.name, cf_orphan.name
");
foreach ($rows as $r) {
$status = $r['with_data'] > 0 ? 'HAS DATA' : 'empty';
echo sprintf(" ModelType '%s' | field '%s' | %d pieces (%d with data) [%s]\n",
$r['name'], $r['lost_field'], $r['affected_pieces'], $r['with_data'], $status);
}
echo sprintf("\nTotal: %d ModelType/field combinations\n", count($rows));
// Check if these fields exist on the current ModelType
echo "\n--- Current CFs on these ModelTypes ---\n\n";
$mtIds = array_unique(array_column($rows, 'id'));
foreach ($mtIds as $mtId) {
$mtName = $conn->fetchOne("SELECT name FROM model_types WHERE id = ?", [$mtId]);
$currentCfs = $conn->fetchAllAssociative(
"SELECT name FROM custom_fields WHERE typepieceid = ? ORDER BY orderindex",
[$mtId]
);
$cfNames = array_column($currentCfs, 'name');
echo sprintf(" '%s': %s\n", $mtName, $cfNames ? implode(', ', $cfNames) : '(aucun CF)');
}