gohorsejobs/backend/migrations/009_unify_schema.sql
Tiago Yamamoto e5e43974a5 fix(migrations): increase users.status VARCHAR(20→30), fix 010 status value
- 009: status column was VARCHAR(20), causing 'force_change_password' (21 chars)
  to fail on INSERT — changed to VARCHAR(30)
- 010: changed initial status from 'force_change_password' to 'pending' (fits
  any column size ≥7 chars, avoids future truncation)
- 046: ALTER TABLE for existing deployments where 009 already applied with VARCHAR(20)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-23 21:17:34 -06:00

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(30) 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)';