百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

actual_db_schema

> 后端框架
开源

轻松地在各分支中保持 DB 架构的同步。按照正常流程安装和使用 rails db:migrate。这个 gem 会自动处理其余部分。

394 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

轻松地在各分支中保持 DB 架构的同步。按照正常流程安装和使用 rails db:migrate。这个 gem 会自动处理其余部分。

ActualDbSchema

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.

What You Get

  • Zero Manual Work: Switch branches freely - phantom migrations roll back automatically
  • No More Schema Conflicts: Clean schema.rb/structure.sql diffs every time, no irrelevant changes
  • Error Prevention: Eliminates ActiveRecord::NotNullViolation and similar errors when switching branches
  • Time Savings: Stop hunting down which branch has the problematic migration
  • Team Productivity: Everyone stays focused on coding, not database maintenance
  • Staging/Sandbox Sync: Keep staging and sandbox databases aligned with your current branch code
  • Visual Management: Web UI to view and manage migrations across all databases

And you get all of that with zero changes to your workflow!

The Problem This Solves

Before ActualDbSchema:

  1. Work on Branch A → Add migration → Run migration
  2. Switch to Branch B → Code breaks with database errors
  3. Manually find and rollback the "phantom" migration
  4. Deal with irrelevant schema.rb diffs
  5. Repeat this tedious process constantly

After ActualDbSchema:

  1. Work on any branch → Add migrations as usual
  2. Switch branches freely → Everything just works
  3. Focus on building features, not fixing database issues

Complete Feature Set

Core Migration Management

  • Phantom Migration Detection: Automatically identifies migrations from other branches
  • Smart Rollback: Rolls back phantom migrations in correct dependency order
  • Irreversible Migration Handling: Safely handles and reports irreversible migrations
  • Multi-Database Support: Works seamlessly with multiple database configurations
  • Schema Format Agnostic: Supports both schema.rb and structure.sql

Automation & Git Integration

  • Automatic Rollback on Migration: Phantom migrations roll back when running db:migrate
  • Git Hook Integration: Optional automatic rollback when switching branches
  • Zero Configuration: Works out of the box with sensible defaults
  • Custom Migration Storage: Configurable location for storing executed migrations

Web Interface & Management

  • Migration Dashboard: Visual overview of all migrations across databases
  • Phantom Migration Browser: Easy-to-use interface for viewing phantom migrations
  • One-Click Rollback: Rollback individual or all phantom migrations via web UI
  • Broken Version Cleanup: Identify and remove orphaned migration records
  • Schema Diff Viewer: Visual diff of schema changes with migration annotations

Developer Tools

  • Console Migrations: Run migration commands directly in Rails console
  • Schema Diff Analysis: Annotated diffs showing which migrations caused changes
  • Migration Search & Filter: Find specific migrations across all databases
  • Detailed Migration Info: View migration status, branch, and database information

Team & Environment Support

  • Multi-Tenant Compatible: Works with apartment gem and similar multi-tenant setups
  • Environment Flexibility: Enable/disable features per environment
  • Team Synchronization: Keeps all team members' databases in sync
  • CI/CD Friendly: No interference with deployment pipelines

Manual Control Options

  • Manual Rollback Mode: Disable automatic rollback for full manual control
  • Selective Rollback: Choose which phantom migrations to rollback
  • Interactive Mode: Step-by-step confirmation for each rollback operation
  • Rake Task Integration: Full set of rake tasks for command-line management

⚡ Quick Start

Add to your Gemfile:

ruby
group :development do
  gem "actual_db_schema"
end

Install and configure:

bash
bundle install
rails actual_db_schema:install

That's it! Now just run rails db:migrate as usual - phantom migrations roll back automatically.

How It Works

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.

Installation

Add this line to your application's Gemfile:

ruby
group :development do
  gem "actual_db_schema"
end

And then execute:

$ bundle install

If 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:

bash
rails actual_db_schema:install

This 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.

Usage

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.

️ Configuration Options

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:

1. Using Environment Variable

Set the environment variable ACTUAL_DB_SCHEMA_MIGRATED_FOLDER to your desired folder path:

bash
export ACTUAL_DB_SCHEMA_MIGRATED_FOLDER="custom/migrated"

2. Using Initializer

Add the following line to your initializer file (config/initializers/actual_db_schema.rb):

ruby
config.migrated_folder = Rails.root.join("custom", "migrated")

3. Store migrations in the database

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:

ruby
config.migrations_storage = :db

Or via environment variable:

bash
export ACTUAL_DB_SCHEMA_MIGRATIONS_STORAGE="db"

If both are set, the initializer setting (config.migrations_storage) takes precedence.

Web Interface

Access the migration management UI at:

http://localhost:3000/rails/phantom_migrations

View and manage:

  • Migration Overview: See all executed migrations with their status, branch, and database
  • Phantom Migrations: Identify migrations from other branches that need rollback
  • Migration Source Code: Browse the source code of every migration ever run (including the phantom ones)
  • One-Click Actions: Rollback or migrate individual migrations directly from the UI
  • Broken Versions: Detect and clean up orphaned migration records safely
  • Schema Diffs: Visual diff of schema changes annotated with their source migrations

UI options

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:

1. Using Environment Variable

Set the environment variable ACTUAL_DB_SCHEMA_UI_ENABLED to true:

bash
export ACTUAL_DB_SCHEMA_UI_ENABLED=true

2. Using Initializer

Add the following line to your initializer file (config/initializers/actual_db_schema.rb):

ruby
config.ui_enabled = true

With this option, the UI can be disabled for all environments or be enabled in specific ones.

Disabling Automatic Rollback

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:

1. Using Environment Variable

Set the environment variable ACTUAL_DB_SCHEMA_AUTO_ROLLBACK_DISABLED to true:

bash
export ACTUAL_DB_SCHEMA_AUTO_ROLLBACK_DISABLED=true

2. Using Initializer

Add the following line to your initializer file (config/initializers/actual_db_schema.rb):

ruby
config.auto_rollback_disabled = true

Rollback Instrumentation

ActualDbSchema emits an ActiveSupport::Notifications event for each successful phantom rollback:

  • Event name: rollback_migration.actual_db_schema
  • Event is always emitted when a phantom rollback succeeds

Event payload

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

Subscribing to rollback events

You can subscribe to rollback events in your initializer to track statistics or perform custom actions:

ruby
# config/initializers/actual_db_schema.rb
ActiveSupport::Notifications.subscribe(ActualDbSchema::Instrumentation::ROLLBACK_EVENT) do |_name, _start, _finish, _id, payload|
  ActualDbSchema::RollbackStatsRepository.record(payload)
end

The 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:

ruby
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 stats

Automatic Phantom Migration Rollback On Branch Switch

By 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:

1. Using Environment Variable

Set the environment variable ACTUAL_DB_SCHEMA_GIT_HOOKS_ENABLED to true:

bash
export ACTUAL_DB_SCHEMA_GIT_HOOKS_ENABLED=true

2. Using Initializer

Add the following line to your initializer file (config/initializers/actual_db_schema.rb):

ruby
config.git_hooks_enabled = true

Installing the Post-Checkout Hook

After enabling Git hooks in your configuration, run the rake task to install the post-checkout hook:

bash
rake actual_db_schema:install_git_hooks

This task will prompt you to choose one of the three options:

  1. Rollback phantom migrations with db:rollback_branches
  2. Migrate up to the latest schema with db:migrate
  3. Skip installing git hook

Based on your selection, a post-checkout hook will be installed or updated in your .git/hooks folder.

Excluding Databases from Proce

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubydbschemamigrationsrails

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架