Skip to main content

Automated dbt project generation from PostgreSQL schemas with semantic layer

Project description

Schemalytics

Automated dbt project generation from PostgreSQL schemas with comprehensive semantic layer for LLM-powered analytics.

Schemalytics extracts your PostgreSQL database schema, intelligently classifies tables as facts or dimensions using AI-powered analysis, and generates a production-ready dbt project with medallion architecture (Bronze โ†’ Silver โ†’ Gold). The tool creates a detailed semantic layer that enables LLMs to understand your data model and generate accurate SQL queries for self-service analytics.

Key Features

โœจ Automated Data Modeling - Extracts schemas and generates dbt projects automatically
๐Ÿ—๏ธ Medallion Architecture - Bronze (raw) โ†’ Silver (dimensional) โ†’ Gold (aggregated)
๐Ÿค– AI-Enhanced Classification - Uses local LLMs (Ollama) to validate table classifications
๐Ÿ”’ Privacy-First - All processing happens locally, no data leaves your machine
๐Ÿ“Š Comprehensive Semantic Layer - 500+ lines of LLM-ready metadata for accurate queries
๐Ÿ“ Complete dbt Documentation - Auto-generated schema.yml files with tests and descriptions
๐ŸŽฏ Industry Templates - 50+ pre-configured industry patterns (E-commerce, SaaS, Finance, etc.)

Installation

Prerequisites

  • Python 3.10+
  • PostgreSQL database (with data to model)
  • Ollama with AI models for intelligent classification

1. Install Ollama & Models

# Install Ollama (https://ollama.ai)
# macOS/Linux:
curl -fsSL https://ollama.com/install.sh | sh

# Pull required models
ollama pull qwen-data:latest
ollama pull qwen2.5-coder:7b  # Fallback model

2. Install Schemalytics

# Clone the repository
git clone https://github.com/yourusername/schemalytics.git
cd schemalytics

# Install in development mode
pip install -e .

3. Verify Installation

# Check if schemalytics is installed
schemalytics --version

# Verify Ollama is running
ollama list

Quick Start

One-Command Generation (Interactive)

schemalytics generate \
  --connection postgresql://user:password@localhost:5432/mydb \
  --output ./my_dbt_project \
  --name my_project

You'll be prompted for:

  1. Industry - Select from 14 main industries (E-commerce, SaaS, Finance, etc.)
  2. Sub-industry - Choose specific business type (B2C, B2B, Marketplace, etc.)
  3. Entities - Review and edit suggested entities (customers, orders, products)
  4. Goals - Review and edit analytical goals (revenue_reporting, customer_ltv)
  5. Temporal Tracking - Choose SCD type (snapshot, historical, or both)
  6. Time Grains - Select Gold layer aggregations (daily, weekly, monthly, yearly)

The tool will:

  • Extract your database schema (14 tables in ~2 seconds)
  • Classify tables as facts/dimensions using AI (5 facts, 9 dimensions)
  • Generate Gold layer aggregates based on your selections
  • Create a complete dbt project with semantic layer

Using Pre-Created Context

# Create context.yaml
cat > context.yaml << EOF
business_type: ecommerce_retail_b2c
entities: [customers, orders, products, order_items]
goals: [revenue_reporting, customer_lifetime_value, inventory_tracking]
temporal: historical
grain: daily,weekly,monthly
EOF

# Generate with context file
schemalytics generate \
  --connection postgresql://user:password@localhost:5432/mydb \
  --context context.yaml \
  --output ./my_dbt_project

Step-by-Step Workflow

# 1. Extract schema
schemalytics extract \
  --connection postgresql://user:password@localhost:5432/mydb \
  --output schema.json

# 2. Generate modeling plan (with AI validation)
schemalytics plan \
  --schema schema.json \
  --context context.yaml \
  --output plan.yaml

# 3. Build dbt project
schemalytics build \
  --schema schema.json \
  --plan plan.yaml \
  --context context.yaml \
  --output ./dbt_project

Generated Project Structure

