You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There currently doesn't seem to be a way to easily reset a database.
Suggested solution
Provide a way to reset the database (via CLI, SDK, Studio). This should delete the database, re-create it from the schema and finally seed it (if there is a seed).
Alternatives
Additional context
Popular frameworks like Ruby on Rails have integrated tools to manage the lifecycle of a database:
db:create creates the database for the current env
db:create:all creates the databases for all envs
db:drop drops the database for the current env
db:drop:all drops the databases for all envs
db:migrate runs migrations for the current env that have not run yet
db:migrate:up runs one specific migration
db:migrate:down rolls back one specific migration
db:migrate:status shows current migration status
db:rollback rolls back the last migration
db:forward advances the current schema version to the next one
db:seed (only) runs the db/seed.rb file
db:schema:load loads the schema into the current env's database
db:schema:dump dumps the current env's schema (and seems to create the db as well)
Original issue
There currently doesn't seem to be a way to easily reset all data. This should be possible via Studio, CLI and/or SDK.
Reacted by Thibaud Courtoison, guog, alechp, Jesús Ferretti, Ranjan Purbey, Émile Fugulin, Cristian Rosescu, Antti Mäki, beeman, Darren and 49 more
We use Prisma v1 CLI to prepare the database for e2e and for integration tests. It's really handy to do prisma reset && prisma seed -e .env.[e2e|int].
What would be the equivalent in Prisma 2?
For those who are interested in truncating tables over dropping and re-creating the database, which is tracked here: #5596
Thanks for sharing! However, after dropping and creating the database, it doesn't recreate the tables. Is there a way to have it allow run a prism db push?
@chinanderm We actually added the --force-reset flag to prisma db push in 2.17 that will force a reset of the database before doing the push.
You can use it like prisma db push --force-reset --preview-feature, I think it's exactly what you needed?
Why? Because db push creates the SQL on the fly while you could have extra SQL that it could not guess (like if you edited the migration.sql files).
So using reset + deploy makes sure you have the same end-state as in production (minus the data).
For doing this inside Jest for speed, I found doing the truncate all in one query to be about 1 second faster (+.2sec vs +1.2sec)
// Get all table names from the information_schema
const result: { table_name: string }[] = await prisma.$queryRaw`
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public' AND table_type = 'BASE TABLE';
`;
// Truncate them all in one combined query
await prisma.$queryRawUnsafe(
`TRUNCATE TABLE ${result.map(({ table_name }) => `"${table_name}"`).join(", ")} RESTART IDENTITY CASCADE`,
);
Activity
janpio commentedon Oct 16, 2019
Clarification: First step here would be to create a spec that defines how this might work and what it might be called.
janpio commentedon Oct 28, 2019
Internal Note: Move to specs
schickling commentedon Nov 7, 2019
First step: Implement this functionality in Studio (See issue prisma/studio#237).
After that we'll investigate further whether/how to expose this functionality via other tools (e.g. CLI, SDK, ...)
timsuchanek commentedon Nov 7, 2019
Internal Note: Dropping, because this is a Studio only thing.
pantharshit00 commentedon Mar 26, 2020
Internal note: Candidating this because of another duplicate issue that refered to same feature was candidated by @schickling
frandiox commentedon Apr 9, 2020
We use Prisma v1 CLI to prepare the database for e2e and for integration tests. It's really handy to do
prisma reset && prisma seed -e .env.[e2e|int].What would be the equivalent in Prisma 2?
16 remaining items
albertoperdomo commentedon Mar 3, 2021
We have
prisma migrate resetnow, which:We are closing this issue.
For those who are interested in truncating tables over dropping and re-creating the database, which is tracked here: #5596
beeman commentedon Mar 3, 2021
You folks are amazing!! 🙏
chinanderm commentedon Mar 9, 2021
Thanks for sharing! However, after dropping and creating the database, it doesn't recreate the tables. Is there a way to have it allow run a
prism db push?Jolg42 commentedon Mar 9, 2021
@chinanderm We actually added the
--force-resetflag toprisma db pushin 2.17 that will force a reset of the database before doing the push.You can use it like
prisma db push --force-reset --preview-feature, I think it's exactly what you needed?chinanderm commentedon Mar 9, 2021
@Jolg42 Yep, that works great. Thank you!
Jolg42 commentedon Mar 9, 2021
@chinanderm Note that if you're already using migrations files I would recommend to use
Why? Because
db pushcreates the SQL on the fly while you could have extra SQL that it could not guess (like if you edited the migration.sql files).So using reset + deploy makes sure you have the same end-state as in production (minus the data).
phoenixeliot commentedon Apr 2, 2024
For doing this inside Jest for speed, I found doing the truncate all in one query to be about 1 second faster (+.2sec vs +1.2sec)
Athar-ali-siddiqui commentedon May 1, 2024
I have used this strategy to ensure the database is cleaned up before executing each test.
campellcl commentedon Dec 11, 2024
If you are trying to get this working from inside a
.tsfile (for instanceseed.ts) you can:In the invoker (i.e.
jesttesting script):s14051sa-png commentedon Jan 30, 2026
All the evidence must be clear.
TML-2790: M:N SQLite demo examples + project plan expansion (slices 4…
TML-2794: M:N slice 5 — PSL authors many-to-many (junction → N:M + th…