26 lines
957 B
JavaScript
26 lines
957 B
JavaScript
const { PrismaClient } = require('@prisma/client');
|
|
const prisma = new PrismaClient();
|
|
(async () => {
|
|
const dups = await prisma.part.groupBy({
|
|
by: ['name', 'carId'],
|
|
where: { isGlobal: false },
|
|
_count: { id: true },
|
|
having: { id: { _count: { gt: 1 } } },
|
|
});
|
|
console.log('duplicate groups', dups.length);
|
|
for (const g of dups) {
|
|
const rows = await prisma.part.findMany({
|
|
where: { name: g.name, carId: g.carId, isGlobal: false },
|
|
orderBy: { createdAt: 'asc' },
|
|
});
|
|
const keep = rows[0];
|
|
const removeIds = rows.slice(1).map(r => r.id);
|
|
console.log('keep', keep.id, 'remove', removeIds, 'for', g.name, g.carId);
|
|
for (const id of removeIds) {
|
|
await prisma.taskPart.updateMany({ where: { partId: id }, data: { partId: keep.id } });
|
|
await prisma.part.delete({ where: { id } });
|
|
}
|
|
}
|
|
console.log('done deduping private parts');
|
|
})().catch(e => { console.error(e); process.exit(1); });
|