-
Notifications
You must be signed in to change notification settings - Fork 2
Lesson 2 - Dao Thi Quynh Anh #17
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
Open
ndthang15
wants to merge
1
commit into
lesson2/main
Choose a base branch
from
lesson2/dao_thi_quynh_anh
base: lesson2/main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
flyway/postgre/migrations/sql/V1_3__Create_lesson2_tables.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 $$; | ||||||
|
|
||||||
| CREATE TABLE IF NOT EXISTS sso_organization ( | ||||||
| organization_id SERIAL 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(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, | ||||||
|
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 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_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) | ||||||
| ); | ||||||
35 changes: 35 additions & 0 deletions
35
flyway/postgre/migrations/sql/V1_4__Insert_lesson2_initial_data.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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')); | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 $$ beginlên đầu file, vàend $$xuống cuối cùng của file).