refactor: enhance department and user data insertion with CTEs and streamline role authority assignments
This commit is contained in:
@@ -59,14 +59,41 @@ CREATE INDEX departments_name_index ON departments (name);
|
|||||||
CREATE INDEX departments_parent_id_index ON departments (parent_id);
|
CREATE INDEX departments_parent_id_index ON departments (parent_id);
|
||||||
|
|
||||||
--- Departments Data Insertion ---
|
--- Departments Data Insertion ---
|
||||||
INSERT INTO departments (id, name, parent_id, sort, status)
|
WITH company_hq AS (
|
||||||
VALUES (1, 'Company HQ', NULL, 1, 'ACTIVE'::STATUS),
|
INSERT INTO departments (name, parent_id, sort, status)
|
||||||
(2, 'Human Resources', 1, 1, 'ACTIVE'::STATUS),
|
VALUES ('Company HQ', NULL, 1, 'ACTIVE'::STATUS)
|
||||||
(3, 'Finance', 1, 2, 'ACTIVE'::STATUS),
|
RETURNING id),
|
||||||
(4, 'Technology', 1, 3, 'ACTIVE'::STATUS),
|
human_resources AS (
|
||||||
(5, 'IT Support', 4, 1, 'ACTIVE'::STATUS),
|
INSERT INTO departments (name, parent_id, sort, status)
|
||||||
(6, 'Software Development', 4, 2, 'ACTIVE'::STATUS),
|
SELECT 'Human Resources', company_hq.id, 1, 'ACTIVE'::STATUS
|
||||||
(7, 'Operations', 1, 4, 'INACTIVE'::STATUS);
|
FROM company_hq
|
||||||
|
RETURNING id),
|
||||||
|
finance AS (
|
||||||
|
INSERT INTO departments (name, parent_id, sort, status)
|
||||||
|
SELECT 'Finance', company_hq.id, 2, 'ACTIVE'::STATUS
|
||||||
|
FROM company_hq
|
||||||
|
RETURNING id),
|
||||||
|
technology AS (
|
||||||
|
INSERT INTO departments (name, parent_id, sort, status)
|
||||||
|
SELECT 'Technology', company_hq.id, 3, 'ACTIVE'::STATUS
|
||||||
|
FROM company_hq
|
||||||
|
RETURNING id),
|
||||||
|
it_support AS (
|
||||||
|
INSERT INTO departments (name, parent_id, sort, status)
|
||||||
|
SELECT 'IT Support', technology.id, 1, 'ACTIVE'::STATUS
|
||||||
|
FROM technology
|
||||||
|
RETURNING id),
|
||||||
|
software_development AS (
|
||||||
|
INSERT INTO departments (name, parent_id, sort, status)
|
||||||
|
SELECT 'Software Development', technology.id, 2, 'ACTIVE'::STATUS
|
||||||
|
FROM technology
|
||||||
|
RETURNING id),
|
||||||
|
operations AS (
|
||||||
|
INSERT INTO departments (name, parent_id, sort, status)
|
||||||
|
SELECT 'Operations', company_hq.id, 4, 'INACTIVE'::STATUS
|
||||||
|
FROM company_hq
|
||||||
|
RETURNING id)
|
||||||
|
SELECT NULL;
|
||||||
|
|
||||||
--- Positions Table ---
|
--- Positions Table ---
|
||||||
DROP TABLE IF EXISTS positions CASCADE;
|
DROP TABLE IF EXISTS positions CASCADE;
|
||||||
@@ -87,27 +114,30 @@ CREATE INDEX positions_code_index ON positions (code);
|
|||||||
CREATE INDEX positions_name_code_index ON positions (name, code);
|
CREATE INDEX positions_name_code_index ON positions (name, code);
|
||||||
|
|
||||||
--- Positions Data Insertion ---
|
--- Positions Data Insertion ---
|
||||||
INSERT INTO positions (id, name, code, description, sort, status)
|
INSERT INTO positions (name, code, description, sort, status)
|
||||||
VALUES (1, 'HR Manager', 'HR-MGR',
|
VALUES ('HR Manager', 'HR-MGR',
|
||||||
'Responsible for overseeing recruitment, employee relations, and staff wellbeing.', 1,
|
'Responsible for overseeing recruitment, employee relations, and staff wellbeing.', 1,
|
||||||
'ACTIVE'),
|
'ACTIVE'),
|
||||||
(2, 'Finance Officer', 'FIN-OFC',
|
('Finance Officer', 'FIN-OFC',
|
||||||
'Handles accounts, prepares financial statements, and ensures compliance with regulations.',
|
'Handles accounts, prepares financial statements, and ensures compliance with regulations.',
|
||||||
2, 'ACTIVE'),
|
2, 'ACTIVE'),
|
||||||
(3, 'IT Support Specialist', 'IT-SPT',
|
('IT Support Specialist', 'IT-SPT',
|
||||||
'Provides technical assistance, manages helpdesk queries, and maintains computer systems.',
|
'Provides technical assistance, manages helpdesk queries, and maintains computer systems.',
|
||||||
3, 'ACTIVE'),
|
3, 'ACTIVE'),
|
||||||
(4, 'Software Engineer', 'SWE-ENG',
|
('Software Engineer', 'SWE-ENG',
|
||||||
'Develops and maintains in-house applications, ensuring code quality and system reliability.',
|
'Develops and maintains in-house applications, ensuring code quality and system reliability.',
|
||||||
4, 'ACTIVE'),
|
4, 'ACTIVE'),
|
||||||
(5, 'Operations Coordinator', 'OPS-CRD',
|
('Operations Coordinator', 'OPS-CRD',
|
||||||
'Assists with day-to-day logistics, procurement, and office organisation.', 5, 'INACTIVE');
|
'Assists with day-to-day logistics, procurement, and office organisation.', 5, 'INACTIVE');
|
||||||
|
|
||||||
--- Users Table ---
|
--- Users Table ---
|
||||||
DROP TABLE IF EXISTS users CASCADE;
|
DROP TABLE IF EXISTS users CASCADE;
|
||||||
|
DROP SEQUENCE IF EXISTS users_id_seq CASCADE;
|
||||||
|
CREATE SEQUENCE users_id_seq START WITH 1;
|
||||||
|
|
||||||
CREATE TABLE users
|
CREATE TABLE users
|
||||||
(
|
(
|
||||||
id BIGINT PRIMARY KEY,
|
id BIGINT PRIMARY KEY DEFAULT nextval('users_id_seq'),
|
||||||
username VARCHAR(64) UNIQUE NOT NULL,
|
username VARCHAR(64) UNIQUE NOT NULL,
|
||||||
password VARCHAR(255),
|
password VARCHAR(255),
|
||||||
full_name VARCHAR(128) NOT NULL,
|
full_name VARCHAR(128) NOT NULL,
|
||||||
@@ -127,14 +157,33 @@ CREATE INDEX users_username_index ON users (username);
|
|||||||
|
|
||||||
--- Users Data Insertion ---
|
--- Users Data Insertion ---
|
||||||
-- NOTE: All phone numbers are generated by ChatGPT, they should not be connected any real person.
|
-- NOTE: All phone numbers are generated by ChatGPT, they should not be connected any real person.
|
||||||
INSERT INTO users(id, username, password, full_name, email, region_abbreviation, phone_number,
|
INSERT INTO users(username, password, full_name, email, region_abbreviation, phone_number,
|
||||||
avatar_url, department_id, position_id, created_at, updated_at)
|
avatar_url, department_id, position_id, created_at, updated_at)
|
||||||
VALUES (1, 'helix', null, 'Helix Admin', 'admin@helix.onixbyte.dev', 'GB', '7000000000',
|
SELECT 'helix',
|
||||||
|
null,
|
||||||
|
'Helix Admin',
|
||||||
|
'admin@helix.onixbyte.dev',
|
||||||
|
'GB',
|
||||||
|
'7000000000',
|
||||||
'https://gravatar.com/avatar/6ef4c4033f6aa8e43d06bd5e462a6173cc2a960633473721a6f1289cd1b5146f',
|
'https://gravatar.com/avatar/6ef4c4033f6aa8e43d06bd5e462a6173cc2a960633473721a6f1289cd1b5146f',
|
||||||
1, 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
|
(SELECT id FROM departments WHERE name = 'Company HQ'),
|
||||||
(2, 'johndoe', null, 'John Doe', 'johndoe@helix.onixbyte.dev', 'GB', '7000000001',
|
(SELECT id FROM positions WHERE code = 'HR-MGR'),
|
||||||
|
CURRENT_TIMESTAMP,
|
||||||
|
CURRENT_TIMESTAMP;
|
||||||
|
|
||||||
|
INSERT INTO users(username, password, full_name, email, region_abbreviation, phone_number,
|
||||||
|
avatar_url, department_id, position_id, created_at, updated_at)
|
||||||
|
SELECT 'johndoe',
|
||||||
|
null,
|
||||||
|
'John Doe',
|
||||||
|
'johndoe@helix.onixbyte.dev',
|
||||||
|
'GB',
|
||||||
|
'7000000001',
|
||||||
'https://gravatar.com/avatar/41bcebddd573747d1bd35ef7fae72ebefd6b47f077d42442a2510d35b0c2db92',
|
'https://gravatar.com/avatar/41bcebddd573747d1bd35ef7fae72ebefd6b47f077d42442a2510d35b0c2db92',
|
||||||
6, 4, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
|
(SELECT id FROM departments WHERE name = 'Software Development'),
|
||||||
|
(SELECT id FROM positions WHERE code = 'SWE-ENG'),
|
||||||
|
CURRENT_TIMESTAMP,
|
||||||
|
CURRENT_TIMESTAMP;
|
||||||
|
|
||||||
--- User Identities Table ---
|
--- User Identities Table ---
|
||||||
DROP TABLE IF EXISTS user_identities CASCADE;
|
DROP TABLE IF EXISTS user_identities CASCADE;
|
||||||
@@ -187,9 +236,12 @@ CREATE TABLE user_roles
|
|||||||
);
|
);
|
||||||
|
|
||||||
--- User Roles Data Insertion ---
|
--- User Roles Data Insertion ---
|
||||||
INSERT INTO user_roles
|
INSERT INTO user_roles (user_id, role_id)
|
||||||
VALUES (1, 1),
|
SELECT u.id, r.id
|
||||||
(2, 2);
|
FROM users u
|
||||||
|
CROSS JOIN roles r
|
||||||
|
WHERE (u.username = 'helix' AND r.code = 'admin')
|
||||||
|
OR (u.username = 'johndoe' AND r.code = 'user');
|
||||||
|
|
||||||
--- Authorities Table ---
|
--- Authorities Table ---
|
||||||
DROP TABLE IF EXISTS authorities CASCADE;
|
DROP TABLE IF EXISTS authorities CASCADE;
|
||||||
@@ -247,27 +299,26 @@ CREATE TABLE role_authorities
|
|||||||
);
|
);
|
||||||
|
|
||||||
--- Role Authorities Data Insertion ---
|
--- Role Authorities Data Insertion ---
|
||||||
INSERT INTO role_authorities
|
-- Admin role gets all authorities
|
||||||
VALUES (1, 1, CURRENT_TIMESTAMP),
|
INSERT INTO role_authorities (role_id, authority_id, created_at)
|
||||||
(1, 2, CURRENT_TIMESTAMP),
|
SELECT r.id, a.id, CURRENT_TIMESTAMP
|
||||||
(1, 3, CURRENT_TIMESTAMP),
|
FROM roles r
|
||||||
(1, 4, CURRENT_TIMESTAMP),
|
CROSS JOIN authorities a
|
||||||
(1, 5, CURRENT_TIMESTAMP),
|
WHERE r.code = 'admin';
|
||||||
(1, 6, CURRENT_TIMESTAMP),
|
|
||||||
(1, 7, CURRENT_TIMESTAMP),
|
-- Normal user role gets specific authorities
|
||||||
(1, 8, CURRENT_TIMESTAMP),
|
INSERT INTO role_authorities (role_id, authority_id, created_at)
|
||||||
(1, 9, CURRENT_TIMESTAMP),
|
SELECT r.id, a.id, CURRENT_TIMESTAMP
|
||||||
(1, 10, CURRENT_TIMESTAMP),
|
FROM roles r
|
||||||
(1, 11, CURRENT_TIMESTAMP),
|
CROSS JOIN authorities a
|
||||||
(1, 12, CURRENT_TIMESTAMP),
|
WHERE r.code = 'user'
|
||||||
(1, 13, CURRENT_TIMESTAMP),
|
AND a.code IN ('system:dashboard:read',
|
||||||
(2, 1, CURRENT_TIMESTAMP),
|
'system:user:read',
|
||||||
(2, 2, CURRENT_TIMESTAMP),
|
'system:user_detail:read',
|
||||||
(2, 3, CURRENT_TIMESTAMP),
|
'system:department:read',
|
||||||
(2, 5, CURRENT_TIMESTAMP),
|
'system:role:read',
|
||||||
(2, 7, CURRENT_TIMESTAMP),
|
'system:authority:read',
|
||||||
(2, 9, CURRENT_TIMESTAMP),
|
'system:audit_log:read');
|
||||||
(2, 11, CURRENT_TIMESTAMP);
|
|
||||||
|
|
||||||
DROP TABLE IF EXISTS assets;
|
DROP TABLE IF EXISTS assets;
|
||||||
CREATE TABLE assets
|
CREATE TABLE assets
|
||||||
|
|||||||
Reference in New Issue
Block a user