Skip to content

Commit

Permalink
MiqAeNamespace should use ancestry instead of acts_as_tree
Browse files Browse the repository at this point in the history
  • Loading branch information
lfu committed Feb 10, 2020
1 parent 7a16daf commit 2dc78b3
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
40 changes: 40 additions & 0 deletions db/migrate/20200203200855_add_ancestry_to_miq_ae_namespace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class AddAncestryToMiqAeNamespace < ActiveRecord::Migration[5.0]
class MiqAeNamespace < ActiveRecord::Base
self.inheritance_column = :_type_disabled # disable STI
end

def update_ns_parent(parent)
ancestry = [parent.ancestry, parent.id].compact.join("/") if parent
MiqAeNamespace.where(:parent_id => parent.try(:id)).each do |ns|
ns.update(:ancestry => ancestry) if ancestry
update_ns_parent(ns)
end
end

def up
add_column :miq_ae_namespaces, :ancestry, :string
add_index :miq_ae_namespaces, :ancestry

say_with_time("Converting MiqAeNamespaces from parent_id to ancestry") do
update_ns_parent(nil)
end

remove_index :miq_ae_namespaces, :column => :parent_id
remove_column :miq_ae_namespaces, :parent_id
end

def down
add_column :miq_ae_namespaces, :parent_id, :bigint
add_index :miq_ae_namespaces, :parent_id

say_with_time("Converting MiqAeNamespaces from ancestry to parent_id") do
MiqAeNamespace.all.each do |ns|
parent_ns_id = ns.ancestry.split("/").last.to_i if ns.ancestry.present?
ns.update(:parent_id => parent_ns_id)
end
end

remove_index :miq_ae_namespaces, :column => :ancestry
remove_column :miq_ae_namespaces, :ancestry
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require_migration

describe AddAncestryToMiqAeNamespace do
let(:ns_stub) { migration_stub(:MiqAeNamespace) }

migration_context :up do
# nodes:
# ns1
# ns11
# ns111
# ns112
# ns2
# ns21 (created before parent)
it "updates tree" do
ns1 = ns_stub.create!
ns2 = ns_stub.create!
ns11 = ns_stub.create!(:parent_id => ns1.id)
ns111 = ns_stub.create!(:parent_id => ns11.id)
ns112 = ns_stub.create!(:parent_id => ns11.id)
ns21 = ns_stub.create!(:parent_id => ns2.id)

migrate

expect(ns1.reload.ancestry).to be_nil
expect(ns2.reload.ancestry).to be_nil

expect(ns11.reload.ancestry).to eq(ns1.id.to_s)
expect(ns111.reload.ancestry).to eq("#{ns1.id}/#{ns11.id}")
expect(ns112.reload.ancestry).to eq("#{ns1.id}/#{ns11.id}")
expect(ns21.reload.ancestry).to eq(ns2.id.to_s)
end
end

migration_context :down do
it "updates tree" do
ns1 = ns_stub.create!
ns2 = ns_stub.create!
ns11 = ns_stub.create!(:ancestry => ns1.id.to_s)
ns111 = ns_stub.create!(:ancestry => "#{ns1.id}/#{ns11.id}")
ns112 = ns_stub.create!(:ancestry => "#{ns1.id}/#{ns11.id}")
ns21 = ns_stub.create!(:ancestry => ns2.id.to_s)

migrate

expect(ns1.reload.parent_id).to eq(nil)
expect(ns2.reload.parent_id).to eq(nil)

expect(ns11.reload.parent_id).to eq(ns1.id)
expect(ns111.reload.parent_id).to eq(ns11.id)
expect(ns112.reload.parent_id).to eq(ns11.id)
expect(ns21.reload.parent_id).to eq(ns2.id)
end
end
end

0 comments on commit 2dc78b3

Please sign in to comment.