A production-grade ETL and data warehouse pipeline I designed and built to turn a flat, messy chocolate sales dataset into a structured PostgreSQL warehouse. Three schemas (raw, operational, analytics), modular ETL with feature engineering, surrogate keys, foreign-key validation, and a single-command orchestrator. Built to be reliable, reproducible, maintainable, analytics-ready, and observable. Not just to transform CSVs.
The source data is a single Kaggle dataset with five flat CSV files (calendar, customers, products, sales, stores). It is the kind of dataset that looks tidy on the surface but breaks the moment you try to model it for analytics. Inconsistent IDs, missing reference rows, no surrogate keys, and zero separation between source data and analytical structure.
The platform is built around a layered architecture that separates concerns at every stage. Raw data is preserved untouched, the operational layer stores cleaned and standardised tables, and the analytics layer is the dimensional warehouse used for reporting. Each layer has its own PostgreSQL schema and its own loader module.
# Run the entire pipeline end-to-end python -m etl.run_all # What run_all.py executes, in order: 1. wipe_all # → drop folders + reset all 3 schemas 2. extract # → Kaggle CLI download, unzip, log row counts 3. clean # → standardise, type cast, engineer features 4. transform # → build dim & fact tables, validate FKs 5. load_raw_schema # → raw_data schema (source fidelity) 6. load_operationals_schema # → operationals schema (cleaned) 7. load_analytics_schema # → analytics schema (dim + fact) # Every step: timed, logged via custom ColorFormatter, fail-fast
Each schema has a clear, single responsibility. Raw is for source fidelity and audit. Operational is for cleaned, standardised data with engineered features but no modelling. Analytics is the dimensional warehouse, indexed and ready for BI workloads.
raw_data (source fidelity, untouched) ├── raw_data.calendar ├── raw_data.customers ├── raw_data.products ├── raw_data.sales └── raw_data.stores operationals (cleaned, standardised, feature-engineered) ├── operationals.cleaned_calendar ├── operationals.cleaned_customers ├── operationals.cleaned_products ├── operationals.cleaned_sales ├── operationals.cleaned_stores └── operationals.cleaned_regions analytics (dimensional model, BI-ready) ├── analytics.dim_calendar ├── analytics.dim_customers ├── analytics.dim_products ├── analytics.dim_stores ├── analytics.dim_regions └── analytics.fact_sales
-- Star schema lineage (mostly star, one intentional snowflake) dim_calendar ─┐ dim_customers ─┤ dim_products ─┼──> fact_sales dim_stores ─┤ │ dim_regions ─┘ │ ↑ direct region_key FK for "revenue by region" queries -- Five FK validations run in transform.py before fact load -- DB enforces FK RESTRICT + 7 CHECK constraints + 13 indexes
The cleaning stage is not just type casting and null handling. Each table gets domain-specific engineered features that make downstream BI work meaningful, not mechanical.
Raw, operational, and analytics each have their own schema rather than collapsing into one. This makes lineage obvious, makes auditing trivial, and lets each layer be wiped or rebuilt independently. Slightly more storage cost, much clearer mental model.
Each pipeline run drops and recreates all schemas before loading. No upsert logic, no partial-load bugs, fully deterministic output. Tradeoff: not suitable for very large datasets, but ideal for project-scale reproducibility and CI testing.
Every dim gets its surrogate key from df.sort_values(natural_id).reset_index(drop=True).index + 1. Deterministic, simple, fast. Tradeoff: keys shift if new rows are added, so this only works because the warehouse is rebuilt from scratch every run. For merged data over time, SHA-256 content hashing would be the right call instead.
Engineered features (tenure, segments, buckets, time of day) are computed in the cleaning stage, not in BI tools. Analysts get clean, semantically meaningful columns from day one, and the logic lives in version-controlled Python rather than SQL workbooks.
Each schema has its own loader module (load_raw_schema, load_operationals_schema, load_analytics_schema). Each one can be run independently for debugging, and the orchestrator just composes them. No god-script.
Before loading fact_sales, the transform stage runs five separate validate_foreign_keys() checks across sales→customers/products/stores/regions/calendar. If any fails, the pipeline halts with sys.exit(1) and a structured error.
The analytics schema defines five FOREIGN KEY ... ON DELETE RESTRICT constraints, seven CHECK constraints (age ≥ 0, quantity ≥ 0, order_date ≤ CURRENT_DATE, prices ≥ 0), unique indexes on every natural ID, and 13 indexes across the fact and dims. PostgreSQL enforces the integrity, not Python.
The schema is mostly a pure star, with one intentional snowflake: fact_sales has a direct region_key FK to dim_regions, saving a join for the most common query ("revenue by region"). The full normalised path via dim_stores is still there. Documented snowflake, not accidental.
Cross-platform UTF-8 detection decides whether the console can render emojis; dual handlers route colorized output to the terminal and clean UTF-8 to a rotating file; a timed() decorator logs how long each stage takes. Observability that holds up at 2am, not just on the demo screen.
python -m etl.run_allThe dimensional model directly supports the kind of questions a sales or growth team would ask of a chocolate-retail dataset. Each one is a single SQL query against the analytics schema, not a multi-step pivot in a spreadsheet.
For production scale the natural evolutions would be: dbt on top of the analytics schema for analyst-authored transformations with tested SQL models; Airflow or Prefect orchestration for scheduled refreshes with retry and alerting; materialised views for the most-queried analytics aggregates; SCD Type 2 dimensions with valid_from/valid_to tracking for time-aware product and category history; incremental loads against the operational layer instead of full refreshes; and migration tooling (Alembic, Flyway) for evolving the three-schema DDL safely.
I'm currently looking for Data Engineer / Analytics Engineer roles. UK-based, open to remote or hybrid.
A 1M-row PySpark pipeline for synthetic banking transactions, with a validated star schema warehouse, deterministic SHA-256 surrogate keys, and idempotent re-runs on PostgreSQL.
read-case-studyA production-style ETL pipeline that loads 9 raw Brazilian e-commerce CSVs into a full PostgreSQL star schema, with idempotent loads and structured logging.
read-case-study