From c2288161fd31b818cfb26c0edaf080d47e7bbf16 Mon Sep 17 00:00:00 2001 From: Matthieu Date: Tue, 17 Mar 2026 19:47:04 +0100 Subject: [PATCH] chore : add script to check missing piece CFs in prod Co-Authored-By: Claude Opus 4.6 (1M context) --- scripts/check-prod-missing-piece-cfs.php | 54 ++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 scripts/check-prod-missing-piece-cfs.php diff --git a/scripts/check-prod-missing-piece-cfs.php b/scripts/check-prod-missing-piece-cfs.php new file mode 100644 index 0000000..5af58ae --- /dev/null +++ b/scripts/check-prod-missing-piece-cfs.php @@ -0,0 +1,54 @@ + '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)'); +}