How to drop a test database after having run all tests?

420 Views Asked by At

I am using the npm package db-migrate in my node-application to perform database migrations on a postgres database. I have an extra test-database, which I want to drop after having run all my tests. For this purpose, I added the following script to my package.json:

"test": "set ENV=test&& npx tsc && db-migrate --env test up && jasmine && db-migrate db:drop --env test test_database_name"

Doing so, I run into the following error:

 Error: Failed to drop database! error: cannot drop the currently open database

I have assumed the error would be due to the fact that I was connected to my test-database using the database adminsitration tool of my IDE (Intellij). The error keeps showing up, though, when both deactivating the connection to my test-database or removing it from my database-administration tool.

Therefore, I have tried the following alternative:

"test": "set ENV=test&& npx tsc && db-migrate --env test up && jasmine && db-migrate --env test reset"

From what I know, the last command of this test-script will drop the test-database, create it again and run all migrations.

It seems to be working correctly, I can remain connected to the test-database with the database-administration tool, all tables are dropped. However, in the end, the only table left in my test-database is a migrations table, which is empty. Why would it be empty? I was expecting to see all the migrations in it that were run after the database was recreated.

0

There are 0 best solutions below