From d2b086ce17f124968105c6816fb177022c0427f9 Mon Sep 17 00:00:00 2001 From: siujamo Date: Thu, 29 Jan 2026 09:27:28 +0800 Subject: [PATCH] refactor: enhance department and user data insertion with CTEs and streamline role authority assignments --- database/init.d/init-en_GB.sql | 143 ++++++++++++++++++++++----------- 1 file changed, 97 insertions(+), 46 deletions(-) diff --git a/database/init.d/init-en_GB.sql b/database/init.d/init-en_GB.sql index 231b269..21c2396 100644 --- a/database/init.d/init-en_GB.sql +++ b/database/init.d/init-en_GB.sql @@ -59,14 +59,41 @@ CREATE INDEX departments_name_index ON departments (name); CREATE INDEX departments_parent_id_index ON departments (parent_id); --- Departments Data Insertion --- -INSERT INTO departments (id, name, parent_id, sort, status) -VALUES (1, 'Company HQ', NULL, 1, 'ACTIVE'::STATUS), - (2, 'Human Resources', 1, 1, 'ACTIVE'::STATUS), - (3, 'Finance', 1, 2, 'ACTIVE'::STATUS), - (4, 'Technology', 1, 3, 'ACTIVE'::STATUS), - (5, 'IT Support', 4, 1, 'ACTIVE'::STATUS), - (6, 'Software Development', 4, 2, 'ACTIVE'::STATUS), - (7, 'Operations', 1, 4, 'INACTIVE'::STATUS); +WITH company_hq AS ( + INSERT INTO departments (name, parent_id, sort, status) + VALUES ('Company HQ', NULL, 1, 'ACTIVE'::STATUS) + RETURNING id), + human_resources AS ( + INSERT INTO departments (name, parent_id, sort, status) + SELECT 'Human Resources', company_hq.id, 1, 'ACTIVE'::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 --- 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); --- Positions Data Insertion --- -INSERT INTO positions (id, name, code, description, sort, status) -VALUES (1, 'HR Manager', 'HR-MGR', +INSERT INTO positions (name, code, description, sort, status) +VALUES ('HR Manager', 'HR-MGR', 'Responsible for overseeing recruitment, employee relations, and staff wellbeing.', 1, 'ACTIVE'), - (2, 'Finance Officer', 'FIN-OFC', + ('Finance Officer', 'FIN-OFC', 'Handles accounts, prepares financial statements, and ensures compliance with regulations.', 2, 'ACTIVE'), - (3, 'IT Support Specialist', 'IT-SPT', + ('IT Support Specialist', 'IT-SPT', 'Provides technical assistance, manages helpdesk queries, and maintains computer systems.', 3, 'ACTIVE'), - (4, 'Software Engineer', 'SWE-ENG', + ('Software Engineer', 'SWE-ENG', 'Develops and maintains in-house applications, ensuring code quality and system reliability.', 4, 'ACTIVE'), - (5, 'Operations Coordinator', 'OPS-CRD', + ('Operations Coordinator', 'OPS-CRD', 'Assists with day-to-day logistics, procurement, and office organisation.', 5, 'INACTIVE'); --- Users Table --- 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 ( - id BIGINT PRIMARY KEY, + id BIGINT PRIMARY KEY DEFAULT nextval('users_id_seq'), username VARCHAR(64) UNIQUE NOT NULL, password VARCHAR(255), full_name VARCHAR(128) NOT NULL, @@ -127,14 +157,33 @@ CREATE INDEX users_username_index ON users (username); --- Users Data Insertion --- -- 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) -VALUES (1, 'helix', null, 'Helix Admin', 'admin@helix.onixbyte.dev', 'GB', '7000000000', - 'https://gravatar.com/avatar/6ef4c4033f6aa8e43d06bd5e462a6173cc2a960633473721a6f1289cd1b5146f', - 1, 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP), - (2, 'johndoe', null, 'John Doe', 'johndoe@helix.onixbyte.dev', 'GB', '7000000001', - 'https://gravatar.com/avatar/41bcebddd573747d1bd35ef7fae72ebefd6b47f077d42442a2510d35b0c2db92', - 6, 4, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP); +SELECT 'helix', + null, + 'Helix Admin', + 'admin@helix.onixbyte.dev', + 'GB', + '7000000000', + 'https://gravatar.com/avatar/6ef4c4033f6aa8e43d06bd5e462a6173cc2a960633473721a6f1289cd1b5146f', + (SELECT id FROM departments WHERE name = 'Company HQ'), + (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', + (SELECT id FROM departments WHERE name = 'Software Development'), + (SELECT id FROM positions WHERE code = 'SWE-ENG'), + CURRENT_TIMESTAMP, + CURRENT_TIMESTAMP; --- User Identities Table --- DROP TABLE IF EXISTS user_identities CASCADE; @@ -187,9 +236,12 @@ CREATE TABLE user_roles ); --- User Roles Data Insertion --- -INSERT INTO user_roles -VALUES (1, 1), - (2, 2); +INSERT INTO user_roles (user_id, role_id) +SELECT u.id, r.id +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 --- DROP TABLE IF EXISTS authorities CASCADE; @@ -247,27 +299,26 @@ CREATE TABLE role_authorities ); --- Role Authorities Data Insertion --- -INSERT INTO role_authorities -VALUES (1, 1, CURRENT_TIMESTAMP), - (1, 2, CURRENT_TIMESTAMP), - (1, 3, CURRENT_TIMESTAMP), - (1, 4, CURRENT_TIMESTAMP), - (1, 5, CURRENT_TIMESTAMP), - (1, 6, CURRENT_TIMESTAMP), - (1, 7, CURRENT_TIMESTAMP), - (1, 8, CURRENT_TIMESTAMP), - (1, 9, CURRENT_TIMESTAMP), - (1, 10, CURRENT_TIMESTAMP), - (1, 11, CURRENT_TIMESTAMP), - (1, 12, CURRENT_TIMESTAMP), - (1, 13, CURRENT_TIMESTAMP), - (2, 1, CURRENT_TIMESTAMP), - (2, 2, CURRENT_TIMESTAMP), - (2, 3, CURRENT_TIMESTAMP), - (2, 5, CURRENT_TIMESTAMP), - (2, 7, CURRENT_TIMESTAMP), - (2, 9, CURRENT_TIMESTAMP), - (2, 11, CURRENT_TIMESTAMP); +-- Admin role gets all authorities +INSERT INTO role_authorities (role_id, authority_id, created_at) +SELECT r.id, a.id, CURRENT_TIMESTAMP +FROM roles r + CROSS JOIN authorities a +WHERE r.code = 'admin'; + +-- Normal user role gets specific authorities +INSERT INTO role_authorities (role_id, authority_id, created_at) +SELECT r.id, a.id, CURRENT_TIMESTAMP +FROM roles r + CROSS JOIN authorities a +WHERE r.code = 'user' + AND a.code IN ('system:dashboard:read', + 'system:user:read', + 'system:user_detail:read', + 'system:department:read', + 'system:role:read', + 'system:authority:read', + 'system:audit_log:read'); DROP TABLE IF EXISTS assets; CREATE TABLE assets