Skip to content

Latest commit

Β 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

README.md

BetterStructureSql Integration App (PostgreSQL)

This is a comprehensive integration/testing app for BetterStructureSql that demonstrates all PostgreSQL features and provides a real-world example of multi-file schema output.

Purpose

  1. Feature Demonstration: Shows off all PostgreSQL capabilities supported by BetterStructureSql
  2. Testing Environment: Used for integration testing and CI/CD validation
  3. Real-World Example: Generates actual multi-file schema output as a reference
  4. Development Playground: Try out new features and configuration options

What's Included

Database Features Demonstrated

This app includes examples of:

  • βœ… PostgreSQL Extensions: pgcrypto, uuid-ossp, pg_trgm
  • βœ… Custom Types: ENUM types for status fields
  • βœ… Functions: PL/pgSQL functions for business logic
  • βœ… Triggers: BEFORE/AFTER triggers with function calls
  • βœ… Sequences: Custom sequences for ID generation
  • βœ… Views: Regular SQL views for denormalized queries
  • βœ… Materialized Views: Cached aggregated data
  • βœ… Foreign Keys: All constraint actions (CASCADE, RESTRICT, SET NULL)
  • βœ… Indexes: btree, gin, gist, partial indexes, expression indexes
  • βœ… Domains: Custom constrained types
  • βœ… Check Constraints: Table-level validation
  • βœ… Array Types: PostgreSQL array columns
  • βœ… JSONB: JSON data with indexing

Migration Count

11 migrations implementing a feature-rich e-commerce-style schema.

Generated Schema Output

When running rails db:schema:dump_better, this app generates:

db/schema/ β”œβ”€β”€ _header.sql # 95 bytes - SET statements β”œβ”€β”€ _manifest.json # 763 bytes - Metadata β”œβ”€β”€ 01_extensions/ β”‚ └── 000001.sql # 3 lines - PostgreSQL extensions β”œβ”€β”€ 02_types/ β”‚ └── 000001.sql # 13 lines - ENUM types β”œβ”€β”€ 03_functions/ β”‚ └── 000001.sql # 332 lines - PL/pgSQL functions β”œβ”€β”€ 04_sequences/ β”‚ └── 000001.sql # 289 lines - Sequence definitions β”œβ”€β”€ 05_tables/ β”‚ β”œβ”€β”€ 000001.sql # 500 lines - Main tables (part 1) β”‚ └── 000002.sql # 479 lines - Main tables (part 2) β”œβ”€β”€ 06_indexes/ β”‚ └── 000001.sql # 397 lines - All indexes β”œβ”€β”€ 07_foreign_keys/ β”‚ └── 000001.sql # 67 lines - Foreign key constraints β”œβ”€β”€ 08_views/ β”‚ └── 000001.sql # 217 lines - Views and materialized views β”œβ”€β”€ 09_triggers/ β”‚ └── 000001.sql # 35 lines - Trigger definitions └── 10_migrations/ └── 000001.sql # 13 lines - Schema migrations INSERT 

Total: 11 files, 2,345 lines of clean SQL across 10 directories

Quick Start

Using Docker (Recommended)

# From project root docker compose up # Access the app at http://localhost:3000

Local Development

  1. Setup Database:

    # Ensure PostgreSQL 12+ is running locally createdb integration_development
  2. Install Dependencies:

    cd integration bundle install
  3. Run Migrations:

    bundle exec rails db:migrate
  4. Generate Schema:

    bundle exec rails db:schema:dump_better
  5. View Output:

    ls -R db/schema/ cat db/schema/_manifest.json

Configuration

See config/initializers/better_structure_sql.rb for the configuration used in this app:

BetterStructureSql.configure do |config| # Multi-file output mode config.output_path = 'db/schema' # Chunking settings config.max_lines_per_file = 500 config.overflow_threshold = 1.1 config.generate_manifest = true # Enable all PostgreSQL features config.include_extensions = true config.include_functions = true config.include_triggers = true config.include_views = true config.include_materialized_views = true config.include_domains = true config.include_sequences = true config.include_custom_types = true # Schema versioning enabled config.enable_schema_versions = true config.schema_versions_limit = 10 # Replace default Rails tasks config.replace_default_dump = true config.replace_default_load = true end

Web UI

The integration app mounts the BetterStructureSql web UI at /schema_versions:

# Start the server bundle exec rails server # Visit http://localhost:3000/schema_versions

Features:

  • Browse all stored schema versions
  • View formatted SQL with syntax highlighting
  • Download individual versions
  • Download ZIP archives of multi-file schemas
  • View manifest metadata

Testing

Run Test Suite

bundle exec rspec

Manual Testing Workflow

  1. Modify Schema: Add a new migration
  2. Run Migration: rails db:migrate
  3. Generate Schema: rails db:schema:dump_better
  4. Store Version: rails db:schema:store
  5. List Versions: rails db:schema:versions
  6. View Web UI: Visit /schema_versions

Test Schema Loading

# Drop and recreate database bundle exec rails db:drop db:create # Load from multi-file schema bundle exec rails db:schema:load_better # Verify bundle exec rails db:schema:dump_better diff -r db/schema db/schema_backup # Should be identical

Example Queries

Check Generated Schema

# View extensions cat db/schema/01_extensions/000001.sql # View custom types cat db/schema/02_types/000001.sql # View functions cat db/schema/03_functions/000001.sql # View first table file cat db/schema/05_tables/000001.sql | head -50 # View manifest cat db/schema/_manifest.json | jq

Database Inspection

-- Connect to database psql integration_development -- List extensions \dx -- List custom types \dT+ -- List functions \df -- List triggers SELECT tgname FROM pg_trigger WHERE tgisinternal = false; -- List views \dv -- List materialized views \dm

Troubleshooting

Schema Not Generating

  1. Check database connection in config/database.yml
  2. Ensure migrations have run: rails db:migrate:status
  3. Check logs in log/development.log

Permission Errors

Ensure your PostgreSQL user has permissions:

GRANT SELECT ON information_schema.tables TO your_user; GRANT SELECT ON pg_catalog.pg_class TO your_user;

Multi-File Output Not Working

  1. Verify config.output_path is a directory (e.g., 'db/schema')
  2. Check that directory is writable
  3. Ensure config.generate_manifest = true

Development

Adding New Features

  1. Create a new migration demonstrating the feature
  2. Run migration and generate schema
  3. Verify output in db/schema/
  4. Commit both migration and generated schema

Resetting Database

# Complete reset bundle exec rails db:drop db:create db:migrate db:schema:dump_better # Just regenerate schema rm -rf db/schema/ bundle exec rails db:schema:dump_better

CI/CD

This integration app is used in GitHub Actions CI:

# .github/workflows/ci.yml - name: Run Integration Tests run: |  cd integration  bundle exec rails db:create db:migrate  bundle exec rails db:schema:dump_better  bundle exec rails db:schema:load_better  bundle exec rspec

File Structure

integration/ β”œβ”€β”€ app/ # Rails app (controllers, models, views) β”œβ”€β”€ config/ β”‚ β”œβ”€β”€ database.yml # PostgreSQL configuration β”‚ └── initializers/ β”‚ └── better_structure_sql.rb # Gem configuration β”œβ”€β”€ db/ β”‚ β”œβ”€β”€ migrate/ # 11 migrations β”‚ └── schema/ # Generated multi-file schema β”œβ”€β”€ Gemfile # Dependencies including better_structure_sql └── README.md # This file 

Learn More

License

Part of the BetterStructureSql project. See LICENSE.