When running tests i get [ error ] Migration completed, but unable to release database lock
Package version
6.9.0
Describe the bug
Apologies if this issue would be better in vine or lucid repos, but it seems to touch on all through pacakges, so thought it best to report here.
Everytime I run a very basic test with npm run test I get the error:
[ error ] Migration completed, but unable to release database lock
This then causes the test results to hang and not quit which is painful when running individual tests within VSCode.
I have looked on discord and found a number of people having a similar issue:
https://discord.com/channels/423256550564691970/1246405673772646432 https://discord.com/channels/423256550564691970/1113490995892457573 https://discord.com/channels/423256550564691970/423256550564691972/1087399322506768497
I have created some steps to recreate:
- Install a new adonisjs project (with Lucid and webstarter kit)
- create
docker-compose.ymlwith following content:
version: '3.8'
services:
st_postgres:
container_name: test_postgres
image: postgres:13
volumes:
- postgres_volume:/var/lib/postgresql/data
- ./docker/postgres-dev-init.sql:/docker-entrypoint-initdb.d/init.sql # will setup dev database adonis_app for us
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
volumes:
postgres_volume:
docker compose up
-
Add the database credentials above to .env
-
In
tests/bootstrap.ts:
export const runnerHooks: Required<Pick<Config, 'setup' | 'teardown'>> = {
setup: [() => testUtils.db().truncate()],
teardown: [],
}
- Create a unit test in
/tests/unit/test.spec.tsit can do anything:
import { test } from '@japa/runner'
test.group('A test just so something executes', (group) => {
test('Test that something happens', async ({ assert }) => {
assert.equal(true, true)
})
})
- Add a migration file (again it can be anything):
import { BaseSchema } from '@adonisjs/lucid/schema'
export default class extends BaseSchema {
protected tableName = 'tests'
async up() {
this.schema.createTable(this.tableName, (table) => {
table.increments('id')
table.timestamp('created_at')
table.timestamp('updated_at')
})
}
async down() {
this.schema.dropTable(this.tableName)
}
}
npm run test
Every time I execute this I get the error:
[ error ] Migration completed, but unable to release database lock
If you remove the testUtils.db().truncate() from the setup command in bootstrap, the test runs fine.
Reproduction repo
No response
Hey @craigharman! 👋🏻
A few questions here:
- Is there any reason to use PSQL 13 instead of the latest release?
- Could you share the SQL script that set up the database?
In fact, the best would be to have a repository with the code you use to reproduce the issue.
It was just what I was using, but have since upgraded to postgres:latest and still have the same issue. I'll put a repo together.
@craigharman this seems related to issue #1027. You can workaround this error with something like this
export const truncateTables = async (schemas: string[] = ['public']) => {
const connection = db.connection();
let tables = await connection.getAllTables(schemas);
tables = tables.filter((table) => !['adonis_schema', 'adonis_schema_versions'].includes(table));
const joinTables = tables.map((table) => `"${table}"`).join(', ');
await connection.rawQuery(`TRUNCATE ${joinTables} RESTART IDENTITY CASCADE;`);
};
fyi, I kind of reproduced the error and provided this workaround here
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.