Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create compliance_checks_alerts table #40

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const severityLevels = ['critical', 'high', 'medium', 'low', 'info']

exports.up = async (knex) => {
await knex.schema.createTable('compliance_checks_alerts', (table) => {
table.increments('id').primary() // Primary key
table.text('title').notNullable()
table.text('description').notNullable()
table.enum('severity', severityLevels).notNullable()

// Foreign key to 'compliance_checks' table
table
.integer('compliance_check_id')
.unsigned()
.references('id')
.inTable('compliance_checks')
.onDelete('CASCADE') // Deletes repository if the organization is deleted
.onUpdate('CASCADE') // Updates repository if the organization ID is updated
.notNullable()

// Foreign key to 'projects' table
table
.integer('project_id')
.unsigned()
.references('id')
.inTable('projects')
.onDelete('CASCADE') // Deletes repository if the organization is deleted
.onUpdate('CASCADE') // Updates repository if the organization ID is updated
.notNullable()

// Timestamps
table.timestamp('created_at').defaultTo(knex.fn.now()).notNullable()
table.timestamp('updated_at').defaultTo(knex.fn.now()).notNullable()
})

// Add trigger to automatically update the 'updated_at' column
await knex.raw(`
CREATE TRIGGER set_updated_at_compliance_checks_alerts
BEFORE UPDATE ON compliance_checks_alerts
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
`)
}

exports.down = async (knex) => {
// Drop trigger
await knex.raw('DROP TRIGGER IF EXISTS set_updated_at_compliance_checks_alerts ON compliance_checks_alerts;')
// Drop table
await knex.schema.dropTableIfExists('compliance_checks_alerts')
}
75 changes: 75 additions & 0 deletions src/database/schema/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,43 @@ CREATE TABLE public.compliance_checks (
);


--
-- Name: compliance_checks_alerts; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.compliance_checks_alerts (
id integer NOT NULL,
title text NOT NULL,
description text NOT NULL,
severity text NOT NULL,
compliance_check_id integer NOT NULL,
project_id integer NOT NULL,
created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
CONSTRAINT compliance_checks_alerts_severity_check CHECK ((severity = ANY (ARRAY['critical'::text, 'high'::text, 'medium'::text, 'low'::text, 'info'::text])))
);


--
-- Name: compliance_checks_alerts_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.compliance_checks_alerts_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


--
-- Name: compliance_checks_alerts_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.compliance_checks_alerts_id_seq OWNED BY public.compliance_checks_alerts.id;


--
-- Name: compliance_checks_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
Expand Down Expand Up @@ -372,6 +409,13 @@ ALTER SEQUENCE public.projects_id_seq OWNED BY public.projects.id;
ALTER TABLE ONLY public.compliance_checks ALTER COLUMN id SET DEFAULT nextval('public.compliance_checks_id_seq'::regclass);


--
-- Name: compliance_checks_alerts id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.compliance_checks_alerts ALTER COLUMN id SET DEFAULT nextval('public.compliance_checks_alerts_id_seq'::regclass);


--
-- Name: github_organizations id; Type: DEFAULT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -407,6 +451,14 @@ ALTER TABLE ONLY public.knex_migrations_lock ALTER COLUMN index SET DEFAULT next
ALTER TABLE ONLY public.projects ALTER COLUMN id SET DEFAULT nextval('public.projects_id_seq'::regclass);


--
-- Name: compliance_checks_alerts compliance_checks_alerts_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.compliance_checks_alerts
ADD CONSTRAINT compliance_checks_alerts_pkey PRIMARY KEY (id);


--
-- Name: compliance_checks compliance_checks_code_name_unique; Type: CONSTRAINT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -502,6 +554,13 @@ ALTER TABLE ONLY public.projects
CREATE TRIGGER set_updated_at_compliance_checks BEFORE UPDATE ON public.compliance_checks FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();


--
-- Name: compliance_checks_alerts set_updated_at_compliance_checks_alerts; Type: TRIGGER; Schema: public; Owner: -
--

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();


--
-- Name: github_organizations set_updated_at_github_organizations; Type: TRIGGER; Schema: public; Owner: -
--
Expand All @@ -523,6 +582,22 @@ CREATE TRIGGER set_updated_at_github_repositories BEFORE UPDATE ON public.github
CREATE TRIGGER set_updated_at_projects BEFORE UPDATE ON public.projects FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();


--
-- Name: compliance_checks_alerts compliance_checks_alerts_compliance_check_id_foreign; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.compliance_checks_alerts
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;


--
-- Name: compliance_checks_alerts compliance_checks_alerts_project_id_foreign; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.compliance_checks_alerts
ADD CONSTRAINT compliance_checks_alerts_project_id_foreign FOREIGN KEY (project_id) REFERENCES public.projects(id) ON UPDATE CASCADE ON DELETE CASCADE;


--
-- Name: github_organizations github_organizations_project_id_foreign; Type: FK CONSTRAINT; Schema: public; Owner: -
--
Expand Down
Loading