From d4fe961e34dafa99cc380b4fbe74563fc3bb6727 Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Tue, 29 Jun 2021 13:47:38 +0500 Subject: [PATCH] Add basic storage for validation events --- app/models/contact.rb | 1 + app/models/validation_event.rb | 9 +++ app/models/validation_event/event_type.rb | 10 +++ ...20210629074044_create_validation_events.rb | 28 +++++++ db/structure.sql | 75 +++++++++++++++++++ 5 files changed, 123 insertions(+) create mode 100644 app/models/validation_event.rb create mode 100644 app/models/validation_event/event_type.rb create mode 100644 db/migrate/20210629074044_create_validation_events.rb diff --git a/app/models/contact.rb b/app/models/contact.rb index f33c22c74..84d4ba962 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -16,6 +16,7 @@ class Contact < ApplicationRecord has_many :domain_contacts has_many :domains, through: :domain_contacts has_many :legal_documents, as: :documentable + has_many :validation_events, as: :validation_eventable has_many :registrant_domains, class_name: 'Domain', foreign_key: 'registrant_id' has_many :actions, dependent: :destroy diff --git a/app/models/validation_event.rb b/app/models/validation_event.rb new file mode 100644 index 000000000..9b69dd44f --- /dev/null +++ b/app/models/validation_event.rb @@ -0,0 +1,9 @@ +class ValidationEvent < ActiveRecord::Base + enum event_type: ValidationEvent::EventType::TYPES, _suffix: true + + belongs_to :validation_eventable, polymorphic: true + + def event_type + @event_type ||= ValidationEvent::EventType.new(read_attribute(:event_kind)) + end +end diff --git a/app/models/validation_event/event_type.rb b/app/models/validation_event/event_type.rb new file mode 100644 index 000000000..7253ec504 --- /dev/null +++ b/app/models/validation_event/event_type.rb @@ -0,0 +1,10 @@ +class ValidationEvent + class EventType + TYPES = { email_validation: 'email_validation', + manual_force_delete: 'manual_force_delete' }.freeze + + def initialize(event_type) + @event_type = event_type + end + end +end diff --git a/db/migrate/20210629074044_create_validation_events.rb b/db/migrate/20210629074044_create_validation_events.rb new file mode 100644 index 000000000..ab58dccf7 --- /dev/null +++ b/db/migrate/20210629074044_create_validation_events.rb @@ -0,0 +1,28 @@ +class CreateValidationEvents < ActiveRecord::Migration[6.1] + + def up + execute <<-SQL + CREATE TYPE validation_type AS ENUM ('email_validation', 'manual_force_delete'); + SQL + + create_table :validation_events do |t| + t.jsonb :event_data + t.boolean :result + t.references :validation_eventable, polymorphic: true + + t.timestamps + end + + add_column :validation_events, :event_type, :validation_type + add_index :validation_events, :event_type + end + + def down + remove_column :validation_events, :event_type + execute <<-SQL + DROP TYPE validation_type; + SQL + + drop_table :validation_events + end +end diff --git a/db/structure.sql b/db/structure.sql index 365e975cc..f96230984 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -65,6 +65,16 @@ CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA public; COMMENT ON EXTENSION pgcrypto IS 'cryptographic functions'; +-- +-- Name: validation_type; Type: TYPE; Schema: public; Owner: - +-- + +CREATE TYPE public.validation_type AS ENUM ( + 'email_validation', + 'manual_force_delete' +); + + -- -- Name: generate_zonefile(character varying); Type: FUNCTION; Schema: public; Owner: - -- @@ -2595,6 +2605,41 @@ CREATE SEQUENCE public.users_id_seq ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id; +-- +-- Name: validation_events; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.validation_events ( + id bigint NOT NULL, + event_data jsonb, + result boolean, + validation_eventable_type character varying, + validation_eventable_id bigint, + created_at timestamp(6) without time zone NOT NULL, + updated_at timestamp(6) without time zone NOT NULL, + event_type public.validation_type +); + + +-- +-- Name: validation_events_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE public.validation_events_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: validation_events_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE public.validation_events_id_seq OWNED BY public.validation_events.id; + + -- -- Name: versions; Type: TABLE; Schema: public; Owner: - -- @@ -3170,6 +3215,13 @@ ALTER TABLE ONLY public.settings ALTER COLUMN id SET DEFAULT nextval('public.set ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_id_seq'::regclass); +-- +-- Name: validation_events id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.validation_events ALTER COLUMN id SET DEFAULT nextval('public.validation_events_id_seq'::regclass); + + -- -- Name: versions id; Type: DEFAULT; Schema: public; Owner: - -- @@ -3814,6 +3866,14 @@ ALTER TABLE ONLY public.users ADD CONSTRAINT users_pkey PRIMARY KEY (id); +-- +-- Name: validation_events validation_events_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.validation_events + ADD CONSTRAINT validation_events_pkey PRIMARY KEY (id); + + -- -- Name: versions versions_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- @@ -4427,6 +4487,20 @@ CREATE INDEX index_users_on_identity_code ON public.users USING btree (identity_ CREATE INDEX index_users_on_registrar_id ON public.users USING btree (registrar_id); +-- +-- Name: index_validation_events_on_event_type; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_validation_events_on_event_type ON public.validation_events USING btree (event_type); + + +-- +-- Name: index_validation_events_on_validation_eventable; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_validation_events_on_validation_eventable ON public.validation_events USING btree (validation_eventable_type, validation_eventable_id); + + -- -- Name: index_versions_on_item_type_and_item_id; Type: INDEX; Schema: public; Owner: - -- @@ -5154,6 +5228,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20200921084356'), ('20210215101019'), ('20210616112332'), +('20210629074044'), ('20210628090353'), ('20210708131814');