From 9cd4bad3a9da13f57d36b1d1f1e47fff750c8440 Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Fri, 27 Feb 2015 18:13:03 +0200 Subject: [PATCH] Map legacy contact fields to contact --- app/models/legacy/contact.rb | 2 + app/models/legacy/object.rb | 10 ++ ...27092508_add_legacy_columns_for_contact.rb | 4 + db/schema.rb | 5 +- lib/tasks/import.rake | 96 ++++++++++++------- 5 files changed, 77 insertions(+), 40 deletions(-) create mode 100644 app/models/legacy/object.rb diff --git a/app/models/legacy/contact.rb b/app/models/legacy/contact.rb index 64da7c886..0951d9df8 100644 --- a/app/models/legacy/contact.rb +++ b/app/models/legacy/contact.rb @@ -1,5 +1,7 @@ module Legacy class Contact < Db self.table_name = :contact + belongs_to :object_registry, foreign_key: :id + belongs_to :object, foreign_key: :id end end diff --git a/app/models/legacy/object.rb b/app/models/legacy/object.rb new file mode 100644 index 000000000..dc1c53d7b --- /dev/null +++ b/app/models/legacy/object.rb @@ -0,0 +1,10 @@ +module Legacy + class Object < Db + self.table_name = :object + + def self.instance_method_already_implemented?(method_name) + return true if method_name == 'update' + super + end + end +end diff --git a/db/migrate/20150227092508_add_legacy_columns_for_contact.rb b/db/migrate/20150227092508_add_legacy_columns_for_contact.rb index 0aba53c38..2d08e3094 100644 --- a/db/migrate/20150227092508_add_legacy_columns_for_contact.rb +++ b/db/migrate/20150227092508_add_legacy_columns_for_contact.rb @@ -1,5 +1,9 @@ class AddLegacyColumnsForContact < ActiveRecord::Migration def change add_column :contacts, :legacy_id, :integer + remove_column :contacts, :type, :string + remove_column :contacts, :reg_no, :string + remove_column :contacts, :created_by_id, :integer + remove_column :contacts, :updated_by_id, :integer end end diff --git a/db/schema.rb b/db/schema.rb index 19104f421..3242725ac 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -74,8 +74,6 @@ ActiveRecord::Schema.define(version: 20150330083700) do create_table "contacts", force: :cascade do |t| t.string "code" - t.string "type" - t.string "reg_no" t.string "phone" t.string "email" t.string "fax" @@ -83,8 +81,6 @@ ActiveRecord::Schema.define(version: 20150330083700) do t.datetime "updated_at" t.string "ident" t.string "ident_type" - t.integer "created_by_id" - t.integer "updated_by_id" t.string "auth_info" t.string "name" t.string "org_name" @@ -97,6 +93,7 @@ ActiveRecord::Schema.define(version: 20150330083700) do t.string "zip" t.string "country_code" t.string "state" + t.integer "legacy_id" end add_index "contacts", ["code"], name: "index_contacts_on_code", using: :btree diff --git a/lib/tasks/import.rake b/lib/tasks/import.rake index 754f1b708..25f455ea4 100644 --- a/lib/tasks/import.rake +++ b/lib/tasks/import.rake @@ -38,45 +38,69 @@ namespace :import do puts '-----> Registrars imported' end - # desc 'Import contacts' - # task contact: :environment do - # puts '-----> Importing contacts...' + desc 'Import contacts' + task contacts: :environment do + puts '-----> Importing contacts...' - # contacts = [] - # existing_ids = Contact.pluck(:legacy_id) + contacts = [] + existing_ids = Contact.pluck(:legacy_id) - # Legacy::Contact.all.each do |x| - # next if existing_ids.include?(x.id) + # 1;"RC";"born number" # not used + # 2;"OP";"identity card number" -> priv + # 3;"PASS";"passwport" -> + # 4;"ICO";"organization identification number" + # 5;"MPSV";"social system identification" # not used + # 6;"BIRTHDAY";"day of birth" - # 1;"RC";"born number" - # 2;"OP";"identity card number" -> priv - # 3;"PASS";"passwport" -> - # 4;"ICO";"organization identification number" - # 5;"MPSV";"social system identification" - # 6;"BIRTHDAY";"day of birth" + ident_type_map = { + 2 => Contact::IDENT_PRIV, + 3 => Contact::IDENT_PASSPORT, + 4 => Contact::IDENT_TYPE_BIC, + 6 => Contact::IDENT_BIRTHDAY + } - # contacts << Contact.new({ - # code: , - # #type: , # not needed - # #reg_no: x.ssn.try(:strip), - # phone: x.telephone.try(:strip), - # email: x.email.try(:strip), - # fax: x.fax.try(:strip), - # ident: x.ssn.try(:strip), - # ident_type: , - # #created_by_id: , # not needed - # #updated_by_id: , # not needed - # auth_info: , - # name: x.name.try(:strip), - # org_name: x.organization.try(:strip), - # registrar_id: , - # creator_str: "rake-#{`whoami`.strip} #{ARGV.join ' '}", - # updator_str: "rake-#{`whoami`.strip} #{ARGV.join ' '}" - # ident_country_code: x.country.try(:strip), - # legacy_id: x.id - # }) - # end + Legacy::Contact.all.each do |x| + next if existing_ids.include?(x.id) + begin + registrar = Registrar.find_by(legacy_id: x.object_registry.crid) - # puts '-----> Contacts imported' - # end + contacts << Contact.new({ + code: x.object_registry.name, + #type: , # not needed + #reg_no: x.ssn.try(:strip), + phone: x.telephone.try(:strip), + email: x.email.try(:strip), + fax: x.fax.try(:strip), + ident: x.ssn.try(:strip), + ident_type: ident_type_map[x.ssntype], + #created_by_id: , # not needed + #updated_by_id: , # not needed + auth_info: x.object.authinfopw.try(:strip), + name: x.name.try(:strip), + org_name: x.organization.try(:strip), + registrar_id: registrar.try(:id), + creator_str: "rake-#{`whoami`.strip} #{ARGV.join ' '}", + updator_str: "rake-#{`whoami`.strip} #{ARGV.join ' '}", + ident_country_code: x.country.try(:strip), + created_at: x.try(:crdate), + legacy_id: x.id, + address: Address.new({ + city: x.city.try(:strip), + street: x.street1.try(:strip), + zip: x.postalcode.try(:strip), + street2: x.street2.try(:strip), + street3: x.street3.try(:strip), + creator_str: "rake-#{`whoami`.strip} #{ARGV.join ' '}", + updator_str: "rake-#{`whoami`.strip} #{ARGV.join ' '}", + country_code: x.country.try(:strip), + state: x.stateorprovince.try(:strip) + }) + }) + rescue => e + binding.pry + end + end + + puts '-----> Contacts imported' + end end