prisma: `Drift detected: Your database schema is not in sync with your migration history` - every migration on Supabase
Bug description
Every time I make any change to my schema prisma has started resetting my entire database with the following statement (even though none of the referenced changes have been made):
Drift detected: Your database schema is not in sync with your migration history.
The following is a summary of the differences between the expected database schema given your migrations files, and the actual schema of the database.
It should be understood as the set of changes to get from the expected schema to the actual schema.
[*] Changed the `pgcrypto` extension
[+] Added extensions
- pg_graphql
[+] Added extensions
- pg_stat_statements
[+] Added extensions
- pgjwt
[+] Added extensions
- pgsodium
[+] Added extensions
- supabase_vault
[+] Added extensions
- uuid-ossp
? We need to reset the "public" schema at
How to reproduce
Here’s the relevant portion of my prisma schema:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
extensions = [pgcrypto]
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["fullTextSearch", "postgresqlExtensions"]
}
Expected behavior
As before, non destructive changes to my schema should not trigger a drift.
Prisma information
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
extensions = [pgcrypto]
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["fullTextSearch", "postgresqlExtensions"]
}
model Account {
id String @id @default(nanoid())
userId String
type String
provider String
providerAccountId String
refresh_token String? @db.Text
access_token String? @db.Text
expires_at Int?
token_type String?
scope String?
id_token String? @db.Text
session_state String?
@@unique([provider, providerAccountId])
}
model Session {
id String @id @default(nanoid())
sessionToken String @unique
userId String
expires DateTime
}
model VerificationToken {
identifier String
token String @unique
expires DateTime
@@unique([identifier, token])
}
Environment & setup
- OS: macOS
- Database: PostgreSQL (Supabase)
- Node.js version: lts/gallium v16.20.0
Prisma Version
prisma : 4.12.0
@prisma/client : 4.12.0
Current platform : darwin
Query Engine (Node-API) : libquery-engine 659ef412370fa3b41cd7bf6e94587c1dfb7f67e7 (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine : migration-engine-cli 659ef412370fa3b41cd7bf6e94587c1dfb7f67e7 (at node_modules/@prisma/engines/migration-engine-darwin)
Format Wasm : @prisma/prisma-fmt-wasm 4.12.0-67.659ef412370fa3b41cd7bf6e94587c1dfb7f67e7
Default Engines Hash : 659ef412370fa3b41cd7bf6e94587c1dfb7f67e7
Studio : 0.483.0
Preview Features : fullTextSearch, postgresqlExtensions
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 6
- Comments: 22 (7 by maintainers)
Solved by disabling all the extensions I could in the Supabase dashboard and adding the following migration to the
migrations
directory manually:While in the
schema.prisma
:Looking for a production level solution here too we’re trying to use the vector extension and running into this issue during migrate dev
So now, how to solve this 🤔 ?
I think the problem is on Prisma Migrate side when
postgresqlExtensions
is enabled. Regardless of ifmultiSchema
is enabled, Migrate will look in all schemas for extensions during the drift detection.Suggestion: Only check the current schemas for extensions for drift detection, meaning:
multiSchema
, the schema defined in the connection URL or default (public
)multiSchema
, the schemas defined in the datasource propertyschemas = ["public", "something"]
Then on Supabase, it should be possible to use the following schema:
Which would mean ignoring the schemas not present in the schemas property.
Current list of all schemas on Supabase:
I tried some things today:
Using the following schema
first
migrate dev
second run, reduces the number of extensions added in the drift
Trying to add these 2 last extensions and schemas is not doable,
migrate dev
will need to reset these, and I think that they are not supposed to be migrated / reset by anyone and should only be managed by Supabase.I tried to deactivate extensions from https://app.supabase.com/project/ljoyzzvkboexvsemukhj/database/extensions but pgsodium cannot be deactivated
I need to talk to some colleagues about this, my first thought is that Migrate needs, somehow to ignore these Supabase specific schemas (vault, pgsodium) at least, and maybe more?
I have a workaround to offer those finding this page: remove previewFeatures and extensions before running
prisma migrate dev
.My setup: I am using Supabase with Prisma 4.13. While my testing and production servers run on Supabase’s cloud service, I do unit testing off a local Docker setup of Supabase. My test runner, the one that comes with RedwoodJS, routinely runs this command automatically to reset then seed the Docker database:
yarn prisma db push --force-reset --accept-data-loss
I use postgis, which is a PostgreSQL extension, one that is unsupported by Prisma. There are postgis fields in my schema with the “unsupported” type. During the data reset step above, the Supabase in Docker loses the extension, so the subsequent
push
fails. To get unit testing to work, I found that I had to add thepostgresqlExtensions
preview feature to my schema.prisma file as follows:Notice that the datasource.extensions section specifies a schema for the extension (“public”).
Recently, running “prisma migrate dev” to apply changes to my hosted Supabase testing server started giving me the “Drift detected: Your database schema is not in sync with your migration history” message, citing the same extensions mentioned above. I assume it’s because Supabase changed the extensions in some of the schemas they manage, which is in their purview. I suspect that, with these features on, Prisma started paying attention to the extensions in other schemas as well, thinking such changes to those schemas represented drift, when for all intents and purposes to the developer, they do not.
The workaround was to remove the
previewFeatures
andextensions
from schema.prisma. Once removed, I was able to runprisma migrate dev
without “drift” being found.Here’s the code at the top of my schema.prisma file now:
I uncomment these two lines when I want to run the unit tests. This will be my workaround until the issue no longer exists. I hope this helps!
I solved it this way:
Add extensions manually in extensions=[…], then do prisma db pull, this will fill out the extensions with the appropriate schema, then add the missing schemas to schemas=[…]
If a migration fails, check which extension made it fail and repeat the process above until there are no more extension left to add in prisma schema
I have added pg_trgm that is not by default on supabase
Notes: I could reproduce this easily with
4.14.0
When starting a fresh project db on supabase With the following schema:
running
npx prisma migrate dev
outputs like in this issueThis drift shows in both cases: if the schema was changed or not. The reset, if saying yes, will be successful but needs to be done every time
migrate dev
is called which is not expected.npx prisma db pull
only addsextensions = [uuid_ossp(map: "uuid-ossp", schema: "extensions")]
that resolved my life here, thank you
I find from documentation. link
doesn’t sync db with schema. why?