skip to content

In a Rails 8.1 app, where does a one-off script that imports a print shop's legacy orders belong: script/, lib/tasks/ or db/migrate/?

level: middleimportance: should knowfreq 40%

answer

  1. script/ for one-off code
  2. requires config/environment first
  3. bin/rails runner script/....rb
  4. lib/tasks for repeatable named tasks
  5. db/migrate is for schema

basics

~10 s

A one-off import belongs in script/, for example script/import/legacy_orders.rb, which loads config/environment and runs with bin/rails runner; lib/tasks/ suits tasks you rerun by name, and db/migrate/ is for schema changes every environment must replay.

solid answer

~40 s

Rails 8.1 apps have a `script/` folder for exactly this. `bin/rails generate script import/legacy_orders` writes `script/import/legacy_orders.rb`, starting with `require_relative "../../config/environment"` so models are available, and you run it with `bin/rails runner script/import/legacy_orders.rb` (adding `-e production` or `RAILS_ENV=production` on the server). It stays in the repository as a record of what was done, and nothing else depends on it. A `.rake` file in `lib/tasks` fits work you will run again by name, such as a nightly reprice. `db/migrate` is the wrong home: migrations describe schema history that every environment, including a fresh test database, must replay, and a data import tied to one production snapshot does not fit that contract.

code

bash · 4 lines
bash
bin/rails generate script import/legacy_orders
#      create  script/import/legacy_orders.rb

RAILS_ENV=production bin/rails runner script/import/legacy_orders.rb

go deeper

for a junior

Recall the script/ folder, the generator that writes into it, and that the script loads config/environment before using models.

for a middle

Explain the lifecycle of each place: script/ runs when asked, lib/tasks defines named commands, db/migrate replays everywhere, and pick by lifecycle.

for a senior

Make one-off scripts safe: idempotent rows, a transaction or batches, counts at the end, a dry run on copied data, and a record of what ran.

for a principal

Set the policy for data changes in production: who may run scripts, how they are reviewed, and when a job or migration is required instead.

## The candidates A print shop is moving off an old system and needs to import 40,000 historical orders from a CSV once. Rails 8.1 offers several places where code that "runs from the command line" can live, and each implies a different lifecycle: | Place | Lifecycle | Typical content | |---|---|---| | `script/` | run deliberately, maybe once | data fixes, imports, one-off reports, benchmarks | | `lib/tasks/*.rake` | run repeatedly by name | `bin/rails shop:reprice`, scheduled maintenance | | `db/migrate/` | replayed by every environment, in order | schema changes | | `bin/` | project executables used daily | `bin/setup`, `bin/dev`, tool binstubs | | `app/jobs/` | runs inside the app, often in the background | work triggered by the app or a schedule | ## Why `script/` fits a one-off import A new Rails 8.1 app contains an empty `script/` folder, and the generator for it describes its purpose as "a one-off or general purpose script, such as a data migration script, cleanup script, etc.". Running `bin/rails generate script import/legacy_orders` writes: ```ruby # script/import/legacy_orders.rb require_relative "../../config/environment" # Your code goes here ``` Points to know: - The `require_relative` line loads the whole application, so `Order` and `Customer` are available; the number of `../` matches the folder depth. - The generator's usage text suggests running it with **`bin/rails runner script/import/legacy_orders.rb`**. On a server, select the environment with `RAILS_ENV=production` or the runner's `-e` option. - The file is committed. Months later, someone asking "where did these orders come from?" can read exactly what ran. - Nothing loads files in `script/` automatically: they do not run at boot, at deploy or in tests, which is what you want from code that should run once. ## Why not `lib/tasks` A Rake task in `lib/tasks` becomes a named command (listed by `bin/rails -T` once it has a `desc`), loaded every time Rake loads the app's tasks. That suits work meant to be **repeated**: a recurring cleanup, a reindex, a task a scheduler calls. Turning a one-off import into a permanent public command invites someone to run it a second time and import duplicates. (Writing tasks is its own subject; the point here is where each kind of code lives.) ## Why not `db/migrate` Migrations are a **schema history**: every developer's database, CI and production replays them in order. A data import tied to a CSV that exists only on one machine breaks that contract: 1. It fails or does nothing in every environment that lacks the file. 2. It runs during deploys, where a slow import of 40,000 rows holds up the release. 3. It stays in the history forever, even after the legacy system is gone. Data changes that every environment genuinely needs are a separate discussion; a one-off import is not one of them. ## Making the script safe to run - Wrap the import in a transaction, or make each row idempotent by looking up an existing order by its legacy ID before creating one. - Print counts at the end (created, skipped, failed) so the run leaves a record. - Test it against a copy of production data before running it for real. - Delete or archive it once the import is confirmed, or keep it with a dated comment explaining what it did.

  • Where does `bin/rails generate benchmark` put its file?
    Under `script/benchmarks/`, for example `script/benchmarks/opt_compare.rb`. The benchmark generator shares the folder with the script generator because both produce code you run deliberately from the command line rather than code the app loads.
  • When would the import be better as a job in `app/jobs`?
    When it must run inside production without a shell session, be retried on failure, or be split into batches processed in parallel. A small script can then enqueue the jobs, while the per-row work lives in the job class with the app's normal error reporting.

  • A one-off data import belongs in db/migrate so it runs on deploy.
  • Files in script/ are loaded automatically when the app boots.
  • Every command-line script should be a Rake task in lib/tasks.
  • One-off scripts go in bin/ next to bin/setup.
  • A script in script/ cannot use the app's models.