-
Notifications
You must be signed in to change notification settings - Fork 1k
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
feat: Generator rollbacks #6947
feat: Generator rollbacks #6947
Conversation
The things holding me back from writing some tests for rolling back the actions of the actual command handlers are:
Just out of interest, was the format of the object returned by |
Okay so I guess this is ready for review in the sense that I can't think of anything else which needs to be added or improved (other than the tests which I have mentioned above). |
Hello! Sorry for the delay, Thanksgiving and everything! I'm gonna look at this today/tomorrow. What direction did you end up going with the tests, did you do a mocked file system? Did all the |
Hey Rob, no worries at all! I decided not to push on with the testing before I got some feedback on the approach being acceptable since it's going to be a little bit of work to change all the tests I think. As for the approach, I noticed all the file io work was ultimately funnelled through one single I haven't tested the destroy commands and they aren't used for the rollback actions. |
Hey @cannikin any thoughts on the approach used here? No problem if you've not had a change to review. If there's positive reaction then I can go ahead and see about updating the testing setup. |
Haven't had a chance to review, will take a look next week! |
That first failure in the actions checks, can you run this command and see if anything changes in the text fixture, other than the migrations, and a couple scenario files?
The migrations always change and the scenarios get new string values, so you can ignore changes to those files. But if anything else changes it should get committed into the repo! |
No significant changes when rebuilding the test-project fixture. The usual migrations and scenarios (I've got another PR sitting which prevents that 😉). There's a minor bump to |
Looks great, really elegant solution! Thanks again! |
* Initial implementation * Run generateTypes after files rolled back * remove empty dirs * initial tests for rollback.js * Add some docs * Remove throw error from testing * Fix scaffold rollback execa call * rollback flag added, tmp stop humanize-string rollback * Regenerate types after rolling back files * Rebuilt the test fixture to check * Add generateType rollback to scaffold * Remove grandparent directories if empty too * add listr2 rollback test to unit tests * add back reverting humanize-string * Add --rollback to docs * rename prepare function * fix prepareForRollback in rollback tests Co-authored-by: Rob Cameron <[email protected]>
@cannikin should we make a note to try and add some tests for this? |
The suite on the rollback code itself is great...you mean on the actual generators? Yeah I mean I dunno if we need a full test suite for every one, maybe just as simple one like the scripts generator, or one that has at least one custom function? Doesn't seem like it'll be very fun though, mocking out the file system. :( |
Okay that's fine then, thanks for confirming. I did recently think it might be possible to do testing like this a little easier in the e2e tests instead of the unit tests but we'll see what the future brings. |
* Initial implementation * Run generateTypes after files rolled back * remove empty dirs * initial tests for rollback.js * Add some docs * Remove throw error from testing * Fix scaffold rollback execa call * rollback flag added, tmp stop humanize-string rollback * Regenerate types after rolling back files * Rebuilt the test fixture to check * Add generateType rollback to scaffold * Remove grandparent directories if empty too * add listr2 rollback test to unit tests * add back reverting humanize-string * Add --rollback to docs * rename prepare function * fix prepareForRollback in rollback tests Co-authored-by: Rob Cameron <[email protected]>
….com:redwoodjs/redwood into feat/dc-kc-decoupled-auth-setup-improvements * 'feat/dc-kc-decoupled-auth-setup-improvements' of github.com:redwoodjs/redwood: chore(deps): update dependency nx to v15.3.2 (#7114) chore(deps): update dependency redis to v4.5.1 (#7115) fix: add missing deps to cli helpers (#7117) Adds ability to delete a cache entry (#7016) Label override flags for dbAuth generator (#6440) fix(deps): update dependency systeminformation to v5.16.6 (#7108) feat: Generator rollbacks (#6947) fix(deps): update dependency @types/node to v16.18.8 (#7107) chore(deps): update dependency supertokens-node to v12.1.3 (#7105)
Would close #82 (the oldest open issue!)
Problem
If any error occurs during the generators then the actions already performed are not undone. This can leave a users project in a broken state. It is also more obvious to a user that the command failed if it rolls back its actions.
Solution
Add a rollback feature to the generator commands. This automatically reverts all actions of the generator when an error occurs.
Changes
lib/rollback.js
which holds an object stack, these objects are used to either undo a file change or invoke a function during a rollback.lib/index.js
file to ensure that when files are written to then a corresponding rollback entry is added.Outstanding
I have tested all of them manually. I created a new test project, init'ed git and ran each command with a deliberate error added as a final step. All rollbacks returned the repo to a state where git showed no differences in git status. We do still need proper tests within each command to make sure the actions are undone by the rollback.
Empty directories are now deleted. This works for 2 levels so rollback can delete the parent and grandparent directory of a file which is removed. Important to note that if a user already has an empty directory it will be removed if the generator creates files within it and rollback removes them all. Using .keep files will still prevent this as it means the directory won't be empty.
rollback.js
One additional unit test needs implemented. This involves mock fs actions which aren't currently supported. I think they are sitting in a different PR which is why I don't add them here.
--rollback
option so rollbacks can be disabled by--no-rollback
✅Added the rollback option to all generators, default to true. It may be excessive for some generators which either create one file or fail so a rollback isn't really necessary but it is only a couple lines to add it in anyway. Keeps the generator options/behaviour consistent.
--rollback
✅Questions
lib/index.js
writeFile
function to add a rollback entry. Other functions likedeleteFile
don't add to the rollback stack simply because no generator uses it. Would it be best to add in rollback entries for these other functions which may be needed in the future or leave it as is for now?yarn rw g types
because it is slightly different from the rest. Should we aim to add rollback support to this too? I don't think it is totally necessary because if I understand correctly this works based on current project files and so would not need a rollback ability.Ping
@cannikin - Since you are the original author of #82