nxtgauge-backend-rust/crates/db/migrations/20260402030000_strict_employee_separation.up.sql
Ashwin Kumar 3935277fb7 Fix backend compile errors after schema migrations
- employees.rs: rewrite for new standalone schema (email/password_hash,
  no user_id/role_id FK — matches 20260402030000 migration)
- migration: DROP old employees table before CREATE (old schema incompatible)
- pricing.rs: merge if-else sqlx::query! branches into single nullable param query
- kb.rs: fix target_roles Option<Vec<String>> unwrap, category_id Some() wrapping
- support.rs: fix .or() call with non-optional user_email (use Some())
- roles.rs: fix employees JOIN from role_id (deleted) to role_code

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-03 02:25:47 +02:00

36 lines
1.5 KiB
SQL

-- UP: 20260402030000_strict_employee_separation.up.sql
-- Drop old employees table (was linked to users — replacing with standalone auth)
DROP TABLE IF EXISTS employees CASCADE;
-- 1. EMPLOYEES (Standalone Table - Not Linked to 'users')
CREATE TABLE employees (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
employee_code VARCHAR(50) UNIQUE,
department_id UUID REFERENCES departments(id) ON DELETE SET NULL,
designation_id UUID REFERENCES designations(id) ON DELETE SET NULL,
role_code VARCHAR(50) NOT NULL DEFAULT 'STAFF',
status VARCHAR(50) NOT NULL DEFAULT 'ACTIVE',
joined_at DATE NOT NULL DEFAULT CURRENT_DATE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- 2. EMPLOYEE SESSIONS (Standalone Auth)
CREATE TABLE IF NOT EXISTS employee_sessions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
employee_id UUID NOT NULL REFERENCES employees(id) ON DELETE CASCADE,
token_hash VARCHAR(255) UNIQUE NOT NULL,
expires_at TIMESTAMPTZ NOT NULL,
revoked BOOLEAN NOT NULL DEFAULT false,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- Indexes
CREATE INDEX IF NOT EXISTS idx_employees_email ON employees(email);
CREATE INDEX IF NOT EXISTS idx_employees_status ON employees(status);
CREATE INDEX IF NOT EXISTS idx_employee_sessions_token ON employee_sessions(token_hash);