class Workarea::MagentoMigrator::MagentoBase

Constants

STORE_ID

Attributes

client[W]
table_name[W]

Public Class Methods

all() click to toggle source
# File lib/workarea/magento_migrator/magento_base.rb, line 36
def all
  client.query(select_query).to_a
end
all_field_names() click to toggle source
# File lib/workarea/magento_migrator/magento_base.rb, line 94
def all_field_names
  own_field_names + field_names.values.flatten
end
allowed_field_names() click to toggle source
# File lib/workarea/magento_migrator/magento_base.rb, line 44
def allowed_field_names
  field_names.values.flatten.map{|fn| fn if columns_to_consider.include?(fn)}
end
attribute_id_query(col) click to toggle source
# File lib/workarea/magento_migrator/magento_base.rb, line 22
def attribute_id_query(col)
  rows = client.query("SELECT attribute_id FROM eav_attribute WHERE attribute_code = '#{col}' AND entity_type_id = #{entity_type_id}")
  rows.to_a.map{|r| r["attribute_id"]}.first
end
client() click to toggle source
# File lib/workarea/magento_migrator/magento_base.rb, line 8
def client
  @@client ||= Workarea::MagentoMigrator::SqlConnection.new.client
end
column_types() click to toggle source
# File lib/workarea/magento_migrator/magento_base.rb, line 17
def column_types()
  rows = client.query("SELECT distinct(backend_type) FROM eav_attribute WHERE entity_type_id = #{entity_type_id}")
  rows.to_a.map{|r| r["backend_type"]}
end
columns_to_consider() click to toggle source
# File lib/workarea/magento_migrator/magento_base.rb, line 40
def columns_to_consider
  YAML.load_file("#{Gem.loaded_specs['workarea-magento_migrator'].full_gem_path}/config/data/#{table_name}.yml")["columns"]["eav"]
end
entity_type_id() click to toggle source
# File lib/workarea/magento_migrator/magento_base.rb, line 12
def entity_type_id
  rows = client.query("SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = '#{table_name}'")
  rows.to_a.map{|ent| ent['entity_type_id']}.first
end
execute_query(query) click to toggle source
# File lib/workarea/magento_migrator/magento_base.rb, line 81
def execute_query(query)
  if client
    rows = client.query(query)
    rows.to_a
  else
    Rails.logger.error(Mysql2::ConnectionError)
  end
end
field_names() click to toggle source
# File lib/workarea/magento_migrator/magento_base.rb, line 98
def field_names
  statement = client.prepare(field_names_query)
  field_names = {}
  column_types.each do |col|
    rows = statement.execute(entity_type_id, col)
    field_names[col.to_s.to_sym] = rows.to_a.map{|r| r["attribute_code"]}.sort
  end
  field_names
end
field_names_query() click to toggle source
# File lib/workarea/magento_migrator/magento_base.rb, line 27
def field_names_query
  "SELECT attribute_code, backend_type FROM eav_attribute WHERE entity_type_id = ? and backend_type = ?"
end
join_clause() click to toggle source
# File lib/workarea/magento_migrator/magento_base.rb, line 64
def join_clause
  join_clauses = []
  index = 1

  field_names.each do |column_type, column_names|
    column_names.each do |column_name|
      if columns_to_consider.include?(column_name)
        attribute_id = attribute_id_query(column_name)
        join_clauses << "\nLEFT JOIN #{table_name}_entity_#{column_type} v#{index} ON e.entity_id = v#{index}.entity_id AND v#{index}.store_id = #{STORE_ID} AND v#{index}.attribute_id = (#{attribute_id}) "
        index += 1
      end
    end
  end

  join_clauses.join
end
own_field_names() click to toggle source
# File lib/workarea/magento_migrator/magento_base.rb, line 90
def own_field_names
  client.query("describe #{table_name}_entity").to_a.map{|tc| tc["Field"]}
end
select_columns() click to toggle source
# File lib/workarea/magento_migrator/magento_base.rb, line 48
def select_columns
  select_cluses = own_field_names.map{|tc| "\ne.#{tc}"}
  index = 1

  field_names.each do |column_type, column_names|
    column_names.each do |column_name|
      if columns_to_consider.include?(column_name)
        select_cluses << " \nv#{index}.value AS '#{column_name}'"
        index += 1
      end
    end
  end

  select_cluses.join(', ')
end
select_query() click to toggle source
# File lib/workarea/magento_migrator/magento_base.rb, line 31
def select_query
  select_query = "SELECT #{select_columns} FROM #{table_name}_entity e #{join_clause} "
  select_query += "\nwhere e.entity_id in (#{ids_to_include})"
end