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
68 changes: 68 additions & 0 deletions flyway/postgre/migrations/sql/V1_3__Create_lesson2_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
CREATE TYPE type_status AS ENUM ('active', 'deactivated', 'deleted');
CREATE TYPE type_sex AS ENUM ('male', 'female');
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE IF NOT EXISTS sso_user (
user_id SERIAL PRIMARY KEY,
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ần thêm generate tự tăng

Suggested change
user_id SERIAL PRIMARY KEY,
user_id SERIAL PRIMARY KEY GENERATED ALWAYS AS IDENTITY,

user_name TEXT NOT NULL,
password TEXT NOT NULL,
email TEXT,
kvp JSON,
date_created TIMESTAMP,
date_modified TIMESTAMP,
modified_by UUID DEFAULT uuid_generate_v4(),
last_logged_in TIMESTAMP,
user_settings JSON DEFAULT '{}'::JSON,
status type_status DEFAULT 'active'
);
CREATE TABLE IF NOT EXISTS sso_organization (
organization_id SERIAL PRIMARY KEY,
Copy link
Owner Author

Choose a reason for hiding this comment

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

Suggested change
organization_id SERIAL PRIMARY KEY,
organization_id SERIAL PRIMARY KEY GENERATED ALWAYS AS IDENTITY,

organization_name TEXT NOT NULL,
kvp JSON,
date_created TIMESTAMP,
date_modified TIMESTAMP,
status type_status DEFAULT 'active' NOT NULL,
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,
role_name TEXT NOT NULL,
role_description TEXT,
organization_id INT,
CONSTRAINT fk_organization FOREIGN KEY (organization_id) REFERENCES sso_organization (organization_id)
);
CREATE TABLE IF NOT EXISTS sso_user_organization (
organization_id INT NOT NULL,
user_id INT NOT NULL,
CONSTRAINT user_org PRIMARY KEY (organization_id, user_id),
CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES sso_user (user_id),
CONSTRAINT fk_org FOREIGN KEY (organization_id) REFERENCES sso_organization (organization_id)
);
CREATE TABLE IF NOT EXISTS sso_user_role (
user_id INT NOT NULL,
role_id UUID NOT NULL,
CONSTRAINT user_role PRIMARY KEY (user_id, role_id),
CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES sso_user (user_id),
CONSTRAINT fk_role FOREIGN KEY (role_id) REFERENCES sso_role (role_id)
);
CREATE TABLE IF NOT EXISTS sso_user_profile (
profile_id UUID DEFAULT uuid_generate_v4() NOT NULL,
user_id INT NOT NULL,
birth_date DATE,
sex type_sex,
title TEXT,
firstname TEXT NOT NULL,
lastname TEXT NOT NULL,
avatar TEXT,
age INT,
address TEXT,
date_created TIMESTAMP,
CONSTRAINT fk_user_profiles FOREIGN KEY (user_id) REFERENCES sso_user (user_id)
);
135 changes: 135 additions & 0 deletions flyway/postgre/migrations/sql/V1_4__Insert_lesson2_initial_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
INSERT INTO sso_organization (
organization_name,
kvp,
date_created,
date_modified,
address,
contact_number,
email,
country,
state,
city,
postal_code,
organization_sub_name,
website_url
)
VALUES (
'XYZ Corporation',
'{"industry": "Technology", "employees": 750}',
NOW(),
NOW(),
'456 Elm St',
'555-1234',
'[email protected]',
'USA',
'California',
'Los Angeles',
'90001',
'XYZ West',
'https://www.xyzcorp.com'
),
(
'ABC Ltd.',
'{"industry": "Finance", "employees": 200}',
NOW(),
NOW(),
'789 Oak Ave',
'555-5678',
'[email protected]',
'USA',
'New York',
'New York City',
'10001',
'ABC East',
'https://www.abcltd.com'
);
INSERT INTO sso_role (role_name, role_description, organization_id)
VALUES (
'Administrator',
'Full access to all features',
2
),
('Manager', 'Access to manage users and roles', 2),
(
'Employee',
'Basic access to view and edit own profile',
2
);
INSERT INTO sso_user (
user_name,
password,
email,
kvp,
date_created,
date_modified,
modified_by,
last_logged_in,
user_settings,
status
)
VALUES (
'user123',
'pass456',
'[email protected]',
'{"role": "admin"}',
NOW(),
NOW(),
NULL,
NOW(),
'{"theme": "dark"}',
'active'
),
(
'user456',
'pass789',
'[email protected]',
'{"role": "user"}',
NOW(),
NOW(),
NULL,
NOW(),
'{"theme": "light"}',
'active'
);
INSERT INTO sso_user_organization (organization_id, user_id)
VALUES (2, 1),
(3, 2);
INSERT INTO sso_user_role (user_id, role_id)
VALUES (1, 'c5089b76-92e5-42f0-81a3-1317322ad372'),
(1, 'ce40b375-f4de-4df8-9848-721573b39ff2');
INSERT INTO sso_user_profile (
user_id,
birth_date,
sex,
title,
firstname,
lastname,
avatar,
age,
address,
date_created
)
VALUES (
1,
'1988-07-15',
'male',
'Mr.',
'John',
'Doe',
'https://example.com/johndoe.jpg',
35,
'456 Elm St, Los Angeles, California, USA',
NOW()
),
(
2,
'1992-03-20',
'female',
'Ms.',
'Jane',
'Smith',
'https://example.com/janesmith.jpg',
29,
'789 Oak Ave, New York City, New York, USA',
NOW()
);