1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 15:18:01 +00:00
Files
til/rails/customize-template-for-new-schema-migration.md

1.5 KiB

Customize Template For New Schema Migration

Rails has a set of generator functionality that we can use to scaffold entire slices of an app all the way down to generating a single migration file.

$ rails generate migration MakeUserStatusColumnNotNull

When we run a migration generator command like that, Rails reaches for the baked-in migration template and creates a new migration file based on the given name and any other local variables that get set internally.

That's the standard behavior. However, we can override the migration template by defining our own template in lib/templates/migration.rb.tt of our Rails app. We'll need to follow the basic structure, but then we can alter it to our needs.

For instance, I typically like to use the #up and #down methods and write raw SQL for my migrations. To help with that this template provides a good starting point.

class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
  def up
    execute <<~SQL
    SQL
  end

  def down
    execute <<~SQL
    SQL
  end
end

We can see in migration_generator.rb how locals get set and what template gets chosen.