diff --git a/flyway/postgre/migrations/sql/V1_3__Create_lesson2_tables.sql b/flyway/postgre/migrations/sql/V1_3__Create_lesson2_tables.sql index 251589a..e670d75 100644 --- a/flyway/postgre/migrations/sql/V1_3__Create_lesson2_tables.sql +++ b/flyway/postgre/migrations/sql/V1_3__Create_lesson2_tables.sql @@ -1,76 +1,65 @@ -CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; +-- DROP TYPE IF EXISTS status_type; +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 ( +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) +); \ No newline at end of file diff --git a/flyway/postgre/migrations/sql/V1_4__Insert_lesson2_initial_data.sql b/flyway/postgre/migrations/sql/V1_4__Insert_lesson2_initial_data.sql index ff0a5c9..bc6ed96 100644 --- a/flyway/postgre/migrations/sql/V1_4__Insert_lesson2_initial_data.sql +++ b/flyway/postgre/migrations/sql/V1_4__Insert_lesson2_initial_data.sql @@ -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', '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'); +insert into sso_organization (organization_name, email, date_created, date_modified ) +values ('Org 1','email1@.gmail.com', 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','email2@.gmail.com', 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', 'admin@example.com', '{"key": "value"}', NULL, NULL, '{}', 'active'), -(uuid_generate_v4(), 'super_admin_user', 'user_password', 'user1@example.com', '{"key": "value"}', NULL, NULL, '{}', 'active'), -(uuid_generate_v4(), 'user', 'user_password', 'user2@example.com', '{"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); \ No newline at end of file