today i learned
Tiny posts about stuff I like.
Nov 13, 2024: Data migrations with data-migrate
What I traditionally would've used Rake tasks for has been replaced with data-migrate, a little gem that handles data migrations in the same way as Rails schema migrations. It's the perfect way to automate data changes in production, offering a single pattern for handling data backfills, seed scripts, and the like.
The pros are numerous:
- Data migrations are easily generated via CLI and are templated
with an
up
anddown
case so folks think about rollbacks. - Just like with Rails schema migrations, there's a migration ID kept around that ensures data migrations are run in order. Old PRs will have merge conflicts.
- You can conditionally run data migrations alongside schema
migrations with
bin/rails db:migrate:with_data
.
It's a really neat gem. I'll probably still rely on the good ol'
Rake task for my personal projects, but will doubtless keep
data-migrate
in the toolbox for teams.
Nov 9, 2024: Cool Rails concerns
There's something super elegant about Writebook's use
of concerns. I especially like Book:Sluggable
:
module Book::Sluggable
extend ActiveSupport::Concern
included do
before_save :generate_slug, if: -> { slug.blank? }
end
def generate_slug
self.slug = title.parameterize
end
end
Here's a few reasons:
- Nesting concerns in a model folder is neat when that concern
is an encapsulation of model-specific functionality:
app/models/book/sluggable.rb
. - Concerns don't have to be big. They do have to be single-purpose.
- Reminds me of a great article by Jorge Manrubla: Vanilla Rails is plenty. Down with service objects!
Oct 26, 2024: Kafka on the Shore
On the inside cover of Kafka on the Shore Murakami explains how his idea for the book started with its title. This approach is opposite to anything I've ever written, though I recognize there's a notable difference between fiction and technical writing. But what a powerful idea: a simple phrase shapes the entire story.
I dug up this quote from an interview:
When I start to write, I don’t have any plan at all. I just wait for the story to come. I don’t choose what kind of story it is or what’s going to happen. I just wait.
I think that's pretty cool.