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