Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
72 changes: 72 additions & 0 deletions flyway/postgre/migrations/sql/V1_3__Create_lesson2_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TYPE org_status_type AS ENUM ('active', 'suspend', 'deleted');

CREATE TYPE user_status_type AS ENUM ('active', 'deactivated', 'deleted');

CREATE TYPE gender_type AS ENUM ('male', 'female');

CREATE TABLE IF NOT EXISTS sso_organization (
org_id int PRIMARY KEY NOT NULL,
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
org_id int PRIMARY KEY NOT NULL,
org_id int PRIMARY KEY NOT NULL generated always as identity,

org_name text NOT NULL,
kvp JSON,
created_time timestamp DEFAULT now(),
updated_time timestamp DEFAULT now(),
status org_status_type DEFAULT 'active' NOT NULL,
address text,
contact_number text,
email text,
country text,
state text,
city text,
postal_code text,
website_url text,
);

CREATE TABLE IF NOT EXISTS sso_user (
user_id uuid DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
username text NOT NULL,
password text NOT NULL,
email text NOT NULL,
kvp json,
created_time timestamp DEFAULT now(),
updated_time timestamp DEFAULT now(),
modified_by uuid,
last_logged_in timestamp,
user_settings json DEFAULT '{}' :: json,
status user_status_type DEFAULT 'active' NOT NULL,
);

CREATE TABLE IF NOT EXISTS sso_user_profile (
profile_id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
user_id uuid NOT NULL REFERENCES sso_user(user_id),
birth_date date,
sex gender_type,
title text,
first_name text,
last_name text,
avatar text,
age int,
address text,
date_created timestamp DEFAULT now(),
date_modified timestamp DEFAULT now(),
);

CREATE TABLE IF NOT EXISTS sso_role (
role_id uuid DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
role_name text NOT NULL,
role_description text,
organization_id int NOT NULL,
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
organization_id int NOT NULL,
organization_id int NOT NULL references sso_organization(org_id),

);

CREATE TABLE IF NOT EXISTS sso_organization_user (
organization_id int REFERENCES sso_user(user_id),
user_id uuid REFERENCES sso_organization(organization_id),
Comment on lines +63 to +64
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
organization_id int REFERENCES sso_user(user_id),
user_id uuid REFERENCES sso_organization(organization_id),
organization_id int REFERENCES sso_organization(org_id),
user_id uuid REFERENCES sso_user(user_id),

CONSTRAINT sso_organization_user_pk PRIMARY KEY (organization_id, user_id),
);

CREATE TABLE IF NOT EXISTS sso_user_role (
user_id uuid REFERENCES sso_role(role_id),
role_id uuid REFERENCES sso_user(user_id),
Comment on lines +69 to +70
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
user_id uuid REFERENCES sso_role(role_id),
role_id uuid REFERENCES sso_user(user_id),
user_id uuid REFERENCES sso_user(user_id),
role_id uuid REFERENCES sso_role(role_id),

CONSTRAINT sso_user_role_pk PRIMARY KEY(user_id, role_id),
);
269 changes: 269 additions & 0 deletions flyway/postgre/migrations/sql/V1_4__Insert_lesson2_initial_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
INSERT INTO
sso_organization (
org_id,
org_name,
kvp,
address,
contact_number,
email,
country,
city,
postal_code,
website_url
)
VALUES
(
1,
'org 1',
'{}',
'643 Pham Van Dong',
'0123-456-789',
'[email protected]',
'Vietnam',
'Hanoi',
'10000',
'org1.com'
),
(
2,
'org 2',
'{}',
'643 Pham Van Dong',
'0123-456-789',
'[email protected]',
'Vietnam',
'Hanoi',
'10000',
'org2.com'
);

INSERT INTO
sso_role (role_name, role_description, org_id)
VALUES
(
'admin',
'admin',
1
),
('user', 'user', 1),
('super-admin', 'super admin', 1);

INSERT INTO
sso_user (
username,
password,
email,
kvp,
modified_by,
last_logged_in,
user_settings,
)
VALUES
(
'admin',
'admin_password',
'[email protected]',
'{}',
NULL,
NULL,
'{}',
),
(
'super_admin',
'user_password',
'[email protected]',
'{}',
NULL,
NULL,
'{}',
),
(
'user',
'user_password',
'[email protected]',
'{}',
NULL,
NULL,
'{}',
);

INSERT INTO
sso_user_profile (
user_id,
birth_date,
sex,
title,
first_name,
last_name,
avatar,
age,
address
)
VALUES
(
(
SELECT
user_id
FROM
sso_user
WHERE
username = 'admin'
),
'1995-01-01',
'male',
'Mr.',
'Admin',
'User',
'url',
33,
'87 PD'
),
(
(
SELECT
user_id
FROM
sso_user
WHERE
username = 'super_admin'
),
'1995-01-01',
'female',
'Ms.',
'User',
'User',
'url',
38,
'87 PD'
),
(
(
SELECT
user_id
FROM
sso_user
WHERE
username = 'user'
),
'1995-01-01',
'male',
'Mr.',
'User',
'User',
'url',
31,
'87 PD'
);

INSERT INTO
sso_organization_user (org_id, user_id)
VALUES
(
(
SELECT
org_id
FROM
sso_organization
LIMIT
1
), (
SELECT
user_id
FROM
sso_user
WHERE
username = 'admin'
)
),
(
(
SELECT
org_id
FROM
sso_organization
LIMIT
1
), (
SELECT
user_id
FROM
sso_user
WHERE
username = 'super_admin'
)
),
(
(
SELECT
org_id
FROM
sso_organization OFFSET 1
LIMIT
1
), (
SELECT
user_id
FROM
sso_user
WHERE
username = 'user'
)
);

INSERT INTO
sso_user_role (user_id, role_id)
VALUES
(
(
SELECT
user_id
FROM
sso_user
WHERE
username = 'admin'
),
(
SELECT
role_id
FROM
sso_role
WHERE
role_name = 'admin'
)
),
(
(
SELECT
user_id
FROM
sso_user
WHERE
username = 'super_admin'
),
(
SELECT
role_id
FROM
sso_role
WHERE
role_name = 'user'
)
),
(
(
SELECT
user_id
FROM
sso_user
WHERE
username = 'user'
),
(
SELECT
role_id
FROM
sso_role
WHERE
role_name = 'super-admin'
)
);