prisma: prisma migrate dev fails with Error: P1017 (prisma > 5.9.1)

Bug description

prisma migrate dev fails with P1017: Server has closed the connection for any prisma version after 5.9.1

This error happens even when the prisma.schema doesn’t have any changes.

Reverting to 5.9.1 solves the issue.

> DEBUG=* yarn prisma migrate dev --create-only

prisma:schemaEngine:rpc SENDING RPC CALL {"id":1,"jsonrpc":"2.0","method":"devDiagnostic","params":{"migrationsDirectoryPath":"/Users/<user>/src/<project>/prisma/migrations"}} +2ms
prisma:schemaEngine:stderr {"timestamp":"2024-06-14T23:42:31.423091Z","level":"INFO","fields":{"message":"Starting schema engine RPC server","git_hash":"..."},"target":"schema_engine"} +9ms
prisma:schemaEngine:stderr {"timestamp":"2024-06-14T23:43:14.769293Z","level":"ERROR","fields":{"message":"Error in PostgreSQL connection: Error { kind: Closed, cause: None }"},"target":"quaint::connector::postgres::native"} +43348ms
prisma:schemaEngine:rpc {
  "jsonrpc": "2.0",
  "error": {
    "code": 4466,
    "message": "An error happened. Check the data field for details.",
    "data": {
      "is_panic": false,
      "message": "Server has closed the connection.",
      "meta": null,
      "error_code": "P1017"
    }
  },
  "id": 1
} +2ms

Error: Error: P1017

Server has closed the connection.

    at Object.<anonymous> /Users/<user>/src/<project>/node_modules/prisma/build/index.js:525:8)
    at rc.handleResponse (/Users/<user>/src/<project>/node_modules/prisma/build/index.js:516:2821)
    at Pl.<anonymous> (/Users/<user>/src/<project>/node_modules/prisma/build/index.js:521:490)
    at Pl.emit (node:events:517:28)
    at addChunk (node:internal/streams/readable:368:12)
    at readableAddChunk (node:internal/streams/readable:341:9)
    at Readable.push (node:internal/streams/readable:278:10)
    at Pl._pushBuffer (/Users/<user>/src/<project>/node_modules/prisma/build/index.js:510:369)
    at Pl._transform (/Users/<user>/src/<project>/node_modules/prisma/build/index.js:510:199)
    at Transform._write (node:internal/streams/transform:175:8)

How to reproduce

  1. Install any prisma version above 5.9.1
  2. Run yarn prisma migrate dev or yarn prisma migrate dev --create-only
  3. See error

Expected behavior

No response

Prisma information

// Add your schema.prisma

// no changes 

Environment & setup

  • OS: macOS
  • Database: PostgreSQL
  • Node.js version: v18.19.0

Prisma Version

5.15.0

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 3
  • Comments: 15 (2 by maintainers)

Most upvoted comments

Have done a bit more digging here. For us, this seems to be an issue with port forwarding that surfaced when we migrated from Docker Desktop to Rancher Desktop. Using kubectl to port forward results in the following port forwarding error when trying to generate migrations:

E0919 15:33:47.207148   61286 portforward.go:413] "Unhandled Error" err=<
	an error occurred forwarding 55366 -> 5432: error forwarding port 5432 to pod 862282a5c96f63162cb4ba4f518956a5c381cf20a6e047734d97addd87a7e174, uid : exit status 1: 2024/09/19 15:33:46 socat[28317] E read(6, 0xffffa51b8000, 8192): Connection reset by peer
 >
error: lost connection to pod

When I instead enable port forwarding using the Rancher Desktop application, migrate dev runs successfully.

Hello, I have exactly the same issue

prisma                  : 5.17.0
@prisma/client          : Not found
Computed binaryTarget   : darwin-arm64
Operating System        : darwin
Architecture            : arm64
Node.js                 : v20.13.1
Query Engine (Node-API) : libquery-engine 393aa359c9ad4a4bb28630fb5613f9c281cde053 (at ../../../../../../../.npm/_npx/2778af9cee32ff87/node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Schema Engine           : schema-engine-cli 393aa359c9ad4a4bb28630fb5613f9c281cde053 (at ../../../../../../../.npm/_npx/2778af9cee32ff87/node_modules/@prisma/engines/schema-engine-darwin-arm64)
Schema Wasm             : @prisma/prisma-schema-wasm 5.17.0-31.393aa359c9ad4a4bb28630fb5613f9c281cde053
Default Engines Hash    : 393aa359c9ad4a4bb28630fb5613f9c281cde053
Studio                  : 0.502.0
Preview Features        : postgresqlExtensions

Running a postgresql (16) database locally on macos (M1) in a kubernetes cluster set up via orbstack Thank you Andrey for finding out that version 5.9.1 is working

Here are the logs of postgres when I start the server then do a DEBUG=* npx prisma migrate dev --create-only --skip-generate

2024-07-23 07:12:01.446 UTC [1] LOG:  starting PostgreSQL 16.2 (Debian 16.2-1.pgdg120+2) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
2024-07-23 07:12:01.446 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2024-07-23 07:12:01.446 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2024-07-23 07:12:01.451 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2024-07-23 07:12:01.455 UTC [16] LOG:  database system was shut down at 2024-07-23 07:11:59 UTC
2024-07-23 07:12:01.460 UTC [1] LOG:  database system is ready to accept connections
2024-07-23 07:12:24.731 UTC [37] FATAL:  terminating connection due to administrator command
2024-07-23 07:12:24.838 UTC [14] LOG:  checkpoint starting: immediate force wait
2024-07-23 07:12:24.862 UTC [14] LOG:  checkpoint complete: wrote 5 buffers (0.0%); 0 WAL file(s) added, 1 removed, 0 recycled; write=0.003 s, sync=0.011 s, total=0.025 s; sync files=5, longest=0.003 s, average=0.003 s; distance=8665 kB, estimate=8665 kB; lsn=16/8873ABB8, redo lsn=16/8873AB80

The logs of the command above are:

prisma:engines binaries to download libquery-engine, schema-engine +123ms
prisma:getSchema prismaConfig null +196ms
prisma:getSchema Checking existence of /Users/username/Documents/git/projectname/src/modules/mydbname/prisma/schema.prisma +0ms
prisma:getSchema Reading schema from single file /Users/username/Documents/git/projectname/src/modules/mydbname/prisma/schema.prisma +1ms
prisma:getSchema prismaConfig null +1ms
prisma:tryLoadEnv Environment variables not found at null +0ms
prisma:tryLoadEnv Environment variables not found at undefined +1ms
prisma:tryLoadEnv No Environment variables loaded +0ms
prisma:getConfig Using getConfig Wasm +0ms
prisma:getConfig config data retrieved without errors in getConfig Wasm +19ms
prisma:getSchema prismaConfig null +1ms
prisma:tryLoadEnv Environment variables not found at null +0ms
prisma:tryLoadEnv Environment variables not found at undefined +0ms
prisma:tryLoadEnv No Environment variables loaded +0ms
prisma:getSchema Reading schema from single file /Users/username/Documents/git/projectname/src/modules/mydbname/prisma/schema.prisma +0ms
Prisma schema loaded from schema.prisma
prisma:getSchema Reading schema from single file /Users/username/Documents/git/projectname/src/modules/mydbname/prisma/schema.prisma +0ms
prisma:getConfig Using getConfig Wasm +1ms
prisma:getConfig config data retrieved without errors in getConfig Wasm +8ms
Datasource "db": PostgreSQL database "mydbname", schema "public" at "localhost:5434"

prisma:validate Using validate Wasm +1ms
prisma:getConfig Using getConfig Wasm +30ms
prisma:getConfig config data retrieved without errors in getConfig Wasm +8ms
prisma:getSchema Reading schema from single file /Users/username/Documents/git/projectname/src/modules/mydbname/prisma/schema.prisma +0ms
prisma:getConfig Using getConfig Wasm +1ms
prisma:getConfig config data retrieved without errors in getConfig Wasm +7ms
prisma:schemaEngine:rpc starting Schema engine with binary: /Users/username/.npm/_npx/2778af9cee32ff87/node_modules/@prisma/engines/schema-engine-darwin-arm64 +62ms
prisma:getSchema Reading schema from single file /Users/username/Documents/git/projectname/src/modules/mydbname/prisma/schema.prisma +0ms
prisma:getConfig Using getConfig Wasm +0ms
prisma:getConfig config data retrieved without errors in getConfig Wasm +8ms
prisma:schemaEngine:rpc SENDING RPC CALL {"id":1,"jsonrpc":"2.0","method":"devDiagnostic","params":{"migrationsDirectoryPath":"/Users/username/Documents/git/projectname/src/modules/mydbname/prisma/migrations"}} +2ms
prisma:schemaEngine:stderr {"timestamp":"2024-07-23T07:12:23.980185Z","level":"INFO","fields":{"message":"Starting schema engine RPC server","git_hash":"393aa359c9ad4a4bb28630fb5613f9c281cde053"},"target":"schema_engine"} +4ms
prisma:schemaEngine:stderr {"timestamp":"2024-07-23T07:12:24.736275Z","level":"ERROR","fields":{"message":"Error in PostgreSQL connection: Error { kind: Closed, cause: None }"},"target":"quaint::connector::postgres::native"} +757ms
prisma:schemaEngine:rpc {
  "jsonrpc": "2.0",
  "error": {
    "code": 4466,
    "message": "An error happened. Check the data field for details.",
    "data": {
      "is_panic": false,
      "message": "Server has closed the connection.",
      "meta": null,
      "error_code": "P1017"
    }
  },
  "id": 1
} +1ms
Error: Error: P1017

Server has closed the connection.

    at Object.<anonymous> (/Users/username/.npm/_npx/2778af9cee32ff87/node_modules/prisma/build/index.js:540:8)
    at hu.handleResponse (/Users/username/.npm/_npx/2778af9cee32ff87/node_modules/prisma/build/index.js:531:2836)
    at ef.<anonymous> (/Users/username/.npm/_npx/2778af9cee32ff87/node_modules/prisma/build/index.js:536:524)
    at ef.emit (node:events:519:28)
    at addChunk (node:internal/streams/readable:559:12)
    at readableAddChunkPushObjectMode (node:internal/streams/readable:536:3)
    at Readable.push (node:internal/streams/readable:391:5)
    at ef._pushBuffer (/Users/username/.npm/_npx/2778af9cee32ff87/node_modules/prisma/build/index.js:525:369)
    at ef._transform (/Users/username/.npm/_npx/2778af9cee32ff87/node_modules/prisma/build/index.js:525:199)
    at Transform._write (node:internal/streams/transform:171:8)

I’ve concluded that the problem lies in Tilt port-forwards. It’s not the same as a docker compose exposed port. Perhaps something like only accepting a single connection and prisma using multiple during migrate? Not sure.

But, a solution that works for me now is to run a locally built prisma tool container in the dev cluster and using that to handle migrations. It’s a little fiddly, could use a better way to sync files back to the project directory, but I don’t see a better way at the moment.

Here’s my setup: ./Dockerfile.prisma

# Use the official Node.js 20 image as the base image
FROM node:20

WORKDIR /tool

COPY prisma prisma

RUN echo "DATABASE_URL=\"postgresql://johndoe:randompassword@postgresql:5432/mydb\"" > .env

# Install Prisma CLI
RUN npm install prisma@6.3.1

# Expose the application port
EXPOSE 4466

# Keep the container running
CMD ["tail", "-f", "/dev/null"]

./kustomize/bases/prisma.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: prisma
  labels:
    app: prisma
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prisma
  template:
    metadata:
      labels:
        app: prisma
    spec:
      containers:
        - name: prisma
          image: prisma-migrator
          env:
            - name: DATABASE_URL
              value: "postgresql://johndoe:randompassword@postgresql:5432/mydb"

./Tiltfile

prisma_path = './prisma'

# Prisma Sync
docker_build(
    'prisma-migrator',
    context='.',
    dockerfile='Dockerfile.prisma',
    only=['prisma'],
    live_update=[
        sync('./prisma', '/tool/prisma'),
    ],
)

k8s_yaml(kustomize('./kustomize'))

k8s_resource('prisma', port_forwards=4466)
k8s_resource('postgresql', port_forwards=5432)

./prisma.sh

#!/bin/bash

PRISMA_POD=$(kubectl get pods -l app=prisma -o jsonpath='{.items[0].metadata.name}')

kubectl exec -it "$PRISMA_POD" -- /bin/bash -c "npx prisma $*"
kubectl cp "$PRISMA_POD":/tool/prisma ./prisma
kubectl cp "$PRISMA_POD":/tool/node_modules/@prisma/client ./node_modules/\@prisma/client

Usage:

./prisma.sh migrate dev --name init

The results of the migration are copied to the local paths, which are synced back into the container via Tilt and thus present on the next prisma tool use.

It’s kind of a hack, but it works.

I made a repo to reproduce this, with a version compatibility test system.

https://github.com/skaramicke/prisma-6.3.1-p1017

Legend

  • ✅ tables exist at the end of the test
  • ❌ tables dont exist.

Results

This is for all major PostgreSQL versions from 10 and up, and all Prisma minor versions from 5.9 and up, including a couple of extra prisma versions that snuck in.

Results

PG 10.0 PG 11.0 PG 12.0 PG 13.0 PG 14.0 PG 15.0 PG 16.0 PG 17.0
Pr 5.9.0 ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅
Pr 5.10.0 ✅ ✅ ✅ ❌ ❌ ❌ ❌ ❌
Pr 5.11.0 ✅ ✅ ✅ ❌ ❌ ❌ ❌ ❌
Pr 5.12.0 ✅ ✅ ✅ ❌ ❌ ❌ ❌ ❌
Pr 5.13.0 ✅ ✅ ✅ ❌ ❌ ❌ ❌ ❌
Pr 5.14.0 ✅ ✅ ✅ ❌ ❌ ❌ ❌ ❌
Pr 5.15.0 ✅ ✅ ✅ ❌ ❌ ❌ ❌ ❌
Pr 5.16.0 ✅ ✅ ✅ ❌ ❌ ❌ ❌ ❌
Pr 5.17.0 ✅ ✅ ✅ ❌ ❌ ❌ ❌ ❌
Pr 5.18.0 ✅ ✅ ✅ ❌ ❌ ❌ ❌ ❌
Pr 5.19.0 ✅ ✅ ✅ ❌ ❌ ❌ ❌ ❌
Pr 5.20.0 ✅ ✅ ✅ ❌ ❌ ❌ ❌ ❌
Pr 5.21.0 ✅ ✅ ✅ ❌ ❌ ❌ ❌ ❌
Pr 5.22.0 ✅ ✅ ✅ ❌ ❌ ❌ ❌ ❌
Pr 6.0.0 ✅ ✅ ✅ ❌ ❌ ❌ ❌ ❌
Pr 6.1.0 ✅ ✅ ✅ ❌ ❌ ❌ ❌ ❌
Pr 6.2.0 ✅ ✅ ✅ ❌ ❌ ❌ ❌ ❌
Pr 6.2.1 ✅ ✅ ✅ ❌ ❌ ❌ ❌ ❌
Pr 6.3.0 ✅ ✅ ✅ ❌ ❌ ❌ ❌ ❌
Pr 6.3.1 ✅ ✅ ✅ ❌ ❌ ❌ ❌ ❌

Code:

To run this you need make, kind and kubectl

  1. Check out Repo
  2. Run make clean to remove versioned test data
  3. Change the two MIN_ version variables in Makefile if needed.
  4. Run make versions/postgresql to get all postgresql versions, or just specify manually.
  5. Run make versions/prisma to get all prisma versions above the specified minumum, or just specify them manually.
  6. Run make version_make_targets to fill in the test combinations.
  7. Run make all_versions to run all tests.

Or just run ./test_compatibility.sh <PostgreSQL version> <Prisma version> to test a specific version once.

The individual results and logs are created in ./results/<PostgreSQL version>/<Prisma version>/

In my case, kubectl is working, because the Postgres log shows the migration SELECT command.

prisma                  : 6.1.0
@prisma/client          : 6.1.0
Computed binaryTarget   : windows
Operating System        : win32
Architecture            : x64
Node.js                 : v22.9.0
Query Engine (Node-API) : libquery-engine 11f085a2012c0f4778414c8db2651556ee0ef959 (at node_modules\@prisma\engines\query_engine-windows.dll.node)
Schema Engine           : schema-engine-cli 11f085a2012c0f4778414c8db2651556ee0ef959 (at node_modules\@prisma\engines\schema-engine-windows.exe)
Schema Wasm             : @prisma/prisma-schema-wasm 6.1.0-21.11f085a2012c0f4778414c8db2651556ee0ef959
Default Engines Hash    : 11f085a2012c0f4778414c8db2651556ee0ef959
Studio                  : 0.503.0
2025-01-03 00:21:06.910 GMT [4844] ERROR:  relation "_prisma_migrations" does not exist at character 126
2025-01-03 00:21:06.910 GMT [4844] STATEMENT:  SELECT "id", "checksum", "finished_at", "migration_name", "logs", "rolled_back_at", "started_at", "applied_steps_count" FROM "_prisma_migrations" ORDER BY "started_at" ASC
2025-01-03 00:21:06.997 GMT [4845] FATAL:  terminating connection due to administrator command
2025-01-03 00:21:07.098 GMT [97] LOG:  checkpoint starting: immediate force wait
2025-01-03 00:21:07.107 GMT [97] LOG:  checkpoint complete: wrote 4 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.002 s, sync=0.003 s, total=0.010 s; sync files=4, longest=0.002 s, average=0.001 s; distance=4269 kB, estimate=4269 kB; lsn=0/3AC2E20, redo lsn=0/3AC2DE8