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
98 changes: 98 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,98 @@
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

do $$
begin
if not exists (select 1 from PG_TYPE where TYPNAME = 'organization_status_type') then
create type organization_status_type
as enum ('active', 'suspend', 'deleted');
end if;
if not exists (select 1 from PG_TYPE where TYPNAME = 'user_status_type') then
create type user_status_type
as enum ('active', 'deactivated', 'deleted');
end if;
if not exists (select 1 from PG_TYPE where TYPNAME = 'gender_type') then
create type gender_type
as enum ('male', 'female');
end if;
end $$;
Copy link
Owner Author

Choose a reason for hiding this comment

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

Rất tốt khi dùng transaction block trong Flyway. Sẽ tốt hơn nếu để cả file vào 1 transaction (Ví dụ: đưa do $$ begin lên đầu file, và end $$ xuống cuối cùng của file).


CREATE TABLE IF NOT EXISTS sso_organization (
organization_id SERIAL 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 SERIAL NOT NULL,
organization_id SERIAL NOT NULL generated always as identity,

organization_name VARCHAR(255) NOT NULL,
status organization_status_type DEFAULT 'active'::organization_status_type NOT NULL,
address VARCHAR(255),
contact_number VARCHAR(255),
email VARCHAR(255),
country VARCHAR(255),
state VARCHAR(255),
city VARCHAR(255),
postal_code VARCHAR(255),
website_url VARCHAR(255),
kvp JSONB,
date_created TIMESTAMPTZ DEFAULT now(),
date_modified TIMESTAMPTZ DEFAULT now(),

constraint sso_organization_pk primary key (organization_id)
);

CREATE TABLE IF NOT EXISTS 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.

Đặt thêm default để ID này có thể tự sinh nếu câu INSERT không gen sẵn uuid truyền vào.

Suggested change
role_id uuid NOT NULL,
role_id uuid NOT NULL DEFAULT uuid_generate_v4(),

role_name VARCHAR(255) NOT NULL,
role_description VARCHAR(255),
organization_id INT,

constraint sso_role_pk primary key (role_id),
constraint sso_role_fk foreign key (organization_id) references sso_organization(organization_id)
);

CREATE TABLE IF NOT EXISTS sso_user (
user_id UUID NOT NULL,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
kvp JSONB,
date_created TIMESTAMPTZ DEFAULT now(),
date_modified TIMESTAMPTZ DEFAULT now(),
modified_by UUID,
last_logged_in TIMESTAMPTZ,
user_settings JSONB DEFAULT '{}' :: JSONB,
status user_status_type DEFAULT 'active' :: user_status_type,

constraint sso_user_pk primary key (user_id)
);

CREATE TABLE IF NOT EXISTS sso_user_profile (
profile_id UUID not NULL,
user_id UUID NOT NULL,
birth_date DATE,
sex gender_type,
title VARCHAR(255),
first_name VARCHAR(255),
last_name VARCHAR(255),
avatar VARCHAR(255),
age INT,
address VARCHAR(255),
date_created TIMESTAMPTZ DEFAULT now(),
date_modified TIMESTAMPTZ DEFAULT now(),

constraint sso_user_profile_pk primary key (profile_id),
constraint sso_user_profile_fk foreign key (user_id) references sso_user(user_id)
);

CREATE TABLE IF NOT EXISTS sso_organization_user (
organization_id BIGINT,
user_id UUID,

constraint sso_organization_user_pk primary key (organization_id, user_id),
constraint sso_organization_user_fk1 FOREIGN KEY (organization_id) REFERENCES sso_organization(organization_id),
constraint sso_organization_user_fk2 FOREIGN KEY (user_id) REFERENCES sso_user(user_id)
);

CREATE TABLE IF NOT EXISTS sso_user_role (
user_id UUID,
role_id UUID,

constraint sso_user_role_pk PRIMARY KEY (user_id, role_id),
constraint sso_user_role_fk1 FOREIGN KEY (user_id) REFERENCES sso_user(user_id),
constraint sso_user_role_fk2 FOREIGN KEY (role_id) REFERENCES sso_role(role_id)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
INSERT INTO sso_organization (organization_id, organization_name, kvp, status, address, contact_number, email, country, state, city, postal_code, website_url)
VALUES
(1, 'org 1', '{"key": "value"}', 'active', '82 Duy Tan', '123-456-7890', '[email protected]', 'Vietnam', 'Hanoi', 'Hanoi', '100000', 'http://example.com'),
(2, 'org 2', '{"key": "value"}', 'active', '83 Duy Tan', '987-654-3210', '[email protected]', 'Vietnam', 'Ho Chi Minh', 'Ho Chi Minh City', '700000', 'http://example.org');

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(), 'super-admin', 'super admin', (SELECT organization_id FROM sso_organization OFFSET 1 LIMIT 1));

INSERT INTO sso_user (user_id, username, password, email, kvp, modified_by, last_logged_in, user_settings, status)
VALUES
(uuid_generate_v4(), 'admin_user', 'admin_password', '[email protected]', '{"key": "value"}', NULL, NULL, '{}', 'active'),
(uuid_generate_v4(), 'super_admin_user', 'user_password', '[email protected]', '{"key": "value"}', NULL, NULL, '{}', 'active'),
(uuid_generate_v4(), 'user', 'user_password', '[email protected]', '{"key": "value"}', NULL, NULL, '{}', 'active');

INSERT INTO sso_user_profile (profile_id, user_id, birth_date, sex, title, first_name, last_name, avatar, age, address)
VALUES
('6474cefb-6c2e-4a57-9ed3-12cfbd724aaa', (SELECT user_id FROM sso_user WHERE username = 'admin_user'), '1990-01-01', 'male', 'Mr.', 'Admin', 'User', 'url', 33, '87 PD'),
('f5cb458e-dbbc-414a-8c5d-7a9393829e0c', (SELECT user_id FROM sso_user WHERE username = 'super_admin_user'), '1985-05-15', 'female', 'Ms.', 'User', 'User', 'url', 38, '87 PD'),
('cb7acc9e-8ade-4848-9292-eaa5945280a6', (SELECT user_id FROM sso_user WHERE username = 'user'), '1992-08-20', 'male', 'Mr.', 'User', 'User', 'url', 31, '87 PD');

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 username = 'admin_user')),
((SELECT organization_id FROM sso_organization LIMIT 1), (SELECT user_id FROM sso_user WHERE username = 'super_admin_user')),
((SELECT organization_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_user'), (SELECT role_id FROM sso_role WHERE role_name = 'admin')),
((SELECT user_id FROM sso_user WHERE username = 'super_admin_user'), (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'));