avram
avram copied to clipboard
Add database cleaning with transaction
Right now you can clean the database with truncation: https://github.com/luckyframework/lucky_record/pull/137
The problem is it is super slow. Transactions are much faster, so transaction cleaning should be added.
It may be helpful to see how DatabaseCleaner did it: https://github.com/DatabaseCleaner/database_cleaner/blob/master/lib/database_cleaner/active_record/transaction.rb
what's your main idea here?
there is LuckyRecord::Repo.transaction
where passes a block and yields inside...
to "clean" the database, instead of run the DatabaseCleaner.new.truncate
command, call the code in a block (like it's done in transaction_spec.rb
)
so, what's next?
checking the transaction
behavior, not sure if user should be obligated to call LuckyRecord::Repo.rollback
for me makes more sense in the end of transaction method, Lucky Records calls it automatically
I see 3 potential problems to solve:
- For flows, we start the app in a separate process so we will have to make sure the transaction we wrap the test in is used in that process
- We raise an error when transactions fail and never explicitly rollback (I don't think) so we will need to figure out if that error is something we can handle from the specs or if we need to update the code to not throw an error to rollback in certain cases
- We need to provide some way to run specs in a transaction without forcing users to copy/paste or write a bunch of code (currently we generate apps with a file that explicitly truncates the database)
Right now avram is depending on calling code to handle rollbacks in cases where a transaction already exists. https://github.com/luckyframework/avram/blob/d43f0361ab3c937b8485cf490802482d3005a8f1/src/avram/database.cr#L182-L191
In scenarios where you're testing what happens when a failure occurs within a transaction, this wouldn't occur if the transaction is made in a spec helper (at least not from within code under test)