Performance Tuning by Joe Damato
I love Heroku and cloud hosting, but, I don’t want/need to pay $20 more a month for their bundling service. I love the idea of DB snapshots, but I think its silly to pay $20 a month for that. I looked around for some different solutions for getting my own database snapshots, but I am not very excited about the yamlDB library — I am little concerned about its integrity. Nick did a great job of getting me about 50% there, but I feel much safer relying on true database .sql dump files from heroku.
After a late night of hacking, I’ve written a solution which does the following:
And with heroku’s free daily cron service, I am able to have automated, secure, and robust backups of my data!
All of this is wrapped into a nice rake task, simply follow the setup instructions in the file and call:
$ heroku rake backups:backup
My rake task relies on the pg_dump utility installed on every heroku dyno, I trust in the quality of the export of this tool. If you use it, leave a comment! If anyone wants to add archive rotation, I am all for it ;)
I am working on a project where I need to be able to make sure that no errant whitespace gets added to the form fields that users are inputting into the database layer. My solution to this problem was to add a #strip! method to Ruby’s Hash object. I can call this in the model, the controller, wherever, and get clean, whitespace free values for my Hash.
Unless you have ultra simplistic needs, (and are using Rails) I always recommend using the fantastic will_paginate plugin for paginating ActiveRecord, and even non-ActiveRecord collections! But, if you have a basic need to to page an array, you could open up the Array class and add this method:
class Array
def page(pg, offset = 10)
self[((pg-1)*offset)..((pg*offset)-1)]
end
end
This is a beautiful little method that adds #page to all arrays, and can be utilized like this:
my_array = [1,2,3,4,5, ... ,20,21,22,23,24,25]
my_array.page(1) #=> [1,2,3,4,5,6,7,8,9,10]
or even
my_array.page(1, 5) #=> [1,2,3,4,5]
Feel free to use this if you like it!