dbt_project/
โ”œโ”€โ”€ dbt_project.yml                 # dbt project configuration
โ”œโ”€โ”€ semantic_layer.yml              # Comprehensive LLM-ready metadata (500+ lines)
โ”œโ”€โ”€ models/
โ”‚   โ”œโ”€โ”€ sources.yml                 # Source definitions
โ”‚   โ”œโ”€โ”€ bronze/                     # Raw passthrough (views)
โ”‚   โ”‚   โ”œโ”€โ”€ schema.yml              # Bronze documentation
โ”‚   โ”‚   โ””โ”€โ”€ bronze_*.sql
โ”‚   โ”œโ”€โ”€ silver/
โ”‚   โ”‚   โ”œโ”€โ”€ dimensions/             # SCD1/SCD2 dimensions
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ schema.yml          # Dimension documentation with tests
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ dim_*.sql
โ”‚   โ”‚   โ””โ”€โ”€ facts/                  # Fact tables
โ”‚   โ”‚       โ”œโ”€โ”€ schema.yml          # Fact documentation with tests
โ”‚   โ”‚       โ””โ”€โ”€ fct_*.sql
โ”‚   โ””โ”€โ”€ gold/                       # Pre-aggregated metrics
โ”‚       โ”œโ”€โ”€ schema.yml              # Metrics documentation
โ”‚       โ””โ”€โ”€ gold_*.sql
โ”œโ”€โ”€ tests/
โ”œโ”€โ”€ macros/
โ””โ”€โ”€ README.md

Semantic Layer for LLM Analytics

The generated semantic_layer.yml provides comprehensive metadata including:

Metrics Catalog

  • SQL formulas and aggregation types
  • Use cases and example queries
  • Data types and null handling rules
  • Common filters and time ranges

Dimensional Model

  • Complete fact and dimension documentation
  • Grain definitions and relationships
  • Join patterns and cardinality
  • SCD type information

Query Guidelines

  • Query strategy (Gold โ†’ Silver โ†’ Bronze)
  • Performance optimization tips
  • Common mistakes and solutions
  • Date and null handling rules

Query Library

  • Pre-built analytical queries
  • Period-over-period comparisons
  • Cross-fact analysis patterns

Example LLM Usage

An LLM can read semantic_layer.yml to understand:

  • Available metrics (daily_revenue, monthly_sales, customer_ltv)
  • Time grains (daily, weekly, monthly, yearly)
  • Dimension relationships and join paths
  • Pre-calculated aggregations in Gold layer

Then generate accurate SQL:

-- LLM understands to query Gold layer first
SELECT 
  daily_date,
  total_revenue,
  order_count,
  avg_order_value
FROM gold_daily_revenue
WHERE daily_date >= CURRENT_DATE - 30
ORDER BY daily_date DESC

How It Works

1. Schema Extraction

SQLAlchemy inspects your PostgreSQL database and extracts:

  • Tables, columns, and data types
  • Primary keys and foreign keys
  • Relationships and constraints

2. Intelligent Classification

Heuristic Analysis:

  • FK graph analysis identifies patterns
  • Tables with many outgoing FKs โ†’ Facts
  • Tables with many incoming FKs โ†’ Dimensions

AI Validation:

  • Local LLM (Ollama) validates classifications
  • Provides reasoning for each decision
  • Suggests corrections for ambiguous cases

3. Interactive Review

  • User reviews proposed model
  • Can edit table classifications
  • Accepts or rejects plan before generation

4. Gold Layer Generation

AI-Powered Metrics:

  • LLM suggests common aggregations based on industry
  • Generates metrics aligned with analytical goals

Heuristic Fallback:

  • Time-based aggregates (daily, weekly, monthly, yearly)
  • Business-specific patterns (e-commerce, SaaS metrics)

5. Template-Based SQL Generation

  • Jinja2 templates ensure syntactically correct SQL
  • LLM fills parameters, doesn't write SQL from scratch
  • Guarantees production-ready, tested code

Industry Support

Available Industries (14 Main Categories)

  1. E-commerce & Retail - B2C, B2B, Marketplace, Subscription
  2. SaaS & Software - B2B, B2C, Platform, Collaboration
  3. Finance & Fintech - Banking, Payments, Lending, Investment, Crypto, Insurance
  4. Healthcare - Provider, Telehealth, Pharmacy, Health Apps
  5. Media & Entertainment - Streaming, Gaming, Social Media, Publishing
  6. Marketing & Advertising - Automation, Ad Networks, Email, Influencer
  7. Education - K-12, Higher Ed, Online Courses, Corporate Training
  8. Logistics & Transportation - Shipping, Warehouse, Rideshare, Delivery
  9. Hospitality & Travel - Hotels, Booking, Restaurants, Vacation Rentals
  10. Real Estate - Residential, Commercial, Property Management
  11. Manufacturing - Production, Supply Chain, Inventory
  12. Human Resources - HRIS, Recruiting, Payroll, Talent Marketplaces
  13. Nonprofit & Government - Fundraising, Public Services
  14. Other/Custom - Generic business patterns

