-
-
Notifications
You must be signed in to change notification settings - Fork 207
Open
Description
Package version
@adonisjs/lucid@21.7.0
Describe the bug
Trying out various solutions from this issues thread, nothing consistently works
e.g. #1040 or #node ace db:truncate throws the error deadlock detected #1027
import db from '@adonisjs/lucid/services/db'
/**
* Truncate all tables except AdonisJS system tables
* to ensure a clean state for tests
*/
export async function truncateTablesExceptAdonis() {
// Get all table names
const tables = await db.rawQuery(
"SELECT tablename FROM pg_tables WHERE schemaname = 'public' AND tablename NOT LIKE 'adonis_%'"
)
const trx = await db.transaction()
// Disable foreign key checks temporarily
try {
await trx.rawQuery('SET session_replication_role = replica;')
await trx.rawQuery('SET CONSTRAINTS ALL DEFERRED;')
// Truncate each table
const tablesToTrunacte = tables.rows.map((row) => row.tablename)
await trx.rawQuery(`TRUNCATE ${tablesToTrunacte.join(',')} RESTART IDENTITY CASCADE;;`)
// Re-enable foreign key checks
await trx.rawQuery('SET session_replication_role = DEFAULT;')
await trx.rawQuery('SET CONSTRAINTS ALL IMMEDIATE;')
await trx.commit()
} catch (error) {
await trx.rollback()
}
}
Reproduction repo
No response
Metadata
Metadata
Assignees
Labels
No labels