From aaaae888bf16e8b450c0d49d028df1588d1172f5 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Thu, 17 Jun 2021 11:06:57 +0300 Subject: [PATCH] implement asynchronous rake task for collect contact data from csv --- ...scan_csv_registry_businnes_contacts_job.rb | 21 ++++++++ app/models/business_registry_contact.rb | 2 + ...12332_create_business_registry_contacts.rb | 11 ++++ db/structure.sql | 54 +++++++++++++++++-- .../scan_csv_registry_business_contacts.rake | 7 +++ 5 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 app/jobs/scan_csv_registry_businnes_contacts_job.rb create mode 100644 app/models/business_registry_contact.rb create mode 100644 db/migrate/20210616112332_create_business_registry_contacts.rb create mode 100644 lib/tasks/scan_csv_registry_business_contacts.rake diff --git a/app/jobs/scan_csv_registry_businnes_contacts_job.rb b/app/jobs/scan_csv_registry_businnes_contacts_job.rb new file mode 100644 index 000000000..ac260a728 --- /dev/null +++ b/app/jobs/scan_csv_registry_businnes_contacts_job.rb @@ -0,0 +1,21 @@ +class ScanCsvRegistryBusinnesContactsJob < ApplicationJob + FILE_NAME = './ettevotja_rekvisiidid_init.csv'.freeze + def perform + BusinessRegistryContact.delete_all + + return p 'File not exist!' unless File.exist?(FILE_NAME) + + CSV.foreach(FILE_NAME, headers: true, col_sep: ";") do |row| + name = row[0] + code = row[1] + status = row[5] + + record = BusinessRegistryContact.create({ + name: name, + registry_code: code, + status: status + }) + p "#{record} is successfully created - #{BusinessRegistryContact.count} count" + end + end +end diff --git a/app/models/business_registry_contact.rb b/app/models/business_registry_contact.rb new file mode 100644 index 000000000..432d1233e --- /dev/null +++ b/app/models/business_registry_contact.rb @@ -0,0 +1,2 @@ +class BusinessRegistryContact < ApplicationRecord +end diff --git a/db/migrate/20210616112332_create_business_registry_contacts.rb b/db/migrate/20210616112332_create_business_registry_contacts.rb new file mode 100644 index 000000000..a5db78ecc --- /dev/null +++ b/db/migrate/20210616112332_create_business_registry_contacts.rb @@ -0,0 +1,11 @@ +class CreateBusinessRegistryContacts < ActiveRecord::Migration[6.1] + def change + create_table :business_registry_contacts do |t| + t.string :name + t.string :registry_code + t.string :status + + t.timestamps + end + end +end diff --git a/db/structure.sql b/db/structure.sql index fc15cbe44..481732b8a 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -497,6 +497,39 @@ CREATE SEQUENCE public.bounced_mail_addresses_id_seq ALTER SEQUENCE public.bounced_mail_addresses_id_seq OWNED BY public.bounced_mail_addresses.id; +-- +-- Name: business_registry_contacts; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.business_registry_contacts ( + id bigint NOT NULL, + name character varying, + registry_code character varying, + status character varying, + created_at timestamp(6) without time zone NOT NULL, + updated_at timestamp(6) without time zone NOT NULL +); + + +-- +-- Name: business_registry_contacts_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE public.business_registry_contacts_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: business_registry_contacts_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE public.business_registry_contacts_id_seq OWNED BY public.business_registry_contacts.id; + + -- -- Name: certificates; Type: TABLE; Schema: public; Owner: - -- @@ -541,7 +574,6 @@ ALTER SEQUENCE public.certificates_id_seq OWNED BY public.certificates.id; -- CREATE TABLE public.contact_requests ( - id integer NOT NULL, whois_record_id integer NOT NULL, secret character varying NOT NULL, @@ -553,7 +585,6 @@ CREATE TABLE public.contact_requests ( created_at timestamp without time zone NOT NULL, updated_at timestamp without time zone NOT NULL, message_id character varying - ); @@ -575,6 +606,7 @@ CREATE SEQUENCE public.contact_requests_id_seq ALTER SEQUENCE public.contact_requests_id_seq OWNED BY public.contact_requests.id; + -- -- Name: contacts; Type: TABLE; Schema: public; Owner: - -- @@ -2766,6 +2798,13 @@ ALTER TABLE ONLY public.blocked_domains ALTER COLUMN id SET DEFAULT nextval('pub ALTER TABLE ONLY public.bounced_mail_addresses ALTER COLUMN id SET DEFAULT nextval('public.bounced_mail_addresses_id_seq'::regclass); +-- +-- Name: business_registry_contacts id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.business_registry_contacts ALTER COLUMN id SET DEFAULT nextval('public.business_registry_contacts_id_seq'::regclass); + + -- -- Name: certificates id; Type: DEFAULT; Schema: public; Owner: - -- @@ -3230,6 +3269,14 @@ ALTER TABLE ONLY public.bounced_mail_addresses ADD CONSTRAINT bounced_mail_addresses_pkey PRIMARY KEY (id); +-- +-- Name: business_registry_contacts business_registry_contacts_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.business_registry_contacts + ADD CONSTRAINT business_registry_contacts_pkey PRIMARY KEY (id); + + -- -- Name: certificates certificates_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- @@ -5104,6 +5151,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20200916125326'), ('20200917104213'), ('20200921084356'), -('20210215101019'); +('20210215101019'), +('20210616112332'); diff --git a/lib/tasks/scan_csv_registry_business_contacts.rake b/lib/tasks/scan_csv_registry_business_contacts.rake new file mode 100644 index 000000000..7de414d0e --- /dev/null +++ b/lib/tasks/scan_csv_registry_business_contacts.rake @@ -0,0 +1,7 @@ +namespace :collect_csv_data do + desc 'Import from csv registry business contact into BusinessRegistryContact model' + + task business_contacts: :environment do + ScanCsvRegistryBusinnesContactsJob.perform_later + end +end \ No newline at end of file