diff --git a/flyway/postgre/migrations/sql/V1_3__Create_lesson2_tables.sql b/flyway/postgre/migrations/sql/V1_3__Create_lesson2_tables.sql new file mode 100644 index 0000000..0b1040d --- /dev/null +++ b/flyway/postgre/migrations/sql/V1_3__Create_lesson2_tables.sql @@ -0,0 +1,72 @@ +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; + +CREATE TYPE org_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 TABLE IF NOT EXISTS sso_organization ( + org_id int PRIMARY KEY NOT NULL, + org_name text NOT NULL, + kvp JSON, + created_time timestamp DEFAULT now(), + updated_time timestamp DEFAULT now(), + status org_status_type DEFAULT 'active' NOT NULL, + address text, + contact_number text, + email text, + country text, + state text, + city text, + postal_code text, + website_url text, +); + +CREATE TABLE IF NOT EXISTS sso_user ( + user_id uuid DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL, + username text NOT NULL, + password text NOT NULL, + email text NOT NULL, + kvp json, + created_time timestamp DEFAULT now(), + updated_time timestamp DEFAULT now(), + modified_by uuid, + last_logged_in timestamp, + user_settings json DEFAULT '{}' :: json, + status user_status_type DEFAULT 'active' NOT NULL, +); + +CREATE TABLE IF NOT EXISTS sso_user_profile ( + profile_id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, + user_id uuid NOT NULL REFERENCES sso_user(user_id), + birth_date date, + sex gender_type, + title text, + first_name text, + last_name text, + avatar text, + age int, + address text, + date_created timestamp DEFAULT now(), + date_modified timestamp DEFAULT now(), +); + +CREATE TABLE IF NOT EXISTS sso_role ( + role_id uuid DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL, + role_name text NOT NULL, + role_description text, + organization_id int NOT NULL, +); + +CREATE TABLE IF NOT EXISTS sso_organization_user ( + organization_id int REFERENCES sso_user(user_id), + user_id uuid REFERENCES sso_organization(organization_id), + CONSTRAINT sso_organization_user_pk PRIMARY KEY (organization_id, user_id), +); + +CREATE TABLE IF NOT EXISTS sso_user_role ( + user_id uuid REFERENCES sso_role(role_id), + role_id uuid REFERENCES sso_user(user_id), + CONSTRAINT sso_user_role_pk PRIMARY KEY(user_id, 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 new file mode 100644 index 0000000..f07dbfc --- /dev/null +++ b/flyway/postgre/migrations/sql/V1_4__Insert_lesson2_initial_data.sql @@ -0,0 +1,269 @@ +INSERT INTO + sso_organization ( + org_id, + org_name, + kvp, + address, + contact_number, + email, + country, + city, + postal_code, + website_url + ) +VALUES + ( + 1, + 'org 1', + '{}', + '643 Pham Van Dong', + '0123-456-789', + 'info@nomail.com', + 'Vietnam', + 'Hanoi', + '10000', + 'org1.com' + ), + ( + 2, + 'org 2', + '{}', + '643 Pham Van Dong', + '0123-456-789', + 'info@nomail.com', + 'Vietnam', + 'Hanoi', + '10000', + 'org2.com' + ); + +INSERT INTO + sso_role (role_name, role_description, org_id) +VALUES + ( + 'admin', + 'admin', + 1 + ), + ('user', 'user', 1), + ('super-admin', 'super admin', 1); + +INSERT INTO + sso_user ( + username, + password, + email, + kvp, + modified_by, + last_logged_in, + user_settings, + ) +VALUES + ( + 'admin', + 'admin_password', + 'admin@example.com', + '{}', + NULL, + NULL, + '{}', + ), + ( + 'super_admin', + 'user_password', + 'user1@example.com', + '{}', + NULL, + NULL, + '{}', + ), + ( + 'user', + 'user_password', + 'user2@example.com', + '{}', + NULL, + NULL, + '{}', + ); + +INSERT INTO + sso_user_profile ( + user_id, + birth_date, + sex, + title, + first_name, + last_name, + avatar, + age, + address + ) +VALUES + ( + ( + SELECT + user_id + FROM + sso_user + WHERE + username = 'admin' + ), + '1995-01-01', + 'male', + 'Mr.', + 'Admin', + 'User', + 'url', + 33, + '87 PD' + ), + ( + ( + SELECT + user_id + FROM + sso_user + WHERE + username = 'super_admin' + ), + '1995-01-01', + 'female', + 'Ms.', + 'User', + 'User', + 'url', + 38, + '87 PD' + ), + ( + ( + SELECT + user_id + FROM + sso_user + WHERE + username = 'user' + ), + '1995-01-01', + 'male', + 'Mr.', + 'User', + 'User', + 'url', + 31, + '87 PD' + ); + +INSERT INTO + sso_organization_user (org_id, user_id) +VALUES + ( + ( + SELECT + org_id + FROM + sso_organization + LIMIT + 1 + ), ( + SELECT + user_id + FROM + sso_user + WHERE + username = 'admin' + ) + ), + ( + ( + SELECT + org_id + FROM + sso_organization + LIMIT + 1 + ), ( + SELECT + user_id + FROM + sso_user + WHERE + username = 'super_admin' + ) + ), + ( + ( + SELECT + org_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' + ), + ( + SELECT + role_id + FROM + sso_role + WHERE + role_name = 'admin' + ) + ), + ( + ( + SELECT + user_id + FROM + sso_user + WHERE + username = 'super_admin' + ), + ( + 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' + ) + ); \ No newline at end of file