fix(seeder): add SSL support and fix foreign key violations
- Add SSL configuration to database pool supporting DB_SSLMODE=require - Fix user seeder FK violations by using RETURNING clause to get actual IDs - Update all user creation queries (superadmin, company admins, candidates) - Ensure correct user_id references when ON CONFLICT DO UPDATE occurs This fixes the seeder hanging issue and foreign key constraint violations that were preventing successful database population.
This commit is contained in:
parent
18ac6d74f0
commit
aab2de7534
3 changed files with 22 additions and 11 deletions
|
|
@ -1,5 +1,6 @@
|
|||
DB_HOST=172.28.22.171
|
||||
DB_PORT=5432
|
||||
DB_USER=usuario
|
||||
DB_PASSWORD=senha123
|
||||
DB_NAME=gohorsejobs
|
||||
DB_HOST=db-60059.dc-sp-1.absamcloud.com
|
||||
DB_PORT=26868
|
||||
DB_USER=yuki
|
||||
DB_PASSWORD=xl1zfmr6e9bb
|
||||
DB_NAME=gohorsejobs_dev
|
||||
DB_SSLMODE=require
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ export const pool = new Pool({
|
|||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.DB_NAME,
|
||||
ssl: process.env.DB_SSLMODE === 'require' ? { rejectUnauthorized: false } : false,
|
||||
});
|
||||
|
||||
// Test database connection
|
||||
|
|
|
|||
|
|
@ -19,18 +19,21 @@ export async function seedUsers() {
|
|||
// User requested identifier 'superadmin'
|
||||
const superAdminIdentifier = 'superadmin';
|
||||
|
||||
await pool.query(`
|
||||
const result = await pool.query(`
|
||||
INSERT INTO core_users (id, tenant_id, name, email, password_hash, status)
|
||||
VALUES ($1, $2, $3, $4, $5, 'ACTIVE')
|
||||
ON CONFLICT (tenant_id, email) DO UPDATE SET password_hash = EXCLUDED.password_hash
|
||||
RETURNING id
|
||||
`, [superAdminId, systemTenantId, 'System Administrator', superAdminIdentifier, superAdminPassword]);
|
||||
|
||||
const actualSuperAdminId = result.rows[0].id;
|
||||
|
||||
// Role
|
||||
await pool.query(`
|
||||
INSERT INTO core_user_roles (user_id, role)
|
||||
VALUES ($1, 'superadmin')
|
||||
ON CONFLICT (user_id, role) DO NOTHING
|
||||
`, [superAdminId]);
|
||||
`, [actualSuperAdminId]);
|
||||
|
||||
console.log(' ✓ SuperAdmin created (superadmin)');
|
||||
|
||||
|
|
@ -53,18 +56,21 @@ export async function seedUsers() {
|
|||
continue;
|
||||
}
|
||||
|
||||
await pool.query(`
|
||||
const result = await pool.query(`
|
||||
INSERT INTO core_users (id, tenant_id, name, email, password_hash, status)
|
||||
VALUES ($1, $2, $3, $4, $5, 'ACTIVE')
|
||||
ON CONFLICT (tenant_id, email) DO UPDATE SET password_hash = EXCLUDED.password_hash
|
||||
RETURNING id
|
||||
`, [userId, tenantId, admin.fullName, admin.email, hash]);
|
||||
|
||||
const actualUserId = result.rows[0].id;
|
||||
|
||||
for (const role of admin.roles) {
|
||||
await pool.query(`
|
||||
INSERT INTO core_user_roles (user_id, role)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT (user_id, role) DO NOTHING
|
||||
`, [userId, role]);
|
||||
`, [actualUserId, role]);
|
||||
}
|
||||
|
||||
console.log(` ✓ User created: ${admin.email}`);
|
||||
|
|
@ -81,17 +87,20 @@ export async function seedUsers() {
|
|||
const hash = await bcrypt.hash(cand.pass, 10);
|
||||
const userId = crypto.randomUUID();
|
||||
|
||||
await pool.query(`
|
||||
const result = await pool.query(`
|
||||
INSERT INTO core_users (id, tenant_id, name, email, password_hash, status)
|
||||
VALUES ($1, $2, $3, $4, $5, 'ACTIVE')
|
||||
ON CONFLICT (tenant_id, email) DO UPDATE SET password_hash = EXCLUDED.password_hash
|
||||
RETURNING id
|
||||
`, [userId, systemTenantId, cand.fullName, cand.email, hash]);
|
||||
|
||||
const actualUserId = result.rows[0].id;
|
||||
|
||||
await pool.query(`
|
||||
INSERT INTO core_user_roles (user_id, role)
|
||||
VALUES ($1, 'candidate')
|
||||
ON CONFLICT (user_id, role) DO NOTHING
|
||||
`, [userId]);
|
||||
`, [actualUserId]);
|
||||
console.log(` ✓ Candidate created: ${cand.email}`);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue