Open jean-smaug opened 2 years ago
Solution for a tool like Supabase is to create another instance of PostgresSQL as shadow database.
I'm closing this issue now. Thanks for all of your work !
Great that you found a workaround. Optimally you should have gotten an error message telling you about the workaround though, and not this:
Error: db error: ERROR: no such database: prisma_migrate_shadow_db_b2ce3e4e-c5ef-41f6-830f-2203a082f1db 0: sql_migration_connector::flavour::postgres::sql_schema_from_migration_history
We should investigate why that error message is not triggering on Supabase.
For any future Prisma + Supabase users Googling this, if, like me, you were trying to do fully-local development, literally following these steps from the original post:
- Download Supabase CLI
supabase init
supabase start
- Init prisma
- Run
prisma migrate dev
You probably read the docs here on manually creating a shadow database here, but those docs heavily imply creating a cloud database.
However, AFAIK, there's nothing preventing you from spinning up a local shadow Supabase DB for Prisma to use via, say, docker-compose.yml
.
The following worked for me.
Put this in a docker-compose.yml
file and run docker-compose up
:
version: '3.8'
services:
# Prisma uses this database to detect schema drift and other dev checks
# See https://www.prisma.io/docs/concepts/components/prisma-migrate/shadow-database
supabase-shadow:
image: supabase/postgres:14.1.0
command: postgres -c config_file=/etc/postgresql/postgresql.conf
restart: unless-stopped
ports:
- 12345:5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
# 👇 Change this volume mapping to something that makes sense for you
- ~/.docker/storage/data:/var/lib/postgresql/data
- ~/.docker/storage/init:/docker-entrypoint-initdb.d
Then add the shadow DB URL to your .env
file:
DATABASE_URL="postgresql://postgres:postgres@localhost:10150/postgres"
SHADOW_DATABASE_URL="postgresql://postgres:postgres@localhost:12345/postgres"
And finally update your schema.prisma
to point shadowDatabaseUrl
to your local shadow DB:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
}
With that, I'm able to run and generate migrations with Prisma, while running the typical Supabase local dev environment with supabase start
.
Hope that helps!
An alternative method to spinning up a shadow db locally by creating a docker-compose.yml
is to create another supabase db locally.
Here are the steps (assuming you have initialized supabase and have a supabase/
directory in the root of your project):
cd prisma
supabase init
prisma/supabase/config.json
. I just changed them to
"ports": {
"api": 64321,
"db": 64322,
"studio": 64323
},
.env
file
SHADOW_DATABASE_URL="postgresql://postgres:postgres@localhost:64322/postgres"
schema.prisma
file with the shadowDatabaseUrl
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
}
supabase start
in the root of your project and run supabase start
in prisma/
Note: you don't have to init the supabase project inside of your prisma/
directory, you can choose any other directory. This method also runs everything else that normally runs when you run supabase start
instead of just starting a Postgres db.
This works for me. Not sure of the side effects, but it works.
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("DATABASE_URL")
}
This will most probably deleted your data sooner or later, don't do that @tenhaus We actually have an issue that this should not be allowed: https://github.com/prisma/prisma/issues/16628
Bug description
I'm trying to generate some migrations using Prisma. I'm using Supabase which is using Postgres under the hood. Also, I tried to run the following command with the local emulator and with the "real project".
When I run
prisma db push
it's working, so the communication between prisma and supabase can be established. But when I try to runprisma migrate dev
I get the following errorHow to reproduce
supabase init
supabase start
prisma migrate dev
Expected behavior
Migrations are generated
Prisma information
It's not my real schema which is private, but I tested with this one and it's also failing 😬
Environment & setup
Prisma Version