-1

I'm new to rails and have just had my first bad experience with the mighty rails console. I overwrote a column in ALL instead of just one record and after hours of googling I did not find a common way to rollback this stupid transaction (ended up fixing 76 rows manually!).

I was working on the live environment on heroku and just wanted to fix one entry. There must be a way to rollback a transaction in the database (postgres) if you did stupid things - not by using pgbackup.

Would be great if you can share some best pratices with me or give me a good reading to start. Thanks!

DonMB
  • 2,550
  • 3
  • 28
  • 59

1 Answers1

0

I'm assuming you've already committed the transaction, but just in case you haven't, you can simply do:

ROLLBACK TRANSACTION;

Since I'm assuming you've tried that, the unfortunate answer is that your only option is a database restore from the last known good state.

There are some options and plugins that might help avoid this problem in the future, but they all come with performance costs. See these other Stack Overflow questions for some information Database Content Versioning and Postgres reverting back.

Community
  • 1
  • 1
Jim Stewart
  • 16,964
  • 5
  • 69
  • 89
  • What do you mean "commited the transaction"? If a run a query on the database, its commited straight away, isn't it? E.g. >> User.update_attributes(:surname, "John") – DonMB Nov 02 '13 at 08:42
  • Normally, yes. I think I misread your question as mentioning the "db console" rather than "rails console". Normally it's safer, in the DB console, to "begin transaction", do your operation, and then either "commit" or "rollback" after you've confirmed the result. You can also do explicit [transactions](http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html) in the Rails console, but that's usually done in code that checks for errors and not in interactive use. Anyway, odds are you're out of luck. – Jim Stewart Nov 03 '13 at 03:20