Each industry includes:

  • Pre-configured entities
  • Analytical goals
  • Common metrics
  • Best practices

Architecture Decisions

Why Local LLM?

  • Privacy - No data sent to external APIs
  • Cost - Zero API fees
  • Control - Works offline, no rate limits
  • Speed - Optimized for consumer hardware (8GB RAM MacBook)

Why Template-Based SQL?

  • Reliability - Guarantees syntactically correct SQL
  • Consistency - Follows dbt best practices
  • Maintainability - Easy to update and extend
  • Quality - Production-tested patterns

Why Gold + Semantic Layer?

  • Performance - Pre-aggregated metrics (10-100x faster)
  • LLM-Ready - Structured metadata for accurate queries
  • Self-Service - Enables non-technical analytics
  • Scalability - Reduces query complexity

Configuration

Connection String Format

postgresql://username:password@hostname:port/database

# Examples:
postgresql://postgres:mypassword@localhost:5432/mydb
postgresql://user@localhost/mydb  # No password
postgresql://user:pass@remote.host:5432/db

Context File Options

business_type: ecommerce_retail_b2c  # Industry_subindustry format
entities:
  - customers
  - orders
  - products
goals:
  - revenue_reporting
  - customer_lifetime_value
  - inventory_tracking
temporal: historical  # snapshot | historical | both
grain: daily,weekly,monthly  # Comma-separated time grains

Development

# Install in editable mode with dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Format code
ruff format .

# Type checking
mypy schemalytics

Troubleshooting

Ollama Not Running

# Check if Ollama is running
curl http://localhost:11434/api/tags

# Start Ollama
ollama serve

Database Connection Issues

# Test connection with psql
psql postgresql://user:pass@localhost:5432/mydb

# Check if database exists
psql -U postgres -l

Model Not Found

# List available models
ollama list

# Pull required model
ollama pull qwen-data:latest

Timeout Issues

Default LLM timeout is 15 minutes. For large schemas, this may need adjustment in llm.py:

LLM_TIMEOUT = 900.0  # Increase if needed

Roadmap

  • Support additional databases (Snowflake, BigQuery, DuckDB)
  • Web UI for interactive modeling
  • Advanced SCD types (Type 3, Type 6)
  • Data profiling and quality checks
  • Custom business logic templates
  • dbt Cloud integration
  • Incremental model generation
  • Multi-tenant support

Contributing

Contributions welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Submit a pull request

License

MIT License - see LICENSE file for details

Support

  • Issues: GitHub Issues
  • Documentation: See semantic_layer.yml in generated projects
  • Examples: Check the examples/ directory

Credits

Built with:


Schemalytics - Transform your database into an LLM-ready analytics platform in minutes.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

schemalytics-0.1.0.tar.gz (104.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

schemalytics-0.1.0-py3-none-any.whl (103.4 kB view details)

Uploaded Python 3

File details

Details for the file schemalytics-0.1.0.tar.gz.

File metadata

  • Download URL: schemalytics-0.1.0.tar.gz
  • Upload date:
  • Size: 104.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for schemalytics-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4cd0bde868c0f0b2b2ae9c5d5ab00d94fd2a19ac3daa8875aee2803cbf0d076a
MD5 fd211a975dfcf489a27da1f5be47fb26
BLAKE2b-256 134de941645a077d1a89165bf421ab47b770d48b4f8f738e109c9857316f6e80

See more details on using hashes here.

File details

Details for the file schemalytics-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: schemalytics-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 103.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for schemalytics-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a804bca0978bba1f28d1a9ec8c520adf35c6d6d296a120b64f61a035d21385c7
MD5 7bfd4d1a8245ec1344d04329674258cb
BLAKE2b-256 3f8c975f81b6e9a77936d0542050f718a24c9df92ee2350851430da7a2c128b9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page