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.
- Feature Demonstration: Shows off all PostgreSQL capabilities supported by BetterStructureSql
- Testing Environment: Used for integration testing and CI/CD validation
- Real-World Example: Generates actual multi-file schema output as a reference
- Development Playground: Try out new features and configuration options
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
11 migrations implementing a feature-rich e-commerce-style schema.
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
# From project root docker compose up # Access the app at http://localhost:3000-
Setup Database:
# Ensure PostgreSQL 12+ is running locally createdb integration_development -
Install Dependencies:
cd integration bundle install -
Run Migrations:
bundle exec rails db:migrate -
Generate Schema:
bundle exec rails db:schema:dump_better -
View Output:
ls -R db/schema/ cat db/schema/_manifest.json
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 endThe integration app mounts the BetterStructureSql web UI at /schema_versions:
# Start the server bundle exec rails server # Visit http://localhost:3000/schema_versionsFeatures:
- Browse all stored schema versions
- View formatted SQL with syntax highlighting
- Download individual versions
- Download ZIP archives of multi-file schemas
- View manifest metadata
bundle exec rspec- Modify Schema: Add a new migration
- Run Migration:
rails db:migrate - Generate Schema:
rails db:schema:dump_better - Store Version:
rails db:schema:store - List Versions:
rails db:schema:versions - View Web UI: Visit
/schema_versions
# 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# 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-- 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- Check database connection in
config/database.yml - Ensure migrations have run:
rails db:migrate:status - Check logs in
log/development.log
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;- Verify
config.output_pathis a directory (e.g.,'db/schema') - Check that directory is writable
- Ensure
config.generate_manifest = true
- Create a new migration demonstrating the feature
- Run migration and generate schema
- Verify output in
db/schema/ - Commit both migration and generated schema
# 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_betterThis 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 rspecintegration/ βββ 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 Part of the BetterStructureSql project. See LICENSE.