This commit is contained in:
Sebastien Castiel
2023-12-11 11:39:04 -05:00
parent 69fa008ea4
commit 9f7ea9ca50
4 changed files with 50 additions and 40 deletions

View File

@@ -10,6 +10,9 @@ async function main() {
withClient(async (client) => {
const prisma = await getPrisma()
// console.log('Deleting all groups…')
// await prisma.group.deleteMany({})
const { rows: groupRows } = await client.query<{
id: string
name: string
@@ -17,6 +20,10 @@ async function main() {
created_at: Date
}>('select id, name, currency, created_at from groups')
const existingGroups = (
await prisma.group.findMany({ select: { id: true } })
).map((group) => group.id)
for (const groupRow of groupRows) {
const participants: Prisma.ParticipantCreateManyInput[] = []
const expenses: Prisma.ExpenseCreateManyInput[] = []
@@ -24,10 +31,7 @@ async function main() {
const participantIdsMapping: Record<number, string> = {}
const expenseIdsMapping: Record<number, string> = {}
const existingGroup = await prisma.group.findUnique({
where: { id: groupRow.id },
})
if (existingGroup) {
if (existingGroups.includes(groupRow.id)) {
console.log(`Group ${groupRow.id} already exists, skipping.`)
continue
}
@@ -65,7 +69,7 @@ async function main() {
paid_by_participant_id: number
is_reimbursement: boolean
}>(
'select id, created_at, description, amount, paid_by_participant_id, is_reimbursement from expenses where group_id = $1::text',
'select id, created_at, description, amount, paid_by_participant_id, is_reimbursement from expenses where group_id = $1::text and deleted_at is null',
[groupRow.id],
)
for (const expenseRow of expenseRows) {