0

So I am right now developing a backend API using GraphQL and either Python with SQLAlchemy or Node.js with Sequelize to write all the data into a SQLite database.

I am still not sure what backend I will end up with. Maybe this question and the answer to my question will lead me to using one or the other.

So my app will have a function where you add dart throws (like double 20, triple 19, ...) into a table with throws. Depending on the game chosen and the conditions chosen this throw will either substract from a total count or maybe be added into a counter table or something like that.

Everytime a throw gets added there will be a check if the game is won and some other checks as well. Those might also write (commit) data to the database.

Now, if the user types in the wrong number / or the automatic darts machine will discover the wrong throw number (malfunction) there is a throw and several changed data in the database which I will have to rollback.

In darts scorer apps you will find in the app store either ios or android you always will have a undo button which will erase the throw from the database and all the "effects" caused by entering the wrong throw.

As a former mssql administrator my first idea was transactional logs and rollback function. But after searching the net for a while I understand both SQLAlchemy and Sequelize will rollback transactions only on exception.

Can I purposely rollback the last "complete" transaction (meaning all changes which for example a function will make by the end of the function) by hitting a button / calling a rest api path like '/undo' and if so are there any code snippets for either SQLAlchemy or Sequelize I am missing?

Best regards, Patrick

edit:

collection of provided answers:

  • command pattern (python)
Community
  • 1
  • 1
Patrick Hener
  • 135
  • 5
  • 16

1 Answers1

2

According to Can I rollback a transaction I've already committed? (data loss) and other sources, no, you cannot rollback a committed transaction. Also this is DB dependent, so doesn't have to do much with ORM or language.

I would recommend to implement the Command pattern, which is great for Undo. So basically do the revert from code, not from the database.

Horatiu Jeflea
  • 7,256
  • 6
  • 38
  • 67
  • So you say it is database dependend. Would using mysql/mariadb or postgresql would change the ability to use rollbacks then? Cause if so I will not hestitate to do so, as I am already using ORM it will not bother me switching backends, I guess? – Patrick Hener Feb 18 '20 at 20:12
  • not sure if there is a DB which supports this, but rollbacks in general are when something went wrong, but your scenario works best by using the code. – Horatiu Jeflea Feb 18 '20 at 20:15
  • I looked at the provided links using command pattern. That might be way over the top concerning my code. Please don't take it wrong but that is more complicated than I am able to handle by the moment. – Patrick Hener Feb 18 '20 at 20:24
  • I read more about the command pattern and watched a few Videos. I understand I would have to maintain a Stack of commands which I later on could reference to undo Actions. But as my App is api driven (flask routes) I will not have a object to instanciate and refer to later on. So command pattern will not work, right? @horatiu – Patrick Hener Feb 19 '20 at 09:30