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
85 changes: 85 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,85 @@
-- Database: admindb

-- DROP DATABASE IF EXISTS admindb;

CREATE TYPE sex AS ENUM ('female', 'male');
CREATE TYPE status AS ENUM ('active', 'deactived', 'deleted');

DROP TABLE IF EXISTS sso_organization;
CREATE TABLE sso_organization (
Comment on lines +8 to +9
Copy link
Owner Author

Choose a reason for hiding this comment

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

Không nên xóa bảng vì có thể gây mất dữ liệu. Chỉ cần câu lệnh CREATE TABLE IF NOT EXISTS thì sẽ không bị lỗi khi bảng đã tồn tại.

Suggested change
DROP TABLE IF EXISTS sso_organization;
CREATE TABLE sso_organization (
CREATE TABLE IF NOT EXISTS sso_organization (

organization_id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
organization_name TEXT,
kvp json,
date_created TIMESTAMP,
date_modified TIMESTAMP,
status status DEFAULT 'active' NOT NULL,
address TEXT,
contact_number TEXT,
email TEXT,
country TEXT,
city TEXT,
postal_code TEXT,
organization_sub_name TEXT,
website_url TEXT
);

DROP TABLE IF EXISTS sso_role;
CREATE TABLE sso_role(
Comment on lines +26 to +27
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
DROP TABLE IF EXISTS sso_role;
CREATE TABLE sso_role(
CREATE TABLE IF NOT EXISTS sso_role(

role_id uuid DEFAULT gen_random_uuid() 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.

Nếu dùng UUID muốn gen random uuid cần cài thêm extension CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

Suggested change
role_id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
role_id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,

role_name TEXT,
rele_description TEXT,
organization_id INT NOT NULL,
CONSTRAINT fk_organization FOREIGN KEY(organization_id) REFERENCES sso_organization(organization_id)

);
DROP TABLE IF EXISTS sso_user;
CREATE TABLE sso_user(
Comment on lines +35 to +36
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
DROP TABLE IF EXISTS sso_user;
CREATE TABLE sso_user(
CREATE TABLE IF NOT EXISTS sso_user(

user_id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
password TEXT,
email TEXT,
kvp json,
date_created TIMESTAMP,
date_modified TIMESTAMP,
modified_by uuid,
last_logged_in TIMESTAMP,
user_settings jsonb DEFAULT '{}'::jsonb,
status status DEFAULT 'active'

);

DROP TABLE IF EXISTS sso_user_profile;
CREATE TABLE sso_user_profile (
Comment on lines +50 to +51
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
DROP TABLE IF EXISTS sso_user_profile;
CREATE TABLE sso_user_profile (
CREATE TABLE IF NOT EXISTS sso_user_profile (

profile_id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
user_id INT,
birth_date DATE,
sex sex,
title TEXT,
fristname TEXT NOT NULL,
lastname TEXT NOT NULL,
avatar TEXT,
age INT,
address TEXT,
date_created TIMESTAMP,
CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES sso_user(user_id)
);



DROP TABLE IF EXISTS sso_organization_user;
CREATE TABLE sso_organization_user(
organization_id INT,
user_id INT,
PRIMARY KEY (organization_id, user_id),
CONSTRAINT fk_organization_id FOREIGN KEY(organization_id) REFERENCES sso_organization(organization_id),
CONSTRAINT fk_user_id FOREIGN KEY(user_id) REFERENCES sso_user(user_id)
);
DROP TABLE IF EXISTS sso_user_role;
CREATE TABLE sso_user_role(
user_id INT,
role_id uuid,
PRIMARY KEY (user_id, role_id),
CONSTRAINT fk_role_id FOREIGN KEY(role_id) REFERENCES sso_role(role_id),
CONSTRAINT fk_user_id FOREIGN KEY(user_id) REFERENCES sso_user(user_id)
);


Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
INSERT INTO sso_organization(organization_name,kvp,date_created,status,address,contact_number,email,country,city,postal_code,organization_sub_name,website_url)
VALUES ('seta','{}','2023-10-10 11:30:30','active','Duy Tân, Cầu Giấy, VN','0123456789', '[email protected]','Việt Nam','Hà Nội','000084','seta','https://seta-international.com');
INSERT INTO sso_role(role_name,rele_description, organization_id)
VALUES ('member','member',1);
INSERT INTO sso_user(password,email,kvp,date_created, status)
VALUES('password','[email protected]','{}','2023-10-10 11:30:00', 'active');
INSERT INTO sso_user_profile (user_id,birth_date,sex,title, fristname, lastname, avatar,age,address, date_created)
VALUES(1,'1-3-2000','male','Hai','Hai','Nguyen','hai_nguyen',23,'Ha Noi','2023-10-10 11:30:00');
INSERT INTO sso_organization_user(organization_id,user_id)
SELECT organization_id, user_id
FROM sso_organization
CROSS JOIN sso_user;
INSERT INTO sso_user_role(user_id, role_id)
SELECT user_id, role_id
FROM sso_user
CROSS JOIN sso_role;