(Fix): init-schema.sql - was corrupted to directory by Docker

This commit is contained in:
2025-11-28 09:56:24 +00:00
parent 00f9ed128b
commit 25ed1d5c56

View File

@@ -1,135 +1,101 @@
-- Work Allocation System Database Schema -- Work Allocation System Database Schema
-- This version is for Docker init (database already created by environment variables) -- MySQL 8.0
-- Departments table -- Create departments table
CREATE TABLE IF NOT EXISTS departments ( CREATE TABLE IF NOT EXISTS departments (
id INT PRIMARY KEY AUTO_INCREMENT, id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE, name VARCHAR(100) NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
); );
-- Sub-departments table (for Groundnut department) -- Create sub_departments table
CREATE TABLE IF NOT EXISTS sub_departments ( CREATE TABLE IF NOT EXISTS sub_departments (
id INT PRIMARY KEY AUTO_INCREMENT, id INT AUTO_INCREMENT PRIMARY KEY,
department_id INT NOT NULL, department_id INT NOT NULL,
name VARCHAR(200) NOT NULL, name VARCHAR(100) NOT NULL,
primary_activity VARCHAR(200) NOT NULL, primary_activity VARCHAR(255),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, FOREIGN KEY (department_id) REFERENCES departments(id) ON DELETE CASCADE,
FOREIGN KEY (department_id) REFERENCES departments(id) ON DELETE CASCADE UNIQUE KEY unique_subdept (department_id, name)
); );
-- Users table (for all roles: SuperAdmin, Supervisor, Contractor, Employee) -- Create users table
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS users (
id INT PRIMARY KEY AUTO_INCREMENT, id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(100) NOT NULL UNIQUE, username VARCHAR(50) NOT NULL UNIQUE,
name VARCHAR(200) NOT NULL, name VARCHAR(100) NOT NULL,
email VARCHAR(200) NOT NULL UNIQUE, email VARCHAR(100) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL,
role ENUM('SuperAdmin', 'Supervisor', 'Contractor', 'Employee') NOT NULL, role ENUM('SuperAdmin', 'Supervisor', 'Contractor', 'Employee') NOT NULL,
department_id INT, department_id INT,
contractor_id INT, contractor_id INT,
is_active BOOLEAN DEFAULT TRUE, is_active BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (department_id) REFERENCES departments(id) ON DELETE SET NULL, FOREIGN KEY (department_id) REFERENCES departments(id) ON DELETE SET NULL,
FOREIGN KEY (contractor_id) REFERENCES users(id) ON DELETE SET NULL FOREIGN KEY (contractor_id) REFERENCES users(id) ON DELETE SET NULL
); );
-- Contractor rates table (rates per contractor + sub-department combination) -- Create work_allocations table
CREATE TABLE IF NOT EXISTS contractor_rates (
id INT PRIMARY KEY AUTO_INCREMENT,
contractor_id INT NOT NULL,
sub_department_id INT,
activity VARCHAR(200),
rate DECIMAL(10, 2) NOT NULL,
effective_date DATE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (contractor_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (sub_department_id) REFERENCES sub_departments(id) ON DELETE SET NULL
);
-- Work allocations table
CREATE TABLE IF NOT EXISTS work_allocations ( CREATE TABLE IF NOT EXISTS work_allocations (
id INT PRIMARY KEY AUTO_INCREMENT, id INT AUTO_INCREMENT PRIMARY KEY,
employee_id INT NOT NULL, employee_id INT NOT NULL,
supervisor_id INT NOT NULL, supervisor_id INT NOT NULL,
contractor_id INT NOT NULL, contractor_id INT NOT NULL,
sub_department_id INT, sub_department_id INT,
activity VARCHAR(100), activity VARCHAR(255),
description TEXT, description TEXT,
assigned_date DATE NOT NULL, assigned_date DATE NOT NULL,
status ENUM('Pending', 'InProgress', 'Completed', 'Cancelled') DEFAULT 'Pending',
completion_date DATE, completion_date DATE,
status ENUM('Pending', 'InProgress', 'Completed', 'Cancelled') DEFAULT 'Pending',
rate DECIMAL(10, 2), rate DECIMAL(10, 2),
units DECIMAL(10, 2), units DECIMAL(10, 2),
total_amount DECIMAL(10, 2), total_amount DECIMAL(10, 2),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (employee_id) REFERENCES users(id) ON DELETE CASCADE, FOREIGN KEY (employee_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (supervisor_id) REFERENCES users(id) ON DELETE CASCADE, FOREIGN KEY (supervisor_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (contractor_id) REFERENCES users(id) ON DELETE CASCADE, FOREIGN KEY (contractor_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (sub_department_id) REFERENCES sub_departments(id) ON DELETE SET NULL FOREIGN KEY (sub_department_id) REFERENCES sub_departments(id) ON DELETE SET NULL
); );
-- Attendance table -- Create attendance table
CREATE TABLE IF NOT EXISTS attendance ( CREATE TABLE IF NOT EXISTS attendance (
id INT PRIMARY KEY AUTO_INCREMENT, id INT AUTO_INCREMENT PRIMARY KEY,
employee_id INT NOT NULL, employee_id INT NOT NULL,
supervisor_id INT NOT NULL, supervisor_id INT NOT NULL,
check_in_time DATETIME NOT NULL, check_in_time DATETIME,
check_out_time DATETIME, check_out_time DATETIME,
work_date DATE NOT NULL, work_date DATE NOT NULL,
status ENUM('CheckedIn', 'CheckedOut') DEFAULT 'CheckedIn', status ENUM('CheckedIn', 'CheckedOut', 'Absent') DEFAULT 'CheckedIn',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (employee_id) REFERENCES users(id) ON DELETE CASCADE, FOREIGN KEY (employee_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (supervisor_id) REFERENCES users(id) ON DELETE CASCADE FOREIGN KEY (supervisor_id) REFERENCES users(id) ON DELETE CASCADE,
UNIQUE KEY unique_attendance (employee_id, work_date)
); );
-- Insert default departments -- Create contractor_rates table
INSERT IGNORE INTO departments (name) VALUES CREATE TABLE IF NOT EXISTS contractor_rates (
('Tudki'), id INT AUTO_INCREMENT PRIMARY KEY,
('Dana'), contractor_id INT NOT NULL,
('Groundnut'); sub_department_id INT,
activity VARCHAR(255),
rate DECIMAL(10, 2) NOT NULL,
effective_date DATE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (contractor_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (sub_department_id) REFERENCES sub_departments(id) ON DELETE SET NULL
);
-- Insert Groundnut sub-departments -- Create indexes for better query performance
INSERT IGNORE INTO sub_departments (department_id, name, primary_activity) CREATE INDEX idx_users_role ON users(role);
SELECT id, 'Mufali Aavak Katai', 'Loading/Unloading' FROM departments WHERE name = 'Groundnut' CREATE INDEX idx_users_department ON users(department_id);
UNION ALL CREATE INDEX idx_users_contractor ON users(contractor_id);
SELECT id, 'Mufali Aavak Dhang', 'Loading/Unloading' FROM departments WHERE name = 'Groundnut' CREATE INDEX idx_work_allocations_employee ON work_allocations(employee_id);
UNION ALL CREATE INDEX idx_work_allocations_supervisor ON work_allocations(supervisor_id);
SELECT id, 'Dhang Se Katai', 'Loading/Unloading' FROM departments WHERE name = 'Groundnut' CREATE INDEX idx_work_allocations_contractor ON work_allocations(contractor_id);
UNION ALL CREATE INDEX idx_work_allocations_date ON work_allocations(assigned_date);
SELECT id, 'Guthli Bori Silai Dhang', 'Loading/Unloading' FROM departments WHERE name = 'Groundnut' CREATE INDEX idx_work_allocations_status ON work_allocations(status);
UNION ALL CREATE INDEX idx_attendance_employee ON attendance(employee_id);
SELECT id, 'Guthali dada Pala Tulai Silai Dhang', 'Loading/Unloading' FROM departments WHERE name = 'Groundnut' CREATE INDEX idx_attendance_date ON attendance(work_date);
UNION ALL CREATE INDEX idx_attendance_status ON attendance(status);
SELECT id, 'Mufali Patthar Bori silai dhang', 'Loading/Unloading' FROM departments WHERE name = 'Groundnut' CREATE INDEX idx_contractor_rates_contractor ON contractor_rates(contractor_id);
UNION ALL CREATE INDEX idx_contractor_rates_date ON contractor_rates(effective_date);
SELECT id, 'Mufali Patthar Bori Utrai', 'Loading/Unloading' FROM departments WHERE name = 'Groundnut'
UNION ALL
SELECT id, 'Bardana Bandal Loading Unloading', 'Loading/Unloading' FROM departments WHERE name = 'Groundnut'
UNION ALL
SELECT id, 'Bardana Gatthi Loading', 'Loading/Unloading' FROM departments WHERE name = 'Groundnut'
UNION ALL
SELECT id, 'Black Dana Loading/Unloading', 'Loading/Unloading' FROM departments WHERE name = 'Groundnut'
UNION ALL
SELECT id, 'Pre Cleaning', 'Pre Cleaning' FROM departments WHERE name = 'Groundnut'
UNION ALL
SELECT id, 'Destoner', 'Destoner' FROM departments WHERE name = 'Groundnut'
UNION ALL
SELECT id, 'Water', 'Water' FROM departments WHERE name = 'Groundnut'
UNION ALL
SELECT id, 'Decordicater', 'Decordicater & Cleaning' FROM departments WHERE name = 'Groundnut'
UNION ALL
SELECT id, 'Round Chalna', 'Round Chalna & Cleaning' FROM departments WHERE name = 'Groundnut'
UNION ALL
SELECT id, 'Cleaning', 'Decordicater & Cleaning' FROM departments WHERE name = 'Groundnut'
UNION ALL
SELECT id, 'Round Chalna No.1', 'Round Chalna No.1' FROM departments WHERE name = 'Groundnut';
-- Note: Admin user will be created by running: npm run seed
-- This ensures the password is properly hashed with bcrypt