Sha256: d1b386eee289a54138fba6681aa0ed1671da6387ba2fe9649a6757519e4ea971

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

require "mysql2/em"

module Workarea
  module MagentoMigrator
    #
    # Sql connection configuration
    #
    #
    # Basic format should look something like this:
    #
    #   sql_connection do
    #     adapter   "mysql"
    #     host      "localhost"
    #     username  "root"
    #     password  "passw0rd"
    #     database  "my_database"
    #   end
    #
    # Possible attributes:
    #
    #   adapter
    #   host
    #   database
    #   username
    #   password
    #   port
    #   encoding
    #   socket
    #   batch_size
    #
    class SqlConnection
      attr_accessor :magento_db_config, :client

      CUSTOMER_QUERY = "select
      c.entity_id,
      c.email,
      (select f.value from customer_entity_varchar f where c.entity_id = f.entity_id and f.attribute_id = 5) fitst_name,
      (select f.value from customer_entity_varchar f where c.entity_id = f.entity_id and f.attribute_id = 7) last_name,
      (select f.value from customer_entity_datetime f where c.entity_id = f.entity_id and f.attribute_id = 11) date_of_birth ,
      c.created_at,
      c.updated_at

      from customer_entity c"

      # List of required fields to bulid a valid sql connection
      REQUIRED_FIELDS = %w{host adapter database}

      def initialize()
        @magento_db_config = Workarea.config.magento_db_config
        @magento_db_config['batch_size'] ||= 10000
        setup_mysql_client
      end

      # Setups up an active_record connection
      def setup_mysql_client
        begin
          @client = Mysql2::Client.new(@magento_db_config.symbolize_keys)
        rescue => e
          Rails.logger.error(e.message)
        end
      end

      def migrate_customers
        if @client
          customers = @client.query(CUSTOMER_QUERY)
          customers.each do |customer|
            Workarea::User.create_from_magento_customer(customer.symbolize_keys)
          end
        else
          Rails.logger.error(Mysql2::ConnectionError)
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
workarea-magento_migrator-1.0.0.pre lib/workarea/magento_migrator/sql_connection.rb