From 6a238f3b9a8db7d88c6120fd6e0df2fc32ef13ea Mon Sep 17 00:00:00 2001 From: Mac Deluca Date: Mon, 11 Dec 2023 15:31:55 -0800 Subject: [PATCH] updated seed for submissions and added trigger to submission_feature_security --- database/package-lock.json | 60 +++++++------- .../20231117000001_security_tables.ts | 83 ++++++++++--------- database/src/seeds/04_mock_test_data.ts | 17 ++-- database/src/seeds/05_security_test_data.ts | 2 +- database/src/seeds/06_submission_data.ts | 59 +++++++++++++ package-lock.json | 3 + 6 files changed, 147 insertions(+), 77 deletions(-) create mode 100644 database/src/seeds/06_submission_data.ts create mode 100644 package-lock.json diff --git a/database/package-lock.json b/database/package-lock.json index aa9d5dea2..2148757cf 100644 --- a/database/package-lock.json +++ b/database/package-lock.json @@ -503,7 +503,7 @@ "color-name": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", - "integrity": "sha512-72fSenhMw2HZMTVHeCA9KCmpEIbzWiQsjN+BHcBbS9vr1mtt+vJjPdksIBNUmKAW8TFUDPJK5SUU3QhE9NEXDw==", + "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=", "dev": true }, "colorette": { @@ -519,7 +519,7 @@ "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", "dev": true }, "core-util-is": { @@ -927,7 +927,7 @@ "fast-levenshtein": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz", - "integrity": "sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==", + "integrity": "sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=", "dev": true }, "fastq": { @@ -995,7 +995,7 @@ "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", "dev": true }, "function-bind": { @@ -1018,7 +1018,7 @@ "functional-red-black-tree": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz", - "integrity": "sha512-dsKNQNdj6xA3T+QlADDA7mOSlX0qiMINjn0cgr+eGHGsbSHzTabcIogz2+p/iqP1Xs6EP/sS2SbqH+brGTbq0g==", + "integrity": "sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=", "dev": true }, "functions-have-names": { @@ -1172,7 +1172,7 @@ "has-flag": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", - "integrity": "sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw==", + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", "dev": true }, "has-property-descriptors": { @@ -1238,13 +1238,13 @@ "imurmurhash": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz", - "integrity": "sha512-JmXMZ6wuvDmLiHEml9ykzqO6lwFbof0GG4IkcGaENdCRDDmMVnny7s5HsIgHCbaq0w2MyPhDqkhTUgS2LU2PHA==", + "integrity": "sha1-khi5srkoojixPcT7a21XbyMUU+o=", "dev": true }, "inflight": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", - "integrity": "sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", "dev": true, "requires": { "once": "^1.3.0", @@ -1287,7 +1287,7 @@ "is-arrayish": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.2.1.tgz", - "integrity": "sha512-zz06S8t0ozoDXMG+ube26zeCTNXcKIPJZJi8hBrF4idCLms4CG9QtK7qBl1boi5ODzFpjswb5JPmHCbMpjaYzg==", + "integrity": "sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0=", "dev": true }, "is-bigint": { @@ -1335,7 +1335,7 @@ "is-extglob": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", - "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", + "integrity": "sha1-qIwCU1eR8C7TfHahueqXc8gz+MI=", "dev": true }, "is-fullwidth-code-point": { @@ -1442,7 +1442,7 @@ "isexe": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", - "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", + "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=", "dev": true }, "js-tokens": { @@ -1476,7 +1476,7 @@ "json-stable-stringify-without-jsonify": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz", - "integrity": "sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==", + "integrity": "sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=", "dev": true }, "jsonparse": { @@ -1519,7 +1519,7 @@ "load-json-file": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-4.0.0.tgz", - "integrity": "sha512-Kx8hMakjX03tiGTLAIdJ+lL0htKnXjEZN6hk/tozf/WOuYGdZBJrZ+rCJRbVCugsjB3jMLn9746NsQIf5VjBMw==", + "integrity": "sha1-L19Fq5HjMhYjT9U62rZo607AmTs=", "dev": true, "requires": { "graceful-fs": "^4.1.2", @@ -1542,7 +1542,7 @@ "lodash.truncate": { "version": "4.4.2", "resolved": "https://registry.npmjs.org/lodash.truncate/-/lodash.truncate-4.4.2.tgz", - "integrity": "sha512-jttmRe7bRse52OsWIMDLaXxWqRAmtIUccAQ3garviCqJjafXOfNMO0yMfNpdD6zbGaTU0P5Nz7e7gAT6cKmJRw==", + "integrity": "sha1-WjUNoLERO4N+z//VgSy+WNbq4ZM=", "dev": true }, "lru-cache": { @@ -1563,7 +1563,7 @@ "memorystream": { "version": "0.3.1", "resolved": "https://registry.npmjs.org/memorystream/-/memorystream-0.3.1.tgz", - "integrity": "sha512-S3UwM3yj5mtUSEfP41UZmt/0SCoVYUcU1rkXv+BQ5Ig8ndL4sPoJNBUJERafdPb5jjHJGuMgytgKvKIf58XNBw==", + "integrity": "sha1-htcJCzDORV1j+64S3aUaR93K+bI=", "dev": true }, "merge2": { @@ -1599,7 +1599,7 @@ "natural-compare": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", - "integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==", + "integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=", "dev": true }, "nice-try": { @@ -1670,7 +1670,7 @@ "path-key": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/path-key/-/path-key-2.0.1.tgz", - "integrity": "sha512-fEHGKCSmUSDPv4uoj8AlD+joPlq3peND+HRYyxFz4KPw4z926S/b8rIuFs2FYJg3BwsxJf6A9/3eIdLaYC+9Dw==", + "integrity": "sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A=", "dev": true }, "semver": { @@ -1682,7 +1682,7 @@ "shebang-command": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-1.2.0.tgz", - "integrity": "sha512-EV3L1+UQWGor21OmnvojK36mhg+TyIKDh3iFBKBohr5xeXIhNBcx8oWdgkTEEQ+BEFFYdLRuqMfd5L84N1V5Vg==", + "integrity": "sha1-RKrGW2lbAzmJaMOfNj/uXer98eo=", "dev": true, "requires": { "shebang-regex": "^1.0.0" @@ -1691,7 +1691,7 @@ "shebang-regex": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-1.0.0.tgz", - "integrity": "sha512-wpoSFAxys6b2a2wHZ1XpDSgD7N9iVjg29Ph9uV/uaP9Ex/KXlkTZTeddxDPSYQpgvzKLGJke2UU0AzoGCjNIvQ==", + "integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=", "dev": true }, "which": { @@ -1732,7 +1732,7 @@ "once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", - "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", "dev": true, "requires": { "wrappy": "1" @@ -1769,7 +1769,7 @@ "parse-json": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/parse-json/-/parse-json-4.0.0.tgz", - "integrity": "sha512-aOIos8bujGN93/8Ox/jPLh7RwVnPEysynVFE+fQZyg6jKELEHwzgKdLRFHUgXJL6kylijVSBC4BvN9OmsB48Rw==", + "integrity": "sha1-vjX1Qlvh9/bHRxhPmKeIy5lHfuA=", "dev": true, "requires": { "error-ex": "^1.3.1", @@ -1779,7 +1779,7 @@ "path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", - "integrity": "sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", "dev": true }, "path-key": { @@ -1869,7 +1869,7 @@ "pify": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/pify/-/pify-3.0.0.tgz", - "integrity": "sha512-C3FsVNH1udSEX48gGX1xfvwTWfsYWj5U+8/uK15BGzIGrKoUpghX8hWZwa/OFnakBiiVNmBvemTJR5mcy7iPcg==", + "integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=", "dev": true }, "postgres-array": { @@ -1880,7 +1880,7 @@ "postgres-bytea": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz", - "integrity": "sha512-xy3pmLuQqRBZBXDULy7KbaitYqLcmxigw14Q5sj8QBVLqEwXfeybIKVWiqAXTlcvdvb0+xkOtDbfQMOf4lST1w==" + "integrity": "sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=" }, "postgres-date": { "version": "1.0.7", @@ -1949,7 +1949,7 @@ "read-pkg": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/read-pkg/-/read-pkg-3.0.0.tgz", - "integrity": "sha512-BLq/cCO9two+lBgiTYNqD6GdtK8s4NpaWrl6/rCO9w0TUS8oJl7cmToOZfRYllKTISY6nt1U7jQ53brmKqY6BA==", + "integrity": "sha1-nLxoaXj+5l0WwA4rGcI3/Pbjg4k=", "dev": true, "requires": { "load-json-file": "^4.0.0", @@ -2073,7 +2073,7 @@ "semver": { "version": "4.3.2", "resolved": "https://registry.npmjs.org/semver/-/semver-4.3.2.tgz", - "integrity": "sha512-VyFUffiBx8hABJ9HYSTXLRwyZtdDHMzMtFmID1aiNAD2BZppBmJm0Hqw3p2jkgxP9BNt1pQ9RnC49P0EcXf6cA==" + "integrity": "sha1-x6BxWKgL7dBSNVt3DYLWZA+AO+c=" }, "shebang-command": { "version": "2.0.0", @@ -2190,7 +2190,7 @@ "sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", - "integrity": "sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==", + "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", "dev": true }, "string-width": { @@ -2269,7 +2269,7 @@ "strip-bom": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", - "integrity": "sha512-vavAMRXOgBVNF6nyEEmL3DBK19iRpDcoIwW+swQ+CbGiu7lju6t+JklA1MHweoWtadgt4ISVUsXLyDq34ddcwA==", + "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=", "dev": true }, "strip-json-comments": { @@ -2333,7 +2333,7 @@ "text-table": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/text-table/-/text-table-0.2.0.tgz", - "integrity": "sha512-N+8UisAXDGk8PFXP4HAzVR9nbfmVJ3zYLAWiTIoqC5v5isinhr+r5uaO8+7r3BMfuNIufIsA7RdpVgacC2cSpw==", + "integrity": "sha1-f17oI66AUgfACvLfSoTsP8+lcLQ=", "dev": true }, "through": { @@ -2518,7 +2518,7 @@ "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", "dev": true }, "xtend": { diff --git a/database/src/migrations/20231117000001_security_tables.ts b/database/src/migrations/20231117000001_security_tables.ts index db048e1ec..2732ed4f2 100644 --- a/database/src/migrations/20231117000001_security_tables.ts +++ b/database/src/migrations/20231117000001_security_tables.ts @@ -92,7 +92,7 @@ export async function up(knex: Knex): Promise { revision_count integer DEFAULT 0 NOT NULL, CONSTRAINT security_string_pk PRIMARY KEY (security_string_id) ); - + COMMENT ON COLUMN security_string.security_string_id IS 'System generated surrogate primary key identifier.'; COMMENT ON COLUMN security_string.security_rule_id IS 'Foreign key to the security_string table.'; COMMENT ON COLUMN security_string.name IS 'The name of the security_string record.'; @@ -106,9 +106,9 @@ export async function up(knex: Knex): Promise { COMMENT ON COLUMN security_string.update_user IS 'The id of the user who updated the record as identified in the system user table.'; COMMENT ON COLUMN security_string.revision_count IS 'Revision count used for concurrency control.'; COMMENT ON TABLE security_string IS 'String security condition.'; - + ---------------------------------------------------------------------------------------- - + CREATE TABLE security_number( security_number_id integer GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1), security_rule_id integer NOT NULL, @@ -126,7 +126,7 @@ export async function up(knex: Knex): Promise { revision_count integer DEFAULT 0 NOT NULL, CONSTRAINT security_number_pk PRIMARY KEY (security_number_id) ); - + COMMENT ON COLUMN security_number.security_number_id IS 'System generated surrogate primary key identifier.'; COMMENT ON COLUMN security_number.security_rule_id IS 'Foreign key to the security_number table.'; COMMENT ON COLUMN security_number.name IS 'The name of the security_number record.'; @@ -140,9 +140,9 @@ export async function up(knex: Knex): Promise { COMMENT ON COLUMN security_number.update_user IS 'The id of the user who updated the record as identified in the system user table.'; COMMENT ON COLUMN security_number.revision_count IS 'Revision count used for concurrency control.'; COMMENT ON TABLE security_number IS 'Number security condition.'; - + ---------------------------------------------------------------------------------------- - + CREATE TABLE security_datetime( security_datetime_id integer GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1), security_rule_id integer NOT NULL, @@ -160,7 +160,7 @@ export async function up(knex: Knex): Promise { revision_count integer DEFAULT 0 NOT NULL, CONSTRAINT security_datetime_pk PRIMARY KEY (security_datetime_id) ); - + COMMENT ON COLUMN security_datetime.security_datetime_id IS 'System generated surrogate primary key identifier.'; COMMENT ON COLUMN security_datetime.security_rule_id IS 'Foreign key to the security_datetime table.'; COMMENT ON COLUMN security_datetime.name IS 'The name of the security_datetime record.'; @@ -174,9 +174,9 @@ export async function up(knex: Knex): Promise { COMMENT ON COLUMN security_datetime.update_user IS 'The id of the user who updated the record as identified in the system user table.'; COMMENT ON COLUMN security_datetime.revision_count IS 'Revision count used for concurrency control.'; COMMENT ON TABLE security_datetime IS 'Datetime security condition.'; - + ---------------------------------------------------------------------------------------- - + CREATE TABLE security_spatial( security_spatial_id integer GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1), security_rule_id integer NOT NULL, @@ -194,7 +194,7 @@ export async function up(knex: Knex): Promise { revision_count integer DEFAULT 0 NOT NULL, CONSTRAINT security_spatial_pk PRIMARY KEY (security_spatial_id) ); - + COMMENT ON COLUMN security_spatial.security_spatial_id IS 'System generated surrogate primary key identifier.'; COMMENT ON COLUMN security_spatial.security_rule_id IS 'Foreign key to the security_spatial table.'; COMMENT ON COLUMN security_spatial.name IS 'The name of the security_spatial record.'; @@ -208,111 +208,114 @@ export async function up(knex: Knex): Promise { COMMENT ON COLUMN security_spatial.update_user IS 'The id of the user who updated the record as identified in the system user table.'; COMMENT ON COLUMN security_spatial.revision_count IS 'Revision count used for concurrency control.'; COMMENT ON TABLE security_spatial IS 'Spatial security condition.'; - + ---------------------------------------------------------------------------------------- -- Create Indexes and Constraints for table: security_rule ---------------------------------------------------------------------------------------- - + -- Add unique end-date key constraint (don't allow 2 records with the same name and a NULL record_end_date) CREATE UNIQUE INDEX security_rule_nuk1 ON security_rule(name, (record_end_date is NULL)) where record_end_date is null; - + ---------------------------------------------------------------------------------------- -- Create Indexes and Constraints for table: submission_feature_security ---------------------------------------------------------------------------------------- - + -- Add unique end-date key constraint (don't allow 2 records with the same submission_feature_id, security_rule_id, and a NULL record_end_date) CREATE UNIQUE INDEX submission_feature_security_nuk1 ON submission_feature_security(submission_feature_id, security_rule_id, (record_end_date is NULL)) where record_end_date is null; - + -- Add foreign key constraint ALTER TABLE submission_feature_security ADD CONSTRAINT submission_feature_security_fk1 FOREIGN KEY (submission_feature_id) REFERENCES submission_feature(submission_feature_id); - + ALTER TABLE submission_feature_security ADD CONSTRAINT submission_feature_security_fk2 FOREIGN KEY (security_rule_id) REFERENCES security_rule(security_rule_id); - + -- add indexes for foreign keys CREATE INDEX submission_feature_security_idx1 ON submission_feature_security(submission_feature_id); - + CREATE INDEX submission_feature_security_idx2 ON submission_feature_security(security_rule_id); - + ---------------------------------------------------------------------------------------- -- Create Indexes and Constraints for table: security_string ---------------------------------------------------------------------------------------- - + -- Add unique end-date key constraint (don't allow 2 records with the same name and a NULL record_end_date) CREATE UNIQUE INDEX security_string_nuk1 ON security_string(name, (record_end_date is NULL)) where record_end_date is null; - + -- Add foreign key constraint ALTER TABLE security_string ADD CONSTRAINT security_string_fk1 FOREIGN KEY (security_rule_id) REFERENCES security_rule(security_rule_id); - + -- add indexes for foreign keys CREATE INDEX security_string_idx1 ON security_string(security_rule_id); - + ---------------------------------------------------------------------------------------- -- Create Indexes and Constraints for table: security_number ---------------------------------------------------------------------------------------- - + -- Add unique end-date key constraint (don't allow 2 records with the same name and a NULL record_end_date) CREATE UNIQUE INDEX security_number_nuk1 ON security_number(name, (record_end_date is NULL)) where record_end_date is null; - + -- Add foreign key constraint ALTER TABLE security_number ADD CONSTRAINT security_number_fk1 FOREIGN KEY (security_rule_id) REFERENCES security_rule(security_rule_id); - + -- add indexes for foreign keys CREATE INDEX security_number_idx1 ON security_number(security_rule_id); - + ---------------------------------------------------------------------------------------- -- Create Indexes and Constraints for table: security_datetime ---------------------------------------------------------------------------------------- - + -- Add unique end-date key constraint (don't allow 2 records with the same name and a NULL record_end_date) CREATE UNIQUE INDEX security_datetime_nuk1 ON security_datetime(name, (record_end_date is NULL)) where record_end_date is null; - + -- Add foreign key constraint ALTER TABLE security_datetime ADD CONSTRAINT security_datetime_fk1 FOREIGN KEY (security_rule_id) REFERENCES security_rule(security_rule_id); - + -- add indexes for foreign keys CREATE INDEX security_datetime_idx1 ON security_datetime(security_rule_id); - + ---------------------------------------------------------------------------------------- -- Create Indexes and Constraints for table: security_spatial ---------------------------------------------------------------------------------------- - + -- Add unique end-date key constraint (don't allow 2 records with the same name and a NULL record_end_date) CREATE UNIQUE INDEX security_spatial_nuk1 ON security_spatial(name, (record_end_date is NULL)) where record_end_date is null; - + -- Add foreign key constraint ALTER TABLE security_spatial ADD CONSTRAINT security_spatial_fk1 FOREIGN KEY (security_rule_id) REFERENCES security_rule(security_rule_id); - + -- add indexes for foreign keys CREATE INDEX security_spatial_idx1 ON security_spatial(security_rule_id); - + ---------------------------------------------------------------------------------------- -- Create audit and journal triggers ---------------------------------------------------------------------------------------- - + create trigger audit_security_rule before insert or update or delete on security_rule for each row execute procedure tr_audit_trigger(); create trigger journal_security_rule after insert or update or delete on security_rule for each row execute procedure tr_journal_trigger(); - + + create trigger audit_submission_feature_security before insert or update or delete on submission_feature_security for each row execute procedure tr_audit_trigger(); + create trigger journal_submission_feature_security after insert or update or delete on submission_feature_security for each row execute procedure tr_journal_trigger(); + create trigger audit_security_string before insert or update or delete on security_string for each row execute procedure tr_audit_trigger(); create trigger journal_security_string after insert or update or delete on security_string for each row execute procedure tr_journal_trigger(); - + create trigger audit_security_number before insert or update or delete on security_number for each row execute procedure tr_audit_trigger(); create trigger journal_security_number after insert or update or delete on security_number for each row execute procedure tr_journal_trigger(); - + create trigger audit_security_datetime before insert or update or delete on security_datetime for each row execute procedure tr_audit_trigger(); create trigger journal_security_datetime after insert or update or delete on security_datetime for each row execute procedure tr_journal_trigger(); - + create trigger audit_security_spatial before insert or update or delete on security_spatial for each row execute procedure tr_audit_trigger(); create trigger journal_security_spatial after insert or update or delete on security_spatial for each row execute procedure tr_journal_trigger(); `); diff --git a/database/src/seeds/04_mock_test_data.ts b/database/src/seeds/04_mock_test_data.ts index 8fa52980a..eb72d506f 100644 --- a/database/src/seeds/04_mock_test_data.ts +++ b/database/src/seeds/04_mock_test_data.ts @@ -85,14 +85,14 @@ const insertRecord = async (knex: Knex) => { } }; -const insertSubmissionRecord = async (knex: Knex): Promise => { - const response = await knex.raw(`${insertSubmission()}`); +export const insertSubmissionRecord = async (knex: Knex, includeSecurityReview = false): Promise => { + const response = await knex.raw(`${insertSubmission(includeSecurityReview)}`); const submission_id = response.rows[0].submission_id; return submission_id; }; -const insertDatasetRecord = async (knex: Knex, options: { submission_id: number }): Promise => { +export const insertDatasetRecord = async (knex: Knex, options: { submission_id: number }): Promise => { const response = await knex.raw( `${insertSubmissionFeature({ submission_id: options.submission_id, @@ -125,7 +125,7 @@ const insertDatasetRecord = async (knex: Knex, options: { submission_id: number return submission_feature_id; }; -const insertSampleSiteRecord = async ( +export const insertSampleSiteRecord = async ( knex: Knex, options: { submission_id: number; parent_submission_feature_id: number } ): Promise => { @@ -146,7 +146,7 @@ const insertSampleSiteRecord = async ( return submission_feature_id; }; -const insertObservationRecord = async ( +export const insertObservationRecord = async ( knex: Knex, options: { submission_id: number; parent_submission_feature_id: number } ): Promise => { @@ -214,12 +214,15 @@ const insertAnimalRecord = async ( return submission_feature_id; }; -const insertSubmission = () => ` +export const insertSubmission = (includeSecurityReview: boolean) => { + const securityReviewTimestamp = includeSecurityReview ? `$$${faker.date.past().toISOString()}$$` : null; + return ` INSERT INTO submission ( uuid, name, description, + security_review_timestamp, source_system ) values @@ -227,10 +230,12 @@ const insertSubmission = () => ` public.gen_random_uuid(), $$${faker.company.name()}$$, $$${faker.lorem.words({ min: 5, max: 100 })}$$, + ${securityReviewTimestamp}, 'SIMS' ) RETURNING submission_id; `; +}; const insertSubmissionFeature = (options: { submission_id: number; diff --git a/database/src/seeds/05_security_test_data.ts b/database/src/seeds/05_security_test_data.ts index 7cb059bb5..5304263df 100644 --- a/database/src/seeds/05_security_test_data.ts +++ b/database/src/seeds/05_security_test_data.ts @@ -24,7 +24,7 @@ export async function seed(knex: Knex): Promise { } } -const insertSecurityRuleRecord = async (knex: Knex): Promise => { +export const insertSecurityRuleRecord = async (knex: Knex): Promise => { const response = await knex.raw(insertSecurityRule()); return response.rows[0].security_rule_id; }; diff --git a/database/src/seeds/06_submission_data.ts b/database/src/seeds/06_submission_data.ts new file mode 100644 index 000000000..c3602dbcb --- /dev/null +++ b/database/src/seeds/06_submission_data.ts @@ -0,0 +1,59 @@ +import { faker } from '@faker-js/faker'; +import { Knex } from 'knex'; +import { insertDatasetRecord, insertSampleSiteRecord, insertSubmissionRecord } from './04_mock_test_data'; +import { insertSecurityRuleRecord } from './05_security_test_data'; + +/** + * Inserts mock submission data + * + * @export + * @param {Knex} knex + * @return {*} {Promise} + */ +export async function seed(knex: Knex): Promise { + await knex.raw(` + SET SCHEMA 'biohub'; + SET SEARCH_PATH = 'biohub','public'; + `); + + // 1. submission (2) without children + await insertSubmissionRecord(knex); + await insertSubmissionRecord(knex); + + // 2. submission (1) without children but with security_review_timestamp + await insertSubmissionRecord(knex, true); + + // 3. submission (1) with children and SECURE (all submission features secure) + await createSubmissionWithSecurity(knex, 'SECURE'); + + // 4. submission (1) with children and PARTIALLY SECURE (some submission features secure) + await createSubmissionWithSecurity(knex, 'PARTIALLY SECURE'); + + // 5. submission (1) with children and UNSECURE (zero submission features secure) + await createSubmissionWithSecurity(knex, 'UNSECURE'); +} + +const insertFeatureSecurity = async (knex: Knex, submission_feature_id: number) => { + const security_rule_id = await insertSecurityRuleRecord(knex); + await knex.raw(` + INSERT INTO submission_feature_security (submission_feature_id, security_rule_id, record_effective_date) + VALUES($$${submission_feature_id}$$, $$${security_rule_id}$$, $$${faker.date.past().toISOString()}$$);`); +}; + +const createSubmissionWithSecurity = async (knex: Knex, securityLevel: 'PARTIALLY SECURE' | 'SECURE' | 'UNSECURE') => { + const submission_id = await insertSubmissionRecord(knex, true); + const parent_submission_feature_id = await insertDatasetRecord(knex, { submission_id }); + const submission_feature_id = await insertSampleSiteRecord(knex, { + parent_submission_feature_id, + submission_id + }); + if (securityLevel === 'PARTIALLY SECURE') { + await insertFeatureSecurity(knex, submission_feature_id); + return; + } + if (securityLevel === 'SECURE') { + await insertFeatureSecurity(knex, parent_submission_feature_id); + await insertFeatureSecurity(knex, submission_feature_id); + return; + } +}; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 000000000..48e341a09 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,3 @@ +{ + "lockfileVersion": 1 +}