Not able to run migrations on Node.js with knex and Typescript

Hello!

I have a Nest.js application running with docker and Postgres database
I am using the multistage build for my docker image for smaller image size and it deploys correctly.
But I have to run my migrations, also written in typescript, for that, I used the following in my fly.toml:

[deploy]
  release_command = "./release_command.sh"

my release_command.sh file was this:

npm i \ 
npx knex migrate:latest --knexfile ./database/config/knexfile.ts

I have to install all packages for running migration due to typescript dependencies, my production image has only the bundled files and removes all dev dependencies.

When I run this command, it seems that the VM running the migration restarts (or crashes for some reason) when I run fly deploy:

==> Release command detected: ./release_command.sh

--> This release will not be available until the release command succeeds.
         Starting instance
         Pulling container image
         Starting instance
         Configuring virtual machine
         Pulling container image
         Unpacking image
         Preparing kernel init
         Configuring firecracker
         Starting virtual machine
         Starting init (commit: 249766e)...
         Setting up swapspace version 1, size = 536866816 bytes
         Preparing to run: `docker-entrypoint.sh ./release_command.sh` as root
         2022/09/12 12:34:34 listening on [fdaa:0:9bfa:a7b:2cc3:cf16:837:2]:22 (DNS: [fdaa::3]:53)
         Starting instance
         Configuring virtual machine
         Pulling container image
         Unpacking image
         Preparing kernel init
         Configuring firecracker
         Starting virtual machine
         Starting init (commit: 249766e)...
         Setting up swapspace version 1, size = 536866816 bytes
         UUID=c80b440a-de44-498b-b716-817c9d4c024f
         Preparing to run: `docker-entrypoint.sh ./release_command.sh` as root
         2022/09/12 12:34:34 listening on [fdaa:0:9bfa:a7b:2cc3:cf16:837:2]:22 (DNS: [fdaa::3]:53)
         up to date, audited 156 packages in 2s
           run `npm fund` for details
         Unpacking image
         Preparing kernel init
         Configuring firecracker
         Starting virtual machine
         Starting init (commit: 249766e)...
         Setting up swapspace version 1, size = 536866816 bytes
         UUID=c80b440a-de44-498b-b716-817c9d4c024f
         Preparing to run: `docker-entrypoint.sh ./release_command.sh` as root
         2022/09/12 12:34:34 listening on [fdaa:0:9bfa:a7b:2cc3:cf16:837:2]:22 (DNS: [fdaa::3]:53)
         up to date, audited 156 packages in 2s
         18 packages are looking for funding
           run `npm fund` for details
         found 0 vulnerabilities
         up to date, audited 156 packages in 2s
         18 packages are looking for funding
           run `npm fund` for details
         found 0 vulnerabilities
         Failed to load external module ts-node/register
         Failed to load external module typescript-node/register
         Failed to load external module typescript-register
         Failed to load external module typescript-require
         Failed to load external module sucrase/register/ts
         Failed to load external module @babel/register
         Working directory changed to /database/config
         Cannot use import statement outside a module
         /database/config/knexfile.ts:1
         import { Knex } from 'knex';
         ^^^^^^
         SyntaxError: Cannot use import statement outside a module
             at Object.compileFunction (node:vm:360:18)
             at wrapSafe (node:internal/modules/cjs/loader:1048:15)
             at Module._compile (node:internal/modules/cjs/loader:1083:27)
             at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
             at Module.load (node:internal/modules/cjs/loader:997:32)
             at Module._load (node:internal/modules/cjs/loader:838:12)
             at Module.require (node:internal/modules/cjs/loader:1021:19)
             at require (node:internal/modules/cjs/helpers:103:18)
             at importFile (/node_modules/knex/lib/migrations/util/import-file.js:11:7)
             at async openKnexfile (/node_modules/knex/bin/cli.js:36:16)
         Starting clean up.
Error release command failed, deployment aborted

This release_command.sh does work when I run the docker image locally.

I had made a test making my production image being bundled with the devDependencies (basically my development stage in the dockerfile) and it ran the migrations successfully, but the image size was about 400mb, so this approach is not a good option for me.

Does anyone have any idea what I can do?

1 Like

Have same issue, my work around was to ignore migrations in the dockerfile and run them locally with the flyctl proxy running to connect to the db.

1 Like

My deploys are also failing and have been failing for around an hour, maybe more. I can confirm that removing the release_command line from fly.toml makes the deploy work, but flyctl still returns an error message/code. Perhaps this is related to the issue with Upstash Redis listed on https://status.flyio.net/, but it would be nice if the Fly team could clarify what’s happening.

--> release v146 created

--> You can detach the terminal anytime without stopping the deployment
==> Monitoring deployment
Error 1 error occurred:
	* No deployment available to monitor