Ask a provider for a classification once, even when it has none
Summary
A capable securities provider that answered a classification request with nothing (no sector, no industry, an unmapped kind) was asked again on every sync. Nothing recorded that it had already been asked. With EODHD each ask spends one of the 20 daily requests prices share. This PR records a successful ask and stops asking after it. It also fixes the reset path, which kept industry and so never let the provider answer again.
Fixes jaysbeekay/sure#348. Decides drop 3's U2 question (jaysbeekay/sure#122, option A); this goes upstream inside U2 (part of we-promise/sure#3494).
What changed
- Migration: a nullable
securities.classification_fetched_atdatetime, with no default and no backfill. Security::Provided#import_provider_details:wants_classificationalso requiresclassification_fetched_at.nil?.- On a successful response it stamps
classification_fetched_atwithupdate_column, whether or not anything came back, the same waystore_constituentsstampsconstituents_fetched_at. - A failed response isn't stamped, so it's retried.
- Capability still gates the ask, not the stamp.
HoldingsController#reset_classificationalso clearsindustryandclassification_fetched_at, so a reset asks the provider again.
Deliberately not changed:
- Time-based re-asking. A reset, or
clear_cache: true, re-asks. - Which providers declare classification.
- Per-provider budget accounting.
Migration notes
One nullable column, no default, no backfill, reversible (add_column in change). Existing securities start un-stamped, so each is asked once more and then settles.
Verification
Tests were written first. On main, four fail:
Security::ClassificationIngestionTest:- "a capable provider that answers with nothing is asked once": no record was written.
- "a failed answer is not recorded, so the next sync asks again": there was no record after the successful retry.
- "an answer with a sector is written and recorded".
HoldingsClassificationTest: "a reset clears the industry and the record of an earlier answer, so the provider is asked again" failed on "the reset kept the industry".
These guards pass before and after:
- a provider that cannot classify leaves no record;
- a locked security leaves no record;
clear_cacheasks a recorded security again.
Breaking the fix on purpose fails a test each time:
| Breakage | Caught by |
|---|---|
| Nil-stamp term dropped from the gate | asked once |
| Stamp written on a failed response | failed answer is retried |
| Stamp written only when data came back | asked once, failed answer is retried |
| Reset keeps the stamp | reset clears the record |
Reset keeps industry | reset clears the industry |
Commands:
bin/rails testonee111cd73: 12,335 runs, 53,066 assertions, 0 failures, 0 errors, 35 skips.bin/rubocopon the changed files: no offenses.bin/brakeman --no-pager: 0 warnings.
Blast radius
import_provider_detailshas three callers:MarketDataImporterandAccount::MarketDataImporter, which passinclude_classification: true;HoldingsController#sync_prices, which passes neither keyword, so classification isn't wanted and it's unaffected.
update_columnskips callbacks, soapply_classification_defaultsdoesn't run for the stamp. Classification attributes still go throughupdate(attrs)as before.- Siblings:
reset_classificationis the only writer that clears classification.- The AI classification proposal writes its own source and lock, and is unchanged.
- Display readers of
classification_source,sectorandindustrydon't read the stamp.