-
Notifications
You must be signed in to change notification settings - Fork 389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Persist Roles and Permissions between test runs #854
Persist Roles and Permissions between test runs #854
Conversation
I would rather stick with the previous solution we had and use spawn instead of execa. |
reset test db with prisma reset reset test db with prisma reset use custom db reset in `cleanupDb` use ; instead of ); for end of sql splitting reset test db with prisma reset use custom db reset in `cleanupDb` use ; instead of ); for end of sql splitting run prisma reset between tests reset test db with prisma reset use custom db reset in `cleanupDb` use ; instead of ); for end of sql splitting run prisma reset between tests use prisma reset for seeding add instructions for customising Role and Permission seeds
317278e
to
ee9ec4e
Compare
Busy benchmarking to check performance of this approach |
Benchmarks
|
This is what I expected and I was very surprised when it looked like it was not much slower in the previous implementation. So I'm actually kind of pleased to see this and know I'm not crazy 😅 I think going with the manual migration runs would be the best option. What do you think? |
Awesome 💪🏻 I've updated to that approach and checked everything is working as expected: One thing I noticed when doing the seed, is prisma sometimes logs out if a query took slightly longer: It would be nice to silence that log so the seed logs are neat, but on the other hand it may be good be leave it there so it is clear when the reset is taking longer than usual 🤔 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm liking this. Thanks! The log from prisma is actually coming from our own client setup and I think it's good 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, got it now. Thanks!
Looks like one of the unit tests is failing in CI. Based on the error it looks like the User table isn't being created properly 🤔 https://github.com/epicweb-dev/epic-stack/actions/runs/11023715807/job/30615526236?pr=854#step:7:38 |
5136011
to
99af7cd
Compare
Hmmm. I re-triggered the CI twice and it passed again, so it seems to be flakey. I suspect its a timing issue, where a migration line that depends on the User table existing, is run before the line that creates the table. I thought the Perhaps you're right and things are in order, but the User table is not being successfully created. Looking into it further 🧐 |
5a00989
to
8d2be4c
Compare
Ok, I have done three things:
I am not sure what else to do until we get another CI failure 🤔 I haven't been able to reproduce the issue locally. What do you think? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy with this. Thank you!
😬 failed build |
It seems like there is still a race condition happening. The error is intermittent, and sometimes the error changes(here and here), leading me to believe things are not always executing in the same order, causing different errors depending on the order. I'm going to continue experimenting tomorrow. Will clean up all of these commits at the end 👍🏻 If you have any ideas for how to investigate/mitigate the possible race condition, please let me know 🙂 |
Things I've tried:
None managed to avoid the race condition and intermittent errors 🤔 I think we can take two paths forward:
What do you think? |
85c0490
to
39c2702
Compare
That seems promising 🤔 Great idea to use a different db tool ⚡ |
Ok, I've run this in CI several times and it looks like it's working fine so I'm gonna merge. Thanks so much for all the iteration on this. I feel really good about this change! |
Ah that's great news! I'm glad it worked finally 😅 Thanks for your patience with all the back and forth 🙏🏻 |
I love what we've done for our seed script. I found a drastically simpler solution for the test side of things here: 7b16bdc |
Problem
Roles and Permissions are cleared from the db after the first test run, so we are not able to create Users in subsequent tests.
Solution
Ensure Roles and Permissions are persisted between tests.
Approaches considered
A. Exclude Roles & Permissions tables from being dropped
We can explicitly exclude the
Role
,Permission
and_PermissionToRole
tables from being dropped between tests, by updating the SQL incleanupDb()
.This option is viable.
The downside of this approach is that if a user changes these tables in their schema, they will also need to know to update the
cleanupDb
function. It may be nicer to avoid these extra touch points.B. Use prisma migrate reset
We attempted to use prisma's built in migration command -
npx prisma migrate reset --force --skip-seed --skip-generate'
.The issue with this approach, is that we use
execa
to run this command, which needs to be run in anode
environment, and some of our tests run in thejsdom
vitest environment.Because of this constraint, this option doesn't seem viable. A or C may be better options.
C. Use a custom reset script (used in this PR)
Instead of
prisma migrate reset
, we can manually drop all tables and run every migration by loading and running the SQL.One potential downside of this approach is the complexity of importing, parsing and running each individual SQL statement from each migration file. We are using
;
as the delimiter, which could break if any data in the SQL statment has this character anywhere except at the end of each SQL statement, but this isn't an issue right now.An upside of this is that it doesn't add any extra touch points like we do in option A.
This is the approach used in this PR.