-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #309 from kbrock/classification_parent
classification#parent_id = nil when no parents
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class ClassificationParentNull < ActiveRecord::Migration[5.0] | ||
class Classification < ActiveRecord::Base | ||
end | ||
|
||
def up | ||
change_column_default(:classifications, :parent_id, nil) | ||
Classification.where(:parent_id => 0).update_all(:parent_id => nil) | ||
end | ||
|
||
def down | ||
change_column_default(:classifications, :parent_id, 0) | ||
Classification.where(:parent_id => nil).update_all(:parent_id => 0) | ||
end | ||
end |
30 changes: 30 additions & 0 deletions
30
spec/migrations/20181130203334_classification_parent_null_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require_migration | ||
|
||
describe ClassificationParentNull do | ||
let(:classification_stub) { migration_stub(:Classification) } | ||
|
||
migration_context :up do | ||
it 'changes parent to nil when parent is 0' do | ||
c = classification_stub.create(:parent_id => 0) | ||
migrate | ||
c.reload | ||
expect(c.parent_id).to be_nil | ||
end | ||
|
||
it 'leaves other parents alone' do | ||
c = classification_stub.create(:parent_id => 1) | ||
migrate | ||
c.reload | ||
expect(c.parent_id).to eq(1) | ||
end | ||
end | ||
|
||
migration_context :down do | ||
it 'changes parent to 0 when parent is null' do | ||
c = classification_stub.create(:parent_id => nil) | ||
migrate | ||
c.reload | ||
expect(c.parent_id).to eq(0) | ||
end | ||
end | ||
end |