Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions docs/production-incident-baseline.md
Original file line number Diff line number Diff line change
Expand Up @@ -15183,11 +15183,13 @@ Drizzle schema and runtime Zod schemas. Findings and remediations:
`0002` files) in a non-executable migration archive. Production's migration
ledger confirmed these are the complete missing applied filenames, so the
repair does not rely on repeated deploy failures to discover them.
Restored `0042_strength_set_activity_id_not_null.sql` to the exact bytes that
production applied from PR #1552's first commit. Later commits in that PR
added a redundant constraint validation before merge, changing the file hash
after the production ledger had recorded it; migration `0004` had already
created and validated that constraint.
Restored migrations `0042` through `0045` to the exact bytes that production
applied from PR #1552's first commit. Later commits in that PR rewrote all four
already-applied files before merge, changing their hashes after the production
ledger had recorded them. A complete comparison of all 73 production ledger
entries against the repository found no missing files and no other unexpected
mismatches; the remaining eight differing hashes are the explicit
transaction-compatibility variants accepted by the migrator.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
- **Validation:** Canonical Postgres rows, merged member IDs, ClickHouse summary,
stream, and zone rows were queried directly in production. Focused web,
mobile, repository, router, and real-Postgres migration tests passed, including
Expand All @@ -15205,11 +15207,16 @@ Drizzle schema and runtime Zod schemas. Findings and remediations:
`cd280085ab7d7092897b537b8994d0906dd3c3d0f75ef1b9c0a3eaa3a6cae2a2`.
The failed deploy quiesced both ClickHouse consumers; they were restored to
one healthy replica each on the prior production image.
- **Remaining risk / follow-up:** Merge and deploy the `0042` restoration, then
The next deploy accepted the restored `0042` and then failed on the same PR's
mutated `0043`, confirming the need to restore the complete four-file set; its
quiesced consumers were also restored to one healthy replica each.
- **Remaining risk / follow-up:** Merge and deploy the complete `0042`–`0045`
restoration, then
verify migrations, the web/worker rollout, both ClickHouse consumers, and the
original climbing activity page. Until that deployment succeeds, production
remains on the previous app image and the user-visible climbing fix is not
live. Failed deploy runs:
[preceding image](https://github.com/Asherlc/dofek/actions/runs/29944229608)
[merged image](https://github.com/Asherlc/dofek/actions/runs/29945041858), and
[complete archive](https://github.com/Asherlc/dofek/actions/runs/29948084505).
[complete archive](https://github.com/Asherlc/dofek/actions/runs/29948084505),
and [partial active-migration restoration](https://github.com/Asherlc/dofek/actions/runs/29950485728).
15 changes: 7 additions & 8 deletions drizzle/0043_tighten_activity_strength_set_invariants.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
-- source-of-truth Drizzle schema, or are protected by a unique index that
-- implicitly forbids NULL once we surface the invariant.

-- strength_set.activity_id is declared NOT NULL in Drizzle
-- (src/db/schema/activity.ts) with an ON DELETE CASCADE FK to activity.id,
-- but the Postgres column was created nullable. All 1020 rows have a value.
ALTER TABLE fitness.strength_set
ALTER COLUMN activity_id SET NOT NULL;

-- activity.external_id is part of unique index activity_provider_external_idx
-- (user_id, provider_id, external_id) and 100% populated in prod. Without
-- NOT NULL the unique index is permissive (NULL is distinct), but UNIQUENESS
-- here is a real invariant: each (user, provider) row must map 1:1 to a
-- provider-side id. Tighten to enforce that invariant at insert time.
ALTER TABLE fitness.activity
ADD CONSTRAINT activity_external_id_not_null_chk
CHECK (external_id IS NOT NULL) NOT VALID;

ALTER TABLE fitness.activity
VALIDATE CONSTRAINT activity_external_id_not_null_chk;

ALTER TABLE fitness.activity
ALTER COLUMN external_id SET NOT NULL;
ALTER COLUMN external_id SET NOT NULL;
67 changes: 18 additions & 49 deletions drizzle/0044_tighten_fk_delete_rules.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,32 @@
--
-- lab_result.panel -> lab_panel: results are owned by their panel; deleting
-- a panel deletes its results.
-- strength_set.exercise -> exercise: exercise is a shared catalog definition;
-- deleting an exercise that is referenced by historical set rows is forbidden.
-- strength_set.exercise -> exercise: deleting an exercise definition removes
-- its set rows (matches activity_interval <-> activity semantics).
-- exercise_alias.exercise -> exercise: aliases are owned by their exercise.
-- exercise_alias.provider -> provider: aliases are provider-owned mappings.
-- daily_metric_value.metric_type -> daily_metric_type: catalog FK; deleting
-- a metric type that is in use should be forbidden, not orphan rows.

ALTER TABLE fitness.lab_result
DROP CONSTRAINT IF EXISTS lab_result_panel_id_fkey,
ADD CONSTRAINT lab_result_panel_id_fkey
FOREIGN KEY (panel_id) REFERENCES fitness.lab_panel (id)
ON DELETE CASCADE ON UPDATE NO ACTION NOT VALID;
DROP CONSTRAINT IF EXISTS lab_result_panel_id_fkey,
ADD CONSTRAINT lab_result_panel_id_fkey
FOREIGN KEY (panel_id) REFERENCES fitness.lab_panel(id)
ON DELETE CASCADE ON UPDATE NO ACTION;

--> statement-breakpoint
ALTER TABLE fitness.lab_result
VALIDATE CONSTRAINT lab_result_panel_id_fkey;

--> statement-breakpoint
ALTER TABLE fitness.strength_set
DROP CONSTRAINT IF EXISTS strength_set_exercise_id_exercise_id_fk,
ADD CONSTRAINT strength_set_exercise_id_exercise_id_fk
FOREIGN KEY (exercise_id) REFERENCES fitness.exercise (id)
ON DELETE RESTRICT ON UPDATE NO ACTION NOT VALID;

--> statement-breakpoint
ALTER TABLE fitness.strength_set
VALIDATE CONSTRAINT strength_set_exercise_id_exercise_id_fk;

--> statement-breakpoint
ALTER TABLE fitness.exercise_alias
DROP CONSTRAINT IF EXISTS exercise_alias_exercise_id_exercise_id_fk,
ADD CONSTRAINT exercise_alias_exercise_id_exercise_id_fk
FOREIGN KEY (exercise_id) REFERENCES fitness.exercise (id)
ON DELETE CASCADE ON UPDATE NO ACTION NOT VALID;
DROP CONSTRAINT IF EXISTS strength_set_exercise_id_exercise_id_fk,
ADD CONSTRAINT strength_set_exercise_id_exercise_id_fk
FOREIGN KEY (exercise_id) REFERENCES fitness.exercise(id)
ON DELETE CASCADE ON UPDATE NO ACTION;

--> statement-breakpoint
ALTER TABLE fitness.exercise_alias
VALIDATE CONSTRAINT exercise_alias_exercise_id_exercise_id_fk;

--> statement-breakpoint
ALTER TABLE fitness.exercise_alias
DROP CONSTRAINT IF EXISTS exercise_alias_provider_id_provider_id_fk,
ADD CONSTRAINT exercise_alias_provider_id_provider_id_fk
FOREIGN KEY (provider_id) REFERENCES fitness.provider (id)
ON DELETE CASCADE ON UPDATE NO ACTION NOT VALID;

--> statement-breakpoint
ALTER TABLE fitness.exercise_alias
VALIDATE CONSTRAINT exercise_alias_provider_id_provider_id_fk;

--> statement-breakpoint
ALTER TABLE fitness.daily_metric_value
DROP CONSTRAINT IF EXISTS daily_metric_value_metric_type_id_fkey,
ADD CONSTRAINT daily_metric_value_metric_type_id_fkey
FOREIGN KEY (metric_type_id) REFERENCES fitness.daily_metric_type (id)
ON DELETE RESTRICT ON UPDATE NO ACTION NOT VALID;
DROP CONSTRAINT IF EXISTS exercise_alias_exercise_id_exercise_id_fk,
ADD CONSTRAINT exercise_alias_exercise_id_exercise_id_fk
FOREIGN KEY (exercise_id) REFERENCES fitness.exercise(id)
ON DELETE CASCADE ON UPDATE NO ACTION;

--> statement-breakpoint
ALTER TABLE fitness.daily_metric_value
VALIDATE CONSTRAINT daily_metric_value_metric_type_id_fkey;
DROP CONSTRAINT IF EXISTS daily_metric_value_metric_type_id_fkey,
ADD CONSTRAINT daily_metric_value_metric_type_id_fkey
FOREIGN KEY (metric_type_id) REFERENCES fitness.daily_metric_type(id)
ON DELETE RESTRICT ON UPDATE NO ACTION;
138 changes: 33 additions & 105 deletions drizzle/0045_check_constraints_and_activity_active_index.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,135 +12,63 @@
-- never reached the activity table).
UPDATE fitness.activity
SET ended_at = started_at + INTERVAL '1 second'
WHERE
provider_id = 'peloton'
AND ended_at = started_at;
WHERE ended_at IS NOT NULL AND ended_at <= started_at;

-- activity: ended_at (when present) must be strictly after started_at.
ALTER TABLE fitness.activity
ADD CONSTRAINT activity_ended_after_started_chk
CHECK (ended_at IS NULL OR ended_at > started_at) NOT VALID;

ALTER TABLE fitness.activity
VALIDATE CONSTRAINT activity_ended_after_started_chk;
ADD CONSTRAINT activity_ended_after_started_chk
CHECK (ended_at IS NULL OR ended_at > started_at);

-- strength_set: weights/reps/distance/duration are physical measurements and
-- cannot be negative when present.
ALTER TABLE fitness.strength_set
ADD CONSTRAINT strength_set_weight_nonneg_chk CHECK (weight_kg IS NULL OR weight_kg >= 0) NOT VALID,
ADD CONSTRAINT strength_set_reps_nonneg_chk CHECK (reps IS NULL OR reps >= 0) NOT VALID,
ADD CONSTRAINT strength_set_distance_nonneg_chk CHECK (distance_meters IS NULL OR distance_meters >= 0) NOT VALID,
ADD CONSTRAINT strength_set_duration_nonneg_chk CHECK (duration_seconds IS NULL OR duration_seconds >= 0) NOT VALID;

ALTER TABLE fitness.strength_set
VALIDATE CONSTRAINT strength_set_weight_nonneg_chk;

ALTER TABLE fitness.strength_set
VALIDATE CONSTRAINT strength_set_reps_nonneg_chk;

ALTER TABLE fitness.strength_set
VALIDATE CONSTRAINT strength_set_distance_nonneg_chk;

ALTER TABLE fitness.strength_set
VALIDATE CONSTRAINT strength_set_duration_nonneg_chk;
ADD CONSTRAINT strength_set_weight_nonneg_chk CHECK (weight_kg IS NULL OR weight_kg >= 0),
ADD CONSTRAINT strength_set_reps_nonneg_chk CHECK (reps IS NULL OR reps >= 0),
ADD CONSTRAINT strength_set_distance_nonneg_chk CHECK (distance_meters IS NULL OR distance_meters >= 0),
ADD CONSTRAINT strength_set_duration_nonneg_chk CHECK (duration_seconds IS NULL OR duration_seconds >= 0);

-- daily_metrics: cumulative measurements cannot be negative when present.
ALTER TABLE fitness.daily_metrics
ADD CONSTRAINT daily_metrics_steps_nonneg_chk CHECK (steps IS NULL OR steps >= 0) NOT VALID,
ADD CONSTRAINT daily_metrics_active_energy_nonneg_chk CHECK (active_energy_kcal IS NULL OR active_energy_kcal >= 0) NOT VALID,
ADD CONSTRAINT daily_metrics_basal_energy_nonneg_chk CHECK (basal_energy_kcal IS NULL OR basal_energy_kcal >= 0) NOT VALID,
ADD CONSTRAINT daily_metrics_distance_nonneg_chk CHECK (distance_km IS NULL OR distance_km >= 0) NOT VALID,
ADD CONSTRAINT daily_metrics_cycling_distance_nonneg_chk CHECK (cycling_distance_km IS NULL OR cycling_distance_km >= 0) NOT VALID,
ADD CONSTRAINT daily_metrics_flights_climbed_nonneg_chk CHECK (flights_climbed IS NULL OR flights_climbed >= 0) NOT VALID,
ADD CONSTRAINT daily_metrics_exercise_minutes_nonneg_chk CHECK (exercise_minutes IS NULL OR exercise_minutes >= 0) NOT VALID,
ADD CONSTRAINT daily_metrics_hrv_nonneg_chk CHECK (hrv IS NULL OR hrv >= 0) NOT VALID;

ALTER TABLE fitness.daily_metrics
VALIDATE CONSTRAINT daily_metrics_steps_nonneg_chk;

ALTER TABLE fitness.daily_metrics
VALIDATE CONSTRAINT daily_metrics_active_energy_nonneg_chk;

ALTER TABLE fitness.daily_metrics
VALIDATE CONSTRAINT daily_metrics_basal_energy_nonneg_chk;

ALTER TABLE fitness.daily_metrics
VALIDATE CONSTRAINT daily_metrics_distance_nonneg_chk;

ALTER TABLE fitness.daily_metrics
VALIDATE CONSTRAINT daily_metrics_cycling_distance_nonneg_chk;

ALTER TABLE fitness.daily_metrics
VALIDATE CONSTRAINT daily_metrics_flights_climbed_nonneg_chk;

ALTER TABLE fitness.daily_metrics
VALIDATE CONSTRAINT daily_metrics_exercise_minutes_nonneg_chk;

ALTER TABLE fitness.daily_metrics
VALIDATE CONSTRAINT daily_metrics_hrv_nonneg_chk;
ADD CONSTRAINT daily_metrics_steps_nonneg_chk CHECK (steps IS NULL OR steps >= 0),
ADD CONSTRAINT daily_metrics_active_energy_nonneg_chk CHECK (active_energy_kcal IS NULL OR active_energy_kcal >= 0),
ADD CONSTRAINT daily_metrics_basal_energy_nonneg_chk CHECK (basal_energy_kcal IS NULL OR basal_energy_kcal >= 0),
ADD CONSTRAINT daily_metrics_distance_nonneg_chk CHECK (distance_km IS NULL OR distance_km >= 0),
ADD CONSTRAINT daily_metrics_cycling_distance_nonneg_chk CHECK (cycling_distance_km IS NULL OR cycling_distance_km >= 0),
ADD CONSTRAINT daily_metrics_flights_climbed_nonneg_chk CHECK (flights_climbed IS NULL OR flights_climbed >= 0),
ADD CONSTRAINT daily_metrics_exercise_minutes_nonneg_chk CHECK (exercise_minutes IS NULL OR exercise_minutes >= 0),
ADD CONSTRAINT daily_metrics_hrv_nonneg_chk CHECK (hrv IS NULL OR hrv >= 0);

-- sleep_session: durations cannot be negative, efficiency is a percentage.
ALTER TABLE fitness.sleep_session
ADD CONSTRAINT sleep_session_duration_nonneg_chk CHECK (duration_minutes IS NULL OR duration_minutes >= 0) NOT VALID,
ADD CONSTRAINT sleep_session_deep_nonneg_chk CHECK (deep_minutes IS NULL OR deep_minutes >= 0) NOT VALID,
ADD CONSTRAINT sleep_session_rem_nonneg_chk CHECK (rem_minutes IS NULL OR rem_minutes >= 0) NOT VALID,
ADD CONSTRAINT sleep_session_light_nonneg_chk CHECK (light_minutes IS NULL OR light_minutes >= 0) NOT VALID,
ADD CONSTRAINT sleep_session_awake_nonneg_chk CHECK (awake_minutes IS NULL OR awake_minutes >= 0) NOT VALID,
ADD CONSTRAINT sleep_session_efficiency_range_chk CHECK (efficiency_pct IS NULL OR (efficiency_pct >= 0 AND efficiency_pct <= 100)) NOT VALID;

ALTER TABLE fitness.sleep_session
VALIDATE CONSTRAINT sleep_session_duration_nonneg_chk;

ALTER TABLE fitness.sleep_session
VALIDATE CONSTRAINT sleep_session_deep_nonneg_chk;

ALTER TABLE fitness.sleep_session
VALIDATE CONSTRAINT sleep_session_rem_nonneg_chk;

ALTER TABLE fitness.sleep_session
VALIDATE CONSTRAINT sleep_session_light_nonneg_chk;

ALTER TABLE fitness.sleep_session
VALIDATE CONSTRAINT sleep_session_awake_nonneg_chk;

ALTER TABLE fitness.sleep_session
VALIDATE CONSTRAINT sleep_session_efficiency_range_chk;
ADD CONSTRAINT sleep_session_duration_nonneg_chk CHECK (duration_minutes IS NULL OR duration_minutes >= 0),
ADD CONSTRAINT sleep_session_deep_nonneg_chk CHECK (deep_minutes IS NULL OR deep_minutes >= 0),
ADD CONSTRAINT sleep_session_rem_nonneg_chk CHECK (rem_minutes IS NULL OR rem_minutes >= 0),
ADD CONSTRAINT sleep_session_light_nonneg_chk CHECK (light_minutes IS NULL OR light_minutes >= 0),
ADD CONSTRAINT sleep_session_awake_nonneg_chk CHECK (awake_minutes IS NULL OR awake_minutes >= 0),
ADD CONSTRAINT sleep_session_efficiency_range_chk CHECK (efficiency_pct IS NULL OR (efficiency_pct >= 0 AND efficiency_pct <= 100));

-- food_entry: serving amounts must be positive when present.
ALTER TABLE fitness.food_entry
ADD CONSTRAINT food_entry_serving_weight_pos_chk CHECK (serving_weight_grams IS NULL OR serving_weight_grams > 0) NOT VALID,
ADD CONSTRAINT food_entry_number_of_units_nonneg_chk CHECK (number_of_units IS NULL OR number_of_units >= 0) NOT VALID;

ALTER TABLE fitness.food_entry
VALIDATE CONSTRAINT food_entry_serving_weight_pos_chk;

ALTER TABLE fitness.food_entry
VALIDATE CONSTRAINT food_entry_number_of_units_nonneg_chk;
ADD CONSTRAINT food_entry_serving_weight_pos_chk CHECK (serving_weight_grams IS NULL OR serving_weight_grams > 0),
ADD CONSTRAINT food_entry_number_of_units_nonneg_chk CHECK (number_of_units IS NULL OR number_of_units >= 0);

-- lab_result: reference range low/high must be ordered when both present.
ALTER TABLE fitness.lab_result
ADD CONSTRAINT lab_result_range_order_chk
CHECK (
reference_range_low IS NULL
OR reference_range_high IS NULL
OR reference_range_high >= reference_range_low
) NOT VALID;

ALTER TABLE fitness.lab_result
VALIDATE CONSTRAINT lab_result_range_order_chk;
ADD CONSTRAINT lab_result_range_order_chk
CHECK (
reference_range_low IS NULL
OR reference_range_high IS NULL
OR reference_range_high >= reference_range_low
);

-- dexa_scan: body composition percentages cannot be negative when present.
ALTER TABLE fitness.dexa_scan
ADD CONSTRAINT dexa_scan_body_fat_pct_range_chk
CHECK (body_fat_pct IS NULL OR (body_fat_pct >= 0 AND body_fat_pct <= 100)) NOT VALID;

ALTER TABLE fitness.dexa_scan
VALIDATE CONSTRAINT dexa_scan_body_fat_pct_range_chk;
ADD CONSTRAINT dexa_scan_body_fat_pct_nonneg_chk CHECK (body_fat_pct IS NULL OR body_fat_pct >= 0);

-- Partial index for dashboard activity visibility: every user-facing query
-- filters out soft-deleted and provider-absent rows. A partial index on the
-- active subset keeps that high-frequency predicate cheap and gives the
-- query planner a tight baseline.
CREATE INDEX IF NOT EXISTS activity_active_user_started_idx -- noqa: PG01
ON fitness.activity (user_id, started_at DESC)
WHERE deleted_at IS NULL AND provider_absent_at IS NULL;
CREATE INDEX IF NOT EXISTS activity_active_user_started_idx
ON fitness.activity (user_id, started_at DESC)
WHERE deleted_at IS NULL AND provider_absent_at IS NULL;