Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a #populate method to migrations #31082

Merged
merged 5 commits into from
Nov 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
* Add `#only_up` to database migrations for code that is only relevant when
Copy link
Contributor

@bf4 bf4 Jan 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs are wrong. function is named up_only not only_up cc @rafaelfranca

via https://guides.rubyonrails.org/5_2_release_notes.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method was briefly called #only_up. This line has already been corrected in the latest https://github.com/rails/rails/blob/5-2-stable/activerecord/CHANGELOG.md

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pedantic-git ah, thanks. I guess the release notes are out of date is all

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bf4 The release notes you link to also call it up_only, as far as I can tell?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

¯\_(ツ)_/¯ 👿 thanks

migrating up, e.g. populating a new column.

*Rich Daley*

* Require raw SQL fragments to be explicitly marked when used in
relation query methods.

Expand Down
18 changes: 18 additions & 0 deletions activerecord/lib/active_record/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,24 @@ def reversible
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)
Expand Down
25 changes: 25 additions & 0 deletions activerecord/test/cases/invertible_migration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ def change
end
end

class UpOnlyMigration < SilentMigration
def change
add_column :horses, :oldie, :boolean, default: false
up_only { execute "update horses set oldie = 'true'" }
end
end

setup do
@verbose_was, ActiveRecord::Migration.verbose = ActiveRecord::Migration.verbose, false
end
Expand Down Expand Up @@ -378,5 +385,23 @@ def test_migrate_revert_add_index_with_name
"horses_index_named index should not exist"
end
end

def test_up_only
InvertibleMigration.new.migrate(:up)
horse1 = Horse.create
# populates existing horses with oldie=true but new ones have default false
UpOnlyMigration.new.migrate(:up)
Horse.reset_column_information
horse1.reload
horse2 = Horse.create

assert horse1.oldie? # created before migration
assert !horse2.oldie? # created after migration

UpOnlyMigration.new.migrate(:down) # should be no error
connection = ActiveRecord::Base.connection
assert !connection.column_exists?(:horses, :oldie)
Horse.reset_column_information
end
end
end