diff options
author | Rich <rich@fishpercolator.co.uk> | 2017-11-14 19:24:00 +0000 |
---|---|---|
committer | Rafael França <rafaelmfranca@gmail.com> | 2017-11-14 14:24:00 -0500 |
commit | df82237a45e930a7ab53b95bee78a3f34c5b92fb (patch) | |
tree | 824f42d4e01f5f1864ba3c0150753df9cdd88730 /activerecord/lib/active_record/migration.rb | |
parent | 53574ff988ce7927ae3cfeb8d7ed21734680c26c (diff) | |
download | rails-df82237a45e930a7ab53b95bee78a3f34c5b92fb.tar.gz rails-df82237a45e930a7ab53b95bee78a3f34c5b92fb.tar.bz2 rails-df82237a45e930a7ab53b95bee78a3f34c5b92fb.zip |
Add a #populate method to migrations (#31082)
* Add a #populate method to migrations
* Address rubocop issues
* Rename to #up_only and use #execute in the examples intead of the model
* Update CHANGELOG
[Rich Daley & Rafael Mendonça França]
Diffstat (limited to 'activerecord/lib/active_record/migration.rb')
-rw-r--r-- | activerecord/lib/active_record/migration.rb | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index c13efa9d70..67c8c9fc08 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -734,6 +734,24 @@ module ActiveRecord execute_block { yield helper } end + # Used to specify an operation that is only run when migrating up + # (for example, populating a new column with its initial values). + # + # In the following example, the new column `published` will be given + # the value `true` for all existing records. + # + # class AddPublishedToPosts < ActiveRecord::Migration[5.3] + # def change + # add_column :posts, :published, :boolean, default: false + # up_only do + # execute "update posts set published = 'true'" + # end + # end + # end + def up_only + execute_block { yield } unless reverting? + end + # Runs the given migration classes. # Last argument can specify options: # - :direction (default is :up) |