gohorsejobs/backend/migrations/009_unify_schema.sql

33 lines
1.3 KiB
SQL

-- Migration: Unify schema - add missing fields to users table
-- Description: Add fields from core_users to users table for unified architecture
-- Add tenant_id (references companies.id)
ALTER TABLE users ADD COLUMN IF NOT EXISTS tenant_id UUID REFERENCES companies(id);
-- Add email if not exists (core_users had this)
ALTER TABLE users ADD COLUMN IF NOT EXISTS email VARCHAR(255);
-- Add name if not exists (mapped from full_name or separate)
ALTER TABLE users ADD COLUMN IF NOT EXISTS name VARCHAR(255);
-- Add status field for compatibility
ALTER TABLE users ADD COLUMN IF NOT EXISTS status VARCHAR(20) DEFAULT 'active';
-- Create user_roles table (replaces core_user_roles)
CREATE TABLE IF NOT EXISTS user_roles (
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
role VARCHAR(50) NOT NULL,
PRIMARY KEY (user_id, role)
);
-- Index for user_roles
CREATE INDEX IF NOT EXISTS idx_user_roles_user_id ON user_roles(user_id);
CREATE INDEX IF NOT EXISTS idx_user_roles_role ON user_roles(role);
-- Index for tenant lookup
CREATE INDEX IF NOT EXISTS idx_users_tenant_id ON users(tenant_id);
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
-- Comments
COMMENT ON COLUMN users.tenant_id IS 'Company ID this user belongs to (NULL for superadmin)';
COMMENT ON TABLE user_roles IS 'Additional roles for users (e.g., admin, recruiter)';