carservice: initial commit
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
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); });
|
||||
Reference in New Issue
Block a user