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
117 changes: 53 additions & 64 deletions flyway/postgre/migrations/sql/V1_3__Create_lesson2_tables.sql
Original file line number Diff line number Diff line change
@@ -1,76 +1,65 @@
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- DROP TYPE IF EXISTS status_type;
Copy link
Owner Author

Choose a reason for hiding this comment

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

Cái này a comment đi 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 status_type AS ENUM ('active', 'suspened', 'deleted');

CREATE TYPE organization_status_type AS ENUM ('active', 'suspend', 'deleted');
CREATE TYPE user_status_type AS ENUM ('active', 'deactivated', 'deleted');
-- DROP TYPE IF EXISTS gender_type;
CREATE TYPE gender_type AS ENUM ('male', 'female');

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,
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()
create table if not EXISTS sso_organization (
organization_id BIGINT not null generated always as identity primary KEY,
organization_name text not null,
kvp jsonb,
date_created TIMESTAMP,
date_modified TIMESTAMP,
status status_type not null default 'active',
address text,
contact_number text,
email text,
country text,
state text,
city text,
postal_code text,
organization_sub_name text,
website_url text
);

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 BIGINT not null generated always as IDENTITY primary key,
user_name text not null,
password text not null,
kvp json,
date_created TIMESTAMP,
date_modified TIMESTAMP,
modified_by BIGINT,
last_logged_in TIMESTAMP,
user_setting json default '{}'::json,
status status_type default 'active'
);

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_role (
role_id BIGINT not null generated always as IDENTITY primary key, -- UUID
role_name text not null,
role_description text,
organization_id BIGINT references sso_organization(organization_id),

);

CREATE TABLE IF NOT EXISTS sso_user_profile (
profile_id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
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(),
FOREIGN KEY (user_id) REFERENCES sso_user(user_id)
create table if not EXISTS sso_user_profile (
profile_id BIGINT not null generated always as IDENTITY primary key,
user_id bigint references sso_user(user_id),
birth_date date,
sex gender_type,
title text,
firstname text not null,
lastname text not null,
avatar text,
age_number int,
address text,
date_created TIMESTAMP
);

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),
FOREIGN KEY (user_id) REFERENCES sso_user(user_id)
);

CREATE TABLE IF NOT EXISTS sso_user_role (
user_id UUID,
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)
);
CREATE TABLE IF NOT EXISTS sso_profile_to_role (
Copy link
Owner Author

Choose a reason for hiding this comment

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

Bảng này như trong Diagram thì anh muốn sso_user_role quan hệ nhiều nhiều giữa user và role. Profile cũng có thể nhưng chưa đúng yêu cầu.

profile_role_id BIGINT not null generated always as IDENTITY primary key,
profile_id BIGINT references sso_user_profile(profile_id),
role_id BIGINT references sso_role(role_id)
);
58 changes: 29 additions & 29 deletions flyway/postgre/migrations/sql/V1_4__Insert_lesson2_initial_data.sql
Original file line number Diff line number Diff line change
@@ -1,35 +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_organization (organization_name, email, date_created, date_modified )
values ('Org 1','[email protected]', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);

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_organization (organization_name, email, date_created, date_modified )
values ('Org 2','[email protected]', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);

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 (user_name, password, date_created, date_modified,last_logged_in )
values ('user_2','seta@123', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,CURRENT_TIMESTAMP);

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_user (user_name, password, date_created, date_modified,last_logged_in )
values ('user_1','seta@123', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,CURRENT_TIMESTAMP);

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'));
insert into sso_role ( role_name, organization_id )
values ('Admin Org 1',1);
insert into sso_role ( role_name, organization_id )
values ('Admin Org 2',2);
insert into sso_role ( role_name, organization_id )
values ('User Org 1',1);

insert into sso_user_profile ( user_id, firstname, lastname, date_created )
values (1,'User1', 'Profile 1 ',CURRENT_TIMESTAMP);
insert into sso_user_profile ( user_id, firstname, lastname, date_created )
values (1,'User1', 'Profile 2',CURRENT_TIMESTAMP);
insert into sso_user_profile ( user_id, firstname, lastname, date_created )
values (2,'User2', 'Profile 1 ',CURRENT_TIMESTAMP);
insert into sso_user_profile ( user_id, firstname, lastname, date_created )
values (2,'User2', 'Profile 2',CURRENT_TIMESTAMP);

insert into sso_profile_to_role ( profile_id, role_id)
values (1,1);
insert into sso_profile_to_role ( profile_id, role_id)
values (2,2);
insert into sso_profile_to_role ( profile_id, role_id)
values (3,3);