mirror of
https://github.com/jorgev259/soc_site-astro.git
synced 2025-06-29 07:57:41 +00:00
Migrate donator requests to limit of 5
This commit is contained in:
parent
1cd11fea2b
commit
042758d27c
5 changed files with 99 additions and 2 deletions
35
prisma/migrate.ts
Normal file
35
prisma/migrate.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import { Prisma } from '@prisma/client'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { createRequire } from 'module'
|
||||
import prismaClient from 'utils/prisma-client'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
|
||||
export type MigrationFn = (tx: Prisma.TransactionClient) => Promise<void>
|
||||
|
||||
interface Migration {
|
||||
id: string
|
||||
migrationFn: MigrationFn
|
||||
}
|
||||
|
||||
const dataMigrationsPath = path.join(import.meta.dirname, 'data_migrations')
|
||||
const migrationFiles = fs.readdirSync(dataMigrationsPath).filter((file) => file.endsWith('.ts'))
|
||||
const migrations: Migration[] = migrationFiles
|
||||
.sort()
|
||||
.map((id) => ({ id, migrationFn: require(path.join(dataMigrationsPath, id)).default }))
|
||||
.filter((migration) => migration.migrationFn !== undefined)
|
||||
|
||||
for (const { id, migrationFn } of migrations) {
|
||||
const startDate = new Date()
|
||||
const migration = await prismaClient.migration.findFirst({ where: { id } })
|
||||
if (migration === null) {
|
||||
console.log(`Migrating ${id}`)
|
||||
await prismaClient.$transaction(async (tx) => {
|
||||
await migrationFn(tx)
|
||||
await tx.migration.create({ data: { id } })
|
||||
})
|
||||
const endDate = new Date()
|
||||
console.log(`Migrated ${id} in ${(endDate.getTime() - startDate.getTime()) / 1000}s`)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue