Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIMSBIOHUB-154: System user table updates #1060

Merged
merged 19 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/src/constants/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export enum SYSTEM_IDENTITY_SOURCE {
IDIR = 'IDIR',
BCEID_BASIC = 'BCEIDBASIC',
BCEID_BUSINESS = 'BCEIDBUSINESS',
SYSTEM = 'SYSTEM'
UNVERIFIED = 'UNVERIFIED'
}

export enum SCHEMAS {
Expand Down
2 changes: 1 addition & 1 deletion api/src/repositories/user-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export class UserRepository extends BaseRepository {
ON
su.user_identity_source_id = uis.user_identity_source_id
WHERE
su.record_end_date IS NULL AND uis.name not in (${SYSTEM_IDENTITY_SOURCE.DATABASE}, ${SYSTEM_IDENTITY_SOURCE.SYSTEM})
su.record_end_date IS NULL AND uis.name not in (${SYSTEM_IDENTITY_SOURCE.DATABASE})
GROUP BY
su.system_user_id,
su.user_guid,
Expand Down
11 changes: 0 additions & 11 deletions api/src/utils/keycloak-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,6 @@ describe('keycloakUtils', () => {

expect(response).to.equal(SYSTEM_IDENTITY_SOURCE.DATABASE);
});

it('returns non null response when valid keycloakToken provided with system source', () => {
const response = getUserIdentitySource({ preferred_username: 'username@system' });

expect(response).to.equal(SYSTEM_IDENTITY_SOURCE.SYSTEM);
});
});

describe('coerceUserIdentitySource', () => {
Expand Down Expand Up @@ -136,10 +130,5 @@ describe('keycloakUtils', () => {
const response = coerceUserIdentitySource('database');
expect(response).to.equal(SYSTEM_IDENTITY_SOURCE.DATABASE);
});

it('should coerce system user identity to SYSTEM', () => {
const response = coerceUserIdentitySource('system');
expect(response).to.equal(SYSTEM_IDENTITY_SOURCE.SYSTEM);
});
});
});
3 changes: 0 additions & 3 deletions api/src/utils/keycloak-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ export const coerceUserIdentitySource = (userIdentitySource: string | null): SYS
case SYSTEM_IDENTITY_SOURCE.IDIR:
return SYSTEM_IDENTITY_SOURCE.IDIR;

case SYSTEM_IDENTITY_SOURCE.SYSTEM:
return SYSTEM_IDENTITY_SOURCE.SYSTEM;

case SYSTEM_IDENTITY_SOURCE.DATABASE:
return SYSTEM_IDENTITY_SOURCE.DATABASE;

Expand Down
3 changes: 2 additions & 1 deletion app/src/hooks/useKeycloakWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export enum SYSTEM_IDENTITY_SOURCE {
BCEID_BUSINESS = 'BCEIDBUSINESS',
BCEID_BASIC = 'BCEIDBASIC',
IDIR = 'IDIR',
DATABASE = 'DATABASE'
DATABASE = 'DATABASE',
UNVERIFIED = 'UNVERIFIED'
}

