Brief
Build and explain a three-event local pipeline from a CSV copy to PostgreSQL, stopping the load if checks reveal a missing or duplicated row.
In the story, Voss wants to circulate a PostgreSQL report showing only E7 and E9. Imani gives you the full fictional copy with E7, TRANSFER-7 and E9 and a fresh local lab. Mara asks for an explainable chain before the meeting: DuckDB read, checks, PostgreSQL load and final state. The short deadline belongs to the story; no software is required to solve the numbers.
Your mission
- Match Docker, DuckDB and PostgreSQL with their lab roles.
- Give four expected counts: received CSV,
reviewedtable, different IDs and PostgreSQL table. - Explain what should happen if CSV has only E7 and E9, or contains E7 twice.
- Say how to stop the lab while keeping its volume, then how to remove it only when the copy is no longer useful.
How to check your work
- Docker starts the service, DuckDB reads and transforms, PostgreSQL stores the shared table.
- All four counts are 3 for the complete copy.
- A two-row source or three rows with only two unique IDs fails before publication.
docker compose downkeeps the volume;docker compose down -vremoves it for this disposable lab.
Course example
Workspace version: the CSV is processed with Python and in-memory SQLite. The lesson’s DuckDB and PostgreSQL example needs its own local tools.
import csv
import sqlite3
from io import StringIO
csv_text = 'event_id,vehicle,attempt\nE7,NOCTIS-01,1\nTRANSFER-7,NOCTIS-01,2\nE9,TEST-02,1\n'
received = list(csv.DictReader(StringIO(csv_text)))
reviewed = [(row['event_id'], row['vehicle'], int(row['attempt']))
for row in received if int(row['attempt']) >= 0]
source_count = len(received)
reviewed_count = len(reviewed)
unique_count = len({row[0] for row in reviewed})
if (source_count, reviewed_count, unique_count) != (3, 3, 3):
raise ValueError('incomplete or invalid practice delivery')
with sqlite3.connect(':memory:') as database:
database.execute('CREATE TABLE reviewed_events (event_id TEXT PRIMARY KEY, vehicle TEXT NOT NULL, attempt INTEGER NOT NULL)')
database.executemany('INSERT INTO reviewed_events VALUES (?, ?, ?)', reviewed)
stored_count = database.execute('SELECT COUNT(*) FROM reviewed_events').fetchone()[0]
if stored_count != reviewed_count:
raise ValueError('load count mismatch')
print(f'{source_count} -> {reviewed_count} -> {stored_count}')