59 lines
2.5 KiB
SQL
59 lines
2.5 KiB
SQL
-- Initialize company setup (companies, statuses, folders) and core user management.
|
|
-- Designed for multi-tenant applications where each company has isolated data.
|
|
|
|
-- Companies Table: Stores information about each client company.
|
|
CREATE TABLE IF NOT EXISTS companies (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
name VARCHAR(255) NOT NULL UNIQUE,
|
|
uprn_required BOOLEAN DEFAULT FALSE,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
|
|
-- Job Statuses Table: Stores custom job statuses defined by each company.
|
|
CREATE TABLE IF NOT EXISTS job_statuses (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
company_id INT NOT NULL,
|
|
name VARCHAR(255) NOT NULL,
|
|
is_default BOOLEAN DEFAULT FALSE,
|
|
sort_order INT DEFAULT 0,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (company_id) REFERENCES companies(id) ON DELETE CASCADE,
|
|
UNIQUE KEY (company_id, name)
|
|
);
|
|
|
|
-- Required Folders Table: Stores mandatory folder structures defined by each company.
|
|
CREATE TABLE IF NOT EXISTS required_folders (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
company_id INT NOT NULL,
|
|
name VARCHAR(255) NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (company_id) REFERENCES companies(id) ON DELETE CASCADE,
|
|
UNIQUE KEY (company_id, name)
|
|
);
|
|
|
|
-- Users Table: Stores user accounts. Each user belongs to a specific company.
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
company_id INT NOT NULL,
|
|
name VARCHAR(255) NOT NULL,
|
|
email VARCHAR(255) NOT NULL UNIQUE,
|
|
password VARCHAR(255) NOT NULL,
|
|
role ENUM('admin', 'standard') DEFAULT 'standard', -- Admin can manage company settings, standard users manage jobs.
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (company_id) REFERENCES companies(id) ON DELETE CASCADE
|
|
);
|
|
|
|
-- Clients Table: Stores clients for each company. Clients can be added, edited, but not deleted.
|
|
CREATE TABLE IF NOT EXISTS clients (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
company_id INT NOT NULL,
|
|
name VARCHAR(255) NOT NULL,
|
|
contact_person VARCHAR(255) DEFAULT NULL,
|
|
email VARCHAR(255) DEFAULT NULL,
|
|
phone VARCHAR(255) DEFAULT NULL,
|
|
is_active BOOLEAN DEFAULT TRUE, -- Clients can be marked inactive instead of deleted.
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (company_id) REFERENCES companies(id) ON DELETE CASCADE,
|
|
UNIQUE KEY (company_id, name) -- Ensure client names are unique per company
|
|
); |