export interface IUserInfo {
Expand Down
160 changes: 99 additions & 61 deletions database/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,40 +131,16 @@ Important things to consider when making any migrations (not all will apply ever

## Naming conventions

#### primary id column

- `<table_name>_id`

#### primary key

- `<table_name>_pk`

#### unique key constraint

- `<table_name>_uk1`

#### unique end-date key constraint

- `<table_name>_nuk1`
See [Adding a unique end-date key constraint](#adding-a-unique-end-date-key-constraint)

#### foreign key constraint

- `<table_name>_fk1`

#### index

- `<table_name>_idx1`

#### trigger

- `tr_<table_name>`
- `tr_<column_name>`
- `tr_<name>`

#### functions used by the api

- `api_<name_of_function>`
| Type | Naming Convention | More Info |
| --- | --- | --- |
| surrogate id column | `<table_name>_id` | |
| primary key | `<table_name>_pk` | |
| unique key constraint | `<table_name>_uk1` | |
| unique end-date key constraint | `<table_name>_nuk1` | [Adding a unique end-date key constraint](#adding-a-unique-end-date-key-constraint) |
| foreign key constraint | `<table_name>_fk1` | |
| index | `<table_name>_idx1` | |
| trigger | `tr_<table_name>` <br> `tr_<column_name>` <br> `tr_<name>` | |
| functions used by the api | `api_<name_of_function>` | |

## Schemas

Expand Down Expand Up @@ -280,17 +256,17 @@ CREATE TABLE <table_name_1>(
CONSTRAINT <table_name_1>_pk PRIMARY KEY (<table_name_1>_id)
);

COMMENT ON COLUMN <table_name_1>.<table_name_1>_id IS 'System generated surrogate primary key identifier.';
COMMENT ON COLUMN <table_name_1>.<column_name_1> IS '<column_name_1_comment>';
COMMENT ON COLUMN <table_name_1>.<column_name_2> IS '<column_name_2_comment>';
COMMENT ON COLUMN <table_name_1>.<record_effective_date> IS 'Record level effective date.';
COMMENT ON COLUMN <table_name_1>.<record_end_date> IS 'Record level end date.';
COMMENT ON COLUMN <table_name_1>.create_date IS 'The datetime the record was created.';
COMMENT ON COLUMN <table_name_1>.create_user IS 'The id of the user who created the record as identified in the system user table.';
COMMENT ON COLUMN <table_name_1>.update_date IS 'The datetime the record was updated.';
COMMENT ON COLUMN <table_name_1>.update_user IS 'The id of the user who updated the record as identified in the system user table.';
COMMENT ON COLUMN <table_name_1>.revision_count IS 'Revision count used for concurrency control.';
COMMENT ON TABLE <table_name_1> IS '<table_name_1_comment>';
COMMENT ON COLUMN <table_name_1>.<table_name_1>_id IS 'System generated surrogate primary key identifier.';
COMMENT ON COLUMN <table_name_1>.<column_name_1> IS '<column_name_1_comment>';
COMMENT ON COLUMN <table_name_1>.<column_name_2> IS '<column_name_2_comment>';
COMMENT ON COLUMN <table_name_1>.<record_effective_date> IS 'Record level effective date.';
COMMENT ON COLUMN <table_name_1>.<record_end_date> IS 'Record level end date.';
COMMENT ON COLUMN <table_name_1>.create_date IS 'The datetime the record was created.';
COMMENT ON COLUMN <table_name_1>.create_user IS 'The id of the user who created the record as identified in the system user table.';
COMMENT ON COLUMN <table_name_1>.update_date IS 'The datetime the record was updated.';
COMMENT ON COLUMN <table_name_1>.update_user IS 'The id of the user who updated the record as identified in the system user table.';
COMMENT ON COLUMN <table_name_1>.revision_count IS 'Revision count used for concurrency control.';
COMMENT ON TABLE <table_name_1> IS '<table_name_1_comment>';

-- Add unique end-date key constraint (assuming the table has a record_end_date column and handles deletions as soft deletes by setting the record_end_date column)
CREATE UNIQUE INDEX <table_name_1>_nuk1 ON <table_name_1>(<column_name_1>, (<record_end_date> is NULL)) where <record_end_date> is null;
Expand All @@ -316,17 +292,17 @@ CREATE TABLE <table_name_2>(
CONSTRAINT <table_name_2>_pk PRIMARY KEY (<table_name_2>_id)
);

COMMENT ON COLUMN <table_name_2>.<table_name_2>_id IS 'System generated surrogate primary key identifier.';
COMMENT ON COLUMN <table_name_2>.<column_name_1> IS '<column_name_1_comment>';
COMMENT ON COLUMN <table_name_2>.<column_name_2> IS '<column_name_2_comment>';
COMMENT ON COLUMN <table_name_2>.<record_effective_date> IS 'Record level effective date.';
COMMENT ON COLUMN <table_name_2>.<record_end_date> IS 'Record level end date.';
COMMENT ON COLUMN <table_name_2>.create_date IS 'The datetime the record was created.';
COMMENT ON COLUMN <table_name_2>.create_user IS 'The id of the user who created the record as identified in the system user table.';
COMMENT ON COLUMN <table_name_2>.update_date IS 'The datetime the record was updated.';
COMMENT ON COLUMN <table_name_2>.update_user IS 'The id of the user who updated the record as identified in the system user table.';
COMMENT ON COLUMN <table_name_2>.revision_count IS 'Revision count used for concurrency control.';
COMMENT ON TABLE <table_name_2> IS '<table_name_2_comment>';
COMMENT ON COLUMN <table_name_2>.<table_name_2>_id IS 'System generated surrogate primary key identifier.';
COMMENT ON COLUMN <table_name_2>.<column_name_1> IS '<column_name_1_comment>';
COMMENT ON COLUMN <table_name_2>.<column_name_2> IS '<column_name_2_comment>';
COMMENT ON COLUMN <table_name_2>.<record_effective_date> IS 'Record level effective date.';
COMMENT ON COLUMN <table_name_2>.<record_end_date> IS 'Record level end date.';
COMMENT ON COLUMN <table_name_2>.create_date IS 'The datetime the record was created.';
COMMENT ON COLUMN <table_name_2>.create_user IS 'The id of the user who created the record as identified in the system user table.';
COMMENT ON COLUMN <table_name_2>.update_date IS 'The datetime the record was updated.';
COMMENT ON COLUMN <table_name_2>.update_user IS 'The id of the user who updated the record as identified in the system user table.';
COMMENT ON COLUMN <table_name_2>.revision_count IS 'Revision count used for concurrency control.';
COMMENT ON TABLE <table_name_2> IS '<table_name_2_comment>';

-- Add foreign key constraint from child table to parent table on <column_name_2>
ALTER TABLE <table_name_2> ADD CONSTRAINT <table_name_2>_fk1
Expand Down Expand Up @@ -355,6 +331,68 @@ create or replace view <table_name_1> as select * from biohub.<table_name_1>;
create or replace view <table_name_2> as select * from biohub.<table_name_2>;
```

### Create new join table

Creating a 'join' table (<table_name_1>) that connects 2 tables (<foreign_table_1>, <foreign_table_2>).

_Note: The example below does not include the creation of the foreign tables (assumes they have already been accounted for)._

_Note: Indexes must be created separately, as foreign keys do not automatically create an index._

```
----------------------------------------------------------------------------------------
-- Create join table
----------------------------------------------------------------------------------------
set search_path=biohub;

CREATE TABLE <table_name_1>(
<table_name_1>_id integer GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
<foreign_column_1> integer NOT NULL,
<foreign_column_2> integer NOT NULL,
create_date timestamptz(6) DEFAULT now() NOT NULL,
create_user integer NOT NULL,
update_date timestamptz(6),
update_user integer,
revision_count integer DEFAULT 0 NOT NULL,
CONSTRAINT <table_name_1>_pk PRIMARY KEY (<table_name_1>_id)
);

COMMENT ON COLUMN <table_name_1>.<table_name_1>_id IS 'System generated surrogate primary key identifier.';
COMMENT ON COLUMN <table_name_1>.<foreign_column_1> IS 'System generated surrogate primary key identifier.';
COMMENT ON COLUMN <table_name_1>.<foreign_column_2> IS 'System generated surrogate primary key identifier.';
COMMENT ON COLUMN <table_name_1>.create_date IS 'The datetime the record was created.';
COMMENT ON COLUMN <table_name_1>.create_user IS 'The id of the user who created the record as identified in the system user table.';
COMMENT ON COLUMN <table_name_1>.update_date IS 'The datetime the record was updated.';
COMMENT ON COLUMN <table_name_1>.update_user IS 'The id of the user who updated the record as identified in the system user table.';
COMMENT ON COLUMN <table_name_1>.revision_count IS 'Revision count used for concurrency control.';
COMMENT ON TABLE <table_name_1> IS 'A associative entity that joins system user and system role.';

-- Add unique key constraint
CREATE UNIQUE INDEX <table_name_1>_uk1 ON <table_name_1>(<foreign_column_1>, <foreign_column_2>);

-- Add foreign key constraint
ALTER TABLE <table_name_1> ADD CONSTRAINT <table_name_1>_fk1
FOREIGN KEY (<foreign_column_1>)
REFERENCES <foreign_table_1>(<foreign_column_1>);

ALTER TABLE <table_name_1> ADD CONSTRAINT <table_name_1>_fk2
FOREIGN KEY (<foreign_column_2>)
REFERENCES <foreign_table_2>(<foreign_column_2>);

-- Add indexes on key columns
CREATE INDEX <table_name_1>_idx1 ON <table_name_1>(<foreign_column_1>);

CREATE INDEX <table_name_1>_idx2 ON <table_name_1>(<foreign_column_2>);

----------------------------------------------------------------------------------------
-- Create views
----------------------------------------------------------------------------------------
set search_path=biohub_dapi_v1;

create or replace view <table_name_1> as select * from biohub.<table_name_1>;

```

## Miscellaneous Migration Snippets

These are loose snippets, and not complete examples for migrations.
Expand Down Expand Up @@ -390,11 +428,11 @@ CREATE INDEX <table_name_2>_idx1 ON <table_name_2>(<column_name_2>);
Should be added to every table.

```
create_date timestamptz(6) DEFAULT now() NOT NULL,
create_user integer NOT NULL,
update_date timestamptz(6),
update_user integer,
revision_count integer DEFAULT 0 NOT NULL,
create_date timestamptz(6) DEFAULT now() NOT NULL,
create_user integer NOT NULL,
update_date timestamptz(6),
update_user integer,
revision_count integer DEFAULT 0 NOT NULL,
```

### Create Audit/Journal Triggers
Expand Down
Loading
Loading