Migration generators use change even for destructive methods [#8267]

This commit is contained in:
Marc-Andre Lafortune 2012-12-18 19:43:33 -05:00
parent 7204d3c63e
commit aedcd68368
2 changed files with 16 additions and 47 deletions

@ -21,28 +21,16 @@ def change
end
end
<%- else -%>
def up
def change
<% attributes.each do |attribute| -%>
<%- if migration_action -%>
<%- if attribute.reference? -%>
remove_reference :<%= table_name %>, :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %>
remove_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %>
<%- else -%>
remove_column :<%= table_name %>, :<%= attribute.name %>
<%- end -%>
<%- end -%>
<%- end -%>
end
def down
<% attributes.reverse.each do |attribute| -%>
<%- if migration_action -%>
<%- if attribute.reference? -%>
add_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %>
<%- else -%>
add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
<%- if attribute.has_index? -%>
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
remove_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
<%- end -%>
remove_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
<%- end -%>
<%- end -%>
<%- end -%>

@ -53,15 +53,10 @@ def test_remove_migration_with_indexed_attribute
run_generator [migration, "title:string:index", "body:text"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :up, content do |up|
assert_match(/remove_column :posts, :title/, up)
assert_match(/remove_column :posts, :body/, up)
end
assert_method :down, content do |down|
assert_match(/add_column :posts, :title, :string/, down)
assert_match(/add_column :posts, :body, :text/, down)
assert_match(/add_index :posts, :title/, down)
assert_method :change, content do |change|
assert_match(/remove_column :posts, :title, :string/, change)
assert_match(/remove_column :posts, :body, :text/, change)
assert_match(/remove_index :posts, :title/, change)
end
end
end
@ -71,14 +66,9 @@ def test_remove_migration_with_attributes
run_generator [migration, "title:string", "body:text"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :up, content do |up|
assert_match(/remove_column :posts, :title/, up)
assert_match(/remove_column :posts, :body/, up)
end
assert_method :down, content do |down|
assert_match(/add_column :posts, :title, :string/, down)
assert_match(/add_column :posts, :body, :text/, down)
assert_method :change, content do |change|
assert_match(/remove_column :posts, :title, :string/, change)
assert_match(/remove_column :posts, :body, :text/, change)
end
end
end
@ -88,14 +78,9 @@ def test_remove_migration_with_references_options
run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :up, content do |up|
assert_match(/remove_reference :books, :author/, up)
assert_match(/remove_reference :books, :distributor, polymorphic: true/, up)
end
assert_method :down, content do |down|
assert_match(/add_reference :books, :author, index: true/, down)
assert_match(/add_reference :books, :distributor, polymorphic: true, index: true/, down)
assert_method :change, content do |change|
assert_match(/remove_reference :books, :author, index: true/, change)
assert_match(/remove_reference :books, :distributor, polymorphic: true, index: true/, change)
end
end
end
@ -192,12 +177,8 @@ def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove
run_generator [migration, "title:string", "content:text"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :up, content do |up|
assert_match(/^\s*$/, up)
end
assert_method :down, content do |down|
assert_match(/^\s*$/, down)
assert_method :change, content do |change|
assert_match(/^\s*$/, change)
end
end
end