gohorsejobs/backend/migrations/010_seed_super_admin.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

52 lines
2 KiB
SQL

-- Migration: Create Super Admin and System Company
-- Description: Inserts the default System Company and Super Admin user.
-- Uses unified tables (companies, users, user_roles)
--
-- ⚠️ SEM HASH HARDCODED — o hash é gerado em runtime pelo seeder-api.
-- Motivo: bcrypt(password + pepper) depende do valor de PASSWORD_PEPPER
-- que varia por ambiente. Hardcodar o hash aqui amarraria o deploy a um
-- pepper específico e quebraria logins silenciosamente se o pepper mudar.
--
-- Fluxo correto após reset do banco:
-- 1. npm run migrate → cria o usuário com senha bloqueada (placeholder)
-- 2. npm run seed → gera o hash correto e ativa o usuário
--
-- O status 'force_change_password' sinaliza que o hash ainda não foi
-- definido pelo seeder. O usuário NÃO consegue logar antes do seed.
-- 1. Insert System Company (for SuperAdmin context)
INSERT INTO companies (name, slug, type, document, email, description, verified, active)
VALUES (
'GoHorse System',
'gohorse-system',
'system',
'00.000.000/0001-91',
'admin@gohorsejobs.com',
'{"tagline": "System Administration Tenant"}',
true,
true
) ON CONFLICT (slug) DO NOTHING;
-- 2. Insert Super Admin User (sem hash — seeder define o hash em runtime)
INSERT INTO users (identifier, password_hash, role, full_name, email, status, active)
VALUES (
'superadmin',
'$invalid-placeholder-run-seeder$',
'superadmin',
'Super Administrator',
'admin@gohorsejobs.com',
'pending',
true
) ON CONFLICT (identifier) DO NOTHING;
-- ON CONFLICT DO NOTHING: não sobrescreve se o seeder já definiu o hash.
-- 3. Assign superadmin role (if user_roles table exists)
DO $$
BEGIN
IF EXISTS (SELECT FROM pg_tables WHERE tablename = 'user_roles') THEN
INSERT INTO user_roles (user_id, role)
SELECT id, 'superadmin' FROM users WHERE identifier = 'superadmin'
ON CONFLICT (user_id, role) DO NOTHING;
END IF;
END $$;