larxius commited on
Commit
9d03927
·
verified ·
1 Parent(s): e56ad53

Update database/local_schema.sql

Browse files
Files changed (1) hide show
  1. database/local_schema.sql +410 -0
database/local_schema.sql ADDED
@@ -0,0 +1,410 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ --
2
+ -- PostgreSQL database dump (Upgraded & Production-Ready for LarShield)
3
+ -- Dumped with full Policy Consent tracking, Vulnerability Lifecycle Management,
4
+ -- Scan Metrics, Foreign Key Cascades, Indexes, and System Seed Data.
5
+ --
6
+
7
+ SET statement_timeout = 0;
8
+ SET lock_timeout = 0;
9
+ SET idle_in_transaction_session_timeout = 0;
10
+ SET transaction_timeout = 0;
11
+ SET client_encoding = 'UTF8';
12
+ SET standard_conforming_strings = on;
13
+ SELECT pg_catalog.set_config('search_path', '', false);
14
+ SET check_function_bodies = false;
15
+ SET xmloption = content;
16
+ SET client_min_messages = warning;
17
+ SET row_security = off;
18
+
19
+ SET default_tablespace = '';
20
+ SET default_table_access_method = heap;
21
+
22
+ -- --------------------------------------------------------
23
+ -- Name: subscription_tiers; Type: TABLE; Schema: public; Owner: postgres
24
+ -- --------------------------------------------------------
25
+ CREATE TABLE public.subscription_tiers (
26
+ id character varying(50) NOT NULL,
27
+ name character varying(100) NOT NULL,
28
+ monthly_price integer DEFAULT 0 NOT NULL,
29
+ yearly_price integer DEFAULT 0 NOT NULL
30
+ );
31
+
32
+ ALTER TABLE public.subscription_tiers OWNER TO postgres;
33
+
34
+ -- --------------------------------------------------------
35
+ -- Name: organizations; Type: TABLE; Schema: public; Owner: postgres
36
+ -- --------------------------------------------------------
37
+ CREATE TABLE public.organizations (
38
+ id character varying(36) NOT NULL,
39
+ name character varying(150) NOT NULL,
40
+ subscription_tier character varying(50) DEFAULT 'free'::character varying NOT NULL,
41
+ status character varying(50) DEFAULT 'active'::character varying,
42
+ api_key character varying(100),
43
+ webhook_url character varying(500),
44
+ report_logo_url character varying(500),
45
+ created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
46
+ );
47
+
48
+ ALTER TABLE public.organizations OWNER TO postgres;
49
+
50
+ -- --------------------------------------------------------
51
+ -- Name: roles; Type: TABLE; Schema: public; Owner: postgres
52
+ -- --------------------------------------------------------
53
+ CREATE TABLE public.roles (
54
+ id character varying(36) NOT NULL,
55
+ name character varying(50) NOT NULL
56
+ );
57
+
58
+ ALTER TABLE public.roles OWNER TO postgres;
59
+
60
+ -- --------------------------------------------------------
61
+ -- Name: users; Type: TABLE; Schema: public; Owner: postgres
62
+ -- --------------------------------------------------------
63
+ CREATE TABLE public.users (
64
+ id character varying(36) NOT NULL,
65
+ email character varying(120) NOT NULL,
66
+ password_hash character varying(128) NOT NULL,
67
+ first_name character varying(100),
68
+ last_name character varying(100),
69
+ role character varying(50) DEFAULT 'org_admin'::character varying NOT NULL,
70
+ org_id character varying(36),
71
+ failed_login_attempts integer DEFAULT 0,
72
+ locked_until timestamp without time zone,
73
+ created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
74
+
75
+ -- Added Policy Consent & Agreement Fields
76
+ terms_accepted_at timestamp without time zone,
77
+ privacy_policy_agreed_at timestamp without time zone,
78
+ policy_version_agreed character varying(20) DEFAULT 'v1.0'::character varying,
79
+
80
+ -- Added Security & MFA Metadata
81
+ mfa_enabled boolean DEFAULT false,
82
+ mfa_secret character varying(100),
83
+ reset_token character varying(255),
84
+ reset_token_expires timestamp without time zone,
85
+ email_verified boolean DEFAULT false
86
+ );
87
+
88
+ ALTER TABLE public.users OWNER TO postgres;
89
+
90
+ -- --------------------------------------------------------
91
+ -- Name: alert_settings; Type: TABLE; Schema: public; Owner: postgres
92
+ -- --------------------------------------------------------
93
+ CREATE TABLE public.alert_settings (
94
+ id character varying(36) NOT NULL,
95
+ user_id character varying(36) NOT NULL,
96
+ email_notifications boolean DEFAULT true,
97
+ webhook_url character varying(500),
98
+ severity_threshold character varying(50) DEFAULT 'Medium'::character varying
99
+ );
100
+
101
+ ALTER TABLE public.alert_settings OWNER TO postgres;
102
+
103
+ -- --------------------------------------------------------
104
+ -- Name: organization_scan_quotas; Type: TABLE; Schema: public; Owner: postgres
105
+ -- --------------------------------------------------------
106
+ CREATE TABLE public.organization_scan_quotas (
107
+ id character varying(36) NOT NULL,
108
+ org_id character varying(36) NOT NULL,
109
+ scan_type character varying(50) NOT NULL,
110
+ allocated_count integer DEFAULT 0 NOT NULL,
111
+ used_count integer DEFAULT 0 NOT NULL
112
+ );
113
+
114
+ ALTER TABLE public.organization_scan_quotas OWNER TO postgres;
115
+
116
+ -- --------------------------------------------------------
117
+ -- Name: scans; Type: TABLE; Schema: public; Owner: postgres
118
+ -- --------------------------------------------------------
119
+ CREATE TABLE public.scans (
120
+ id character varying(36) NOT NULL,
121
+ org_id character varying(36) NOT NULL,
122
+ user_id character varying(36) NOT NULL,
123
+ target_url character varying(500) NOT NULL,
124
+ scan_type character varying(50) DEFAULT 'Full'::character varying NOT NULL,
125
+ status character varying(50) DEFAULT 'queued'::character varying NOT NULL,
126
+ security_score integer,
127
+ started_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
128
+ completed_at timestamp without time zone,
129
+ auth_headers json,
130
+ scan_options json,
131
+ ssl_info json,
132
+
133
+ -- Added Diagnostics & Pre-aggregated Metrics
134
+ duration_seconds integer,
135
+ error_message text,
136
+ critical_count integer DEFAULT 0,
137
+ high_count integer DEFAULT 0,
138
+ medium_count integer DEFAULT 0,
139
+ low_count integer DEFAULT 0
140
+ );
141
+
142
+ ALTER TABLE public.scans OWNER TO postgres;
143
+
144
+ -- --------------------------------------------------------
145
+ -- Name: vulnerabilities; Type: TABLE; Schema: public; Owner: postgres
146
+ -- --------------------------------------------------------
147
+ CREATE TABLE public.vulnerabilities (
148
+ id character varying(36) NOT NULL,
149
+ scan_id character varying(36) NOT NULL,
150
+ title character varying(200) NOT NULL,
151
+ severity character varying(50) NOT NULL,
152
+ category character varying(100) NOT NULL,
153
+ description text NOT NULL,
154
+ remediation text NOT NULL,
155
+ cvss_score double precision NOT NULL,
156
+ detected_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
157
+ evidence text,
158
+ payload text,
159
+ request_details text,
160
+ response_details text,
161
+ is_false_positive boolean DEFAULT false,
162
+ cwe_ids json,
163
+ owasp_category character varying(100),
164
+ exploit_poc json,
165
+ remediation_code text,
166
+
167
+ -- Added Lifecycle Status Tracking
168
+ status character varying(50) DEFAULT 'open'::character varying NOT NULL,
169
+ remediated_at timestamp without time zone,
170
+ remediated_by character varying(36)
171
+ );
172
+
173
+ ALTER TABLE public.vulnerabilities OWNER TO postgres;
174
+
175
+ -- --------------------------------------------------------
176
+ -- Name: scheduled_scans; Type: TABLE; Schema: public; Owner: postgres
177
+ -- --------------------------------------------------------
178
+ CREATE TABLE public.scheduled_scans (
179
+ id character varying(36) NOT NULL,
180
+ org_id character varying(36) NOT NULL,
181
+ user_id character varying(36) NOT NULL,
182
+ target_url character varying(500) NOT NULL,
183
+ scan_type character varying(50) DEFAULT 'Full'::character varying NOT NULL,
184
+ frequency character varying(50) DEFAULT 'daily'::character varying NOT NULL,
185
+ schedule_time character varying(5),
186
+ day_of_week character varying(20),
187
+ day_of_month integer,
188
+ specific_date character varying(20),
189
+ is_active boolean DEFAULT true,
190
+ auth_headers json,
191
+ created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
192
+ last_run_at timestamp without time zone
193
+ );
194
+
195
+ ALTER TABLE public.scheduled_scans OWNER TO postgres;
196
+
197
+ -- --------------------------------------------------------
198
+ -- Name: payments; Type: TABLE; Schema: public; Owner: postgres
199
+ -- --------------------------------------------------------
200
+ CREATE TABLE public.payments (
201
+ id character varying(36) NOT NULL,
202
+ org_id character varying(36),
203
+ user_id character varying(36) NOT NULL,
204
+ razorpay_payment_id character varying(100),
205
+ razorpay_order_id character varying(100),
206
+ stripe_session_id character varying(100),
207
+ stripe_payment_id character varying(100),
208
+ tier_id character varying(50) NOT NULL,
209
+ amount integer NOT NULL,
210
+ currency character varying(10) DEFAULT 'USD'::character varying,
211
+ status character varying(50) DEFAULT 'successful'::character varying,
212
+ created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
213
+ );
214
+
215
+ ALTER TABLE public.payments OWNER TO postgres;
216
+
217
+ -- --------------------------------------------------------
218
+ -- Name: audit_logs; Type: TABLE; Schema: public; Owner: postgres
219
+ -- --------------------------------------------------------
220
+ CREATE TABLE public.audit_logs (
221
+ id character varying(36) NOT NULL,
222
+ admin_id character varying(36) NOT NULL,
223
+ action character varying(255) NOT NULL,
224
+ target_id character varying(100),
225
+ created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
226
+ );
227
+
228
+ ALTER TABLE public.audit_logs OWNER TO postgres;
229
+
230
+ -- --------------------------------------------------------
231
+ -- Name: demo_bookings; Type: TABLE; Schema: public; Owner: postgres
232
+ -- --------------------------------------------------------
233
+ CREATE TABLE public.demo_bookings (
234
+ id character varying(36) NOT NULL,
235
+ email character varying(255) NOT NULL,
236
+ company_size character varying(100) NOT NULL,
237
+ meeting_date character varying(100) NOT NULL,
238
+ meeting_time character varying(50) NOT NULL,
239
+ status character varying(50) DEFAULT 'pending'::character varying NOT NULL,
240
+ created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
241
+ );
242
+
243
+ ALTER TABLE public.demo_bookings OWNER TO postgres;
244
+
245
+ -- --------------------------------------------------------
246
+ -- Name: email_logs; Type: TABLE; Schema: public; Owner: postgres
247
+ -- --------------------------------------------------------
248
+ CREATE TABLE public.email_logs (
249
+ id character varying(36) NOT NULL,
250
+ recipient character varying(255) NOT NULL,
251
+ subject character varying(255) NOT NULL,
252
+ status character varying(50) DEFAULT 'sent'::character varying NOT NULL,
253
+ error_message text,
254
+ sent_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
255
+ );
256
+
257
+ ALTER TABLE public.email_logs OWNER TO postgres;
258
+
259
+ -- --------------------------------------------------------
260
+ -- Name: reports; Type: TABLE; Schema: public; Owner: postgres
261
+ -- --------------------------------------------------------
262
+ CREATE TABLE public.reports (
263
+ id character varying(36) NOT NULL,
264
+ scan_id character varying(36) NOT NULL,
265
+ org_id character varying(36) NOT NULL,
266
+ report_type character varying(50) NOT NULL,
267
+ file_path character varying(500),
268
+ generated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
269
+ );
270
+
271
+ ALTER TABLE public.reports OWNER TO postgres;
272
+
273
+ -- --------------------------------------------------------
274
+ -- Primary Keys & Unique Constraints
275
+ -- --------------------------------------------------------
276
+ ALTER TABLE ONLY public.subscription_tiers ADD CONSTRAINT subscription_tiers_pkey PRIMARY KEY (id);
277
+
278
+ ALTER TABLE ONLY public.organizations ADD CONSTRAINT organizations_pkey PRIMARY KEY (id);
279
+ ALTER TABLE ONLY public.organizations ADD CONSTRAINT organizations_api_key_key UNIQUE (api_key);
280
+
281
+ ALTER TABLE ONLY public.roles ADD CONSTRAINT roles_pkey PRIMARY KEY (id);
282
+ ALTER TABLE ONLY public.roles ADD CONSTRAINT roles_name_key UNIQUE (name);
283
+
284
+ ALTER TABLE ONLY public.users ADD CONSTRAINT users_pkey PRIMARY KEY (id);
285
+ ALTER TABLE ONLY public.users ADD CONSTRAINT users_email_key UNIQUE (email);
286
+
287
+ ALTER TABLE ONLY public.alert_settings ADD CONSTRAINT alert_settings_pkey PRIMARY KEY (id);
288
+
289
+ ALTER TABLE ONLY public.organization_scan_quotas ADD CONSTRAINT organization_scan_quotas_pkey PRIMARY KEY (id);
290
+ ALTER TABLE ONLY public.organization_scan_quotas ADD CONSTRAINT uix_org_scan_type UNIQUE (org_id, scan_type);
291
+
292
+ ALTER TABLE ONLY public.scans ADD CONSTRAINT scans_pkey PRIMARY KEY (id);
293
+
294
+ ALTER TABLE ONLY public.vulnerabilities ADD CONSTRAINT vulnerabilities_pkey PRIMARY KEY (id);
295
+
296
+ ALTER TABLE ONLY public.scheduled_scans ADD CONSTRAINT scheduled_scans_pkey PRIMARY KEY (id);
297
+
298
+ ALTER TABLE ONLY public.payments ADD CONSTRAINT payments_pkey PRIMARY KEY (id);
299
+ ALTER TABLE ONLY public.payments ADD CONSTRAINT payments_razorpay_payment_id_key UNIQUE (razorpay_payment_id);
300
+ ALTER TABLE ONLY public.payments ADD CONSTRAINT payments_stripe_payment_id_key UNIQUE (stripe_payment_id);
301
+ ALTER TABLE ONLY public.payments ADD CONSTRAINT payments_stripe_session_id_key UNIQUE (stripe_session_id);
302
+
303
+ ALTER TABLE ONLY public.audit_logs ADD CONSTRAINT audit_logs_pkey PRIMARY KEY (id);
304
+
305
+ ALTER TABLE ONLY public.demo_bookings ADD CONSTRAINT demo_bookings_pkey PRIMARY KEY (id);
306
+
307
+ ALTER TABLE ONLY public.email_logs ADD CONSTRAINT email_logs_pkey PRIMARY KEY (id);
308
+
309
+ ALTER TABLE ONLY public.reports ADD CONSTRAINT reports_pkey PRIMARY KEY (id);
310
+
311
+ -- --------------------------------------------------------
312
+ -- Foreign Key Constraints with ON DELETE CASCADE / SET NULL
313
+ -- --------------------------------------------------------
314
+ ALTER TABLE ONLY public.organizations
315
+ ADD CONSTRAINT organizations_subscription_tier_fkey FOREIGN KEY (subscription_tier) REFERENCES public.subscription_tiers(id);
316
+
317
+ ALTER TABLE ONLY public.users
318
+ ADD CONSTRAINT users_org_id_fkey FOREIGN KEY (org_id) REFERENCES public.organizations(id) ON DELETE SET NULL;
319
+
320
+ ALTER TABLE ONLY public.alert_settings
321
+ ADD CONSTRAINT alert_settings_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
322
+
323
+ ALTER TABLE ONLY public.organization_scan_quotas
324
+ ADD CONSTRAINT organization_scan_quotas_org_id_fkey FOREIGN KEY (org_id) REFERENCES public.organizations(id) ON DELETE CASCADE;
325
+
326
+ ALTER TABLE ONLY public.payments
327
+ ADD CONSTRAINT payments_org_id_fkey FOREIGN KEY (org_id) REFERENCES public.organizations(id) ON DELETE CASCADE,
328
+ ADD CONSTRAINT payments_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
329
+
330
+ ALTER TABLE ONLY public.scans
331
+ ADD CONSTRAINT scans_org_id_fkey FOREIGN KEY (org_id) REFERENCES public.organizations(id) ON DELETE CASCADE,
332
+ ADD CONSTRAINT scans_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
333
+
334
+ ALTER TABLE ONLY public.scheduled_scans
335
+ ADD CONSTRAINT scheduled_scans_org_id_fkey FOREIGN KEY (org_id) REFERENCES public.organizations(id) ON DELETE CASCADE,
336
+ ADD CONSTRAINT scheduled_scans_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
337
+
338
+ ALTER TABLE ONLY public.vulnerabilities
339
+ ADD CONSTRAINT vulnerabilities_scan_id_fkey FOREIGN KEY (scan_id) REFERENCES public.scans(id) ON DELETE CASCADE;
340
+
341
+ ALTER TABLE ONLY public.reports
342
+ ADD CONSTRAINT reports_scan_id_fkey FOREIGN KEY (scan_id) REFERENCES public.scans(id) ON DELETE CASCADE,
343
+ ADD CONSTRAINT reports_org_id_fkey FOREIGN KEY (org_id) REFERENCES public.organizations(id) ON DELETE CASCADE;
344
+
345
+ -- --------------------------------------------------------
346
+ -- Indexes for High-Performance Queries
347
+ -- --------------------------------------------------------
348
+ CREATE INDEX idx_users_org_id ON public.users(org_id);
349
+ CREATE INDEX idx_scans_org_id ON public.scans(org_id);
350
+ CREATE INDEX idx_scans_user_id ON public.scans(user_id);
351
+ CREATE INDEX idx_scans_status ON public.scans(status);
352
+ CREATE INDEX idx_vulnerabilities_scan_id ON public.vulnerabilities(scan_id);
353
+ CREATE INDEX idx_vulnerabilities_severity ON public.vulnerabilities(severity);
354
+ CREATE INDEX idx_vulnerabilities_status ON public.vulnerabilities(status);
355
+ CREATE INDEX idx_payments_org_id ON public.payments(org_id);
356
+ CREATE INDEX idx_payments_user_id ON public.payments(user_id);
357
+ CREATE INDEX idx_scheduled_scans_is_active ON public.scheduled_scans(is_active);
358
+ CREATE INDEX idx_reports_scan_id ON public.reports(scan_id);
359
+ CREATE INDEX idx_reports_org_id ON public.reports(org_id);
360
+
361
+ -- --------------------------------------------------------
362
+ -- Seed Data: Subscription Tiers, System Roles, Default Org, Admin User
363
+ -- --------------------------------------------------------
364
+ INSERT INTO public.subscription_tiers (id, name, monthly_price, yearly_price) VALUES
365
+ ('free', 'Free Tier Plan', 0, 0),
366
+ ('pro', 'Pro Security Plan', 49, 490),
367
+ ('enterprise', 'Enterprise Shield Plan', 199, 1990)
368
+ ON CONFLICT (id) DO NOTHING;
369
+
370
+ INSERT INTO public.roles (id, name) VALUES
371
+ ('role-001', 'super_admin'),
372
+ ('role-002', 'org_admin'),
373
+ ('role-003', 'soc_analyst'),
374
+ ('role-004', 'support_engineer'),
375
+ ('role-005', 'executive'),
376
+ ('role-006', 'read_only')
377
+ ON CONFLICT (id) DO NOTHING;
378
+
379
+ INSERT INTO public.organizations (id, name, subscription_tier, status, api_key, created_at) VALUES
380
+ ('org-default-001', 'LarShield Security Enterprise', 'enterprise', 'active', 'larshield_live_api_key_883920193847', CURRENT_TIMESTAMP)
381
+ ON CONFLICT (id) DO NOTHING;
382
+
383
+ INSERT INTO public.users (
384
+ id, email, password_hash, first_name, last_name, role, org_id,
385
+ terms_accepted_at, privacy_policy_agreed_at, policy_version_agreed, email_verified
386
+ ) VALUES (
387
+ 'user-admin-001',
388
+ 'admin@larshield.com',
389
+ '$2b$12$eImiTXuWVxfM37uY4JANjO5E/0w5/qO1F1kR04.3jF96Wd58e3.6u',
390
+ 'System',
391
+ 'Admin',
392
+ 'org_admin',
393
+ 'org-default-001',
394
+ CURRENT_TIMESTAMP,
395
+ CURRENT_TIMESTAMP,
396
+ 'v1.0',
397
+ true
398
+ )
399
+ ON CONFLICT (id) DO NOTHING;
400
+
401
+ INSERT INTO public.alert_settings (id, user_id, email_notifications, severity_threshold) VALUES
402
+ ('alert-admin-001', 'user-admin-001', true, 'Medium')
403
+ ON CONFLICT (id) DO NOTHING;
404
+
405
+ INSERT INTO public.organization_scan_quotas (id, org_id, scan_type, allocated_count, used_count) VALUES
406
+ ('quota-001', 'org-default-001', 'Full', 1000, 0),
407
+ ('quota-002', 'org-default-001', 'Port', 5000, 0),
408
+ ('quota-003', 'org-default-001', 'SSL', 5000, 0),
409
+ ('quota-004', 'org-default-001', 'OWASP', 2000, 0)
410
+ ON CONFLICT (id) DO NOTHING;