mirror of
https://github.com/internetee/registry.git
synced 2025-07-03 09:43:36 +02:00
Contact importing
This commit is contained in:
parent
20f8192f78
commit
e195d3948e
5 changed files with 92 additions and 46 deletions
|
@ -3,6 +3,5 @@ module Legacy
|
||||||
self.table_name = :contact
|
self.table_name = :contact
|
||||||
belongs_to :object_registry, foreign_key: :id
|
belongs_to :object_registry, foreign_key: :id
|
||||||
belongs_to :object, foreign_key: :id
|
belongs_to :object, foreign_key: :id
|
||||||
belongs_to :registrar, foreign_key: :crid
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,5 +2,6 @@ module Legacy
|
||||||
class ObjectRegistry < Db
|
class ObjectRegistry < Db
|
||||||
self.table_name = :object_registry
|
self.table_name = :object_registry
|
||||||
self.inheritance_column = nil
|
self.inheritance_column = nil
|
||||||
|
belongs_to :registrar, foreign_key: :crid, primary_key: :legacy_id, class_name: '::Registrar'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
class AddLegacyColumnsForAddress < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
add_column :addresses, :legacy_contact_id, :integer
|
||||||
|
end
|
||||||
|
end
|
|
@ -11,7 +11,11 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
|
<<<<<<< HEAD
|
||||||
ActiveRecord::Schema.define(version: 20150330083700) do
|
ActiveRecord::Schema.define(version: 20150330083700) do
|
||||||
|
=======
|
||||||
|
ActiveRecord::Schema.define(version: 20150302130224) do
|
||||||
|
>>>>>>> Contact importing
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
|
@ -29,6 +33,7 @@ ActiveRecord::Schema.define(version: 20150330083700) do
|
||||||
t.string "updator_str"
|
t.string "updator_str"
|
||||||
t.string "country_code"
|
t.string "country_code"
|
||||||
t.string "state"
|
t.string "state"
|
||||||
|
t.integer "legacy_contact_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table "api_users", force: :cascade do |t|
|
create_table "api_users", force: :cascade do |t|
|
||||||
|
@ -630,6 +635,7 @@ ActiveRecord::Schema.define(version: 20150330083700) do
|
||||||
t.inet "current_sign_in_ip"
|
t.inet "current_sign_in_ip"
|
||||||
t.inet "last_sign_in_ip"
|
t.inet "last_sign_in_ip"
|
||||||
t.string "identity_code"
|
t.string "identity_code"
|
||||||
|
t.string "roles", array: true
|
||||||
t.string "creator_str"
|
t.string "creator_str"
|
||||||
t.string "updator_str"
|
t.string "updator_str"
|
||||||
t.string "country_code"
|
t.string "country_code"
|
||||||
|
|
|
@ -43,9 +43,6 @@ namespace :import do
|
||||||
start = Time.now.to_i
|
start = Time.now.to_i
|
||||||
puts '-----> Importing contacts...'
|
puts '-----> Importing contacts...'
|
||||||
|
|
||||||
contacts = []
|
|
||||||
existing_ids = Contact.pluck(:legacy_id)
|
|
||||||
|
|
||||||
# 1;"RC";"born number" # not used
|
# 1;"RC";"born number" # not used
|
||||||
# 2;"OP";"identity card number" -> priv
|
# 2;"OP";"identity card number" -> priv
|
||||||
# 3;"PASS";"passwport" ->
|
# 3;"PASS";"passwport" ->
|
||||||
|
@ -60,58 +57,96 @@ namespace :import do
|
||||||
6 => Contact::IDENT_BIRTHDAY
|
6 => Contact::IDENT_BIRTHDAY
|
||||||
}
|
}
|
||||||
|
|
||||||
contacts = []
|
contact_columns = [
|
||||||
|
"code",
|
||||||
|
"phone",
|
||||||
|
"email",
|
||||||
|
"fax",
|
||||||
|
"created_at",
|
||||||
|
"ident",
|
||||||
|
"ident_type",
|
||||||
|
"auth_info",
|
||||||
|
"name",
|
||||||
|
"org_name",
|
||||||
|
"registrar_id",
|
||||||
|
"creator_str",
|
||||||
|
"updator_str",
|
||||||
|
"ident_country_code",
|
||||||
|
"legacy_id"
|
||||||
|
]
|
||||||
|
|
||||||
|
address_columns = [
|
||||||
|
"city",
|
||||||
|
"street",
|
||||||
|
"zip",
|
||||||
|
"street2",
|
||||||
|
"street3",
|
||||||
|
"creator_str",
|
||||||
|
"updator_str",
|
||||||
|
"country_code",
|
||||||
|
"state",
|
||||||
|
"legacy_contact_id"
|
||||||
|
]
|
||||||
|
|
||||||
|
contacts, addresses = [], []
|
||||||
|
existing_contact_ids = Contact.pluck(:legacy_id)
|
||||||
|
existing_address_ids = Address.pluck(:legacy_contact_id)
|
||||||
|
user = "rake-#{`whoami`.strip} #{ARGV.join ' '}"
|
||||||
|
count = 0
|
||||||
|
|
||||||
|
Legacy::Contact.includes(:object_registry, :object, :object_registry => :registrar).find_each(batch_size: 10000).with_index do |x, index|
|
||||||
|
next if existing_contact_ids.include?(x.id)
|
||||||
|
next if existing_address_ids.include?(x.id)
|
||||||
|
count += 1
|
||||||
|
|
||||||
Legacy::Contact.includes(:object_registry, :object, :registrar).find_each(batch_size: 10000) do |x|
|
|
||||||
next if existing_ids.include?(x.id)
|
|
||||||
begin
|
begin
|
||||||
# registrar = Registrar.find_by(legacy_id: x.object_registry.crid)
|
contacts << [
|
||||||
|
x.object_registry.name,
|
||||||
|
x.telephone.try(:strip),
|
||||||
|
x.email.try(:strip),
|
||||||
|
x.fax.try(:strip),
|
||||||
|
x.try(:crdate),
|
||||||
|
x.ssn.try(:strip),
|
||||||
|
ident_type_map[x.ssntype],
|
||||||
|
x.object.authinfopw.try(:strip),
|
||||||
|
x.name.try(:strip),
|
||||||
|
x.organization.try(:strip),
|
||||||
|
x.object_registry.try(:registrar).try(:id),
|
||||||
|
user,
|
||||||
|
user,
|
||||||
|
x.country.try(:strip),
|
||||||
|
x.id
|
||||||
|
]
|
||||||
|
|
||||||
contacts << Contact.new({
|
addresses << [
|
||||||
code: x.object_registry.name,
|
x.city.try(:strip),
|
||||||
#type: , # not needed
|
x.street1.try(:strip),
|
||||||
#reg_no: x.ssn.try(:strip),
|
x.postalcode.try(:strip),
|
||||||
phone: x.telephone.try(:strip),
|
x.street2.try(:strip),
|
||||||
email: x.email.try(:strip),
|
x.street3.try(:strip),
|
||||||
fax: x.fax.try(:strip),
|
user,
|
||||||
ident: x.ssn.try(:strip),
|
user,
|
||||||
ident_type: ident_type_map[x.ssntype],
|
x.country.try(:strip),
|
||||||
#created_by_id: , # not needed
|
x.stateorprovince.try(:strip),
|
||||||
#updated_by_id: , # not needed
|
x.id
|
||||||
auth_info: x.object.authinfopw.try(:strip),
|
]
|
||||||
name: x.name.try(:strip),
|
|
||||||
org_name: x.organization.try(:strip),
|
|
||||||
registrar_id: x.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)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
if contacts.size % 10000 == 0
|
if contacts.size % 10000 == 0
|
||||||
puts "commtting #{contacts.size}"
|
Contact.import contact_columns, contacts, validate: false
|
||||||
puts Time.now.to_i - start
|
Address.import address_columns, addresses, validate: false
|
||||||
# Contact.import contacts, validate: false
|
contacts, addresses = [], []
|
||||||
contacts = []
|
|
||||||
end
|
end
|
||||||
rescue => e
|
rescue => e
|
||||||
|
puts "ERROR on index #{index}"
|
||||||
puts e
|
puts e
|
||||||
puts x.inspect
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
puts '-----> Contacts imported'
|
Contact.import contact_columns, contacts, validate: false
|
||||||
|
Address.import address_columns, addresses, validate: false
|
||||||
|
|
||||||
|
puts '-----> Updating relations...'
|
||||||
|
ActiveRecord::Base.connection.execute('UPDATE addresses SET contact_id = legacy_contact_id WHERE legacy_contact_id IS NOT NULL AND contact_id IS NULL')
|
||||||
|
puts "-----> Imported #{count} new contacts in #{Time.now.to_i - start} seconds"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue