1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-05 05:00:19 -05:00

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
This commit is contained in:
Papina 2019-12-02 23:28:18 +10:00 committed by Kyle Spearrin
parent 9e470c1f7a
commit 665e78ec1c
22 changed files with 262 additions and 49 deletions

View File

@ -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'

View File

@ -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$

View File

@ -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$

View File

@ -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
)

View File

@ -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;
$$

View File

@ -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$

View File

@ -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$

View File

@ -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;

View File

@ -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,

View File

@ -1,4 +1,4 @@
CREATE VIEW cipher_view
CREATE OR REPLACE VIEW cipher_view
AS
SELECT
*

View File

@ -1,4 +1,4 @@
CREATE VIEW collection_view
CREATE OR REPLACE VIEW collection_view
AS
SELECT
*

View File

@ -1,4 +1,4 @@
CREATE VIEW device_view
CREATE OR REPLACE VIEW device_view
AS
SELECT
*

View File

@ -1,4 +1,4 @@
CREATE VIEW event_view
CREATE OR REPLACE VIEW event_view
AS
SELECT
*

View File

@ -1,4 +1,4 @@
CREATE VIEW folder_view
CREATE OR REPLACE VIEW folder_view
AS
SELECT
*

View File

@ -1,4 +1,4 @@
CREATE VIEW grant_view
CREATE OR REPLACE VIEW grant_view
AS
SELECT
*

View File

@ -1,4 +1,4 @@
CREATE VIEW group_view
CREATE OR REPLACE VIEW group_view
AS
SELECT
*

View File

@ -1,4 +1,4 @@
CREATE VIEW installation_view
CREATE OR REPLACE VIEW installation_view
AS
SELECT
*

View File

@ -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,

View File

@ -1,4 +1,4 @@
CREATE VIEW organization_user_user_details_view
CREATE OR REPLACE VIEW organization_user_user_details_view
AS
SELECT
ou.id,

View File

@ -1,4 +1,4 @@
CREATE VIEW organization_user_view
CREATE OR REPLACE VIEW organization_user_view
AS
SELECT
*

View File

@ -1,4 +1,4 @@
CREATE VIEW organization_view
CREATE OR REPLACE VIEW organization_view
AS
SELECT
*

View File

@ -1,4 +1,4 @@
CREATE VIEW transaction_view
CREATE OR REPLACE VIEW transaction_view
AS
SELECT
*