lucid icon indicating copy to clipboard operation
lucid copied to clipboard

When running tests i get [ error ] Migration completed, but unable to release database lock

Open craigharman opened this issue 1 year ago • 2 comments

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:

  1. Install a new adonisjs project (with Lucid and webstarter kit)
  2. create docker-compose.yml with 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

  1. Add the database credentials above to .env

  2. In tests/bootstrap.ts:

export const runnerHooks: Required<Pick<Config, 'setup' | 'teardown'>> = {
  setup: [() => testUtils.db().truncate()],
  teardown: [],
}
  1. Create a unit test in /tests/unit/test.spec.ts it 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)
  })
})
  1. 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)
  }
}
  1. 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

craigharman avatar Jun 22 '24 08:06 craigharman

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.

RomainLanz avatar Jun 27 '24 06:06 RomainLanz

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 avatar Jul 01 '24 07:07 craigharman

@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

gonzalezger avatar Feb 10 '25 19:02 gonzalezger

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.

stale[bot] avatar Apr 26 '25 01:04 stale[bot]