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

classification#parent_id = nil when no parents #309

Merged
merged 1 commit into from
Jun 13, 2019
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
14 changes: 14 additions & 0 deletions db/migrate/20181130203334_classification_parent_null.rb
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 spec/migrations/20181130203334_classification_parent_null_spec.rb
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