ruby
22 lines · 7 steps
How to backfill a column safely in Rails
A batched migration fills a new full_name column without locking the whole users table.
Explained by
highlit
1class BackfillUsersFullName < ActiveRecord::Migration[7.1]
2 disable_ddl_transaction!
3
4 BATCH_SIZE = 5_000
5
6 class User < ActiveRecord::Base
7 self.table_name = :users
8 end
9
10 def up
11 User.unscoped.where(full_name: nil).in_batches(of: BATCH_SIZE) do |relation|
12 relation.update_all("full_name = TRIM(CONCAT(first_name, ' ', last_name))")
13 sleep(0.05)
14 end
15 end
16
17 def down
18 User.unscoped.where.not(full_name: nil).in_batches(of: BATCH_SIZE) do |relation|
19 relation.update_all(full_name: nil)
20 end
21 end
22end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Backfilling large tables in bounded batches keeps locks short and avoids blocking production traffic.
- 2Defining a lightweight model inside the migration insulates it from later changes to the real class.
- 3A reversible migration pairs its up-transformation with a down that returns the column to its prior state.
Related explainers
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
ruby
class LogAggregator BUCKET_FORMAT = "%Y-%m-%dT%H:%M" def initialize(entries)
Bucketing log entries by the minute in Ruby
aggregation
hashing
enumerable
Intermediate
5 steps
ruby
class WeeklySignupsReport DEFAULT_WEEKS = 12 def initialize(weeks: DEFAULT_WEEKS, source: User.all)
Building a weekly signups report in Rails
service object
aggregation
group by
Intermediate
7 steps
ruby
class ApplicationController < ActionController::Base EXPERIMENTS = { checkout_button_color: %w[control blue green], onboarding_flow: %w[control streamlined]
How A/B test cohorts are assigned in Rails
a-b-testing
cookies
hashing
Intermediate
8 steps
ruby
class SnippetHighlighter CONTEXT_RADIUS = 60 MAX_TERMS = 8
Building search-result snippets in Ruby
regular-expressions
text-processing
search
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-to-backfill-a-column-safely-in-rails-explained-ruby-4685/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.