Level 6 / Project 04 - Upsert Strategy Lab¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | — | — | Flashcards | — | — |
Focus¶
- insert/update decision logic
Why this project exists¶
This project gives you level-appropriate practice in a realistic operations context. Goal: run the baseline, alter behavior, break one assumption, recover safely, and explain the fix.
Run (copy/paste)¶
Use <repo-root> as the folder containing this repository's README.md.
cd <repo-root>/projects/level-6/04-upsert-strategy-lab
python project.py --input data/sample_input.txt --output data/output_summary.json
pytest -q
Expected terminal output¶
{
"strategy": "on_conflict",
"input_rows": 6,
"inserted": 4,
"updated": 2,
"final_products": 4,
...
}
Expected artifacts¶
data/output_summary.json— upsert results with insert/update counts- Passing tests (
pytest -q→ 6+ passed) - Updated
notes.md
Alter it (required)¶
- Add a
last_updated_bycolumn that records which strategy performed the upsert. - Run the same input with
--strategy replaceand--strategy on_conflict— compare theproductsoutput to see the difference. - Add a
--dry-runflag that validates and counts without actually writing to the database. - Re-run script and tests after each change.
Break it (required)¶
- Feed a CSV row with a non-numeric
price(e.g. "free") and observe the error handling. - Feed duplicate SKUs with conflicting data and compare how
replacevson_conflicthandle it. - Remove the
skucolumn from the CSV and observe the KeyError.
Fix it (required)¶
- Add input validation that catches non-numeric prices before the database insert.
- Add a
--log-conflictsflag that prints when an existing row is being overwritten. - Handle missing columns gracefully with a clear error message.
Explain it (teach-back)¶
- What is the difference between
INSERT OR REPLACEandINSERT ... ON CONFLICT DO UPDATE? - Why does
REPLACEdelete and re-insert whileON CONFLICTupdates in place? - When would you choose one strategy over the other in production?
- What role does the
UNIQUEconstraint play in making upserts work?
Mastery check¶
You can move on when you can: - run baseline without docs, - explain one core function line-by-line, - break and recover in one session, - keep tests passing after your change.
Related Concepts¶
| ← Prev | Home | Next → |
|---|---|---|