Open lesson

Mission: deliver the transfer page without losing it

Command palette

This mission workspace keeps code and notes in this browser tab. Export them before closing it. Running code is practice, not an automatic grade for the mission.

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

  1. Match Docker, DuckDB and PostgreSQL with their lab roles.
  2. Give four expected counts: received CSV, reviewed table, different IDs and PostgreSQL table.
  3. Explain what should happen if CSV has only E7 and E9, or contains E7 twice.
  4. 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 down keeps the volume; docker compose down -v removes 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}')

Code

Use Tab to leave the editor. Ctrl+Enter runs the source. The lines beginning with # are task reminders. Python ignores them. Write your code beneath them.

Python console

Loading laboratory…

Explore Python or SQL here; this is not an operating-system shell.

This is a disposable browser experiment. The code run here is not recorded as a completed mission.

Each command starts a fresh disposable worker: it runs a captured copy of your current source, then your command with the lesson fixtures. Changes are discarded afterward. Commands never save source or award XP. Ctrl+Enter executes the command.

Virtual workspace files

Source and declarative assets are readable here without JavaScript. Python input files are created at runtime; SQL setup initializes the disposable database.

source.py

Current editor source. Inspection shows your current edits.

# Match Docker, DuckDB and PostgreSQL with their lab roles.
# Give four expected counts: received CSV, `reviewed` table, 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.

exemple.py

Declarative lesson asset; this is not a file created in the Python filesystem.

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}')