Add BetterStructureSql to your Rails application's Gemfile:
# Gemfile
gem 'better_structure_sql'
# Database adapter (choose one based on your database)
gem 'pg' # For PostgreSQL (primary support)
gem 'mysql2' # For MySQL 8.0+ (experimental)
gem 'sqlite3' # For SQLite 3.35+ (experimental)Note: The gem currently requires the pg gem as a dependency. Multi-database adapters (MySQL, SQLite) are implemented but require manual gem installation.
Install the gem:
bundle installRun the installation generator to create configuration and migration files:
rails generate better_structure_sql:installThis creates:
config/initializers/better_structure_sql.rb- Configuration file- Migration for
better_structure_sql_schema_versionstable (if schema versions enabled)
If you only want clean structure.sql generation without version storage:
# config/initializers/better_structure_sql.rb
BetterStructureSql.configure do |config|
config.replace_default_dump = true
config.enable_schema_versions = false
endThen run:
rails db:schema:dump_betterFor complete functionality including schema version storage:
# config/initializers/better_structure_sql.rb
BetterStructureSql.configure do |config|
# Replace Rails default
config.replace_default_dump = true
# Enable schema versioning
config.enable_schema_versions = true
config.schema_versions_limit = 10 # Keep last 10 versions
endRun the installation task to create the schema versions table:
rails generate better_structure_sql:install
rails db:migrateTo make BetterStructureSql the default for rake db:schema:dump:
# config/initializers/better_structure_sql.rb
BetterStructureSql.configure do |config|
config.replace_default_dump = true
endNow when you run:
rails db:schema:dumpIt will use BetterStructureSql instead of pg_dump.
If you prefer explicit control, leave replace_default_dump = false and use:
rails db:schema:dump_betterAfter installation, generate a schema dump:
rails db:schema:dump_betterCheck db/structure.sql - it should be clean without database-specific dump tool noise!
BetterStructureSql auto-detects your database adapter from ActiveRecord. Ensure your config/database.yml is configured correctly:
development:
adapter: postgresql
encoding: unicode
database: your_app_development
pool: 5
host: localhostdevelopment:
adapter: mysql2
encoding: utf8mb4
collation: utf8mb4_unicode_ci
database: your_app_development
pool: 5
host: localhostdevelopment:
adapter: sqlite3
database: db/development.sqlite3
pool: 5- Rails: 7.0 or higher
- Ruby: 2.7 or higher
- Database:
- PostgreSQL 12+ (production-ready)
- MySQL 8.0+ (experimental)
- SQLite 3.35+ (experimental)
- Gems:
rubyzip >= 2.0.0(for multi-file ZIP support)
If schema versions are enabled, make sure you ran migrations:
rails db:migrateCheck that you have:
- A valid database connection
- Migrations run (
rails db:migrate) - Tables in your database
Ensure your database user has permissions to query metadata tables:
PostgreSQL:
information_schematablespg_catalogtables (for extensions, functions, triggers)- Your application schemas
MySQL:
information_schematablesmysql.proctable (for stored procedures)SHOWprivileges
SQLite:
- Read access to
sqlite_mastertable - PRAGMA query permissions