Skip to content

Commit 953472a

Browse files
authored
Merge pull request #42 from secure-dashboards/feat/add-compliance-checks
2 parents d0bab06 + 4ca52a5 commit 953472a

File tree

2 files changed

+126
-0
lines changed

2 files changed

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

src/database/schema/schema.sql

+76
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,44 @@ CREATE SEQUENCE public.compliance_checks_id_seq
128128
ALTER SEQUENCE public.compliance_checks_id_seq OWNED BY public.compliance_checks.id;
129129

130130

131+
--
132+
-- Name: compliance_checks_results; Type: TABLE; Schema: public; Owner: -
133+
--
134+
135+
CREATE TABLE public.compliance_checks_results (
136+
id integer NOT NULL,
137+
severity text NOT NULL,
138+
status text NOT NULL,
139+
rationale text NOT NULL,
140+
compliance_check_id integer NOT NULL,
141+
project_id integer NOT NULL,
142+
created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
143+
updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
144+
CONSTRAINT compliance_checks_results_severity_check CHECK ((severity = ANY (ARRAY['critical'::text, 'high'::text, 'medium'::text, 'low'::text, 'info'::text]))),
145+
CONSTRAINT compliance_checks_results_status_check CHECK ((status = ANY (ARRAY['unknown'::text, 'passed'::text, 'failed'::text])))
146+
);
147+
148+
149+
--
150+
-- Name: compliance_checks_results_id_seq; Type: SEQUENCE; Schema: public; Owner: -
151+
--
152+
153+
CREATE SEQUENCE public.compliance_checks_results_id_seq
154+
AS integer
155+
START WITH 1
156+
INCREMENT BY 1
157+
NO MINVALUE
158+
NO MAXVALUE
159+
CACHE 1;
160+
161+
162+
--
163+
-- Name: compliance_checks_results_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
164+
--
165+
166+
ALTER SEQUENCE public.compliance_checks_results_id_seq OWNED BY public.compliance_checks_results.id;
167+
168+
131169
--
132170
-- Name: compliance_checks_tasks; Type: TABLE; Schema: public; Owner: -
133171
--
@@ -453,6 +491,13 @@ ALTER TABLE ONLY public.compliance_checks ALTER COLUMN id SET DEFAULT nextval('p
453491
ALTER TABLE ONLY public.compliance_checks_alerts ALTER COLUMN id SET DEFAULT nextval('public.compliance_checks_alerts_id_seq'::regclass);
454492

455493

494+
--
495+
-- Name: compliance_checks_results id; Type: DEFAULT; Schema: public; Owner: -
496+
--
497+
498+
ALTER TABLE ONLY public.compliance_checks_results ALTER COLUMN id SET DEFAULT nextval('public.compliance_checks_results_id_seq'::regclass);
499+
500+
456501
--
457502
-- Name: compliance_checks_tasks id; Type: DEFAULT; Schema: public; Owner: -
458503
--
@@ -519,6 +564,14 @@ ALTER TABLE ONLY public.compliance_checks
519564
ADD CONSTRAINT compliance_checks_pkey PRIMARY KEY (id);
520565

521566

567+
--
568+
-- Name: compliance_checks_results compliance_checks_results_pkey; Type: CONSTRAINT; Schema: public; Owner: -
569+
--
570+
571+
ALTER TABLE ONLY public.compliance_checks_results
572+
ADD CONSTRAINT compliance_checks_results_pkey PRIMARY KEY (id);
573+
574+
522575
--
523576
-- Name: compliance_checks_tasks compliance_checks_tasks_pkey; Type: CONSTRAINT; Schema: public; Owner: -
524577
--
@@ -613,6 +666,13 @@ CREATE TRIGGER set_updated_at_compliance_checks BEFORE UPDATE ON public.complian
613666
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();
614667

615668

669+
--
670+
-- Name: compliance_checks_results set_updated_at_compliance_checks_results; Type: TRIGGER; Schema: public; Owner: -
671+
--
672+
673+
CREATE TRIGGER set_updated_at_compliance_checks_results BEFORE UPDATE ON public.compliance_checks_results FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();
674+
675+
616676
--
617677
-- Name: compliance_checks_tasks set_updated_at_compliance_checks_tasks; Type: TRIGGER; Schema: public; Owner: -
618678
--
@@ -657,6 +717,22 @@ ALTER TABLE ONLY public.compliance_checks_alerts
657717
ADD CONSTRAINT compliance_checks_alerts_project_id_foreign FOREIGN KEY (project_id) REFERENCES public.projects(id) ON UPDATE CASCADE ON DELETE CASCADE;
658718

659719

720+
--
721+
-- Name: compliance_checks_results compliance_checks_results_compliance_check_id_foreign; Type: FK CONSTRAINT; Schema: public; Owner: -
722+
--
723+
724+
ALTER TABLE ONLY public.compliance_checks_results
725+
ADD CONSTRAINT compliance_checks_results_compliance_check_id_foreign FOREIGN KEY (compliance_check_id) REFERENCES public.compliance_checks(id) ON UPDATE CASCADE ON DELETE CASCADE;
726+
727+
728+
--
729+
-- Name: compliance_checks_results compliance_checks_results_project_id_foreign; Type: FK CONSTRAINT; Schema: public; Owner: -
730+
--
731+
732+
ALTER TABLE ONLY public.compliance_checks_results
733+
ADD CONSTRAINT compliance_checks_results_project_id_foreign FOREIGN KEY (project_id) REFERENCES public.projects(id) ON UPDATE CASCADE ON DELETE CASCADE;
734+
735+
660736
--
661737
-- Name: compliance_checks_tasks compliance_checks_tasks_compliance_check_id_foreign; Type: FK CONSTRAINT; Schema: public; Owner: -
662738
--

0 commit comments

Comments
 (0)