diff --git a/internal/database/migrations/20260425145831_fix_recording_numeric_precision.down.sql b/internal/database/migrations/20260425145831_fix_recording_numeric_precision.down.sql new file mode 100644 index 00000000..2801155d --- /dev/null +++ b/internal/database/migrations/20260425145831_fix_recording_numeric_precision.down.sql @@ -0,0 +1,11 @@ +BEGIN; + +-- Revert fcr_value and cum_depletion_rate back to NUMERIC(7,3). +-- WARNING: any value with an integer part > 9999 (e.g. high-FCR early-laying recordings) +-- will fail the cast and must be cleared first, or this rollback will error. + +ALTER TABLE recordings + ALTER COLUMN fcr_value TYPE NUMERIC(7,3) USING fcr_value::NUMERIC(7,3), + ALTER COLUMN cum_depletion_rate TYPE NUMERIC(7,3) USING cum_depletion_rate::NUMERIC(7,3); + +COMMIT; diff --git a/internal/database/migrations/20260425145831_fix_recording_numeric_precision.up.sql b/internal/database/migrations/20260425145831_fix_recording_numeric_precision.up.sql new file mode 100644 index 00000000..a084184a --- /dev/null +++ b/internal/database/migrations/20260425145831_fix_recording_numeric_precision.up.sql @@ -0,0 +1,13 @@ +BEGIN; + +-- fcr_value and cum_depletion_rate were created as NUMERIC(7,3) (max integer part: 9999). +-- Early-laying flocks produce very few eggs relative to total feed consumed, so +-- FCR = usageInGrams / totalEggWeightGrams can legitimately exceed 9999 (e.g. ~31 740). +-- Widening to NUMERIC(15,3) keeps the same 3-decimal-place scale and is +-- fully backward-compatible: no existing value will be truncated or altered. + +ALTER TABLE recordings + ALTER COLUMN fcr_value TYPE NUMERIC(15,3) USING fcr_value::NUMERIC(15,3), + ALTER COLUMN cum_depletion_rate TYPE NUMERIC(15,3) USING cum_depletion_rate::NUMERIC(15,3); + +COMMIT;