mirror of
https://github.com/jbranchaud/til
synced 2026-01-02 22:58:01 +00:00
Add Customize Template For New Schema Migration as a Rails TIL
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||
|
||||
_1643 TILs and counting..._
|
||||
_1644 TILs and counting..._
|
||||
|
||||
See some of the other learning resources I work on:
|
||||
- [Get Started with Vimium](https://egghead.io/courses/get-started-with-vimium~3t5f7)
|
||||
@@ -1032,6 +1032,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
||||
- [Creating Records of Has_One Associations](rails/creating-records-of-has-one-associations.md)
|
||||
- [Custom Validation Message](rails/custom-validation-message.md)
|
||||
- [Customize Paths And Helpers For Devise Routes](rails/customize-paths-and-helpers-for-devise-routes.md)
|
||||
- [Customize Template For New Schema Migration](rails/customize-template-for-new-schema-migration.md)
|
||||
- [Customize The Path Of A Resource Route](rails/customize-the-path-of-a-resource-route.md)
|
||||
- [Define The Root Path For The App](rails/define-the-root-path-for-the-app.md)
|
||||
- [Delete Paranoid Records](rails/delete-paranoid-records.md)
|
||||
|
||||
41
rails/customize-template-for-new-schema-migration.md
Normal file
41
rails/customize-template-for-new-schema-migration.md
Normal file
@@ -0,0 +1,41 @@
|
||||
# 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.
|
||||
|
||||
```bash
|
||||
$ rails generate migration MakeUserStatusColumnNotNull
|
||||
```
|
||||
|
||||
When we run a migration generator command like that, Rails reaches for the
|
||||
[baked-in migration
|
||||
template](https://github.com/rails/rails/blob/92be9af152f721588b7414119c931ea92930947b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb.tt)
|
||||
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.
|
||||
|
||||
```ruby
|
||||
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`](https://github.com/rails/rails/blob/92be9af152f721588b7414119c931ea92930947b/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb#L26-L43)
|
||||
how locals get set and what template gets chosen.
|
||||
Reference in New Issue
Block a user