Keep DB schema in sync across branches effortlessly. Install and use rails db:migrate as normal. The gem handles the rest automatically.
Keep DB schema in sync across branches effortlessly. Install and use rails db:migrate as normal. The gem handles the rest automatically.
Stop database headaches when switching Git branches in Rails
Keep your database schema perfectly synchronized across Git branches, eliminate broken tests and schema conflicts, and save wasted hours on phantom migrations.
schema.rb/structure.sql diffs every time, no irrelevant changesActiveRecord::NotNullViolation and similar errors when switching branchesAnd you get all of that with zero changes to your workflow!
Before ActualDbSchema:
schema.rb diffsAfter ActualDbSchema:
schema.rb and structure.sqldb:migrateAdd to your Gemfile:
group :development do
gem "actual_db_schema"
endInstall and configure:
bundle install
rails actual_db_schema:installThat's it! Now just run rails db:migrate as usual - phantom migrations roll back automatically.
This gem stores all run migrations with their code in the tmp/migrated folder. Whenever you perform a schema dump, it rolls back the phantom migrations.
The phantom migrations list is the difference between the migrations you've executed (in the tmp/migrated folder) and the current ones (in the db/migrate folder).
Therefore, all you do is run rails db:migrate in your current branch. actual_db_schema will ensure the DB schema is up-to-date. You'll never have an inaccurate schema.rb file again.
Add this line to your application's Gemfile:
group :development do
gem "actual_db_schema"
endAnd then execute:
$ bundle installIf you cannot commit changes to the repo or Gemfile, consider the local Gemfile installation described in this post.
Next, generate your ActualDbSchema initializer file by running:
rails actual_db_schema:installThis will create a config/initializers/actual_db_schema.rb file that lists all available configuration options, allowing you to customize them as needed. The installation process will also prompt you to install the post-checkout Git hook, which automatically rolls back phantom migrations when switching branches. If enabled, this hook will run the schema actualization rake task every time you switch branches, which can slow down branch changes. Therefore, you might not always want this automatic actualization on every switch; in that case, running rails db:migrate manually provides a faster, more controlled alternative.
For more details on the available configuration options, see the sections below.
Just run rails db:migrate inside the current branch. It will roll back all phantom migrations for all configured databases in your database.yml.
[!WARNING] This solution implies that all migrations are reversible. The irreversible migrations should be solved manually. At the moment, the gem ignores them. You will see warnings in the terminal for each irreversible migrations.
The gem offers the following rake tasks that can be manually run according to your preferences:
rails db:rollback_branches - run it to manually rolls back phantom migrations.rails db:rollback_branches:manual - run it to manually rolls back phantom migrations one by one.rails db:phantom_migrations - displays a list of phantom migrations.By default, actual_db_schema stores all run migrations in the tmp/migrated folder. However, if you want to change this location, you can configure it in two ways:
Set the environment variable ACTUAL_DB_SCHEMA_MIGRATED_FOLDER to your desired folder path:
export ACTUAL_DB_SCHEMA_MIGRATED_FOLDER="custom/migrated"Add the following line to your initializer file (config/initializers/actual_db_schema.rb):
config.migrated_folder = Rails.root.join("custom", "migrated")If you want to share executed migrations across environments (e.g., staging or sandboxes), store them in the main database instead of the local filesystem:
config.migrations_storage = :dbOr via environment variable:
export ACTUAL_DB_SCHEMA_MIGRATIONS_STORAGE="db"If both are set, the initializer setting (config.migrations_storage) takes precedence.
Access the migration management UI at:
http://localhost:3000/rails/phantom_migrationsView and manage:
By default, the UI is enabled in the development environment. If you prefer to enable the UI for another environment, you can do so in two ways:
Set the environment variable ACTUAL_DB_SCHEMA_UI_ENABLED to true:
export ACTUAL_DB_SCHEMA_UI_ENABLED=trueAdd the following line to your initializer file (config/initializers/actual_db_schema.rb):
config.ui_enabled = trueWith this option, the UI can be disabled for all environments or be enabled in specific ones.
By default, the automatic rollback of migrations is enabled. If you prefer to perform manual rollbacks, you can disable the automatic rollback in two ways:
Set the environment variable ACTUAL_DB_SCHEMA_AUTO_ROLLBACK_DISABLED to true:
export ACTUAL_DB_SCHEMA_AUTO_ROLLBACK_DISABLED=trueAdd the following line to your initializer file (config/initializers/actual_db_schema.rb):
config.auto_rollback_disabled = trueActualDbSchema emits an ActiveSupport::Notifications event for each successful phantom rollback:
rollback_migration.actual_db_schema| Field | Description |
|---|---|
version |
Migration version that was rolled back |
name |
Migration class name |
database |
Current database name from Active Record config |
schema |
Tenant schema name (or nil for default schema) |
branch |
Branch associated with the migration metadata |
manual_mode |
Whether rollback was run in manual mode |
You can subscribe to rollback events in your initializer to track statistics or perform custom actions:
# config/initializers/actual_db_schema.rb
ActiveSupport::Notifications.subscribe(ActualDbSchema::Instrumentation::ROLLBACK_EVENT) do |_name, _start, _finish, _id, payload|
ActualDbSchema::RollbackStatsRepository.record(payload)
endThe RollbackStatsRepository persists rollback events to a database table (actual_db_schema_rollback_events) that is automatically excluded from schema dumps.
Read aggregated stats at runtime:
ActualDbSchema::RollbackStatsRepository.stats
# => { total: 3, by_database: { "primary" => 3 }, by_schema: { "default" => 3 }, by_branch: { "main" => 3 } }
ActualDbSchema::RollbackStatsRepository.total_rollbacks
# => 3
ActualDbSchema::RollbackStatsRepository.reset!
# Clears all recorded statsBy default, the automatic rollback of migrations on branch switch is disabled. If you prefer to automatically rollback phantom migrations whenever you switch branches with git checkout, you can enable it in two ways:
Set the environment variable ACTUAL_DB_SCHEMA_GIT_HOOKS_ENABLED to true:
export ACTUAL_DB_SCHEMA_GIT_HOOKS_ENABLED=trueAdd the following line to your initializer file (config/initializers/actual_db_schema.rb):
config.git_hooks_enabled = trueAfter enabling Git hooks in your configuration, run the rake task to install the post-checkout hook:
rake actual_db_schema:install_git_hooksThis task will prompt you to choose one of the three options:
db:rollback_branchesdb:migrateBased on your selection, a post-checkout hook will be installed or updated in your .git/hooks folder.
No open issues yet, or sync has not completed.