In a Rails host app, what does `bin/rails billing:install:migrations` do with an engine's migrations, and why are they copied instead of run in place?
answer
- copy, then db:migrate
- new timestamp after the host's last
- filename suffix .billing.rb
- skipped when the name exists
- railties:install:migrations FROM=
basics
~10 sbilling:install:migrations copies the engine's db/migrate files into the host's db/migrate with fresh versions and a .billing.rb suffix, skipping names already copied. The host owns its schema, so it reviews and runs them with db:migrate.
solid answer
~30 sAn engine with a `db/migrate` folder registers `billing:install:migrations`, which sets `FROM=billing` and runs `railties:install:migrations`. Each migration is copied into the host's `db/migrate` with a new version, the current UTC timestamp or one more than the host's newest, so it sorts after existing migrations. It is named like `20261006120000_create_billing_invoices.billing.rb` and starts with `# This migration comes from billing (originally ...)`. Migrations whose name already exists are skipped, so rerunning after an engine upgrade copies only new ones. Nothing is migrated yet: the host runs `bin/rails db:migrate`. Copying keeps one schema history in the host and lets the team review the SQL.
code
bash · 6 linesbin/rails billing:install:migrations
# Copied migration 20261006120000_create_billing_invoices.billing.rb from billing
# Copied migration 20261006120001_create_billing_payments.billing.rb from billing
bin/rails railties:install:migrations FROM=billing,payroll
bin/rails db:migratego deeper
Remember the two commands: billing:install:migrations to copy, then db:migrate to run.
Explain renumbering, the .billing.rb suffix with its origin comment, and why reruns copy only new names.
Own the upgrade workflow: rerun the install task on every engine bump, watch for name clashes and renamed migrations, and use SCOPE to back an engine out.
Decide whether engine schema changes are reviewed in the host like any other migration, which keeps one schema history across many engines.
## Where the task comes from Every engine with migrations gets an install task for free. `Rails::Engine` declares a `rake_tasks` block that, when the engine has a `db/migrate` directory, defines `<engine_name>:install:migrations`. For an isolated `Billing::Engine` the engine name is `billing`, so the host runs: ```bash bin/rails billing:install:migrations ``` The task sets `FROM=billing` and invokes the generic Active Record task `railties:install:migrations`, which can also copy from every engine at once (no `FROM`) or from a list (`FROM=billing,payroll`). Framework engines use the same machinery: `bin/rails active_storage:install` invokes `active_storage:install:migrations`. ## What the copy does, step by step 1. **Find sources.** For each engine selected by `FROM`, take its `db/migrate` path (or `MIGRATIONS_PATH` relative to the engine root when that variable is set). 2. **Pick the destination.** The host's first migrations path, or the migrations path of another database when `DATABASE=animals` names one. 3. **Skip duplicates.** If the host already has a migration with the same **name** (for example `CreateBillingInvoices`), the file is skipped; when the existing file came from a different source, the task also prints `NOTE: Migration ... has been skipped`. 4. **Renumber.** Each new copy gets a fresh version: the current UTC timestamp, or the newest destination version plus one if that is later. Copies therefore sort after everything the host already has, in the engine's original order. 5. **Mark the origin.** The file is written as `<version>_<name>.billing.rb`, and a comment `# This migration comes from billing (originally <old version>)` is inserted after any magic comment. No SQL runs. The host then applies the copies with `bin/rails db:migrate` like any other migration. ## Why copy instead of migrating in place | Concern | Copying into the host | |---|---| | One history | `schema_migrations` and `db/schema.rb` stay the host's single record of what ran | | Ordering | Fresh versions place engine changes after the host's, avoiding out-of-order versions | | Review | The SQL is in the host's repository and code review before it touches production | | Customisation | The host can adjust a copy, for example adding an index, before running it | | Upgrades | Rerunning the task after a gem upgrade copies only migrations it has not seen | The trade-off is that the host now owns those files. If an engine later edits a migration it already shipped, the host never receives the change, because the name already exists. ## Scope: the `.billing` suffix The suffix is not decoration. Active Record reads it as the migration's **scope**, and `db:migrate` honours `SCOPE`, so the engine's copies can be targeted on their own. The engines guide uses that to back an engine out before removing it: `bin/rails db:migrate SCOPE=billing VERSION=0`. ## Pitfalls - **Renamed migrations reappear.** Duplicate detection is by name, so an engine that renames a migration ships it again as a new one. - **Name clashes between engines.** Two engines that both ship `CreateSettings` collide; the second is skipped with a NOTE and must be renamed by hand. - **Forgetting to rerun.** Upgrading the engine gem without rerunning the install task leaves new tables missing until something fails at runtime. - **Editing copies casually.** A modified copy diverges from what the engine's authors test against; record why in the migration. ## Authoring migrations inside an engine The engine's authors shape how smooth installation is for the host: - name migrations distinctly, ideally with the engine's prefix (`CreateBillingInvoices`), because duplicate detection compares names across every source; - never edit a migration that has shipped; add a new one, since hosts that already copied the old file will not receive the edit; - keep migrations independent of host tables where possible, or document the host table a foreign key expects; - mention the install task in the engine's README and changelog whenever a release adds migrations. ## In the HR monolith With billing extracted to `engines/billing`, the workflow becomes: write the migration inside the engine (running `bin/rails generate migration` from the engine's root writes it to the engine's `db/migrate`), then in the host run `bin/rails billing:install:migrations` and `bin/rails db:migrate`, and commit both the copied files and the updated `db/schema.rb`.
- How do you copy an engine's migrations into a secondary database?Pass `DATABASE=<name>` to `railties:install:migrations`; the task copies into that database's `migrations_paths` and raises if the configuration has no custom migrations path.
- Why can't the host just add the engine's db/migrate to its migration paths?It can be done, but the host then runs files it does not review, versions from the engine's timeline can land out of order, and an engine upgrade changes the host's schema implicitly. The copy model keeps each change as a reviewed file in the host's history.
- install:migrations runs the engine's migrations against the database
- Copied migrations keep the engine's original version numbers
- Rerunning the task duplicates every migration with new timestamps
- Engines' migrations run automatically from the gem on db:migrate
- The .billing suffix in the filename is only cosmetic