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
85 changes: 85 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,85 @@
CREATE TYPE enum AS ENUM ('active', 'suspend', 'deleted');
CREATE TYPE enum_org AS ENUM ('active', 'deactivated', 'deleted');
CREATE TYPE enum_sex AS ENUM ('male', 'female');
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TABLE public.sso_organization (
organization_id int4 NOT NULL GENERATED ALWAYS AS IDENTITY( INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START 1 CACHE 1 NO CYCLE),
organization_name text NOT NULL,
date_created timestamp NULL,
date_modified timestamp NULL,
address text NULL,
contact_number text NULL,
email text NULL,
country text NULL,
state text NULL,
city text NULL,
postal_code text NULL,
organization_sub_name text NULL,
website_url text NULL,
kvp json NULL,
status public."enum_org" NOT NULL DEFAULT 'active'::enum_org,
CONSTRAINT sso_organization_pk PRIMARY KEY (organization_id)
);

CREATE TABLE public.sso_organization_user (
user_id int4 NULL,
organization_id int4 NULL
Comment on lines +26 to +27
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 int4 NULL,
organization_id int4 NULL
user_id int4 NOT NULL,
organization_id int4 NOT NULL,
constraint user_org primary key (organization_id,user_id)

);

-- public.sso_organization_user foreign keys
ALTER TABLE public.sso_organization_user ADD CONSTRAINT sso_org_user_fk FOREIGN KEY (user_id) REFERENCES public.sso_user(user_id);
ALTER TABLE public.sso_organization_user ADD CONSTRAINT sso_org_user_fk_1 FOREIGN KEY (organization_id) REFERENCES public.sso_organization(organization_id);

CREATE TABLE public.sso_role (
role_id uuid 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
role_id uuid NOT NULL,
role_id uuid NOT NULL default uuid_generate_v4(),

role_name text NOT NULL,
role_description text NULL,
organization_id int4 NULL,
CONSTRAINT sso_role_pk PRIMARY KEY (role_id)
);

-- public.sso_role foreign keys
ALTER TABLE public.sso_role ADD CONSTRAINT sso_role_fk FOREIGN KEY (organization_id) REFERENCES public.sso_organization(organization_id);

CREATE TABLE public.sso_role_user (
user_id int4 NULL,
role_id uuid NULL
Comment on lines +46 to +47
Copy link
Owner Author

Choose a reason for hiding this comment

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

Tương tự bảng sso_organization_user

);

-- public.sso_role_user foreign keys
ALTER TABLE public.sso_role_user ADD CONSTRAINT sso_role_user_fk FOREIGN KEY (user_id) REFERENCES public.sso_user(user_id);
ALTER TABLE public.sso_role_user ADD CONSTRAINT sso_role_user_fk_1 FOREIGN KEY (role_id) REFERENCES public.sso_role(role_id);

CREATE TABLE public.sso_user (
user_id int4 NOT NULL GENERATED ALWAYS AS IDENTITY( INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START 1 CACHE 1 NO CYCLE),
user_name text NOT NULL,
"password" text NOT NULL,
email text NULL,
kvp json NULL,
date_created timestamp NULL,
date_modified timestamp NULL,
modified_by uuid NULL,
last_logged_in timestamp NULL,
user_settings json NULL DEFAULT '{}'::json,
status public."enum" NULL DEFAULT 'active'::enum,
CONSTRAINT sso_user_pk PRIMARY KEY (user_id)
);

CREATE TABLE public.sso_user_profile (
profile_id uuid NOT NULL,
birth_date date NULL,
title text NULL,
firstname text NOT NULL,
lastname text NOT NULL,
avatar text NULL,
age int4 NULL,
address text NULL,
date_created timestamp NULL,
sex public."enum_sex" NULL,
user_id int4 NULL,
CONSTRAINT sso_user_profile_pk PRIMARY KEY (profile_id)
);

-- public.sso_user_profile foreign keys
ALTER TABLE public.sso_user_profile ADD CONSTRAINT sso_user_profile_fk FOREIGN KEY (user_id) REFERENCES public.sso_user(user_id);
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
INSERT INTO sso_organization ( organization_name, kvp, status, address, contact_number, email, country, state, city, postal_code, website_url)
VALUES
('org id 1', '{"key": "value"}', 'active', '123 Street', '012345678', '[email protected]', 'Vietnam', 'Hanoi', 'Hanoi', '100000', 'http://test.com'),
('org id 2', '{"key": "value"}', 'suspended', '456 Street', '0123456789', '[email protected]', 'America', 'Los Angeles', 'Los AngelesCity', '700000', 'http://test1.com');

INSERT INTO sso_role (role_id, role_name, role_description, organization_id)
VALUES
(uuid_generate_v4(), 'Admin', 'admin', (SELECT organization_id FROM sso_organization LIMIT 1)),
(uuid_generate_v4(), 'User', 'user', (SELECT organization_id FROM sso_organization LIMIT 1)),
(uuid_generate_v4(), 'Manage', 'manage', (SELECT organization_id FROM sso_organization OFFSET 1 LIMIT 1));

INSERT INTO sso_user ( user_name, password, email, kvp, modified_by, last_logged_in, user_settings, status)
VALUES
( 'Bob', 'admin_password', '[email protected]', '{"key": "value"}', NULL, NULL, '{}', 'active'),
( 'Minh', 'user_password', '[email protected]', '{"key": "value"}', NULL, NULL, '{}', 'active'),
( 'David', 'user_password', '[email protected]', '{"key": "value"}', NULL, NULL, '{}', 'active');

INSERT INTO sso_user_profile (profile_id, user_id, birth_date, sex, title, firstname, lastname, avatar, age, address)
VALUES
(uuid_generate_v4(), (SELECT user_id FROM sso_user WHERE user_id = 1), '1997-01-13', 'male', 'Mr.', 'Admin', 'User', 'avatar_url1', 33, '123 Test St'),
(uuid_generate_v4(), (SELECT user_id FROM sso_user WHERE user_id = 2), '1992-06-24', 'male', 'Mr.', 'User', 'One', 'avatar_url2', 38, '456 Test St'),
(uuid_generate_v4(), (SELECT user_id FROM sso_user WHERE user_id = 3), '1995-08-21', 'male', 'Mr.', 'User', 'Two', 'avatar_url3', 31, '789 Test St');


INSERT INTO sso_organization_user (organization_id, user_id)
VALUES
((SELECT organization_id FROM sso_organization LIMIT 1), (SELECT user_id FROM sso_user WHERE user_id = 1)),
((SELECT organization_id FROM sso_organization LIMIT 1), (SELECT user_id FROM sso_user WHERE user_id = 2)),
((SELECT organization_id FROM sso_organization OFFSET 1 LIMIT 1), (SELECT user_id FROM sso_user WHERE user_id = 3));

INSERT INTO sso_role_user (user_id, role_id)
VALUES
((SELECT user_id FROM sso_user WHERE user_id = 1), (SELECT role_id FROM sso_role WHERE role_id = '20c8f6a7-d1db-4c82-8101-afdd44765da8')),
((SELECT user_id FROM sso_user WHERE user_id = 2), (SELECT role_id FROM sso_role WHERE role_id = '771a6dee-2d88-47ca-a928-599bae9b09b5')),
((SELECT user_id FROM sso_user WHERE user_id = 3), (SELECT role_id FROM sso_role WHERE role_id = '75076631-15cb-4bf9-b01c-09d587f8c060'));