Skip to content

fix(db): drop the 9-column recommendations natural key index (#806) - #821

Open
cristim wants to merge 2 commits into
mainfrom
fix/806-drop-9col-index
Open

cristim wants to merge 2 commits into
mainfrom
fix/806-drop-9col-index

Conversation

@cristim

@cristim cristim commented Oct 12, 2026

Copy link
Copy Markdown
Member

Closes #806

Last PR of the chain (#815 migration 000111, #817 PR-B, #818 frontend, #819 PR-C ON CONFLICT on 11 columns). Do not merge before #819 is merged and deployed to dev.

What

Migration 000112 drops the 9-column unique index recommendations_natural_key_az_idx. With the 11-column recommendations_natural_key_az_tp_idx as the only arbiter, EC2 recs that differ only by platform or tenancy coexist in one batch and across runs (no more transitional SQLSTATE 23505).

  • Down: deletes rows that are duplicates under the 9-column key only (keeps newest collected_at, then highest id), then recreates the index exactly as 000109 defined it.
  • Tests: migration test (up/down, coexistence, dedupe, index definition, re-apply), store tests (platform-only and tenancy-only pairs in one batch, same-key re-upsert in place, regional/non-EC2 unaffected, cross-run Linux then Windows, 9-column index gone), and TestPersistCollectionPlatformTenancyInOneBatch using the real convertRecommendations.
  • Verified on a throwaway local PostgreSQL 17 (not a shared DB). The new store/persist tests fail without 000112 and pass with it.

The branch is stacked on #819 (head 3ca3721); this PR's own diff is the single commit on top. Rebase onto main after #819 merges.

Migration number

000112 was the next free number on origin/main when this was written. platform#118 ladder work also plans a 000112; whichever of the two lands second renumbers.

Prod runbook (order matters)

  1. Read prod's current schema version FIRST.
  2. Dispatch database-migration.yml for 000111 with steps limited to the true count of pending migrations up to and including 000111 only. The workflow default (steps empty or 0) is all and would also apply 000112.
  3. Release PR-C (fix(config): upsert recommendations on the 11-column index with platform and tenancy #819, with fix(purchase): carry EC2 platform and tenancy in rec ID, identity key and idempotency key (#806) #817/fix(frontend): include platform and tenancy in EC2 recommendation cell key #818) to prod.
  4. Only then dispatch this migration immediately.
  5. Never apply this migration before PR-C is live in prod: old code's ON CONFLICT on the 9 columns needs the index and every collection write would fail.

Dev

Merge to main only after PR-C is deployed to dev. Dev auto-migrates all pending migrations at startup. Old instances overlapping the rollout keep a valid arbiter only until this migration runs.

Rollback

down is only safe together with a code rollback. PR-C code needs the 11-column arbiter, and once the 9-column index is gone, rolling back PR-C code needs down of this migration (steps=1) first. Down collapses platform/tenancy-only duplicates; the next collection repopulates them.

Review gate: independent review, CI, and local verification evidence to be recorded on this PR before merge.

🤖 Generated with Claude Code

cristim and others added 2 commits October 12, 2026 22:28
…orm and tenancy

Point the recommendations upsert ON CONFLICT arbiter at
recommendations_natural_key_az_tp_idx (the 9 existing key columns plus the
generated platform and tenancy columns from migration 000111). The insert
column list is unchanged and the generated columns are never written or
updated.

Add TestPostgresStore_UpsertRecommendations_PlatformTenancyArbiter (same-key
re-upsert in place, regional non-EC2 rows with empty platform/tenancy,
pre-PR-C row found). Give the legacy-row case in the AZ arbiter test the
platform and tenancy the old collector always stored in details. Bump the
Azure pricing integration test from migration 109 to 111, the only
pinned-schema test that calls the recommendations store.

Refs #806

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Migration 000112 drops recommendations_natural_key_az_idx so EC2 recs that
differ only by platform or tenancy coexist; the 11-column index from 000111
is the upsert arbiter since PR-C. Down collapses platform/tenancy-only
duplicates (keeps newest collected_at) and recreates the 9-column index as
000109 defined it. Adds store, persistCollection and migration tests.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@cristim cristim added urgency/this-sprint Within the current sprint triaged Item has been triaged priority/p1 Next up; this sprint severity/high Significant harm impact/many Affects most users effort/m Days type/bug Defect labels Oct 12, 2026
@coderabbitai

coderabbitai Bot commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

  • Run on-demand review

This review includes 7 billable files and costs up to $1.75.

  • Ask an admin to make reviews automatic

Open in CodeRabbit

Reviews can continue after your included limit without a manual trigger. An admin must approve usage-based billing.

Or wait 41 minutes for your next included review.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available. Your 60 included PR review attempts over the past 7 days set your current allowance at 1 review per hour.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository: LeanerCloud/cloud-commitments-platform/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Essentials
  • Run ID: aaf83514-f405-4087-9cfd-8a1041cabfbd

📥 Commits

Reviewing files that changed from the base of the PR and between 20f6f4f and 57a0e0b.


📒 Files selected for processing (7)
  • internal/config/store_postgres_recommendations.go
  • internal/config/store_postgres_recommendations_test.go
  • internal/database/postgres/migrations/000112_drop_recommendations_9col_natural_key.down.sql
  • internal/database/postgres/migrations/000112_drop_recommendations_9col_natural_key.up.sql
  • internal/database/postgres/migrations/000112_drop_recommendations_9col_natural_key_test.go
  • internal/purchase/azure_pricing_integration_test.go
  • internal/scheduler/scheduler_global_filters_integration_test.go

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/m Days impact/many Affects most users priority/p1 Next up; this sprint severity/high Significant harm triaged Item has been triaged type/bug Defect urgency/this-sprint Within the current sprint

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(recommendations): EC2 tenancy and platform missing from the recommendations natural key and rec ID

1 participant