Added almost 20 thousand entities from another db into payload. To keep relational integrity working, I'm keeping the old ids instead of letting payload assign new ids to the insertions, with payload.create({collection: 'stuff', data: {id: entry.oldId}})
When you provide an id during creation, the relevant sequence is not updated, but the thing is that there's no way to update the relevant sequences through the provided apis, so we have to run 'raw' sql, probably with payload.db.execute() or payload.db.drizzle.execute()...
So far, this is where I'm stuck:
const transactionID = await payload.db.beginTransaction()
if (transactionID === null) {
throw new Error('Could not get a transaction ID')
}
try {
// many more things before, one of them is the 'stuff' collection seeding
const drizzleTransaction = (await payload.db.sessions![transactionID].db) as PgTransaction<
NodePgQueryResultHKT,
Record<string, unknown>,
ExtractTablesWithRelations<Record<string, unknown>>
>
await payload.db.execute({
db: drizzleTransaction,
drizzle: payload.db.drizzle,
sql: sql`
SELECT setval(
pg_get_serial_sequence(${sql.raw(`'${collectionSource}'`)}, 'id'),
(SELECT MAX(id) FROM ${sql.raw(collectionSource)})
)
`,
})
// many more things after, with an error that should rollback stuff for testing purposes
await payload.db.commitTransaction(transactionID)
} catch (error) {
await payload.db.rollbackTransaction(transactionID)
throw error
}
This updates the sequences but disregards the transaction: when things fail, the sequence is still updated
Anyone has any ideas?