Uecko_ERP/docs/dev/seed-local-admin.sql

159 lines
3.3 KiB
MySQL
Raw Permalink Normal View History

2026-07-06 11:51:53 +00:00
-- ============================================================
-- seed-local-admin.sql
-- ERP Backend - Local development seed
-- ============================================================
--
-- Purpose:
-- Creates a local admin account, one active company and the
-- active membership between them.
2026-07-06 15:47:05 +00:00
-- Tables touched:
-- - identity_accounts
-- - companies
-- - identity_company_memberships
2026-07-06 11:51:53 +00:00
--
-- Intended use:
-- Local/dev environments only.
--
-- Credentials:
-- email: admin@local.test
-- password: password
2026-07-06 11:51:53 +00:00
--
-- Generate bcrypt hash from the workspace:
--
-- node -e "const bcrypt=require('bcrypt'); bcrypt.hash('Admin123!', 10).then(console.log)"
--
2026-07-06 12:04:47 +00:00
-- Replace the hash below if you want to regenerate it.
2026-07-06 11:51:53 +00:00
-- This script is idempotent:
-- - It does not duplicate the admin account if the email exists.
-- - It does not duplicate the company if the slug exists.
-- - It does not duplicate the membership if it already exists.
-- ============================================================
SET @admin_id = UUID();
2026-07-06 12:04:47 +00:00
SET @company_id = UUID();
2026-07-06 11:51:53 +00:00
SET @admin_email = 'admin@local.test';
SET
@admin_password_hash = '$2a$10$BP8gyjtjx4hjDtWXMRLm1unogTE82Ja8mdF.rPNvZWliaaVoZvSBW';
-- password
2026-07-06 11:51:53 +00:00
2026-07-06 12:04:47 +00:00
SET @company_slug = 'rodax';
2026-07-06 12:04:47 +00:00
SET @company_legal_name = 'Rodax Software S.L.';
2026-07-06 12:04:47 +00:00
SET @company_trade_name = 'Rodax';
2026-07-06 11:51:53 +00:00
INSERT INTO
identity_accounts (
id,
email,
password_hash,
name,
avatar_url,
language_code,
status,
created_at,
updated_at
)
SELECT
@admin_id,
@admin_email,
@admin_password_hash,
'Administrador',
NULL,
'es',
'active',
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP
WHERE
NOT EXISTS (
SELECT 1
FROM identity_accounts
WHERE
email = @admin_email
);
SELECT id INTO @admin_id
FROM identity_accounts
WHERE
email = @admin_email
LIMIT 1;
INSERT INTO
companies (
id,
legal_name,
trade_name,
tin,
slug,
email,
phone,
website,
status,
created_at,
updated_at
)
SELECT
@company_id,
@company_legal_name,
@company_trade_name,
NULL,
@company_slug,
@admin_email,
NULL,
NULL,
'active',
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP
WHERE
NOT EXISTS (
SELECT 1
FROM companies
WHERE
slug = @company_slug
);
SELECT id INTO @company_id
FROM companies
WHERE
slug = @company_slug
LIMIT 1;
INSERT INTO
identity_company_memberships (
id,
account_id,
company_id,
status,
created_at,
updated_at
)
SELECT UUID(), @admin_id, @company_id, 'active', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE
NOT EXISTS (
SELECT 1
FROM identity_company_memberships
WHERE
account_id = @admin_id
AND company_id = @company_id
);
SELECT
a.id AS account_id,
a.email,
a.status AS account_status,
c.id AS company_id,
c.legal_name,
c.trade_name,
c.slug,
c.status AS company_status,
m.status AS membership_status
FROM
identity_accounts a
JOIN identity_company_memberships m ON m.account_id = a.id
JOIN companies c ON c.id = m.company_id
WHERE
a.email = @admin_email
AND c.slug = @company_slug;