This commit is contained in:
heyethereum
2024-08-11 07:58:19 +08:00
commit 1625de948f
21 changed files with 1374546 additions and 0 deletions

291
sql/Create_all_tables.sql Normal file
View File

@@ -0,0 +1,291 @@
-- Create schema if it doesn't exist
CREATE SCHEMA IF NOT EXISTS safeqr;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Create Enum for scan_history status
CREATE TYPE scan_status AS ENUM ('ACTIVE', 'INACTIVE');
-- safeqr."user" definition
-- Drop table
-- DROP TABLE safeqr."user";
CREATE TABLE safeqr."user" (
id varchar(255) NOT NULL,
"name" varchar(255) NULL,
email varchar(255) NULL,
"source" varchar(255) NULL,
date_created timestamptz DEFAULT now() NULL,
date_updated timestamptz DEFAULT now() NULL,
roles _text NULL,
status varchar(255) DEFAULT 'ACTIVE'::character varying NULL,
gmail_history_id int8 DEFAULT '-1'::integer NULL,
CONSTRAINT user_pkey PRIMARY KEY (id)
);
-- safeqr.qr_code_types definition
-- Drop table
-- DROP TABLE safeqr.qr_code_types;
CREATE TABLE safeqr.qr_code_types (
id bigserial NOT NULL,
"type" varchar(255) NOT NULL,
description varchar(255) NULL,
prefix varchar(255) NULL,
table_name varchar(255) NULL,
CONSTRAINT qr_code_types_pkey PRIMARY KEY (id)
);
-- safeqr.qr_code definition
-- Drop table
-- DROP TABLE safeqr.qr_code;
CREATE TABLE safeqr.qr_code (
id uuid DEFAULT safeqr.uuid_generate_v4() NOT NULL,
qr_code_type_id int8 NULL,
user_id varchar(255) NULL,
contents text NULL,
created_at timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
result_category text NULL,
CONSTRAINT qr_code_pkey PRIMARY KEY (id),
CONSTRAINT qr_code_qr_code_type_id_fkey FOREIGN KEY (qr_code_type_id) REFERENCES safeqr.qr_code_types(id),
CONSTRAINT qr_code_user_id_fkey FOREIGN KEY (user_id) REFERENCES safeqr."user"(id)
);
-- safeqr.scan_history definition
-- Drop table
-- DROP TABLE safeqr.scan_history;
CREATE TABLE safeqr.scan_history (
id bigserial NOT NULL,
qr_code_id uuid NULL,
user_id varchar(255) NULL,
status varchar(255) DEFAULT 'ACTIVE'::safeqr.scan_status NULL,
date_created timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
date_updated timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
bookmarked bool DEFAULT false NULL,
CONSTRAINT scan_history_pkey PRIMARY KEY (id),
CONSTRAINT scan_history_fk FOREIGN KEY (qr_code_id) REFERENCES safeqr.qr_code(id) ON DELETE CASCADE ON UPDATE CASCADE
);
-- safeqr.scan_bookmark definition
-- Drop table
-- DROP TABLE safeqr.scan_bookmark;
CREATE TABLE safeqr.scan_bookmark (
id bigserial NOT NULL,
qr_code_id uuid NULL,
user_id varchar(255) NULL,
status varchar(255) DEFAULT 'ACTIVE'::safeqr.bookmark_status NULL,
date_created timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
date_updated timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
CONSTRAINT scan_bookmark_pkey PRIMARY KEY (id),
CONSTRAINT scan_bookmark_fk FOREIGN KEY (qr_code_id) REFERENCES safeqr.qr_code(id) ON DELETE CASCADE ON UPDATE CASCADE
);
-- safeqr.url definition
-- Drop table
-- DROP TABLE safeqr.url;
CREATE TABLE safeqr.url (
id uuid DEFAULT uuid_generate_v4() NOT NULL,
qr_code_id uuid NULL,
"domain" text NULL,
subdomain text NULL,
top_level_domain text NULL,
query text NULL,
fragment text NULL,
redirect int4 DEFAULT 0 NULL,
"path" text NULL,
redirect_chain _text NULL,
hsts_header _text NULL,
ssl_stripping _bool NULL,
ssl_error text NULL,
dns_error text NULL,
hostname_embedding int4 NULL,
javascript_check text NULL,
shortening_service text NULL,
has_ip_address text NULL,
tracking_descriptions _text NULL,
url_encoding text NULL,
classifications text NULL,
has_executable text NULL,
CONSTRAINT url_pkey PRIMARY KEY (id),
CONSTRAINT url_qr_code_id_fkey FOREIGN KEY (qr_code_id) REFERENCES safeqr.qr_code(id)
);
-- safeqr."text" definition
-- Drop table
-- DROP TABLE safeqr."text";
CREATE TABLE safeqr."text" (
id uuid DEFAULT safeqr.uuid_generate_v4() NOT NULL,
qr_code_id uuid NULL,
"text" varchar(2048) NULL,
CONSTRAINT text_pkey PRIMARY KEY (id),
CONSTRAINT text_fk FOREIGN KEY (qr_code_id) REFERENCES safeqr.qr_code(id) ON DELETE CASCADE ON UPDATE CASCADE
);
-- safeqr.phone definition
-- Drop table
-- DROP TABLE safeqr.phone;
CREATE TABLE safeqr.phone (
id uuid DEFAULT safeqr.uuid_generate_v4() NOT NULL,
qr_code_id uuid NULL,
phone text NULL,
CONSTRAINT phone_pkey PRIMARY KEY (id),
CONSTRAINT phone_fk FOREIGN KEY (qr_code_id) REFERENCES safeqr.qr_code(id) ON DELETE CASCADE ON UPDATE CASCADE
);
-- safeqr.sms definition
-- Drop table
-- DROP TABLE safeqr.sms;
CREATE TABLE safeqr.sms (
id uuid DEFAULT safeqr.uuid_generate_v4() NOT NULL,
qr_code_id uuid NULL,
phone text NULL,
message text NULL,
CONSTRAINT sms_pkey PRIMARY KEY (id),
CONSTRAINT sms_fk FOREIGN KEY (qr_code_id) REFERENCES safeqr.qr_code(id)
);
-- Create Email table
CREATE TABLE safeqr.email (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
qr_code_id UUID REFERENCES safeqr.qr_code(id),
email VARCHAR(200),
title VARCHAR(200),
message VARCHAR(7089)
);
-- Create GeoLocation table
CREATE TABLE safeqr.geolocation (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
qr_code_id UUID REFERENCES safeqr.qr_code(id),
latitude DOUBLE PRECISION,
longitude DOUBLE PRECISION
);
-- Create Wifi table
CREATE TABLE safeqr.wifi (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
qr_code_id UUID REFERENCES safeqr.qr_code(id),
ssid VARCHAR(200),
password VARCHAR(200),
encryption VARCHAR(200),
hidden BOOLEAN
);
-- Create Bitcoin table
CREATE TABLE safeqr.bitcoin (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
qr_code_id UUID REFERENCES safeqr.qr_code(id),
address VARCHAR(200),
amount BIGINT,
message VARCHAR(250)
);
-- Create VCard table
CREATE TABLE safeqr.vcard (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
qr_code_id UUID REFERENCES safeqr.qr_code(id),
version VARCHAR(50),
name VARCHAR(250),
organisation VARCHAR(250),
title VARCHAR(50),
address VARCHAR(250),
tel_work VARCHAR(250),
tel_cell VARCHAR(250),
email VARCHAR(250),
url VARCHAR(2048)
);
-- safeqr.gmail_emails definition
-- Drop table
-- DROP TABLE safeqr.gmail_emails;
CREATE TABLE safeqr.gmail_emails (
user_id varchar NOT NULL,
subject text NULL,
date_received timestamptz NULL,
date_created timestamptz DEFAULT CURRENT_TIMESTAMP NULL,
message_id varchar NOT NULL,
history_id int8 NULL,
id uuid DEFAULT safeqr.uuid_generate_v4() NOT NULL,
CONSTRAINT gmail_pk PRIMARY KEY (id),
CONSTRAINT gmail_unique UNIQUE (user_id, message_id),
CONSTRAINT gmail_user_fk FOREIGN KEY (user_id) REFERENCES safeqr."user"(id) ON DELETE CASCADE ON UPDATE CASCADE
);
-- safeqr.url definition
-- Drop table
-- DROP TABLE safeqr.url;
CREATE TABLE safeqr.url (
id uuid DEFAULT safeqr.uuid_generate_v4() NOT NULL,
qr_code_id uuid NULL,
"domain" text NULL,
subdomain text NULL,
top_level_domain text NULL,
query text NULL,
fragment text NULL,
redirect int4 DEFAULT 0 NULL,
"path" text NULL,
redirect_chain _text NULL,
hsts_header _text NULL,
ssl_stripping _bool NULL,
ssl_error text NULL,
dns_error text NULL,
hostname_embedding int4 NULL,
javascript_check text NULL,
shortening_service text NULL,
has_ip_address text NULL,
tracking_descriptions _text NULL,
CONSTRAINT url_pkey PRIMARY KEY (id),
CONSTRAINT url_qr_code_id_fkey FOREIGN KEY (qr_code_id) REFERENCES safeqr.qr_code(id)
);
-- safeqr.gmail_cid definition
-- Drop table
-- DROP TABLE safeqr.gmail_cid;
CREATE TABLE safeqr.gmail_cid (
gmail_id uuid NOT NULL,
cid varchar NOT NULL,
attachment_id text NOT NULL,
decoded_content text NOT NULL,
qr_code_id uuid NOT NULL,
id uuid DEFAULT safeqr.uuid_generate_v4() NOT NULL,
CONSTRAINT gmail_cid_pk PRIMARY KEY (id),
CONSTRAINT gmail_cid_unique UNIQUE (gmail_id, cid, decoded_content),
CONSTRAINT gmail_cid_gmail_emails_fk FOREIGN KEY (gmail_id) REFERENCES safeqr.gmail_emails(id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT gmail_cid_qr_code_fk FOREIGN KEY (qr_code_id) REFERENCES safeqr.qr_code(id) ON DELETE CASCADE ON UPDATE CASCADE
);

16
sql/Drop_all_tables.sql Normal file
View File

@@ -0,0 +1,16 @@
-- Drop the tables with CASCADE to handle dependencies
DROP TABLE IF EXISTS safeqr.vcard CASCADE;
DROP TABLE IF EXISTS safeqr.bitcoin CASCADE;
DROP TABLE IF EXISTS safeqr.wifi CASCADE;
DROP TABLE IF EXISTS safeqr.geolocation CASCADE;
DROP TABLE IF EXISTS safeqr.email CASCADE;
DROP TABLE IF EXISTS safeqr.sms CASCADE;
DROP TABLE IF EXISTS safeqr.phone CASCADE;
DROP TABLE IF EXISTS safeqr.text CASCADE;
DROP TABLE IF EXISTS safeqr.url CASCADE;
DROP TABLE IF EXISTS safeqr.scan_bookmark CASCADE;
DROP TABLE IF EXISTS safeqr.scan_history CASCADE;
DROP TABLE IF EXISTS safeqr.qr_code CASCADE;
DROP TABLE IF EXISTS safeqr.qr_code_types CASCADE;
DROP TABLE IF EXISTS safeqr."user" CASCADE;

97
sql/Dummy_data.sql Normal file
View File

@@ -0,0 +1,97 @@
INSERT INTO safeqr."user" (
id,
cognitoid,
firstname,
lastname,
email,
source,
password,
salt,
cognito_id,
first_name,
last_name
) VALUES (
'test-unique-id',
'test-cognito-id',
'Test',
'User',
'test.user@example.com',
'test-source',
'test-password-hash',
'test-salt',
NULL,
NULL,
NULL
);
-- Insert into QR_Code_Types table
-- Insert additional QR code types into the qr_code_types table
INSERT INTO safeqr.qr_code_types (type, description, prefix, table_name) VALUES
('URL', 'Uniform Resource Locator', 'http://', 'url'),
('URL', 'Uniform Resource Locator', 'https://', 'url'),
('EMAIL', 'Email Address', 'mailto:', 'email'),
('PHONE', 'Phone Number', 'tel:', 'phone'),
('SMS', 'Short Message Service', 'smsto:', 'sms'),
('GEOLOCATION', 'Geographic Location', 'geo:', 'geolocation'),
('WIFI', 'Wireless Network Configuration', 'WIFI:', 'wifi'),
('BITCOIN', 'Cryptocurrency Address', 'bitcoin:', 'bitcoin'),
('VCARD', 'Virtual Contact File', 'BEGIN:VCARD', 'vcard'),
('TEXT', 'Text', '', 'text');
-- Insert into QR_Code table
INSERT INTO safeqr.qr_code (id, qr_code_type_id, user_id, contents, created_at) VALUES
(gen_random_uuid(), 1, 'test-unique-id', 'http://example.com', CURRENT_TIMESTAMP),
(gen_random_uuid(), 2, NULL, 'https://example.com', CURRENT_TIMESTAMP),
(gen_random_uuid(), 3, 'test-unique-id', 'mailto:test@example.com', CURRENT_TIMESTAMP);
-- Insert into Scan_History table
INSERT INTO safeqr.scan_history (qr_code_id, user_id) VALUES
((SELECT id FROM safeqr.qr_code LIMIT 1), 'test-unique-id'),
((SELECT id FROM safeqr.qr_code LIMIT 1 OFFSET 1), 'test-unique-id'),
((SELECT id FROM safeqr.qr_code LIMIT 1 OFFSET 2), 'test-unique-id');
-- Insert into Scan_Bookmark table
INSERT INTO safeqr.scan_bookmark (qr_code_id, user_id, active) VALUES
((SELECT id FROM safeqr.qr_code LIMIT 1), 'test-unique-id', 'ACTIVE'),
((SELECT id FROM safeqr.qr_code LIMIT 1 OFFSET 1), 'test-unique-id', 'ACTIVE'),
((SELECT id FROM safeqr.qr_code LIMIT 1 OFFSET 2), 'test-unique-id', 'DELETED');
-- Insert into URL table
INSERT INTO safeqr.url (id, qr_code_id, url, query, fragment) VALUES
(gen_random_uuid(), (SELECT id FROM safeqr.qr_code WHERE contents LIKE 'http://%' LIMIT 1), 'http://example.com', 'key=value', 'fragment'),
(gen_random_uuid(), (SELECT id FROM safeqr.qr_code WHERE contents LIKE 'https://%' LIMIT 1), 'https://example.com', 'key=value', 'fragment');
-- Insert into Text table
INSERT INTO safeqr.text (id, qr_code_id, text) VALUES
(gen_random_uuid(), (SELECT id FROM safeqr.qr_code WHERE contents = 'Some text' LIMIT 1), 'Some example text');
-- Insert into Phone table
INSERT INTO safeqr.phone (id, qr_code_id, phone) VALUES
(gen_random_uuid(), (SELECT id FROM safeqr.qr_code LIMIT 1), '+1234567890');
-- Insert into SMS table
INSERT INTO safeqr.sms (id, qr_code_id, phone, message) VALUES
(gen_random_uuid(), (SELECT id FROM safeqr.qr_code LIMIT 1), '+1234567890', 'This is a test SMS message.');
-- Insert into Email table
INSERT INTO safeqr.email (id, qr_code_id, email, title, message) VALUES
(gen_random_uuid(), (SELECT id FROM safeqr.qr_code WHERE contents LIKE 'mailto:%' LIMIT 1), 'test@example.com', 'Test Email', 'This is a test email body.');
-- Insert into GeoLocation table
INSERT INTO safeqr.geolocation (id, qr_code_id, latitude, longitude) VALUES
(gen_random_uuid(), (SELECT id FROM safeqr.qr_code LIMIT 1), 37.7749, -122.4194);
-- Insert into Wifi table
INSERT INTO safeqr.wifi (id, qr_code_id, ssid, password, encryption, hidden) VALUES
(gen_random_uuid(), (SELECT id FROM safeqr.qr_code LIMIT 1), 'TestSSID', 'password123', 'WPA2', FALSE);
-- Insert into Bitcoin table
INSERT INTO safeqr.bitcoin (id, qr_code_id, address, amount, message) VALUES
(gen_random_uuid(), (SELECT id FROM safeqr.qr_code LIMIT 1), '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', 5000000000, 'Test Bitcoin transaction');
-- Insert into VCard table
INSERT INTO safeqr.vcard (id, qr_code_id, version, name, organisation, title, address, tel_work, tel_cell, email, url) VALUES
(gen_random_uuid(), (SELECT id FROM safeqr.qr_code LIMIT 1), '4.0', 'John Doe', 'Example Corp', 'Developer', '1234 Elm St, Springfield, IL', '+1234567890', '+0987654321', 'johndoe@example.com', 'http://example.com');

View File

@@ -0,0 +1,11 @@
INSERT INTO safeqr.qr_code_types ("type",description,prefix,table_name) VALUES
('URL','Uniform Resource Locator','http://','url'),
('EMAIL','Email Address','mailto:','email'),
('PHONE','Phone Number','tel:','phone'),
('SMS','Short Message Service','smsto:','sms'),
('GEOLOCATION','Geographic Location','geo:','geolocation'),
('WIFI','Wireless Network Configuration','WIFI:','wifi'),
('BITCOIN','Cryptocurrency Address','bitcoin:','bitcoin'),
('VCARD','Virtual Contact File','BEGIN:VCARD','vcard'),
('URL','Secure Uniform Resource Locator','https://','url'),
('TEXT','Text','','text');