fix(identity-service): 修复 seed 脚本会清除所有用户数据的严重 bug

问题:每次部署 identity-service 时,seed 脚本会执行 deleteMany() 删除所有用户数据
修复:
- 检查现有用户数量,如果 > 5 则跳过 seed(保护生产数据)
- 移除所有 deleteMany() 调用
- 使用 upsert 确保系统账户存在而不是先删后建
- 管理员账户只在不存在时创建

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2025-12-22 03:28:59 -08:00
parent 9bc7bb1200
commit 9b9427d6a8
1 changed files with 52 additions and 42 deletions

View File

@ -61,68 +61,78 @@ const SYSTEM_ACCOUNTS = [
async function main() {
console.log('Seeding database...');
// 清理现有数据
await prisma.deadLetterEvent.deleteMany();
await prisma.smsCode.deleteMany();
await prisma.userEvent.deleteMany();
await prisma.deviceToken.deleteMany();
await prisma.walletAddress.deleteMany();
await prisma.userDevice.deleteMany();
await prisma.userAccount.deleteMany();
// 检查是否已有用户数据,如果有则跳过 seed防止数据丢失
const existingUserCount = await prisma.userAccount.count();
if (existingUserCount > 5) {
console.log(`Database already has ${existingUserCount} users, skipping seed to prevent data loss.`);
console.log('Seed is only intended for initial database setup.');
return;
}
// 初始化账户序列号生成器 (新格式: D + YYMMDD + 5位序号)
await prisma.accountSequenceGenerator.deleteMany();
// 确保账户序列号生成器存在(不删除,使用 upsert
const today = new Date();
const year = String(today.getFullYear()).slice(-2);
const month = String(today.getMonth() + 1).padStart(2, '0');
const day = String(today.getDate()).padStart(2, '0');
const dateKey = `${year}${month}${day}`;
await prisma.accountSequenceGenerator.create({
data: {
id: 1,
dateKey: dateKey,
currentSequence: 0,
},
});
// 只有当序列号生成器不存在时才创建
const existingGenerator = await prisma.accountSequenceGenerator.findFirst();
if (!existingGenerator) {
await prisma.accountSequenceGenerator.create({
data: {
id: 1,
dateKey: dateKey,
currentSequence: 0,
},
});
console.log(`Initialized account sequence generator for date ${dateKey}`);
} else {
console.log(`Account sequence generator already exists (dateKey=${existingGenerator.dateKey}, seq=${existingGenerator.currentSequence})`);
}
// 创建系统账户
console.log('Creating system accounts...');
// 创建系统账户(使用 upsert不会删除现有数据
console.log('Ensuring system accounts exist...');
for (const account of SYSTEM_ACCOUNTS) {
await prisma.userAccount.upsert({
where: { userId: account.userId },
update: account,
update: {
nickname: account.nickname,
referralCode: account.referralCode,
status: account.status,
},
create: account,
});
console.log(` - Created system account: ${account.nickname} (accountSequence=${account.accountSequence})`);
console.log(` - Ensured system account: ${account.nickname} (accountSequence=${account.accountSequence})`);
}
// 创建管理员账户
console.log('Creating admin accounts...');
// 创建管理员账户(使用 upsert不会删除现有数据
console.log('Ensuring admin accounts exist...');
for (const admin of ADMIN_ACCOUNTS) {
const passwordHash = await bcrypt.hash(admin.password, 10);
await prisma.adminAccount.upsert({
const existing = await prisma.adminAccount.findUnique({
where: { email: admin.email },
update: {
passwordHash,
nickname: admin.nickname,
role: admin.role,
},
create: {
email: admin.email,
passwordHash,
nickname: admin.nickname,
role: admin.role,
status: 'ACTIVE',
},
});
console.log(` - Created admin account: ${admin.email} (role=${admin.role})`);
if (!existing) {
const passwordHash = await bcrypt.hash(admin.password, 10);
await prisma.adminAccount.create({
data: {
email: admin.email,
passwordHash,
nickname: admin.nickname,
role: admin.role,
status: 'ACTIVE',
},
});
console.log(` - Created admin account: ${admin.email} (role=${admin.role})`);
} else {
console.log(` - Admin account already exists: ${admin.email}`);
}
}
console.log('Database seeded successfully!');
console.log(`- Initialized account sequence generator for date ${dateKey}`);
console.log(`- Created ${SYSTEM_ACCOUNTS.length} system accounts (S0000000001-S0000000005)`);
console.log(`- Created ${ADMIN_ACCOUNTS.length} admin accounts`);
console.log('Database seed completed successfully!');
console.log(`- System accounts ensured (${SYSTEM_ACCOUNTS.length})`);
console.log(`- Admin accounts ensured (${ADMIN_ACCOUNTS.length})`);
}
main()