Skip to content

Commit 3107f14

Browse files
authored
Merge pull request #40 from secure-dashboards/feat/add-compliance-checks
2 parents 2bbb998 + 1cfc73f commit 3107f14

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const severityLevels = ['critical', 'high', 'medium', 'low', 'info']
2+
3+
exports.up = async (knex) => {
4+
await knex.schema.createTable('compliance_checks_alerts', (table) => {
5+
table.increments('id').primary() // Primary key
6+
table.text('title').notNullable()
7+
table.text('description').notNullable()
8+
table.enum('severity', severityLevels).notNullable()
9+
10+
// Foreign key to 'compliance_checks' table
11+
table
12+
.integer('compliance_check_id')
13+
.unsigned()
14+
.references('id')
15+
.inTable('compliance_checks')
16+
.onDelete('CASCADE') // Deletes repository if the organization is deleted
17+
.onUpdate('CASCADE') // Updates repository if the organization ID is updated
18+
.notNullable()
19+
20+
// Foreign key to 'projects' table
21+
table
22+
.integer('project_id')
23+
.unsigned()
24+
.references('id')
25+
.inTable('projects')
26+
.onDelete('CASCADE') // Deletes repository if the organization is deleted
27+
.onUpdate('CASCADE') // Updates repository if the organization ID is updated
28+
.notNullable()
29+
30+
// Timestamps
31+
table.timestamp('created_at').defaultTo(knex.fn.now()).notNullable()
32+
table.timestamp('updated_at').defaultTo(knex.fn.now()).notNullable()
33+
})
34+
35+
// Add trigger to automatically update the 'updated_at' column
36+
await knex.raw(`
37+
CREATE TRIGGER set_updated_at_compliance_checks_alerts
38+
BEFORE UPDATE ON compliance_checks_alerts
39+
FOR EACH ROW
40+
EXECUTE FUNCTION update_updated_at_column();
41+
`)
42+
}
43+
44+
exports.down = async (knex) => {
45+
// Drop trigger
46+
await knex.raw('DROP TRIGGER IF EXISTS set_updated_at_compliance_checks_alerts ON compliance_checks_alerts;')
47+
// Drop table
48+
await knex.schema.dropTableIfExists('compliance_checks_alerts')
49+
}

src/database/schema/schema.sql

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,43 @@ CREATE TABLE public.compliance_checks (
7171
);
7272

7373

74+
--
75+
-- Name: compliance_checks_alerts; Type: TABLE; Schema: public; Owner: -
76+
--
77+
78+
CREATE TABLE public.compliance_checks_alerts (
79+
id integer NOT NULL,
80+
title text NOT NULL,
81+
description text NOT NULL,
82+
severity text NOT NULL,
83+
compliance_check_id integer NOT NULL,
84+
project_id integer NOT NULL,
85+
created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
86+
updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
87+
CONSTRAINT compliance_checks_alerts_severity_check CHECK ((severity = ANY (ARRAY['critical'::text, 'high'::text, 'medium'::text, 'low'::text, 'info'::text])))
88+
);
89+
90+
91+
--
92+
-- Name: compliance_checks_alerts_id_seq; Type: SEQUENCE; Schema: public; Owner: -
93+
--
94+
95+
CREATE SEQUENCE public.compliance_checks_alerts_id_seq
96+
AS integer
97+
START WITH 1
98+
INCREMENT BY 1
99+
NO MINVALUE
100+
NO MAXVALUE
101+
CACHE 1;
102+
103+
104+
--
105+
-- Name: compliance_checks_alerts_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
106+
--
107+
108+
ALTER SEQUENCE public.compliance_checks_alerts_id_seq OWNED BY public.compliance_checks_alerts.id;
109+
110+
74111
--
75112
-- Name: compliance_checks_id_seq; Type: SEQUENCE; Schema: public; Owner: -
76113
--
@@ -372,6 +409,13 @@ ALTER SEQUENCE public.projects_id_seq OWNED BY public.projects.id;
372409
ALTER TABLE ONLY public.compliance_checks ALTER COLUMN id SET DEFAULT nextval('public.compliance_checks_id_seq'::regclass);
373410

374411

412+
--
413+
-- Name: compliance_checks_alerts id; Type: DEFAULT; Schema: public; Owner: -
414+
--
415+
416+
ALTER TABLE ONLY public.compliance_checks_alerts ALTER COLUMN id SET DEFAULT nextval('public.compliance_checks_alerts_id_seq'::regclass);
417+
418+
375419
--
376420
-- Name: github_organizations id; Type: DEFAULT; Schema: public; Owner: -
377421
--
@@ -407,6 +451,14 @@ ALTER TABLE ONLY public.knex_migrations_lock ALTER COLUMN index SET DEFAULT next
407451
ALTER TABLE ONLY public.projects ALTER COLUMN id SET DEFAULT nextval('public.projects_id_seq'::regclass);
408452

409453

454+
--
455+
-- Name: compliance_checks_alerts compliance_checks_alerts_pkey; Type: CONSTRAINT; Schema: public; Owner: -
456+
--
457+
458+
ALTER TABLE ONLY public.compliance_checks_alerts
459+
ADD CONSTRAINT compliance_checks_alerts_pkey PRIMARY KEY (id);
460+
461+
410462
--
411463
-- Name: compliance_checks compliance_checks_code_name_unique; Type: CONSTRAINT; Schema: public; Owner: -
412464
--
@@ -502,6 +554,13 @@ ALTER TABLE ONLY public.projects
502554
CREATE TRIGGER set_updated_at_compliance_checks BEFORE UPDATE ON public.compliance_checks FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();
503555

504556

557+
--
558+
-- Name: compliance_checks_alerts set_updated_at_compliance_checks_alerts; Type: TRIGGER; Schema: public; Owner: -
559+
--
560+
561+
CREATE TRIGGER set_updated_at_compliance_checks_alerts BEFORE UPDATE ON public.compliance_checks_alerts FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();
562+
563+
505564
--
506565
-- Name: github_organizations set_updated_at_github_organizations; Type: TRIGGER; Schema: public; Owner: -
507566
--
@@ -523,6 +582,22 @@ CREATE TRIGGER set_updated_at_github_repositories BEFORE UPDATE ON public.github
523582
CREATE TRIGGER set_updated_at_projects BEFORE UPDATE ON public.projects FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();
524583

525584

585+
--
586+
-- Name: compliance_checks_alerts compliance_checks_alerts_compliance_check_id_foreign; Type: FK CONSTRAINT; Schema: public; Owner: -
587+
--
588+
589+
ALTER TABLE ONLY public.compliance_checks_alerts
590+
ADD CONSTRAINT compliance_checks_alerts_compliance_check_id_foreign FOREIGN KEY (compliance_check_id) REFERENCES public.compliance_checks(id) ON UPDATE CASCADE ON DELETE CASCADE;
591+
592+
593+
--
594+
-- Name: compliance_checks_alerts compliance_checks_alerts_project_id_foreign; Type: FK CONSTRAINT; Schema: public; Owner: -
595+
--
596+
597+
ALTER TABLE ONLY public.compliance_checks_alerts
598+
ADD CONSTRAINT compliance_checks_alerts_project_id_foreign FOREIGN KEY (project_id) REFERENCES public.projects(id) ON UPDATE CASCADE ON DELETE CASCADE;
599+
600+
526601
--
527602
-- Name: github_organizations github_organizations_project_id_foreign; Type: FK CONSTRAINT; Schema: public; Owner: -
528603
--

0 commit comments

Comments
 (0)