Clear the whole database?
For my test environment I'd like to clear the whole db.. is there a way to achieve this using drizzle ?
33 Replies
You can’t do it with drizzle. You can delete the tables content with a script, but you can’t delete the indexes and alike, you have to run SQL for that, as far as I know.
Thanks for your answer ! do you know if there is a way to retrieve the all the table names programmatically ? so I can automate my script and don't have to add a table name each time I create one when prototyping...
I mean something like this :
I’d presume, internally there has to be, don’t know how. Following this thread to see if any information comes up.
Maybe we can loop over some values in the db object, it should all the table in it. db.query[tableName]. But I don’t know if it’s limited to .query
I’ll check when I get home
Thanks man
I have it !
Got home, did it myself, and only now seeing this 😅...
Here's the code if you're intrested:
Since using PlanetScale, cascading doesn't work, but you can change the query to whatever you need.
Hahaha thanks buddy ! sorry for the time you took but I appreciate it ! Thanks a lot !
Why are you using
DELETE FROM
and not TRUNCATE
?
This may help planetScale users wondering how to do it 😉It should work for all dbs, just change the db type to the right one
You’re right, I tried first to use the orm delete method so I stuck with it.
alright ^^ thanks a lot !
It’s ok, I needed something like this for myself
Hello Mendy, I saw your reset script and was trying to make one myself. I came up with a slight variation but I think I made a mistake somewhere and was wondering how you did it
I used the db variable from where the connection is created: but I see db._.schema as undefined at my side. Is there a certain order or something I have to do to know the variable is not undefined?
I have a stupid question: rather than resetting your database in place, why not actually drop and recreate the underlying database itself? it should be easy using e.g. docker
that way you'll never have an issue where you've added a construct (function, trigger, new role, etc) that isn't cleared properly by your clear script
That would achieve the same goal yes, but since I use planetscale, I can't use docker. Do you know the best way to do this for planetscale?
no idea - I have never had tests run against an actual hosted database, I've always set them up to run locally
having tests against an actual hosted database seems like a synchronization problem too
imagine you have the test run in a CI action, and you push two branches at once, whoops! now one test is clearing data while the other is still running
I would rather prefer locally too, but the way planetscale uses mysql through vitess, it is a lot easier to use a branch on there than to try and set that up locally for now. I haven't seen anyone do a complete copy of a planetscale db locally yet
if planetscale is "mysql compatible" could you not just run mysql in a container?
actually nvm, @thimo_o if you use the planetscale api to open a new "branch" for each test execution, it'll probably work?
Opening a new branch would be quite overkill I think, since I then need to also push the schema again and wait for planetscale to configure it. I wonder how Prisma does it (although this was mostly for a local sqlite thing too ) https://github.com/prisma/prisma/issues/11261
I think I saw a tweet from drizzle before, where they said it was quite easy to use multiple providers at once, like an sqlite and a planetscale one, that might be a sweet spot. Do you know how that might be done perhaps?
I def would not do that - you'll miss cases where underlying database behavior causes a bug in your application
yeah, thats also true, I hope I can get my clear script working then
can you not just create a planetscale branch from an empty database>?
(I don't know how it works)
or actually @thimo_o probably the best solution is to have a separate planetscale database instance just for testing
where the mainline branch is never modified
on a test run, create a new branch, do your stuff, drop the branch
that way you never run the risk of your automated database branching stuff breaking your main database
I now have a separate account just for testing, so that works quite good
Hi ! I had the same issue...
db._.schema
was undefined... So I am using this script to clear my db :
That's very smart!
I've stolen your code to put it into my gist :p (with credits of course) https://gist.github.com/rphlmr/0d1722a794ed5a16da0fdf6652902b15
Haha no problem
You didn’t tag me so I’m only seeing it now. @louistiti_ solution works great. Lmk if you still want help with my specific implementation
@mendy.l @louistiti I tried the raw example, but I'm doing/forgetting something I think
In the for loop of the raw example I get tables: ExecutedQuery, which I can not iterate over. Do I have to change something here so the typing is correct?
Mendy, I had db..schema undefined but I still don't know why. Do you know why that might happen?
For my db variable I have this:
That should be correct right?
This would work I think :
@thimo_o
(If you are using my implementation)
@louistiti_ I do get Property 'forEach' does not exist on type 'ExecutedQuery'.. Does your tables variable have another type?
please send your whole function here
did you await the
db.execute
?
this is mandatory@louistiti_ As far as I know I do the same
This is what made the planetscale version work for me, I had to add the schema in my drizzle function and use truncate table
package.json script
ohhhhhhh yeah ! I remember now struggling with that ! I had the same issue as you and this fixed everything... sorry I forgot 🫠
I was not able to use the execute function, so I did the following
The variable
schema
is defined as
Where I export all the schemas usedFYI I'm using a more nuclear option which works great :