InsightPix DB is the foundational database service of the InsightPix ecosystem — a modular system designed to gather user feedback on tagged images and transform it into measurable insights.
This repository contains everything related to the PostgreSQL schema, triggers, functions, migrations, and testing used to maintain consistency and generate metrics directly inside the database layer.
This module handles:
- Core relational structure (
images,votes,tags, and derived metric tables). - SQL logic for automatic aggregation using triggers and recursive queries.
- Versioned migrations through Flyway.
- Database unit testing with pgTAP.
- Containerized deployment with Docker Compose.
By keeping the logic close to the data layer, InsightPix DB ensures that all analytical metrics remain consistent, even if different backend or API layers evolve independently.
Core Entities:
images: stores uploaded images and metadata (tags, URLs, titles).votes: stores user votes with timestamps.image_metrics: pre-aggregated results by tag and date, updated via triggers.
Key Features:
- Automatic recalculation of metrics upon every vote.
- Periodic aggregation functions (daily, monthly, yearly).
- Optimized queries for fast analytics retrieval.
Example Flow:
- A new vote is inserted into the
votestable. - A trigger fires to update or insert a record in
image_metrics. - Metrics are available for the backend API immediately.
To support analytical workloads and historical trend visualization, InsightPix DB includes a periodic snapshot fact table called:
category_metrics_periodic
This table stores aggregated metrics over fixed time intervals (year, quarter, month, week, day, hour, and day period).
Each row represents a pre-calculated summary for a specific category_id and time bucket, allowing queries such as:
- “Which category received more positive votes this week?”
- “What is the trend of engagement per quarter?”
- “At what time of day are users most active?”
By decoupling analytical aggregation from transactional tables, the system supports both:
- Real-time updates (via triggers on
votesandimage_metrics) - Historical summaries (via
category_metrics_periodic)
This table implements a Periodic Snapshot Fact Table — a core pattern in dimensional modeling for time-series analytics. It complements the transactional and aggregated layers:
| Layer | Type | Example Table | Purpose |
|---|---|---|---|
| 1 Transaction | Fact Table | votes |
Raw user events |
| 2 Aggregation | Snapshot | image_metrics |
On-demand metric updates |
| 3 History | Periodic Snapshot | category_metrics_periodic |
Time-bucketed trend storage |
| 4 Derived | Materialized View | tag_stats_mv |
Precomputed analytical results |
Each level feeds the next in the data lineage:
votes → image_metrics → category_metrics_periodic → tag_stats_mv → dashboards
This design enables InsightPix DB to act not only as a transactional backend but also as a compact analytical data mart.
| Component | Technology | Purpose |
|---|---|---|
| Database | PostgreSQL 16 | Core engine |
| Migrations | Flyway | Schema versioning |
| Unit Tests | pgTAP | SQL testing |
| Containerization | Docker Compose | Local orchestration |
| Scripting | Bash / psql |
Migration & seed automation |
insightpix-db/
├── docker/
│ ├── Dockerfile
│ └── init/
│ ├── optionals/
│ │ └── 01_tags.sql
│ ├── 01_schema.sql
│ └── 99_optionals.sh
├── migrations/
│ ├── V1__initial_schema.sql
│ └── ...
├── tests/
│ ├── 01_schema_tests.sql
│ └── ...
├── docker-compose.yml
├── scripts/
│ ├── migrate.sh
│ └── backup.sh
├── .env.example
└── README.md
git clone https://github.com/ladriangb/insightpix-db.git
cd insightpix-dbCopy and edit .env.example:
cp .env.example .envExample content:
POSTGRES_USER=insightpix
POSTGRES_PASSWORD=insightpix
POSTGRES_DB=insightpix
POSTGRES_PORT=5432
docker compose up -dThe database will:
- Create the schema and relations.
- Load triggers and functions.
- Seed initial data for development.
Access the container:
docker exec -it insightpix-db psql -U insightpix -d insightpixRun a test query:
SELECT * FROM images LIMIT 5;Tests are written using pgTAP and run automatically during CI builds. To execute them locally:
docker compose exec db bash -c "sh /scripts/test.sh"Expected output:
All tests successful.
Files=3, Tests=45, 5 wallclock secs
Result: PASS
- All SQL files under
/docker/init/are automatically loaded on first container run. - Subsequent updates should be versioned under
/migrations/. - Use consistent naming conventions: lowercase, underscores, plural table names.
- Keep triggers idempotent and ensure each has rollback coverage.
MIT License © 2025 Luis Adrian Gonzalez Benavides
Part of the InsightPix ecosystem — a full DevOps + Full-Stack demonstration project built with modular architecture, real data logic, and automated testing.