Skip to content

Commit 60f9499

Browse files
authored
Notifications Service Rewrite: Implement Postgres storage and Schema Management (#4720)
We are rewriting the notifications service in go to better align with our engineering capabilities. In this PR we continue to implement the infrastructural components of the new implementation. This PR implements the lower-level database component for the new service. The only behavior implemented is to connect to Postgres and migrate the schema. Integration testing is added to verify that the migration tooling can correctly migrate an existing database/schema (from the elixir implementation) and the tooling can also correctly migrate an empty database instance (i.e., the fresh install case). * Add baseline test for 1.0 schema * Fix file hierarchy and refactor schema baseline tests * Connect notifications2 to postgres on start * TODONE
1 parent 0dbf335 commit 60f9499

File tree

7 files changed

+622
-76
lines changed

7 files changed

+622
-76
lines changed

components/notifications-service2/cmd/notifications-service/commands/serve.go

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/chef/automate/components/automate-deployment/pkg/toml"
88
"github.com/chef/automate/components/notifications-service2/pkg/config"
9+
"github.com/chef/automate/components/notifications-service2/pkg/storage/postgres"
910
"github.com/spf13/cobra"
1011
)
1112

@@ -35,6 +36,10 @@ func runServeCommand(cmd *cobra.Command, args []string) error {
3536
if err != nil {
3637
return err
3738
}
39+
_, err = postgres.Start(c)
40+
if err != nil {
41+
return err
42+
}
3843
for {
3944
fmt.Println("hello from notifications-service2")
4045
s, _ := toml.Marshal(c)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package schema
2+
3+
// PgDump1dot0Schema is the schema created by the 1.0 (elixir) version of the
4+
// notifications service.
5+
// * Exact version used: chef/notifications-service/1.0.0/20210208193336
6+
// * pg_dump command:
7+
/*
8+
hab pkg exec core/postgresql-client pg_dump \
9+
"postgresql://[email protected]:10145/notifications_service?sslmode=verify-ca&sslcert=/hab/svc/notifications-service/config/service.crt&sslkey=/hab/svc/notifications-service/config/service.key&sslrootcert=/hab/svc/notifications-service/config/root_ca.crt" \
10+
--format=plain \
11+
--schema=public \
12+
--inserts \
13+
--no-privileges \
14+
--no-owner
15+
*/
16+
const PgDump1dot0Schema = `
17+
--
18+
-- PostgreSQL database dump
19+
--
20+
21+
-- Dumped from database version 9.6.11
22+
-- Dumped by pg_dump version 9.6.11
23+
24+
SET statement_timeout = 0;
25+
SET lock_timeout = 0;
26+
SET idle_in_transaction_session_timeout = 0;
27+
SET client_encoding = 'UTF8';
28+
SET standard_conforming_strings = on;
29+
SELECT pg_catalog.set_config('search_path', '', false);
30+
SET check_function_bodies = false;
31+
SET client_min_messages = warning;
32+
SET row_security = off;
33+
34+
--
35+
-- Name: rule_action; Type: TYPE; Schema: public; Owner: -
36+
--
37+
38+
CREATE TYPE public.rule_action AS ENUM (
39+
'SlackAlert',
40+
'WebhookAlert',
41+
'ServiceNowAlert'
42+
);
43+
44+
45+
--
46+
-- Name: rule_event; Type: TYPE; Schema: public; Owner: -
47+
--
48+
49+
CREATE TYPE public.rule_event AS ENUM (
50+
'CCRSuccess',
51+
'CCRFailure',
52+
'ComplianceSuccess',
53+
'ComplianceFailure',
54+
'Assets'
55+
);
56+
57+
58+
--
59+
-- Name: log_and_clean_event(character varying, public.rule_event, smallint); Type: FUNCTION; Schema: public; Owner: -
60+
--
61+
62+
CREATE FUNCTION public.log_and_clean_event(id character varying, event_type public.rule_event, delete_older_than smallint) RETURNS boolean
63+
LANGUAGE plpgsql
64+
AS $$
65+
DECLARE
66+
already_processed BOOLEAN;
67+
BEGIN
68+
already_processed = false;
69+
-- First clean up old events:
70+
DELETE FROM processed_events
71+
WHERE at < (CURRENT_TIMESTAMP - (delete_older_than * interval '1 second'));
72+
73+
-- Now try to insert - failure due to duplicate insert means we already
74+
-- processed the event.
75+
BEGIN
76+
INSERT INTO processed_events(inbound_id, event, at)
77+
VALUES (id, event_type, CURRENT_TIMESTAMP);
78+
EXCEPTION WHEN unique_violation THEN
79+
already_processed = true;
80+
END;
81+
RETURN already_processed;
82+
END;
83+
$$;
84+
85+
86+
SET default_tablespace = '';
87+
88+
SET default_with_oids = false;
89+
90+
--
91+
-- Name: migrations; Type: TABLE; Schema: public; Owner: -
92+
--
93+
94+
CREATE TABLE public.migrations (
95+
num integer NOT NULL,
96+
descr text,
97+
at timestamp with time zone NOT NULL
98+
);
99+
100+
101+
--
102+
-- Name: processed_events; Type: TABLE; Schema: public; Owner: -
103+
--
104+
105+
CREATE UNLOGGED TABLE public.processed_events (
106+
inbound_id character varying(64) NOT NULL,
107+
event public.rule_event NOT NULL,
108+
at timestamp with time zone DEFAULT now() NOT NULL
109+
);
110+
111+
112+
--
113+
-- Name: rules; Type: TABLE; Schema: public; Owner: -
114+
--
115+
116+
CREATE TABLE public.rules (
117+
id uuid NOT NULL,
118+
name text NOT NULL,
119+
event public.rule_event NOT NULL,
120+
action public.rule_action NOT NULL,
121+
url text NOT NULL,
122+
secret_id character varying,
123+
critical_controls_only boolean DEFAULT false
124+
);
125+
126+
127+
--
128+
-- Data for Name: migrations; Type: TABLE DATA; Schema: public; Owner: -
129+
--
130+
131+
INSERT INTO public.migrations VALUES (1, 'Migration tracking setup', '2021-02-10 22:35:56.038134+00');
132+
INSERT INTO public.migrations VALUES (2, 'Create notifications data types and relations', '2021-02-10 22:35:56.042387+00');
133+
INSERT INTO public.migrations VALUES (3, 'Enable deduplication of received events', '2021-02-10 22:35:56.063957+00');
134+
INSERT INTO public.migrations VALUES (4, 'Add ServiceNowAlert as a rule_action', '2021-02-10 22:35:56.080764+00');
135+
INSERT INTO public.migrations VALUES (5, 'Add secret ID to rules table', '2021-02-10 22:35:56.085704+00');
136+
INSERT INTO public.migrations VALUES (6, 'Add support for Assets event', '2021-02-10 22:35:56.089865+00');
137+
INSERT INTO public.migrations VALUES (7, 'Add critical_controls_only to rules table', '2021-02-10 22:35:56.091723+00');
138+
INSERT INTO public.migrations VALUES (8, 'UPDATE rules SET critical_controls_only=FALSE', '2021-02-10 22:35:56.102042+00');
139+
INSERT INTO public.migrations VALUES (9, 'DELETE rules with event=''Assets''', '2021-02-10 22:35:56.107859+00');
140+
141+
142+
--
143+
-- Data for Name: processed_events; Type: TABLE DATA; Schema: public; Owner: -
144+
--
145+
146+
147+
148+
--
149+
-- Data for Name: rules; Type: TABLE DATA; Schema: public; Owner: -
150+
--
151+
152+
153+
154+
--
155+
-- Name: processed_events processed_events_pkey; Type: CONSTRAINT; Schema: public; Owner: -
156+
--
157+
158+
ALTER TABLE ONLY public.processed_events
159+
ADD CONSTRAINT processed_events_pkey PRIMARY KEY (inbound_id, event);
160+
161+
162+
--
163+
-- Name: rules rules_name_key; Type: CONSTRAINT; Schema: public; Owner: -
164+
--
165+
166+
ALTER TABLE ONLY public.rules
167+
ADD CONSTRAINT rules_name_key UNIQUE (name);
168+
169+
170+
--
171+
-- Name: rules rules_pkey; Type: CONSTRAINT; Schema: public; Owner: -
172+
--
173+
174+
ALTER TABLE ONLY public.rules
175+
ADD CONSTRAINT rules_pkey PRIMARY KEY (id);
176+
177+
178+
--
179+
-- PostgreSQL database dump complete
180+
--
181+
182+
`

0 commit comments

Comments
 (0)