Antes: 010_seed_super_admin.sql tinha hash bcrypt fixo amarrado a um pepper específico. Qualquer mudança no PASSWORD_PEPPER quebrava todos os logins silenciosamente após reset do banco. Agora: - migration 010: insere superadmin com placeholder inválido + force_change_password. ON CONFLICT DO NOTHING preserva o hash se o seeder já rodou. - seeder users.js: faz upsert de 'lol' com bcrypt(senha + env.PASSWORD_PEPPER) em runtime. Mudar o pepper e re-rodar o seeder é suficiente para atualizar as credenciais sem tocar em nenhuma migration. - docs/AGENTS.md: atualiza gotcha #1 explicando o novo fluxo migrate → seed - docs/DEVOPS.md: fix opção 1 do troubleshooting inclui re-deploy do seeder Fluxo correto após reset do banco (coberto pelo start.sh opções 2, 6, 8): npm run migrate → superadmin criado, hash = placeholder npm run seed → hash recalculado com PEPPER do ambiente, status = active Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
52 lines
2 KiB
SQL
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',
|
|
'force_change_password',
|
|
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 $$;
|
|
|