-
Notifications
You must be signed in to change notification settings - Fork 2
lesson 2 - tran dang thinh - homework #4
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,69 +1,86 @@ | ||||||
| CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; | ||||||
| DROP TYPE IF EXISTS organization_status_type; | ||||||
| DROP TYPE IF EXISTS user_status_type; | ||||||
| DROP TYPE IF EXISTS sex_type; | ||||||
|
Comment on lines
+1
to
+3
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mình ko cần DROP TYPE vì script chỉ chạy 1 lần, và xóa nếu nó đang được gắn vào bảng có thể gây lỗi. |
||||||
|
|
||||||
| CREATE TYPE organization_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 TYPE sex_type AS ENUM ('male', 'female'); | ||||||
|
|
||||||
| DROP TABLE IF EXISTS sso_organization; | ||||||
| DROP TABLE IF EXISTS sso_user; | ||||||
| DROP TABLE IF EXISTS sso_roles; | ||||||
| DROP TABLE IF EXISTS sso_user_profile; | ||||||
| DROP TABLE IF EXISTS sso_organization_user; | ||||||
| DROP TABLE IF EXISTS sso_user_role; | ||||||
|
Comment on lines
+9
to
+14
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mình không nên xóa bảng, vì nếu lỡ script chạy lần 2 vì 1 lý do nào đó, có thể sẽ bị mất dữ liệu thực tế. |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
| CREATE TABLE IF NOT EXISTS sso_organization ( | ||||||
| organization_id SERIAL PRIMARY KEY NOT NULL, | ||||||
| organization_name VARCHAR(255) NOT NULL, | ||||||
| status organization_status_type DEFAULT 'active'::organization_status_type NOT NULL, | ||||||
| organization_id INT NOT NULL, | ||||||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| organization_name VARCHAR(100) NOT NUll, | ||||||
| kvp JSON, | ||||||
| date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||||||
| date_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||||||
| status organization_status_type DEFAULT 'active', | ||||||
| address VARCHAR(255), | ||||||
| contact_number VARCHAR(255), | ||||||
| email VARCHAR(255), | ||||||
| contact_number VARCHAR(100), | ||||||
| email VARCHAR(100), | ||||||
| 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() | ||||||
| postal_code VARCHAR(10), | ||||||
| organization_sub_name VARCHAR(10), | ||||||
| website_url VARCHAR(255) | ||||||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| PRIMARY KEY (organization_id) | ||||||
| ); | ||||||
|
|
||||||
| CREATE TABLE IF NOT EXISTS sso_role ( | ||||||
| role_id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL, | ||||||
| role_name VARCHAR(255) NOT NULL, | ||||||
| role_description VARCHAR(255), | ||||||
| organization_id INT, | ||||||
| FOREIGN KEY (organization_id) REFERENCES sso_organization(organization_id) | ||||||
| CREATE TABLE IF NOT EXISTS sso_user ( | ||||||
| user_id UUID DEFAULT uuid_generate_v4(), | ||||||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Khóa chính thì mình nên không cho phép NULL
Suggested change
|
||||||
| username VARCHAR(100) NOT NULL, | ||||||
| password VARCHAR(100) NOT NULL, | ||||||
| email VARCHAR(100) NOT NULL, | ||||||
| kvp JSON, | ||||||
| date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||||||
| date_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||||||
| modified_by UUID DEFAULT uuid_generate_v4(), | ||||||
| last_logged_in TIMESTAMP, | ||||||
| user_settings JSON DEFAULT '{}', | ||||||
| status user_status_type DEFAULT 'active' | ||||||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| PRIMARY KEY (user_id) | ||||||
| ); | ||||||
|
|
||||||
| CREATE TABLE IF NOT EXISTS sso_user ( | ||||||
| user_id UUID DEFAULT uuid_generate_v4() PRIMARY KEY 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 | ||||||
| CREATE TABLE IF NOT EXISTS sso_organization_user ( | ||||||
| organization_id INT NOT NULL, | ||||||
| user_id UUID, | ||||||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cột này mình cũng để NOT NULL nhé
Suggested change
|
||||||
| PRIMARY KEY (organization_id, user_id), | ||||||
| FOREIGN KEY (organization_id) REFERENCES sso_organization(organization_id), | ||||||
| FOREIGN KEY (user_id) REFERENCES sso_user(user_id) | ||||||
| ); | ||||||
|
|
||||||
| CREATE TABLE IF NOT EXISTS sso_roles ( | ||||||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tên bảng để số ít thôi nhé
Suggested change
|
||||||
| role_id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, | ||||||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ở dưới có primary key rồi nên ở trên ko cần nữa nhé, với thêm NOT NULL cho khóa chính
Suggested change
|
||||||
| role_name VARCHAR(255) NOT NULL | ||||||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| role_description VARCHAR(255), | ||||||
| organization_id INT NOT NULL, | ||||||
| status organization_status_type, | ||||||
| PRIMARY KEY (role_id) | ||||||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| FOREIGN KEY (organization_id) REFERENCES sso_organization(organization_id) | ||||||
| ); | ||||||
|
|
||||||
| CREATE TABLE IF NOT EXISTS sso_user_profile ( | ||||||
| profile_id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, | ||||||
| profile_id UUID DEFAULT uuid_generate_v4(), | ||||||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| user_id UUID NOT NULL, | ||||||
| birth_date DATE, | ||||||
| sex gender_type, | ||||||
| sex sex_type, | ||||||
| title VARCHAR(255), | ||||||
| first_name VARCHAR(255), | ||||||
| last_name VARCHAR(255), | ||||||
| first_name VARCHAR(10), | ||||||
| last_name VARCHAR(10), | ||||||
| avatar VARCHAR(255), | ||||||
| age INT, | ||||||
| address VARCHAR(255), | ||||||
| date_created TIMESTAMPTZ DEFAULT now(), | ||||||
| date_modified TIMESTAMPTZ DEFAULT now(), | ||||||
| FOREIGN KEY (user_id) REFERENCES sso_user(user_id) | ||||||
| ); | ||||||
|
|
||||||
| CREATE TABLE IF NOT EXISTS sso_organization_user ( | ||||||
| organization_id BIGINT, | ||||||
| user_id UUID, | ||||||
| PRIMARY KEY (organization_id, user_id), | ||||||
| FOREIGN KEY (organization_id) REFERENCES sso_organization(organization_id), | ||||||
| date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||||||
| date_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||||||
| PRIMARY KEY(profile_id) | ||||||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| FOREIGN KEY (user_id) REFERENCES sso_user(user_id) | ||||||
| ); | ||||||
|
|
||||||
|
|
@@ -72,5 +89,5 @@ CREATE TABLE IF NOT EXISTS sso_user_role ( | |||||
| role_id UUID, | ||||||
| PRIMARY KEY (user_id, role_id), | ||||||
| FOREIGN KEY (user_id) REFERENCES sso_user(user_id), | ||||||
| FOREIGN KEY (role_id) REFERENCES sso_role(role_id) | ||||||
| ); | ||||||
| FOREIGN KEY (role_id) REFERENCES sso_roles(role_id) | ||||||
| ); | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,30 @@ | ||
| INSERT INTO sso_organization (organization_id, organization_name, kvp, status, address, contact_number, email, country, state, city, postal_code, website_url) | ||
| INSERT INTO sso_organization (organization_id, organization_name, kvp, status, address, contact_number, email, country, state, city, postal_code, organization_sub_name, website_url) | ||
| VALUES | ||
| (1, 'org 1', '{"key": "value"}', 'active', '82 Duy Tan', '123-456-7890', 'info@example.com', 'Vietnam', 'Hanoi', 'Hanoi', '100000', 'http://example.com'), | ||
| (2, 'org 2', '{"key": "value"}', 'active', '83 Duy Tan', '987-654-3210', 'contact@example.com', 'Vietnam', 'Ho Chi Minh', 'Ho Chi Minh City', '700000', 'http://example.org'); | ||
| (1, 'TEST 1', '{"key": "value 1"}', 'active', 'active 1', '123456789', 'test@example.com', 'Vietnam', 'Hanoi', 'Hanoi', '100000', 'Sub Organization 1', 'http://example.com'), | ||
| (2, 'TEST 2', '{"key": "value 2"}', 'active', 'active 1', '123456789', 'test@example.com', 'Vietnam', 'Ho Chi Minh', 'Ho Chi Minh City', '700000', 'Sub Organization 2', 'http://example.org'); | ||
|
|
||
| INSERT INTO sso_role (role_id, role_name, role_description, organization_id) | ||
| INSERT INTO sso_roles (role_id, role_name, role_description, organization_id, status) | ||
| 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)); | ||
| (uuid_generate_v4(), 'role_name_1', 'role_description', 1, 'active'), | ||
| (uuid_generate_v4(), 'role_name_2', 'role_description', 2, 'active'), | ||
|
|
||
|
|
||
| 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'); | ||
| (uuid_generate_v4(), 'user_1', 'password1', '[email protected]', '{"key": "value1"}', NULL, NULL, '{"setting_key": "value1"}', 'active'), | ||
| (uuid_generate_v4(), 'user_2', 'password1', '[email protected]', '{"key": "value2"}', NULL, NULL, '{"setting_key": "value2"}', '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'); | ||
| (uuid_generate_v4(), (SELECT user_id FROM sso_user WHERE username = 'user_1'), '1990-01-01', 'first_name_1', 'last_name_1', 'Admin', 'User', 'avatar_1', 100, 'address1'), | ||
| (uuid_generate_v4(), (SELECT user_id FROM sso_user WHERE username = 'user_2'), '1985-05-15', 'first_name_2', 'last_name_2', 'User', 'One', 'avatar_2', 100, 'address2'), | ||
|
|
||
| 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')); | ||
| (1, (SELECT user_id FROM sso_user WHERE username = 'user_1')), | ||
| (2, (SELECT user_id FROM sso_user WHERE username = 'user_2')), | ||
|
|
||
| INSERT INTO sso_user_role (user_id, role_id) | ||
| INSERT INTO sso_user_roles (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')); | ||
|
|
||
| ((SELECT user_id FROM sso_user WHERE username = 'user_1'), (SELECT role_id FROM sso_roles WHERE role_name = 'role_name_1')), | ||
| ((SELECT user_id FROM sso_user WHERE username = 'user_2'), (SELECT role_id FROM sso_roles WHERE role_name = 'role_name_2')), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thêm cái này mới dùng được hàm
uuid_generate_v4()