From 66d7d80bccae482da933ad34742797cbad94c712 Mon Sep 17 00:00:00 2001 From: Stas Date: Fri, 30 Oct 2015 13:56:15 +0200 Subject: [PATCH] User import added --- app/models/legacy/registrar.rb | 1 + app/models/legacy/registrar_acl.rb | 5 +++ .../20151029152638_add_legacy_id_to_users.rb | 5 +++ db/schema-read-only.rb | 3 +- db/structure.sql | 5 ++- lib/tasks/import.rake | 40 +++++++++++++++++++ 6 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 app/models/legacy/registrar_acl.rb create mode 100644 db/migrate/20151029152638_add_legacy_id_to_users.rb diff --git a/app/models/legacy/registrar.rb b/app/models/legacy/registrar.rb index 80a09a945..048529d62 100644 --- a/app/models/legacy/registrar.rb +++ b/app/models/legacy/registrar.rb @@ -3,6 +3,7 @@ module Legacy self.table_name = :registrar has_many :invoices, foreign_key: :registrarid + has_one :acl, foreign_key: :registrarid, class_name: "Legacy::RegistrarAcl" def account_balance invoices.sum(:credit) diff --git a/app/models/legacy/registrar_acl.rb b/app/models/legacy/registrar_acl.rb new file mode 100644 index 000000000..711d8a2d6 --- /dev/null +++ b/app/models/legacy/registrar_acl.rb @@ -0,0 +1,5 @@ +module Legacy + class RegistrarAcl < Db + self.table_name = :registraracl + end +end diff --git a/db/migrate/20151029152638_add_legacy_id_to_users.rb b/db/migrate/20151029152638_add_legacy_id_to_users.rb new file mode 100644 index 000000000..17e69d7df --- /dev/null +++ b/db/migrate/20151029152638_add_legacy_id_to_users.rb @@ -0,0 +1,5 @@ +class AddLegacyIdToUsers < ActiveRecord::Migration + def change + add_column :users, :legacy_id, :integer + end +end diff --git a/db/schema-read-only.rb b/db/schema-read-only.rb index ca244af5f..b362619b0 100644 --- a/db/schema-read-only.rb +++ b/db/schema-read-only.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150921111842) do +ActiveRecord::Schema.define(version: 20151029152638) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -1053,6 +1053,7 @@ ActiveRecord::Schema.define(version: 20150921111842) do t.datetime "remember_created_at" t.integer "failed_attempts", default: 0, null: false t.datetime "locked_at" + t.integer "legacy_id" end add_index "users", ["identity_code"], name: "index_users_on_identity_code", using: :btree diff --git a/db/structure.sql b/db/structure.sql index fbe00eb4d..b2480a04e 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2658,7 +2658,8 @@ CREATE TABLE users ( encrypted_password character varying DEFAULT ''::character varying NOT NULL, remember_created_at timestamp without time zone, failed_attempts integer DEFAULT 0 NOT NULL, - locked_at timestamp without time zone + locked_at timestamp without time zone, + legacy_id integer ); @@ -4946,3 +4947,5 @@ INSERT INTO schema_migrations (version) VALUES ('20150921110152'); INSERT INTO schema_migrations (version) VALUES ('20150921111842'); +INSERT INTO schema_migrations (version) VALUES ('20151029152638'); + diff --git a/lib/tasks/import.rake b/lib/tasks/import.rake index 54f85b105..b2ea9f6e5 100644 --- a/lib/tasks/import.rake +++ b/lib/tasks/import.rake @@ -52,6 +52,7 @@ namespace :import do desc 'Import all' task all: :environment do Rake::Task['import:registrars'].invoke + Rake::Task['import:users'].invoke Rake::Task['import:contacts'].invoke Rake::Task['import:domains'].invoke Rake::Task['import:zones'].invoke @@ -123,6 +124,45 @@ namespace :import do puts "-----> Imported #{count} new registrars in #{(Time.zone.now.to_f - start).round(2)} seconds" end + desc 'Import users' + task users: :environment do + start = Time.zone.now.to_f + puts '-----> Importing users...' + + users = [] + ips = [] + + existing_ids = ApiUser.pluck(:legacy_id) + + count = 0 + + Legacy::Registrar.all.each do |x| + + next if existing_ids.include?(x.id) + count += 1 + + users << ApiUser.new({ + username: x.handle.try(:strip), + password: x.acl.try(:password), + registrar_id: Registrar.find_by(legacy_id: x.try(:id)).try(:id), + legacy_id: x.try(:id) + }) + + if x.acl.try(:ipaddr) + ips << WhiteIp.new({ + registrar_id: Registrar.find_by(legacy_id: x.try(:id)).try(:id), + ipv4: x.acl.try(:ipaddr) + }) + end + end + + ApiUser.import users, validate: false + if ips + WhiteIp.import ips, validate: false + end + puts "-----> Imported #{count} new users in #{(Time.zone.now.to_f - start).round(2)} seconds" + end + desc 'Import contacts' task contacts: :environment do start = Time.zone.now.to_f