Skip to content

Commit

Permalink
Introduce new models TransformationMapping and TransformationMappingItem
Browse files Browse the repository at this point in the history
Create a complete mapping through a virtual column.
  • Loading branch information
bzwei committed Feb 5, 2018
1 parent a07ec73 commit ccd117c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
64 changes: 64 additions & 0 deletions app/models/transformation_mapping.rb
@@ -0,0 +1,64 @@
class TransformationMapping < ApplicationRecord
has_many :transformation_mapping_items, :dependent => :destroy

validates :name, :presence => true, :uniqueness => true

virtual_column :mappings, :type => :string

# forward compatible: next release the table will be polymorphic
virtual_column :type, :type => :string

def self.inheritance_column
"none"
end

def type
"MigrationInfrastructureMapping"
end

def type=(type)
end

# Assumption: source and destination types are the same. Works for infrastructure mapping
# group mapping items first by destination_type, then by destination_id
# simplify records with IDs.
# Sample result (Hash with key being the underscore case of the model)
#
# ems_cluster:
# - destination: "2":
# sources: ["100", "200"]
# - destination: "3"
# sources: ["301", "302"]
# network:
# - destination: "2"
# sources: ["202", "204"]
# storage:
# - destination: "4"
# sources: ["33", "32"]
def mappings
dst_type_groups = transformation_mapping_items.group_by(&:destination_type)
dst_type_groups.each_with_object({}) do |(dst_type, items), results|
dst_groups = items.group_by { |item| item.destination_id.to_s }
results[dst_type.underscore] = dst_groups.each_with_object({}) do |(dst_id, item_array), dst_id_groups|
dst_id_groups["destination"] = dst_id
dst_id_groups["sources"] = item_array.collect { |item| item.source_id.to_s }
end
end
end

def mappings=(options)
options.each do |dst_type, item_map_array|
item_map_array.each do |item_map|
destination_id = item_map['destination']
item_map['sources'].each do |source_id|
transformation_mapping_items << TransformationMappingItem.new(
:source_id => source_id,
:source_type => dst_type.camelize,
:destination_id => destination_id,
:destination_type => dst_type.camelize
)
end
end
end
end
end
7 changes: 7 additions & 0 deletions app/models/transformation_mapping_item.rb
@@ -0,0 +1,7 @@
class TransformationMappingItem < ApplicationRecord
belongs_to :transformation_mapping
belongs_to :source, :polymorphic => true
belongs_to :destination, :polymorphic => true

validates :source_id, :uniqueness => {:scope => [:transformation_mapping_id, :source_type]}
end

0 comments on commit ccd117c

Please sign in to comment.