From 665e78ec1c8de13f3d6eac4a6862e532f698149b Mon Sep 17 00:00:00 2001 From: Papina Date: Mon, 2 Dec 2019 23:28:18 +1000 Subject: [PATCH] PostgreSQL - Functions (#578) * PostgreSQL initial commit of translation from SQL Server to PostgreSQL * snake_case added. set search path for schema. schema qualified name no longer needed for creation and access of functions. * Table DDL for PostgreSQL * Rename User.sql to user.sql * PostgreSQL views, snake_case column fix for user_create, rename of users.sql file to lowercase * user_update function * remove tabs * cast types, and table DDL changed to match * case * resolve casting issues. casting is done inside function * update views * more functions * more functions * removed casting * spelling mistake * spelling * changes to functions, added replace --- src/Sql/PostgreSQL/Functions/user_create.sql | 60 ++++++++------- .../Functions/user_delete_by_id.sql | 71 ++++++++++++++++++ .../Functions/user_read_by_email.sql | 20 +++++ src/Sql/PostgreSQL/Functions/user_search.sql | 4 +- src/Sql/PostgreSQL/Functions/user_update.sql | 75 +++++++++++++++++++ .../PostgreSQL/Functions/user_update_keys.sql | 27 +++++++ .../user_update_renewal_reminder_date.sql | 20 +++++ src/Sql/PostgreSQL/Schema/bitwarden.sql | 4 +- src/Sql/PostgreSQL/Tables/user.sql | 4 +- src/Sql/PostgreSQL/Views/cipher_view.sql | 2 +- src/Sql/PostgreSQL/Views/collection_view.sql | 2 +- src/Sql/PostgreSQL/Views/device_view.sql | 2 +- src/Sql/PostgreSQL/Views/event_view.sql | 2 +- src/Sql/PostgreSQL/Views/folder_view.sql | 2 +- src/Sql/PostgreSQL/Views/grant_view.sql | 2 +- src/Sql/PostgreSQL/Views/group_view.sql | 2 +- .../PostgreSQL/Views/installation_view.sql | 2 +- ...ization_user_organization_details_view.sql | 2 +- .../organization_user_user_details_view.sql | 2 +- .../Views/organization_user_view.sql | 2 +- .../PostgreSQL/Views/organization_view.sql | 2 +- src/Sql/PostgreSQL/Views/transaction_view.sql | 2 +- 22 files changed, 262 insertions(+), 49 deletions(-) create mode 100644 src/Sql/PostgreSQL/Functions/user_delete_by_id.sql create mode 100644 src/Sql/PostgreSQL/Functions/user_read_by_email.sql create mode 100644 src/Sql/PostgreSQL/Functions/user_update.sql create mode 100644 src/Sql/PostgreSQL/Functions/user_update_keys.sql create mode 100644 src/Sql/PostgreSQL/Functions/user_update_renewal_reminder_date.sql diff --git a/src/Sql/PostgreSQL/Functions/user_create.sql b/src/Sql/PostgreSQL/Functions/user_create.sql index d6cd7f5d93..3314427b4d 100644 --- a/src/Sql/PostgreSQL/Functions/user_create.sql +++ b/src/Sql/PostgreSQL/Functions/user_create.sql @@ -1,34 +1,36 @@ +DROP FUNCTION IF EXISTS user_create(UUID,VARCHAR,VARCHAR,BOOLEAN,VARCHAR,VARCHAR,VARCHAR,VARCHAR,TEXT,VARCHAR,TEXT,TEXT,TIMESTAMPTZ,TEXT,TEXT,TEXT,BOOLEAN,TIMESTAMPTZ,TIMESTAMPTZ,BIGINT,SMALLINT,SMALLINT,VARCHAR,VARCHAR,VARCHAR,SMALLINT,INT,TIMESTAMPTZ,TIMESTAMPTZ); + CREATE OR REPLACE FUNCTION user_create ( - _id uuid, - _name varchar(50), - _email varchar(50), - _email_verified bit, - _master_password varchar(300), - _master_password_hint varchar(50), - _culture varchar(10), - _security_stamp varchar(50), - _two_factor_providers text, - _two_factor_recovery_code varchar(32), - _equivalent_domains text, - _excluded_global_equivalent_domains text, - _account_revision_date timestamptz, - _key text, - _public_key text, - _private_key text, - _premium bit, - _premium_expiration_date timestamptz, - _renewal_reminder_date timestamptz, - _storage bigint, - _max_storage_gb smallint, - _gateway smallint, - _gateway_customer_id varchar(50), - _gateway_subscription_id varchar(50), - _license_key varchar(100), - _kdf smallint, - _kdf_iterations int, - _creation_date timestamptz, - _revision_date timestamptz + _id UUID, + _name VARCHAR, + _email VARCHAR, + _email_verified BOOLEAN, + _master_password VARCHAR, + _master_password_hint VARCHAR, + _culture VARCHAR, + _security_stamp VARCHAR, + _two_factor_providers TEXT, + _two_factor_recovery_code VARCHAR, + _equivalent_domains TEXT, + _excluded_global_equivalent_domains TEXT, + _account_revision_date TIMESTAMPTZ, + _key TEXT, + _public_key TEXT, + _private_key TEXT, + _premium BOOLEAN, + _premium_expiration_date TIMESTAMPTZ, + _renewal_reminder_date TIMESTAMPTZ, + _storage BIGINT, + _max_storage_gb SMALLINT, + _gateway SMALLINT, + _gateway_customer_id VARCHAR, + _gateway_subscription_id VARCHAR, + _license_key VARCHAR, + _kdf SMALLINT, + _kdf_iterations INT, + _creation_date TIMESTAMPTZ, + _revision_date TIMESTAMPTZ ) RETURNS VOID LANGUAGE 'plpgsql' diff --git a/src/Sql/PostgreSQL/Functions/user_delete_by_id.sql b/src/Sql/PostgreSQL/Functions/user_delete_by_id.sql new file mode 100644 index 0000000000..0321eabfbf --- /dev/null +++ b/src/Sql/PostgreSQL/Functions/user_delete_by_id.sql @@ -0,0 +1,71 @@ +DROP FUNCTION IF EXISTS user_delete_by_id (UUID); + +CREATE OR REPLACE FUNCTION user_delete_by_id +( + _id UUID +) +RETURNS VOID +LANGUAGE 'plpgsql' +AS +$BODY$ +BEGIN + -- functions dont support commit/rollback transactions, only v11+ procedures can do this + DELETE FROM + cipher + WHERE + user_id = _id; + + -- Delete folders + DELETE FROM + folder + WHERE + user_id = _id; + + -- Delete devices + DELETE FROM + device + WHERE + user_id = _id; + + -- Delete collection users + DELETE FROM + collection_user CU + USING + organization_user OU + WHERE + OU.id = CU.organization_user_id + AND + OU.user_id = _id; + + -- Delete group users + DELETE FROM + group_user GU + USING + organization_user OU + WHERE + OU.id = GU.organization_user_id + AND + OU.user_id = _id; + + -- Delete organization users + DELETE + FROM + organization_user + WHERE + user_id = _id; + + -- Delete U2F logins + DELETE + FROM + u2f + WHERE + user_id = _id; + + -- Finally, delete the user + DELETE + FROM + "user" + WHERE + id = _id; +END +$BODY$ diff --git a/src/Sql/PostgreSQL/Functions/user_read_by_email.sql b/src/Sql/PostgreSQL/Functions/user_read_by_email.sql new file mode 100644 index 0000000000..fcadb529e5 --- /dev/null +++ b/src/Sql/PostgreSQL/Functions/user_read_by_email.sql @@ -0,0 +1,20 @@ +DROP FUNCTION IF EXISTS user_read_by_email (VARCHAR); + +CREATE OR REPLACE FUNCTION user_read_by_email +( + _email VARCHAR +) +RETURNS SETOF user_view +LANGUAGE 'plpgsql' +AS +$BODY$ +BEGIN + RETURN QUERY + SELECT + * + FROM + user_view + WHERE + email = _email; +END +$BODY$ diff --git a/src/Sql/PostgreSQL/Functions/user_search.sql b/src/Sql/PostgreSQL/Functions/user_search.sql index 8765a9dd0e..0c0d2738a5 100644 --- a/src/Sql/PostgreSQL/Functions/user_search.sql +++ b/src/Sql/PostgreSQL/Functions/user_search.sql @@ -1,8 +1,8 @@ -DROP FUNCTION IF EXISTS user_search; +DROP FUNCTION IF EXISTS user_search (VARCHAR,INT,INT); CREATE OR REPLACE FUNCTION user_search ( - _email VARCHAR(50), + _email VARCHAR, _skip INT DEFAULT 0, _take INT DEFAULT 25 ) diff --git a/src/Sql/PostgreSQL/Functions/user_update.sql b/src/Sql/PostgreSQL/Functions/user_update.sql new file mode 100644 index 0000000000..7cf44b90ed --- /dev/null +++ b/src/Sql/PostgreSQL/Functions/user_update.sql @@ -0,0 +1,75 @@ +DROP FUNCTION IF EXISTS user_update(UUID,VARCHAR,VARCHAR,BOOLEAN,VARCHAR,VARCHAR,VARCHAR,VARCHAR,TEXT,VARCHAR,TEXT,TEXT,TIMESTAMPTZ,TEXT,TEXT,TEXT,BOOLEAN,TIMESTAMPTZ,TIMESTAMPTZ,BIGINT,SMALLINT,SMALLINT,VARCHAR,VARCHAR,VARCHAR,SMALLINT,INT,TIMESTAMPTZ,TIMESTAMPTZ) + +CREATE OR REPLACE FUNCTION user_update +( + _id UUID, + _name VARCHAR, + _email VARCHAR, + _email_verified BOOLEAN, + _master_password VARCHAR, + _master_password_hint VARCHAR, + _culture VARCHAR, + _security_stamp VARCHAR, + _two_factor_providers TEXT, + _two_factor_recovery_code VARCHAR, + _equivalent_domains TEXT, + _excluded_global_equivalent_domains TEXT, + _account_revision_date TIMESTAMPTZ, + _key TEXT, + _public_key TEXT, + _private_key TEXT , + _premium BOOLEAN, + _premium_expiration_date TIMESTAMPTZ, + _renewal_reminder_date TIMESTAMPTZ, + _storage BIGINT, + _max_storage_gb SMALLINT, + _gateway SMALLINT, + _gateway_customer_id VARCHAR, + _gateway_subscription_id VARCHAR, + _license_key VARCHAR, + _kdf SMALLINT, + _kdf_iterations INT, + _creation_date TIMESTAMPTZ, + _revision_date TIMESTAMPTZ +) +RETURNS VOID +LANGUAGE plpgsql +AS +$$ +begin + + UPDATE + "user" + SET name = _name, + email = _email, + email_verified = _email_verified, + master_password = _master_password, + master_password_hint = _master_password_hint, + culture = _culture, + security_stamp = _security_stamp, + two_factor_providers = _two_factor_providers, + two_factor_recovery_code = _two_factor_recovery_code, + equivalent_domains = _equivalent_domains, + excluded_global_equivalent_domains = _excluded_global_equivalent_domains, + account_revision_date = _account_revision_date, + key = _key, + public_key = _public_key, + private_key = _private_key, + premium = _premium, + premium_expiration_date = _premium_expiration_date, + renewal_reminder_date = _renewal_reminder_date, + storage = _storage, + max_storage_gb = _max_storage_gb, + gateway = _gateway, + gateway_customer_id = _gateway_customer_id, + gateway_subscription_id = _gateway_subscription_id, + license_key = _license_key, + kdf = _kdf, + kdf_iterations = _kdf_iterations, + creation_date = _creation_date, + revision_date = _revision_date + WHERE + id = _id + ; +end; +$$ diff --git a/src/Sql/PostgreSQL/Functions/user_update_keys.sql b/src/Sql/PostgreSQL/Functions/user_update_keys.sql new file mode 100644 index 0000000000..90c7a25168 --- /dev/null +++ b/src/Sql/PostgreSQL/Functions/user_update_keys.sql @@ -0,0 +1,27 @@ +DROP FUNCTION IF EXISTS user_update_keys (UUID,VARCHAR,TEXT,TEXT,TIMESTAMPTZ); + +CREATE OR REPLACE FUNCTION user_update_keys +( + _id UUID, + _security_stamp VARCHAR, + _key TEXT, + _private_key TEXT, + _revision_date TIMESTAMPTZ +) +RETURNS VOID +LANGUAGE 'plpgsql' +AS +$BODY$ +BEGIN + UPDATE + "user" + SET + security_stamp = _security_stamp, + key = _key, + private_key = _private_key, + revision_date = _revision_date, + account_revision_date = _revision_date + WHERE + id = _id; +END +$BODY$ diff --git a/src/Sql/PostgreSQL/Functions/user_update_renewal_reminder_date.sql b/src/Sql/PostgreSQL/Functions/user_update_renewal_reminder_date.sql new file mode 100644 index 0000000000..2dc43f5813 --- /dev/null +++ b/src/Sql/PostgreSQL/Functions/user_update_renewal_reminder_date.sql @@ -0,0 +1,20 @@ +DROP FUNCTION IF EXISTS user_update_renewal_reminder_date (UUID,TIMESTAMPTZ); + +CREATE OR REPLACE FUNCTION user_update_renewal_reminder_date +( + _id UUID, + _renewal_reminder_date TIMESTAMPTZ +) +RETURNS VOID +LANGUAGE 'plpgsql' +AS +$BODY$ +BEGIN + UPDATE + "user" + SET + renewal_reminder_date = _renewal_reminder_date + WHERE + id = _id; +end +$BODY$ diff --git a/src/Sql/PostgreSQL/Schema/bitwarden.sql b/src/Sql/PostgreSQL/Schema/bitwarden.sql index f70f531bfc..25b50cfe38 100644 --- a/src/Sql/PostgreSQL/Schema/bitwarden.sql +++ b/src/Sql/PostgreSQL/Schema/bitwarden.sql @@ -1,5 +1,3 @@ -DROP SCHEMA bitwarden cascade; - +DROP SCHEMA if exists bitwarden cascade; CREATE SCHEMA bitwarden AUTHORIZATION bitwarden; - ALTER ROLE bitwarden SET search_path TO bitwarden; diff --git a/src/Sql/PostgreSQL/Tables/user.sql b/src/Sql/PostgreSQL/Tables/user.sql index e5446354c7..716f791d3c 100644 --- a/src/Sql/PostgreSQL/Tables/user.sql +++ b/src/Sql/PostgreSQL/Tables/user.sql @@ -4,7 +4,7 @@ CREATE TABLE "user" ( id UUID NOT NULL, name VARCHAR (50) NULL, email VARCHAR (50) NOT NULL, - email_verified BIT NOT NULL, + email_verified BOOLEAN NOT NULL, master_password VARCHAR (300) NOT NULL, master_password_hint VARCHAR (50) NULL, culture VARCHAR (10) NOT NULL, @@ -17,7 +17,7 @@ CREATE TABLE "user" ( key TEXT NULL, public_key TEXT NULL, private_key TEXT NULL, - premium BIT NOT NULL, + premium BOOLEAN NOT NULL, premium_expiration_date TIMESTAMPTZ NULL, renewal_reminder_date TIMESTAMPTZ NULL, storage BIGINT NULL, diff --git a/src/Sql/PostgreSQL/Views/cipher_view.sql b/src/Sql/PostgreSQL/Views/cipher_view.sql index 3a240fc3a1..8ab20c7d88 100644 --- a/src/Sql/PostgreSQL/Views/cipher_view.sql +++ b/src/Sql/PostgreSQL/Views/cipher_view.sql @@ -1,4 +1,4 @@ -CREATE VIEW cipher_view +CREATE OR REPLACE VIEW cipher_view AS SELECT * diff --git a/src/Sql/PostgreSQL/Views/collection_view.sql b/src/Sql/PostgreSQL/Views/collection_view.sql index cbc19f36a9..89ea0ce5aa 100644 --- a/src/Sql/PostgreSQL/Views/collection_view.sql +++ b/src/Sql/PostgreSQL/Views/collection_view.sql @@ -1,4 +1,4 @@ -CREATE VIEW collection_view +CREATE OR REPLACE VIEW collection_view AS SELECT * diff --git a/src/Sql/PostgreSQL/Views/device_view.sql b/src/Sql/PostgreSQL/Views/device_view.sql index 1aa89d09b1..f2ed62e4c2 100644 --- a/src/Sql/PostgreSQL/Views/device_view.sql +++ b/src/Sql/PostgreSQL/Views/device_view.sql @@ -1,4 +1,4 @@ -CREATE VIEW device_view +CREATE OR REPLACE VIEW device_view AS SELECT * diff --git a/src/Sql/PostgreSQL/Views/event_view.sql b/src/Sql/PostgreSQL/Views/event_view.sql index df687bcd35..fe507f4042 100644 --- a/src/Sql/PostgreSQL/Views/event_view.sql +++ b/src/Sql/PostgreSQL/Views/event_view.sql @@ -1,4 +1,4 @@ -CREATE VIEW event_view +CREATE OR REPLACE VIEW event_view AS SELECT * diff --git a/src/Sql/PostgreSQL/Views/folder_view.sql b/src/Sql/PostgreSQL/Views/folder_view.sql index 506e1f8d39..45fa5c61d4 100644 --- a/src/Sql/PostgreSQL/Views/folder_view.sql +++ b/src/Sql/PostgreSQL/Views/folder_view.sql @@ -1,4 +1,4 @@ -CREATE VIEW folder_view +CREATE OR REPLACE VIEW folder_view AS SELECT * diff --git a/src/Sql/PostgreSQL/Views/grant_view.sql b/src/Sql/PostgreSQL/Views/grant_view.sql index 11e07ff0de..5f01479441 100644 --- a/src/Sql/PostgreSQL/Views/grant_view.sql +++ b/src/Sql/PostgreSQL/Views/grant_view.sql @@ -1,4 +1,4 @@ -CREATE VIEW grant_view +CREATE OR REPLACE VIEW grant_view AS SELECT * diff --git a/src/Sql/PostgreSQL/Views/group_view.sql b/src/Sql/PostgreSQL/Views/group_view.sql index b1cb0a4f67..000a6d00d3 100644 --- a/src/Sql/PostgreSQL/Views/group_view.sql +++ b/src/Sql/PostgreSQL/Views/group_view.sql @@ -1,4 +1,4 @@ -CREATE VIEW group_view +CREATE OR REPLACE VIEW group_view AS SELECT * diff --git a/src/Sql/PostgreSQL/Views/installation_view.sql b/src/Sql/PostgreSQL/Views/installation_view.sql index 14d60e10b6..c20b032cd3 100644 --- a/src/Sql/PostgreSQL/Views/installation_view.sql +++ b/src/Sql/PostgreSQL/Views/installation_view.sql @@ -1,4 +1,4 @@ -CREATE VIEW installation_view +CREATE OR REPLACE VIEW installation_view AS SELECT * diff --git a/src/Sql/PostgreSQL/Views/organization_user_organization_details_view.sql b/src/Sql/PostgreSQL/Views/organization_user_organization_details_view.sql index 44afbaa684..f5de1ce36b 100644 --- a/src/Sql/PostgreSQL/Views/organization_user_organization_details_view.sql +++ b/src/Sql/PostgreSQL/Views/organization_user_organization_details_view.sql @@ -1,4 +1,4 @@ -CREATE VIEW organization_user_organization_details_view +CREATE OR REPLACE VIEW organization_user_organization_details_view AS SELECT ou.user_id, diff --git a/src/Sql/PostgreSQL/Views/organization_user_user_details_view.sql b/src/Sql/PostgreSQL/Views/organization_user_user_details_view.sql index 70c05ccf0d..f436c65bd0 100644 --- a/src/Sql/PostgreSQL/Views/organization_user_user_details_view.sql +++ b/src/Sql/PostgreSQL/Views/organization_user_user_details_view.sql @@ -1,4 +1,4 @@ -CREATE VIEW organization_user_user_details_view +CREATE OR REPLACE VIEW organization_user_user_details_view AS SELECT ou.id, diff --git a/src/Sql/PostgreSQL/Views/organization_user_view.sql b/src/Sql/PostgreSQL/Views/organization_user_view.sql index ba9df2fd8f..0238c84981 100644 --- a/src/Sql/PostgreSQL/Views/organization_user_view.sql +++ b/src/Sql/PostgreSQL/Views/organization_user_view.sql @@ -1,4 +1,4 @@ -CREATE VIEW organization_user_view +CREATE OR REPLACE VIEW organization_user_view AS SELECT * diff --git a/src/Sql/PostgreSQL/Views/organization_view.sql b/src/Sql/PostgreSQL/Views/organization_view.sql index deeb0196d9..005e7f39a2 100644 --- a/src/Sql/PostgreSQL/Views/organization_view.sql +++ b/src/Sql/PostgreSQL/Views/organization_view.sql @@ -1,4 +1,4 @@ -CREATE VIEW organization_view +CREATE OR REPLACE VIEW organization_view AS SELECT * diff --git a/src/Sql/PostgreSQL/Views/transaction_view.sql b/src/Sql/PostgreSQL/Views/transaction_view.sql index c93cfc965a..9eb4b01d1d 100644 --- a/src/Sql/PostgreSQL/Views/transaction_view.sql +++ b/src/Sql/PostgreSQL/Views/transaction_view.sql @@ -1,4 +1,4 @@ -CREATE VIEW transaction_view +CREATE OR REPLACE VIEW transaction_view AS SELECT *